Azure REST API を使用して Apache Hadoop クラスターを作成する

Azure Resource Manager テンプレートと Azure REST API を使用して HDInsight クラスターを作成する方法について説明します。

Azure REST API を使用すると、Azure プラットフォームでホストされたサービスで、HDInsight クラスターなど新しいリソースの作成を含む管理操作を実行できます。

Note

このドキュメントの手順では、curl (https://curl.haxx.se/) ユーティリティを使用して Azure REST API と通信します。

テンプレートの作成

Azure Resource Manager テンプレートは、リソース グループとその中のすべてのリソース (HDInsight など) について記述する JSON ドキュメントです。このテンプレート ベースのアプローチでは、HDInsight で必要なリソースを 1 つのテンプレートで定義することができます。

次の JSON ドキュメントは、https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.hdinsight/hdinsight-linux-ssh-password/azuredeploy.json からのテンプレートとパラメーター ファイルを併せたもので、ここでは、SSH ユーザー アカウントをセキュリティ保護するためにパスワードを使用して Linux ベースのクラスターを作成します。

{
    "properties": {
        "template": {
            "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "parameters": {
                "clusterType": {
                    "type": "string",
                    "allowedValues": ["hadoop",
                    "hbase",
                    "spark"],
                    "metadata": {
                        "description": "The type of the HDInsight cluster to create."
                    }
                },
                "clusterName": {
                    "type": "string",
                    "metadata": {
                        "description": "The name of the HDInsight cluster to create."
                    }
                },
                "clusterLoginUserName": {
                    "type": "string",
                    "metadata": {
                        "description": "These credentials can be used to submit jobs to the cluster and to log into cluster dashboards."
                    }
                },
                "clusterLoginPassword": {
                    "type": "securestring",
                    "metadata": {
                        "description": "The password must be at least 10 characters in length and must contain at least one digit, one non-alphanumeric character, and one upper or lower case letter."
                    }
                },
                "sshUserName": {
                    "type": "string",
                    "metadata": {
                        "description": "These credentials can be used to remotely access the cluster."
                    }
                },
                "sshPassword": {
                    "type": "securestring",
                    "metadata": {
                        "description": "The password must be at least 10 characters in length and must contain at least one digit, one non-alphanumeric character, and one upper or lower case letter."
                    }
                },
                "clusterStorageAccountName": {
                    "type": "string",
                    "metadata": {
                        "description": "The name of the storage account to be created and be used as the cluster's storage."
                    }
                },
                "clusterWorkerNodeCount": {
                    "type": "int",
                    "defaultValue": 4,
                    "metadata": {
                        "description": "The number of nodes in the HDInsight cluster."
                    }
                }
            },
            "variables": {
                "defaultApiVersion": "2015-05-01-preview",
                "clusterApiVersion": "2015-03-01-preview"
            },
            "resources": [{
                "name": "[parameters('clusterStorageAccountName')]",
                "type": "Microsoft.Storage/storageAccounts",
                "location": "[resourceGroup().location]",
                "apiVersion": "[variables('defaultApiVersion')]",
                "dependsOn": [],
                "tags": {

                },
                "properties": {
                    "accountType": "Standard_LRS"
                }
            },
            {
                "name": "[parameters('clusterName')]",
                "type": "Microsoft.HDInsight/clusters",
                "location": "[resourceGroup().location]",
                "apiVersion": "[variables('clusterApiVersion')]",
                "dependsOn": ["[concat('Microsoft.Storage/storageAccounts/',parameters('clusterStorageAccountName'))]"],
                "tags": {

                },
                "properties": {
                    "clusterVersion": "3.6",
                    "osType": "Linux",
                    "clusterDefinition": {
                        "kind": "[parameters('clusterType')]",
                        "configurations": {
                            "gateway": {
                                "restAuthCredential.isEnabled": true,
                                "restAuthCredential.username": "[parameters('clusterLoginUserName')]",
                                "restAuthCredential.password": "[parameters('clusterLoginPassword')]"
                            }
                        }
                    },
                    "storageProfile": {
                        "storageaccounts": [{
                            "name": "[concat(parameters('clusterStorageAccountName'),'.blob.core.windows.net')]",
                            "isDefault": true,
                            "container": "[parameters('clusterName')]",
                            "key": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('clusterStorageAccountName')), variables('defaultApiVersion')).key1]"
                        }]
                    },
                    "computeProfile": {
                        "roles": [{
                            "name": "headnode",
                            "targetInstanceCount": "2",
                            "hardwareProfile": {
                                "vmSize": "{}" 
                            },
                            "osProfile": {
                                "linuxOperatingSystemProfile": {
                                    "username": "[parameters('sshUserName')]",
                                    "password": "[parameters('sshPassword')]"
                                }
                            }
                        },
                        {
                            "name": "workernode",
                            "targetInstanceCount": "[parameters('clusterWorkerNodeCount')]",
                            "hardwareProfile": {
                                "vmSize": "{}"
                            },
                            "osProfile": {
                                "linuxOperatingSystemProfile": {
                                    "username": "[parameters('sshUserName')]",
                                    "password": "[parameters('sshPassword')]"
                                }
                            }
                        }]
                    }
                }
            }],
            "outputs": {
                "cluster": {
                    "type": "object",
                    "value": "[reference(resourceId('Microsoft.HDInsight/clusters',parameters('clusterName')))]"
                }
            }
        },
        "mode": "incremental",
        "Parameters": {
            "clusterName": {
                "value": "newclustername"
            },
            "clusterType": {
                "value": "hadoop"
            },
            "clusterStorageAccountName": {
                "value": "newstoragename"
            },
            "clusterLoginUserName": {
                "value": "admin"
            },
            "clusterLoginPassword": {
                "value": "changeme"
            },
            "sshUserName": {
                "value": "sshuser"
            },
            "sshPassword": {
                "value": "changeme"
            }
        }
    }
}

このサンプルをこのドキュメントの手順で使用します。 Parameters セクションのサンプルの "" は、ご自身のクラスターの値に置き換えてください。

重要

テンプレートでは、HDInsight クラスターに既定数 (4) の worker ノードを使用します。 32 個を超えるワーカー ノードを予定している場合、コア数が 8 個以上で RAM が 14 GB 以上のサイズのヘッド ノードを選択する必要があります。

ノードのサイズと関連コストに関する詳細については、「 HDInsight の価格」を参照してください。

Azure サブスクリプションにサインインします。

Azure CLI の概要」に記載されている手順に従って、az login コマンドを使用して自分のサブスクリプションに接続します。

サービス プリンシパルの作成

Note

以下の手順は、「リソースにアクセスするためのサービス プリンシパルを Azure CLI で作成する」ドキュメントの「パスワードを使用したサービス プリンシパルの作成」セクションを要約したものです。 この手順を実行すると、Azure REST API への認証に使用されるサービス プリンシパルが作成されます。

  1. コマンド ラインで、次のコマンドを使用して Azure サブスクリプションを一覧表示します。

    az account list --query '[].{Subscription_ID:id,Tenant_ID:tenantId,Name:name}'  --output table
    

    一覧で、使用するサブスクリプションを選択し、Subscription_ID 列と Tenant_ID 列を書き留めます。 これらの値を保存します。

  2. 次のコマンドを使用して、Microsoft Entra ID にアプリケーションを作成します。

    az ad app create --display-name "exampleapp" --homepage "https://www.contoso.org" --identifier-uris "https://www.contoso.org/example" --password <Your password> --query 'appId'
    

    --display-name--homepage、および --identifier-uris の値を自分の値に置き換えます。 新しい Active Directory エントリのパスワードを指定します。

    Note

    --home-page--identifier-uris の値は、インターネット上にホストされている実際の Web ページを参照する必要はありませんが、 一意の URI である必要があります。

    このコマンドから返される値は、新しいアプリケーションの アプリ ID です。 この値を保存します。

  3. 次のコマンドを使用して、アプリ ID を使用するサービス プリンシパルを作成します。

    az ad sp create --id <App ID> --query 'objectId'
    

    このコマンドから返される値は、オブジェクト ID です。 この値を保存します。

  4. 所有者の役割を、オブジェクト ID 値を使用するサービス プリンシパルに割り当てます。 前に取得したサブスクリプション ID を使用します。

    az role assignment create --assignee <Object ID> --role Owner --scope /subscriptions/<Subscription ID>/
    

認証トークンの取得

次のコマンドを使用して、認証トークンを取得します。

curl -X "POST" "https://login.microsoftonline.com/$TENANTID/oauth2/token" \
-H "Cookie: flight-uxoptin=true; stsservicecookie=ests; x-ms-gateway-slice=productionb; stsservicecookie=ests" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=$APPID" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_secret=$PASSWORD" \
--data-urlencode "resource=https://management.azure.com/"

$TENANTID$APPID$PASSWORD に、前に取得または使用した値を設定します。

この要求が成功したら、200 シリーズの応答が届きます。応答本文に JSON ドキュメントが含まれています。

この要求によって返される JSON ドキュメントには、access_token という名前の要素が含まれます。 access_token の値が、REST API への認証要求で使用されます。

{
    "token_type":"Bearer",
    "expires_in":"3599",
    "expires_on":"1463409994",
    "not_before":"1463406094",
    "resource":"https://management.azure.com/","access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWoNBVGZNNXBPWWlKSE1iYTlnb0VLWSIsImtpZCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuYXp1cmUuY29tLyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI2Ny8iLCJpYXQiOjE0NjM0MDYwOTQsIm5iZiI6MTQ2MzQwNjA5NCwiZXhwIjoxNDYzNDA5OTk5LCJhcHBpZCI6IjBlYzcyMzM0LTZkMDMtNDhmYi04OWU1LTU2NTJiODBiZDliYiIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI0Ny8iLCJvaWQiOiJlNjgxZTZiMi1mZThkLTRkZGUtYjZiMS0xNjAyZDQyNWQzOWYiLCJzdWIiOiJlNjgxZTZiMi1mZThkLTRkZGUtYjZiMS0xNjAyZDQyNWQzOWYiLCJ0aWQiOiI3MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDciLCJ2ZXIiOiIxLjAifQ.nJVERbeDHLGHn7ZsbVGBJyHOu2PYhG5dji6F63gu8XN2Cvol3J1HO1uB4H3nCSt9DTu_jMHqAur_NNyobgNM21GojbEZAvd0I9NY0UDumBEvDZfMKneqp7a_cgAU7IYRcTPneSxbD6wo-8gIgfN9KDql98b0uEzixIVIWra2Q1bUUYETYqyaJNdS4RUmlJKNNpENllAyHQLv7hXnap1IuzP-f5CNIbbj9UgXxLiOtW5JhUAwWLZ3-WMhNRpUO2SIB7W7tQ0AbjXw3aUYr7el066J51z5tC1AK9UC-mD_fO_HUP6ZmPzu5gLA6DxkIIYP3grPnRVoUDltHQvwgONDOw"
}

リソース グループを作成する

次のコマンドを実行してリソース グループを作成します。

  • $SUBSCRIPTIONID には、サービス プリンシパルの作成時に受け取ったサブスクリプション ID に設定します。
  • $ACCESSTOKEN には、前の手順で受け取ったアクセス トークンを設定します。
  • DATACENTERLOCATION をリソース グループやリソースを作成するデータ センターに置き換えます。 たとえば、"South Central US" のようになります。
  • $RESOURCEGROUPNAME には、このグループで使用する名前を設定します。
curl -X "PUT" "https://management.azure.com/subscriptions/$SUBSCRIPTIONID/resourcegroups/$RESOURCEGROUPNAME?api-version=2015-01-01" \
    -H "Authorization: Bearer $ACCESSTOKEN" \
    -H "Content-Type: application/json" \
    -d $'{
"location": "DATACENTERLOCATION"
}'

この要求が成功したら、200 シリーズの応答が届きます。応答本文に含まれる JSON ドキュメントにグループに関する情報が含まれています。 "provisioningState" 要素には値 "Succeeded" が含まれています。

デプロイの作成

次のコマンドを実行して、テンプレートをリソース グループにデプロイします。

  • $DEPLOYMENTNAME には、このデプロイで使用する名前を設定します。
curl -X "PUT" "https://management.azure.com/subscriptions/$SUBSCRIPTIONID/resourcegroups/$RESOURCEGROUPNAME/providers/microsoft.resources/deployments/$DEPLOYMENTNAME?api-version=2015-01-01" \
-H "Authorization: Bearer $ACCESSTOKEN" \
-H "Content-Type: application/json" \
-d "{set your body string to the template and parameters}"

Note

テンプレートをファイルに保存した場合は、-d "{ template and parameters}" の代わりに次のコマンドを使用できます。

--data-binary "@/path/to/file.json"

この要求が成功したら、200 シリーズの応答が届きます。応答本文に含まれる JSON ドキュメントにデプロイ操作に関する情報が含まれています。

重要

デプロイは送信済みですが、まだ完了していません。 デプロイを完了するには時間がかかる場合があります (通常約 15 分)。

デプロイの状態の確認

デプロイの状態を確認するには、次のコマンドを使用します。

curl -X "GET" "https://management.azure.com/subscriptions/$SUBSCRIPTIONID/resourcegroups/$RESOURCEGROUPNAME/providers/microsoft.resources/deployments/$DEPLOYMENTNAME?api-version=2015-01-01" \
-H "Authorization: Bearer $ACCESSTOKEN" \
-H "Content-Type: application/json"

これにより返される JSON ドキュメントにデプロイ操作に関する情報が含まれています。 "provisioningState" 要素にはデプロイの状態が含まれています。 この要素に値 "Succeeded" が含まれている場合は、デプロイが正常に完了しています。

トラブルシューティング

HDInsight クラスターの作成で問題が発生した場合は、「アクセス制御の要件」を参照してください。

次のステップ

HDInsight クラスターが正常に作成されたので、クラスターの使用方法について、以下のトピックを参照してください。

Apache Hadoop クラスター

Apache HBase クラスター