共用方式為

使用脚本来自定义 Azure HDInsight 集群

Azure HDInsight 提供了一种称为 脚本操作 的配置方法,用于调用自定义脚本以定制群集。 这些脚本用于安装其他组件并更改配置设置。 脚本操作可以在创建群集的过程中或之后使用。

脚本操作也可以作为 HDInsight 应用程序发布到 Azure 市场。

了解脚本动作

脚本操作是在 HDInsight 群集中的节点上运行的 Bash 的脚本。 脚本操作的特点和功能如下:

  • Bash 脚本 URI(访问文件的位置)必须可从 HDInsight 资源提供程序和群集访问。

  • 以下是可能的存储位置:

    • 对于常规(非 ESP)集群:

      • Azure 存储帐户中的一个 Blob 存储,这个存储帐户可以是 HDInsight 群集的主存储帐户或附加存储帐户。 HDInsight 在创建群集期间被授予对这两种类型的存储帐户的访问权限。

        重要

        不要旋转此 Azure 存储帐户上的存储密钥,因为这会导致后续在其中存储的脚本操作失败。

      • 不建议将 Data Lake Storage Gen2 用于脚本操作。 abfs:// 不支持 Bash 脚本 URI。 https:// URI 可以使用,但仅在容器具有公共访问权限,并且 HDInsight 资源提供程序的防火墙已打开时才可用,因此不推荐这样做。

      • 可通过 https:// 路径访问的公共文件共享服务。 示例包括 Azure Blob、GitHub 或 OneDrive。 有关 URI 示例,请参阅 示例脚本操作脚本

    • 对于具有 ESP 的群集,支持 wasb://wasbs://http[s]:// URI。

  • 脚本操作可以被限制为仅在某些节点类型上运行。 例如头节点或工作器节点。

  • 脚本动作可以持久保存或临时作用

    • 持久化脚本操作必须具有唯一的名称。 持久化脚本用于在通过扩展操作添加到集群的新工作节点上进行自定义。 缩放作发生时,持久化脚本也可能将更改应用于另一个节点类型。 例如一个头节点。
    • 临时 脚本不会持久化。 用于群集创建期间的脚本操作会自动持久化。 它们不会应用于脚本运行后添加到群集的工作器节点。 然后,可以将 临时 脚本提升为持久化脚本,或将持久化脚本降级到 临时 脚本。 失败的脚本不会被持久化,即使你明确指示它们应该如此。
  • 脚本作可以接受脚本在执行期间使用的参数。

  • 脚本操作在群集节点上以root级权限运行。

  • 脚本作可以通过 Azure 门户、Azure PowerShell、Azure CLI 或 HDInsight .NET SDK 使用。

  • 删除或修改 VM 上的服务文件的脚本作可能会影响服务运行状况和可用性。

群集保留已运行的所有脚本的历史记录。 如果需要查找用于升级或降级操作的脚本 ID,历史记录会有所帮助。

重要

无法自动撤消脚本作所做的更改。 手动还原更改或提供一个脚本来还原这些更改。

Permissions

对于加入到域中的 HDInsight 群集,在群集上使用脚本操作时,需要两个必要的 Apache Ambari 权限:

  • AMBARI.RUN_CUSTOM_COMMAND。 默认情况下,Ambari 管理员角色具有此权限。
  • CLUSTER.RUN_CUSTOM_COMMAND. 默认情况下,HDInsight 群集管理员和 Ambari 管理员都具有此权限。

有关使用已加入域的 HDInsight 的权限的详细信息,请参阅 使用企业安全性包管理 HDInsight 群集

存取控制

如果您不是 Azure 订阅的管理员或所有者,则您的帐户必须至少具备Contributor访问包含 HDInsight 群集的资源组的权限。

至少具有对 Azure 订阅的参与者访问权限的人员必须事先注册资源供应商。 当具有订阅参与者访问权限的用户创建资源时,会发生服务提供者注册。 如果没有创建资源,请参阅 使用 REST 注册提供程序

获取有关使用访问管理的详细信息:

使用脚本的操作方法

可以选择将脚本作配置为在首次创建群集时运行,或者在现有群集上运行它。

群集创建过程中的脚本操作

创建群集期间使用的脚本作与在现有群集上运行的脚本作略有不同:

  • 脚本会自动保存。
  • 脚本中的失败可能导致群集创建过程失败。

在创建过程中脚本动作何时运行,下图说明:

群集创建过程中的阶段。

在配置 HDInsight 时运行脚本。 该脚本在群集中的所有指定节点上并行运行。 它在节点上以根权限运行。

可以执行停止和启动服务等操作,包括 Apache Hadoop 相关服务。 如果停止服务,请确保 Ambari 和其他 Hadoop 相关服务在脚本完成之前正在运行。 这些必需的服务在创建群集时确定群集的运行状况和状态。

在创建群集时,可以同时使用多个脚本操作。 按照指定这些脚本的顺序调用这些脚本。

注释

如果脚本存在于除指定为群集存储之外的任何其他存储帐户(在群集创建时),则需要公共访问。

重要

脚本动作必须在 60 分钟内完成,否则会超时。在集群配置期间,脚本与其他设置和配置进程同时运行。 可能会由于诸如 CPU 时间或网络带宽这样的资源竞争,导致脚本完成时间比在开发环境中所需时间更长。

若要最大程度地减少运行脚本所需的时间,请避免从源下载和编译应用程序等任务。 预编译应用程序和将二进制文件存储在 Azure 存储中。

对正在运行的群集执行脚本操作

已运行的群集上的脚本失败不会自动导致群集更改为失败状态。 脚本完成后,群集应返回到正在运行的状态。 即使群集处于运行状态,失败的脚本也可能导致系统故障。 例如,脚本可能会删除群集所需的文件。

脚本操作使用 root 权限运行。 在将脚本应用到群集之前,请确保了解脚本的作用。

将脚本应用到群集时,群集状态将从 “正在运行 ”更改为 “已接受”。 然后,它会更改为 HDInsight 配置 ,最后返回到 “运行” 以成功编写脚本。 脚本状态记录在脚本操作历史中。 此信息告知脚本是成功还是失败。 例如, Get-AzHDInsightScriptActionHistory PowerShell cmdlet 显示脚本的状态。 它返回类似于以下文本的信息:

ScriptExecutionId : 635918532516474303
StartTime         : 8/14/2017 7:40:55 PM
EndTime           : 8/14/2017 7:41:05 PM
Status            : Succeeded

重要

如果在创建群集后更改群集用户、管理员、密码,则针对此群集运行的脚本作可能会失败。 如果有任何持久化的脚本行为针对工作节点,那么在缩放群集时,这些脚本可能会失败。

示例脚本操作

脚本操作脚本可以通过以下工具来使用:

  • Azure 门户
  • Azure PowerShell
  • Azure CLI
  • HDInsight .NET SDK

HDInsight 提供脚本,用于在 HDInsight 群集上安装以下组件:

Name Script
添加 Azure 存储帐户 https://hdiconfigactions.blob.core.windows.net/linuxaddstorageaccountv01/add-storage-account-v01.sh。 请参阅 将其他存储帐户添加到 HDInsight
安装 Hue https://hdiconfigactions.blob.core.windows.net/linuxhueconfigactionv02/install-hue-uber-v02.sh。 请参阅 在 HDInsight Hadoop 群集上安装和使用 Hue
预加载 Hive 库 https://hdiconfigactions.blob.core.windows.net/linuxsetupcustomhivelibsv01/setup-customhivelibs-v01.sh。 创建 HDInsight 群集时,请参阅 添加自定义 Apache Hive 库

群集创建过程中的脚本操作

本部分介绍创建 HDInsight 群集时可以使用脚本作的不同方式。

在从 Azure 门户创建集群时使用脚本操作

  1. 使用 Azure 门户开始创建群集,如在 HDInsight 中创建基于 Linux 的群集中所述。 在“配置 + 定价”选项卡中,选择“+ 添加脚本操作”

    Azure 门户群集脚本动作。

  2. 使用 “选择脚本 ”条目选择预制脚本。 若要使用自定义脚本,请选择“ 自定义”。 然后为您的脚本提供 名称Bash 脚本 URI

    在选择脚本窗体中添加脚本。

    下表描述了窗体上的元素:

    资产 价值
    选择脚本 若要使用自己的脚本,请选择“ 自定义”。 否则,请选择其中一个提供的脚本。
    Name 指定脚本操作的名称。
    Bash 脚本 URI 指定脚本的 URI。
    主节点/工作节点/ZooKeeper 指定运行脚本的节点: HeadWorkerZooKeeper
    参数 指定参数(如果脚本需要)。

    使用 保存此脚本操作 条目来确保在缩放操作期间应用该脚本。

  3. 选择 “创建 ”以保存脚本。 然后,可以使用 + 提交新 脚本来添加另一个脚本。

    HDInsight 多个脚本操作。

    添加脚本后,返回到 “配置 + 定价 ”选项卡。

  4. 像往常一样完成剩余的群集创建步骤。

使用 Azure 资源管理器模板中的脚本操作

脚本操作可以与 Azure 资源管理器模板一起使用。 有关示例,请参阅 创建 HDInsight Linux 群集并运行脚本作

在此示例中,使用以下代码添加脚本操作:

"scriptActions": [
    {
        "name": "setenvironmentvariable",
        "uri": "[parameters('scriptActionUri')]",
        "parameters": "headnode"
    }
]

获取有关如何部署模板的详细信息:

在通过 Azure PowerShell 创建群集期间使用脚本操作

在本部分中,您将使用 Add-AzHDInsightScriptAction cmdlet 来调用脚本以自定义集群。 在开始之前,请确保安装和配置 Azure PowerShell。 若要使用这些 PowerShell 命令,需要 AZ 模块

以下脚本演示如何在使用 PowerShell 创建群集时应用脚本作:

# Login to your Azure subscription
$context = Get-AzContext
if ($context -eq $null) 
{
    Connect-AzAccount -Environment AzureChinaCloud
}
$context

# If you have multiple subscriptions, set the one to use
# $subscriptionID = "<subscription ID to use>"
# Select-AzSubscription -SubscriptionId $subscriptionID

# Get user input/default values
$resourceGroupName = Read-Host -Prompt "Enter the resource group name"
$location = Read-Host -Prompt "Enter the Azure region to create resources in"

# Create the resource group
New-AzResourceGroup -Name $resourceGroupName -Location $location

$defaultStorageAccountName = Read-Host -Prompt "Enter the name of the storage account"

# Create an Azure storae account and container
New-AzStorageAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $defaultStorageAccountName `
    -Type Standard_LRS `
    -Location $location
$defaultStorageAccountKey = (Get-AzStorageAccountKey `
                                -ResourceGroupName $resourceGroupName `
                                -Name $defaultStorageAccountName)[0].Value
$defaultStorageContext = New-AzStorageContext `
                                -StorageAccountName $defaultStorageAccountName `
                                -StorageAccountKey $defaultStorageAccountKey

# Get information for the HDInsight cluster
$clusterName = Read-Host -Prompt "Enter the name of the HDInsight cluster"
# Cluster login is used to secure HTTPS services hosted on the cluster
$httpCredential = Get-Credential -Message "Enter Cluster login credentials" -UserName "admin"
# SSH user is used to remotely connect to the cluster using SSH clients
$sshCredential = Get-Credential -Message "Enter SSH user credentials"

# Default cluster size (# of worker nodes), version, type, and OS
$clusterSizeInNodes = "4"
$clusterVersion = "3.5"
$clusterType = "Hadoop"
$clusterOS = "Linux"
# Set the storage container name to the cluster name
$defaultBlobContainerName = $clusterName

# Create a blob container. This holds the default data store for the cluster.
New-AzStorageContainer `
    -Name $clusterName -Context $defaultStorageContext

# Create an HDInsight configuration object
$config = New-AzHDInsightClusterConfig
# Add the script action
    $scriptActionUri="https://hdiconfigactions.blob.core.windows.net/linuxgiraphconfigactionv01/giraph-installer-v01.sh"
# Add for the head nodes
$config = Add-AzHDInsightScriptAction `
    -Config $config `
    -Name "Install Giraph" `
    -NodeType HeadNode `
    -Uri $scriptActionUri
# Continue adding the script action for any other node types
# that it must run on.
$config = Add-AzHDInsightScriptAction `
    -Config $config `
    -Name "Install Giraph" `
    -NodeType WorkerNode `
    -Uri $scriptActionUri

# Create the cluster using the configuration object
New-AzHDInsightCluster `
    -Config $config `
    -ResourceGroupName $resourceGroupName `
    -ClusterName $clusterName `
    -Location $location `
    -ClusterSizeInNodes $clusterSizeInNodes `
    -ClusterType $clusterType `
    -OSType $clusterOS `
    -Version $clusterVersion `
    -HttpCredential $httpCredential `
    -DefaultStorageAccountName "$defaultStorageAccountName.blob.core.chinacloudapi.cn" `
    -DefaultStorageAccountKey $defaultStorageAccountKey `
    -DefaultStorageContainer $containerName `
    -SshCredential $sshCredential

创建群集前可能需要几分钟时间。

在从 HDInsight .NET SDK 创建群集期间使用脚本操作

HDInsight .NET SDK 提供客户端库,以便更轻松地从 .NET 应用程序使用 HDInsight。 有关代码示例,请参阅 脚本作

对正在运行的群集执行脚本操作

本部分介绍如何在正在运行的群集上应用脚本作。

从 Azure 门户将脚本作应用到正在运行的群集

  1. 登录到 Azure 门户 并找到群集。

  2. 在默认视图中,在 “设置”下,选择“ 脚本作”。

  3. 脚本操作页顶部,选择+ 提交新

    将脚本添加到正在运行的群集。

  4. 使用 “选择脚本 ”条目选择预制脚本。 若要使用自定义脚本,请选择“ 自定义”。 然后为脚本提供 名称Bash 脚本 URI

    在选择脚本窗体中添加脚本。

    下表描述了窗体上的元素:

    资产 价值
    选择脚本 若要使用自己的脚本,请选择 “自定义”。 否则,请选择提供的脚本。
    Name 指定脚本操作的名称。
    Bash 脚本 URI 指定脚本的 URI。
    主节点/工作节点/Zookeeper 指定运行脚本的节点: HeadWorkerZooKeeper
    参数 指定参数(如果脚本需要)。

    使用 持久化此脚本操作 条目来确保在缩放操作期间应用脚本。

  5. 最后,选择“ 创建 ”按钮,将脚本应用到群集。

从 Azure PowerShell 将脚本作应用到正在运行的群集

若要使用这些 PowerShell 命令,需要 AZ 模块。 以下示例演示如何将脚本作应用于正在运行的群集:

# Get information for the HDInsight cluster
$clusterName = Read-Host -Prompt "Enter the name of the HDInsight cluster"
$scriptActionName = Read-Host -Prompt "Enter the name of the script action"
$scriptActionUri = Read-Host -Prompt "Enter the URI of the script action"
# The node types that the script action is applied to
$nodeTypes = "headnode", "workernode"

# Apply the script and mark as persistent
Submit-AzHDInsightScriptAction -ClusterName $clusterName `
    -Name $scriptActionName `
    -Uri $scriptActionUri `
    -NodeTypes $nodeTypes `
    -PersistOnSuccess

操作完成后,你会收到类似于以下文本的信息:

OperationState  : Succeeded
ErrorMessage    :
Name            : Giraph
Uri             : https://hdiconfigactions.blob.core.windows.net/linuxgiraphconfigactionv01/giraph-installer-v01.sh
Parameters      :
NodeTypes       : {HeadNode, WorkerNode}

通过 Azure CLI 将脚本作应用到正在运行的群集

在开始之前,请确保安装和配置 Azure CLI。 确保具有最新版本。 有关详细信息,请参阅安装 Azure CLI

  1. 对 Azure 订阅进行身份验证:

    az cloud set -n AzureChinaCloud
    az login
    # az cloud set -n AzureCloud   //means return to Public Azure.
    
  2. 将脚本作应用于正在运行的群集:

    az hdinsight script-action execute --cluster-name CLUSTERNAME --name SCRIPTNAME --resource-group RESOURCEGROUP --roles ROLES
    

    有效角色为 headnode,,workernodezookeepernodeedgenode。 如果脚本应用于多个节点类型,请用空格分隔角色。 例如,--roles headnode workernode

    若要持久保存脚本,请添加 --persist-on-success。 还可以稍后使用 az hdinsight script-action promote 来持久保存脚本。

使用 REST API 将脚本作应用到正在运行的群集

请参阅 Azure HDInsight 中的群集 REST API

从 HDInsight .NET SDK 向正在运行的集群应用脚本操作

有关使用 .NET SDK 将脚本应用到群集的示例,请参阅 针对正在运行的基于 Linux 的 HDInsight 群集应用脚本作

查看历史记录以及升级和降级脚本操作

Azure 门户

  1. 登录到 Azure 门户 并找到群集。

  2. 在默认视图中,在 “设置”下,选择“ 脚本作”。

  3. 此群集的脚本历史记录显示在脚本操作部分。 此信息包括持久化脚本的列表。 以下屏幕截图显示 Solr 脚本已在此群集上运行。 屏幕截图不显示任何持久化脚本。

    门户脚本操作的提交历史记录。

  4. 从历史记录中选择一个脚本,以显示此脚本的 “属性” 部分。 在屏幕顶部,可以重新运行脚本或提升脚本。

    脚本操作属性促进。

  5. 还可以在脚本操作部分的条目右侧选择省略号 ...,以执行操作。

    删除持久化的脚本操作。

Azure PowerShell

cmdlet 功能
Get-AzHDInsightPersistedScriptAction 检索有关持久化脚本操作的信息。 此 cmdlet 不会撤消脚本执行的作,只会删除持久化标志。
Get-AzHDInsightScriptActionHistory 检索应用于群集的脚本作的历史记录或特定脚本的详细信息。
Set-AzHDInsightPersistedScriptAction ad hoc 脚本作提升为持久化脚本作。
Remove-AzHDInsightPersistedScriptAction 将持久化脚本操作降级为ad hoc操作。

以下示例脚本演示如何使用 cmdlet 来提升并降级脚本。

# Get a history of scripts
Get-AzHDInsightScriptActionHistory -ClusterName mycluster

# From the list, we want to get information on a specific script
Get-AzHDInsightScriptActionHistory -ClusterName mycluster `
    -ScriptExecutionId 635920937765978529

# Promote this to a persisted script
# Note: the script must have a unique name to be promoted
# if the name is not unique, you receive an error
Set-AzHDInsightPersistedScriptAction -ClusterName mycluster `
    -ScriptExecutionId 635920937765978529

# Demote the script back to ad hoc
# Note that demotion uses the unique script name instead of
# execution ID.
Remove-AzHDInsightPersistedScriptAction -ClusterName mycluster `
    -Name "Install Giraph"

Azure CLI

Command Description
az hdinsight script-action delete 删除群集的指定持久化脚本操作。 此命令不会撤消脚本执行的作,只会删除持久化标志。
az hdinsight script-action execute 对指定的 HDInsight 群集执行脚本作。
az hdinsight script-action list 列出指定群集的所有持久化脚本操作。
az hdinsight script-action list-execution-history 列出指定群集的所有脚本执行历史记录。
az hdinsight script-action promote 将指定的即席脚本执行提升为持久化脚本。
az hdinsight script-action show-execution-details 获取给定脚本执行 ID 的脚本执行详细信息。

HDInsight .NET SDK

有关使用 .NET SDK 从群集检索脚本历史记录、升级或降级脚本的示例,请参阅针对正在运行的基于 Linux 的 HDInsight 群集应用脚本作。

注释

此示例还演示如何使用 .NET SDK 安装 HDInsight 应用程序。

后续步骤