Compartir a través de

教程:使用 Azure 自动化创建 Azure HDInsight 群集

使用 Azure 自动化可创建在云中运行的脚本,并按需或按计划管理 Azure 资源。 本文介绍如何通过创建 PowerShell runbook 来创建和删除 Azure HDInsight 群集。

本教程中,您将学习如何:

  • 安装与 HDInsight 交互所需的模块。
  • 创建并存储群集创建期间所需的凭据。
  • 创建新的自动化 Runbook 以创建 HDInsight 群集。

如果没有 Azure 订阅,请在开始前创建一个试用版订阅

先决条件

安装 HDInsight 模块

  1. 登录到 Azure 门户

  2. 选择自动化帐户。

  3. 在“共享资源”下,选择“模块库”

  4. 在框中输入 AzureRM.Profile,然后选择 Enter 进行搜索。 选择可用的搜索结果。

  5. 在 AzureRM.profile 屏幕上,选择“导入”。 选择“更新 Azure 模块”对应的框,然后选择“确定”。

    显示导入 AzureRM.profile 模块的屏幕截图。

  6. 返回到模块库。 在“共享资源”下,选择“模块库”

  7. 输入 HDInsight,然后选择“AzureRM.HDInsight”。

    显示浏览 HDInsight 模块的屏幕截图。

  8. 在 AzureRM.HDInsight 面板上,选择“导入”“确定”。>

    显示 AzureRM.HDInsight 模块的导入消息的屏幕截图。

创建凭据

  1. 在“共享资源”下,选择“凭据”

  2. 选择“添加凭据”

  3. 在“新凭据”面板上输入所需的信息。 此凭据用于存储群集密码。 使用它登录 Ambari。

    资产 价值
    Name cluster-password
    用户名 admin
    密码 SECURE_PASSWORD
    确认密码 SECURE_PASSWORD
  4. 选择 创建

  5. 使用用户名 sshuser 和所选密码对新的凭据 ssh 密码 重复相同的过程。 选择 创建。 此凭据用于存储群集的安全外壳协议密码。

    显示如何创建新凭证的屏幕截图。

创建 runbook 以创建群集

  1. 在“过程自动化”下,选择“Runbook”。

  2. 选择“创建 Runbook”

  3. 在“创建 Runbook”窗格中,输入 Runbook 的名称,例如 hdinsight-cluster-create。 在“Runbook 类型”下拉列表中,选择 PowerShell

  4. 选择 创建

    屏幕截图显示了如何创建 Runbook。

  5. 在“编辑 PowerShell Runbook”屏幕上输入以下代码,然后选择“发布”。

    显示发布 Runbook 的屏幕截图。

    Param
    (
      [Parameter (Mandatory= $true)]
      [String] $subscriptionID,
    
      [Parameter (Mandatory= $true)]
      [String] $resourceGroup,
    
      [Parameter (Mandatory= $true)]
      [String] $storageAccount,
    
      [Parameter (Mandatory= $true)]
      [String] $containerName,
    
      [Parameter (Mandatory= $true)]
      [String] $clusterName
    )
    ### Authenticate to Azure 
    $Conn = Get-AutomationConnection -Name 'AzureRunAsConnection'
    Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
    
    # Set cluster variables
    $storageAccountKey = (Get-AzureRmStorageAccountKey -Name $storageAccount -ResourceGroupName $resourceGroup)[0].value 
    
    # Setting cluster credentials
    
    #Automation credential for Cluster Admin
    $clusterCreds = Get-AutomationPSCredential -Name 'cluster-password'
    
    #Automation credential for user to SSH into cluster
    $sshCreds = Get-AutomationPSCredential -Name 'ssh-password' 
    
    $clusterType = "Hadoop" #Use any supported cluster type (Hadoop, HBase, etc.)
    $clusterOS = "Linux"
    $clusterWorkerNodes = 3
    $clusterNodeSize = "Standard_D3_v2"
    $location = Get-AzureRmStorageAccount -StorageAccountName $storageAccount -ResourceGroupName $resourceGroup | %{$_.Location}
    
    ### Provision HDInsight cluster
    New-AzureRmHDInsightCluster -ClusterName $clusterName -ResourceGroupName $resourceGroup -Location $location -DefaultStorageAccountName "$storageAccount.blob.core.chinacloudapi.cn" -DefaultStorageAccountKey $storageAccountKey -DefaultStorageContainer $containerName -ClusterType $clusterType -OSType $clusterOS -Version "3.6" -HttpCredential $clusterCreds -SshCredential $sshCreds -ClusterSizeInNodes $clusterWorkerNodes -HeadNodeSize $clusterNodeSize -WorkerNodeSize $clusterNodeSize
    

创建 Runbook 以删除群集

  1. 在“过程自动化”下,选择“Runbook”。

  2. 选择“创建 Runbook”

  3. 在“创建 Runbook”窗格中,输入 Runbook 的名称,例如 hdinsight-cluster-delete。 在“Runbook 类型”下拉列表中,选择 PowerShell

  4. 选择 创建

  5. 在“编辑 PowerShell Runbook”屏幕上输入以下代码,然后选择“发布”。

    Param
    (
      [Parameter (Mandatory= $true)]
      [String] $clusterName
    )
    
    ### Authenticate to Azure 
    $Conn = Get-AutomationConnection -Name 'AzureRunAsConnection'
    Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
    
    Remove-AzureRmHDInsightCluster -ClusterName $clusterName
    

运行 Runbook

本部分介绍如何运行 Runbook。

创建群集

  1. 查看自动化帐户的 Runbook 列表。 在“过程自动化”下,选择“Runbook”。

  2. 选择 hdinsight-cluster-create 或创建群集创建 Runbook 时使用的名称。

  3. 选择“开始”以立即运行 Runbook。 你还可以安排 Runbook 定期运行。 有关详细信息,请参阅在自动化中计划 Runbook

  4. 输入脚本所需的参数,然后选择“确定”。 此步骤会创建一个新的 HDInsight 群集,该群集具有你在 CLUSTERNAME 参数中指定的名称。

    屏幕截图显示了如何运行群集 Runbook。

删除群集

通过选择创建的 hdinsight-cluster-delete Runbook 来删除群集。 选择“开始”,输入 CLUSTERNAME 参数,然后选择“确定”。

清理资源

如果资源不再被需要,请删除所创建的自动化帐户以避免意外的费用。 转到 Azure 门户,选择创建了自动化帐户的资源组,选择自动化帐户,然后选择“删除”。

后续步骤