了解如何使用 Azure 资源管理器模板和 Azure REST API 创建 HDInsight 群集。
Azure REST API 允许对托管在 Azure 平台中的服务执行管理操作,包括创建新资源(例如 HDInsight 群集)。
注意
本文档中的步骤使用 curl (https://curl.haxx.se/) 实用工具与 Azure REST API 进行通信。
创建模板
Azure 资源管理器模板是描述资源组及其中所有资源(如 HDInsight)的 JSON 文档。利用此基于模本的方法,你可以对一个模板中 HDInsight 所需的资源进行定义。
下面的 JSON 文档是来自 https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.hdinsight/hdinsight-linux-ssh-password/azuredeploy.json 的模板与参数文件的组合形式,它将创建基于 Linux 的群集,并使用密码保护 SSH 用户帐户。
{
    "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.chinacloudapi.cn')]",
                            "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 个)的辅助角色节点。 如果计划使用 32 个以上的辅助角色节点,则必须选择至少具有 8 个核心和 14 GB ram 的头节点大小。
有关节点大小和相关费用的详细信息,请参阅 HDInsight 定价。
登录到 Azure 订阅
请按照 Azure CLI 入门中所述的步骤操作,并使用 az login 命令连接到订阅。
创建服务主体
注意
这些步骤是使用 Azure CLI 创建服务主体以访问资源文档的“使用密码创建服务主体”部分的缩减版本。 这些步骤创建用于向 Azure REST API 进行身份验证的服务主体。
- 从命令行使用以下命令列出 Azure 订阅。 - az account list --query '[].{Subscription_ID:id,Tenant_ID:tenantId,Name:name}' --output table- 在列表中,选择要使用的订阅并记下 Subscription_ID 和 Tenant_ID 列。 保存这些值。 
- 使用以下命令在 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 条目提供密码。- 注意 - --home-page和- --identifier-uris值无需引用在 Internet 上承载的实际网页。 它们必须是唯一的 URI。- 此命令返回的值是新应用程序的应用 ID。 保存此值。 
- 通过以下命令使用应用 ID 创建服务主体。 - az ad sp create --id <App ID> --query 'objectId'- 此命令返回的值是对象 ID。 保存此值。 
- 使用对象 ID 值向服务主体分配所有者角色。 使用前面获取的订阅 ID。 - az role assignment create --assignee <Object ID> --role Owner --scope /subscriptions/<Subscription ID>/
获取身份验证令牌
使用以下命令检索身份验证令牌:
curl -X "POST" "https://login.chinacloudapi.cn/$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.chinacloudapi.cn/"
将 $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.chinacloudapi.cn/","access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWoNBVGZNNXBPWWlKSE1iYTlnb0VLWSIsImtpZCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuYXp1cmUuY29tLyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI2Ny8iLCJpYXQiOjE0NjM0MDYwOTQsIm5iZiI6MTQ2MzQwNjA5NCwiZXhwIjoxNDYzNDA5OTk5LCJhcHBpZCI6IjBlYzcyMzM0LTZkMDMtNDhmYi04OWU1LTU2NTJiODBiZDliYiIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI0Ny8iLCJvaWQiOiJlNjgxZTZiMi1mZThkLTRkZGUtYjZiMS0xNjAyZDQyNWQzOWYiLCJzdWIiOiJlNjgxZTZiMi1mZThkLTRkZGUtYjZiMS0xNjAyZDQyNWQzOWYiLCJ0aWQiOiI3MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDciLCJ2ZXIiOiIxLjAifQ.nJVERbeDHLGHn7ZsbVGBJyHOu2PYhG5dji6F63gu8XN2Cvol3J1HO1uB4H3nCSt9DTu_jMHqAur_NNyobgNM21GojbEZAvd0I9NY0UDumBEvDZfMKneqp7a_cgAU7IYRcTPneSxbD6wo-8gIgfN9KDql98b0uEzixIVIWra2Q1bUUYETYqyaJNdS4RUmlJKNNpENllAyHQLv7hXnap1IuzP-f5CNIbbj9UgXxLiOtW5JhUAwWLZ3-WMhNRpUO2SIB7W7tQ0AbjXw3aUYr7el066J51z5tC1AK9UC-mD_fO_HUP6ZmPzu5gLA6DxkIIYP3grPnRVoUDltHQvwgONDOw"
}
创建资源组
使用以下命令创建资源组。
- 将 $SUBSCRIPTIONID设置为创建服务主体时收到的订阅 ID。
- 将 $ACCESSTOKEN设置为在上一步骤中收到的访问令牌。
- 将 DATACENTERLOCATION替换为要在其中创建资源组和资源的数据中心。 例如“China East”。
- 将 $RESOURCEGROUPNAME设置为要为此组使用的名称:
curl -X "PUT" "https://management.chinacloudapi.cn/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.chinacloudapi.cn/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}"
注意
如果将模板保存到了文件中,则可以使用以下命令而不是 -d "{ template and parameters}":
--data-binary "@/path/to/file.json"
如果此请求成功,会收到 200 系列响应,且响应正文包含一个 JSON 文档,其中包含有关部署操作的信息。
重要
部署已提交,但尚未完成。 部署通常需要大约 15 分钟才能完成。
检查部署状态
若要检查部署的状态,请使用以下命令:
curl -X "GET" "https://management.chinacloudapi.cn/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 群集后,请参考以下主题来了解如何使用群集。