使用指令碼動作來自訂 Azure HDInsight 叢集

Azure HDInsight 提供稱為 腳本動作 的組態方法,可叫用自定義腳本來自定義叢集。 這些腳本可用來安裝其他元件並變更組態設定。 腳本動作可以在叢集建立期間或之後使用。

腳本動作也可以發佈至 Azure Marketplace 作為 HDInsight 應用程式。 如需 HDInsight 應用程式的詳細資訊,請參閱 在 Azure Marketplace 中發布 HDInsight 應用程式。

瞭解腳本動作

腳本動作是在 HDInsight 叢集中的節點上執行的 Bash 腳本。 文稿動作的特性和功能如下:

  • Bash 腳本 URI(存取檔案的位置)必須可從 HDInsight 資源提供者和叢集存取。

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

    • 針對一般 (非 ESP) 叢集:

      • Azure 儲存體 帳戶中的 Blob,這是 HDInsight 叢集的主要或額外記憶體帳戶。 HDInsight 會在叢集建立期間授與這兩種類型的記憶體帳戶存取權。

        重要

        請勿輪替此 Azure 儲存體 帳戶上的記憶體密鑰,因為它會導致後續腳本動作與儲存在那裡的腳本失敗。

      • Data Lake 儲存體 Gen1:服務主體 HDInsight 用來存取 Data Lake 儲存體 必須具有腳本的讀取許可權。 Bash 文稿 URI 格式為 adl://DATALAKESTOREACCOUNTNAME.azuredatalakestore.net/path_to_file

      • 不建議使用 Data Lake 儲存體 Gen2 來執行腳本動作。 abfs:// Bash 腳本 URI 不支援。 https:// URI 是可能的,但這些適用於具有公用存取權的容器,以及針對 HDInsight 資源提供者開啟的防火牆,因此不建議使用。

      • 可透過 https:// 路徑存取的公用檔案共享服務。 範例包括 Azure Blob、GitHub 或 OneDrive。 例如 URI,請參閱 範例腳本動作腳本

    • 針對具有 ESP 的 wasb:// 叢集,支援 或 wasbs://http[s]:// URI。

  • 腳本動作只能限制在特定節點類型上執行。 例如前端節點或背景工作節點。

  • 腳本動作可以保存或 臨機操作

    • 持續性腳本動作必須具有唯一的名稱。 持續性腳本可用來自定義透過調整作業新增至叢集的新背景工作節點。 當發生調整作業時,保存的腳本也可能將變更套用至另一個節點類型。 例如前端節點。
    • 臨機操作 腳本不會保存。 叢集建立期間使用的腳本動作會自動保存。 它們不會套用至腳本執行之後新增至叢集的背景工作節點。 然後,您可以將臨機操作腳本升級為持續性腳本,或將保存的腳本降級為臨機操作腳本。 失敗的腳本不會保存,即使您特別指出它們應該是。
  • 腳本動作可以接受腳本在執行期間所使用的參數。

  • 腳本動作會在叢集節點上以根層級許可權執行。

  • 腳本動作可以透過 Azure 入口網站、Azure PowerShell、Azure CLI 或 HDInsight .NET SDK 使用。

  • 在 VM 上移除或修改服務檔案的腳本動作可能會影響服務健康情況和可用性。

叢集會保留所有已執行的腳本歷程記錄。 當您需要尋找腳本標識碼以進行升階或降級作業時,歷程記錄會有所説明。

重要

無法自動復原文稿動作所做的變更。 手動反轉變更,或提供反轉這些變更的腳本。

權限

針對已加入網域的 HDInsight 叢集,當您搭配叢集使用腳本動作時,需要有兩個 Apache Ambari 許可權:

  • AMBARI。RUN_CUSTOM_COMMAND。 Ambari 管理員 istrator 角色預設具有此許可權。
  • 集群。RUN_CUSTOM_COMMAND。 HDInsight 叢集 管理員 istrator 和 Ambari 管理員 istrator 預設都有此許可權。

如需使用已加入網域的 HDInsight 許可權的詳細資訊,請參閱 使用企業安全性套件管理 HDInsight 叢集。

存取控制

如果您不是 Azure 訂用帳戶的系統管理員或擁有者,您的帳戶必須至少有 Contributor 包含 HDInsight 叢集的資源群組存取權。

至少具有 Azure 訂用帳戶參與者存取權的人員必須先前已註冊提供者。 當具有訂用帳戶參與者存取權的使用者建立資源時,就會進行提供者註冊。 若未建立資源,請參閱 使用 REST 註冊提供者。

取得使用存取管理的詳細資訊:

使用腳本動作的方法

您可以選擇設定文稿動作,以在叢集第一次建立時執行,或是在現有的叢集上執行。

叢集建立程式中的腳本動作

在叢集建立期間使用的文稿動作與在現有叢集上執行的腳本動作稍有不同:

  • 腳本會自動保存。
  • 腳本中的失敗可能會導致叢集建立程序失敗。

下圖說明文稿動作在建立程式期間何時執行:

Stages during cluster creation.

腳本會在設定 HDInsight 時執行。 腳本會在叢集中的所有指定節點上平行執行。 它會以節點上的根許可權執行。

您可以執行停止和啟動服務等作業,包括 Apache Hadoop 相關服務。 如果您停止服務,請確定Ambari和其他Hadoop相關服務在腳本完成之前正在執行。 這些必要服務會決定叢集建立時的健全狀況和狀態。

在叢集建立期間,您可以一次使用許多腳本動作。 這些文稿會依照指定的順序叫用。

注意

如果腳本存在於指定為叢集記憶體的任何其他記憶體帳戶中(在叢集建立時間),則需要公用存取。

重要

腳本動作必須在 60 分鐘內完成,或逾時。在叢集布建期間,腳本會與其他安裝和設定程式同時執行。 競爭 CPU 時間或網路頻寬等資源,可能會導致腳本比開發環境中需要更長的時間才能完成。

若要將執行腳本所需的時間降到最低,請避免從來源下載和編譯應用程式等工作。 先行編譯應用程式,並將二進位檔儲存在 Azure 儲存體 中。

執行中叢集上的腳本動作

已在執行中叢集上的腳本失敗不會自動造成叢集變更為失敗狀態。 腳本完成之後,叢集應該會回到執行中狀態。 即使叢集處於執行中狀態,失敗的腳本可能已中斷。 例如,腳本可能會刪除叢集所需的檔案。

腳本動作會以根許可權執行。 在將腳本套用至叢集之前,請確定您瞭解腳本的功能。

當您將腳本套用至叢集時,叢集狀態會從 [執行] 變更為 [已接受]。 然後它會變更為 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 叢集上安裝下列元件:

名稱 指令碼
新增 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 portal cluster script action.

  2. 使用 [ 選取腳本 ] 項目來選取預先建立的腳本。 若要使用自定義文本,請選取 [ 自定義]。 然後提供腳本的名稱Bash腳本URI

    Add a script in the select script form.

    下表描述表單上的元素:

    屬性
    選取腳本 若要使用您自己的腳本,請選取 [ 自定義]。 否則,請選取其中一個提供的腳本。
    名稱 指定文稿動作的名稱。
    Bash 指令碼 URI 指定文稿的 URI。
    Head/Worker/ZooKeeper 指定腳本執行所在的節點: HeadWorkerZooKeeper
    參數 如果腳本需要,請指定參數。

    使用保存 此腳本動作 專案,以確定腳本會在調整作業期間套用。

  3. 選取 [建立] 以儲存腳本。 然後,您可以使用 +提交新的 來新增另一個腳本。

    HDInsight multiple script actions.

    當您完成新增文稿時,您會返回 [ 組態 + 定價 ] 索引標籤。

  4. 如往常一樣完成其餘的叢集建立步驟。

使用來自 Azure Resource Manager 範本動作

腳本動作可以搭配 Azure Resource Manager 範本使用。 如需範例,請參閱 建立 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
}
$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.windows.net" `
    -DefaultStorageAccountKey $defaultStorageAccountKey `
    -DefaultStorageContainer $containerName `
    -SshCredential $sshCredential

建立叢集前可能需要幾分鐘的時間。

從 HDInsight .NET SDK 建立叢集期間使用腳本動作

HDInsight .NET SDK 提供用戶端連結庫,可讓您更輕鬆地從 .NET 應用程式使用 HDInsight。 如需程式代碼範例,請參閱 腳本動作

執行中叢集的指令碼動作

本節說明如何在執行中的叢集上套用腳本動作。

從 Azure 入口網站 將腳本動作套用至執行中的叢集

  1. 登入 Azure 入口網站 並找出您的叢集。

  2. 從預設檢視的 [設定] 底下,選取 [腳本動作]。

  3. 從 [腳本動作] 頁面頂端,選取 [+ 提交新]。

    Add a script to a running cluster.

  4. 使用 [ 選取腳本 ] 項目來選取預先建立的腳本。 若要使用自定義文本,請選取 [ 自定義]。 然後提供腳本的名稱Bash腳本URI

    Add a script in the select script form.

    下表描述表單上的元素:

    屬性
    選取腳本 若要使用您自己的腳本,請選取 [自定義]。 否則,請選取提供的腳本。
    名稱 指定文稿動作的名稱。
    Bash 指令碼 URI 指定文稿的 URI。
    Head/Worker/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 login
    
  2. 將文稿動作套用至執行中的叢集:

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

    有效的角色為 headnode、、zookeepernodeworkernodeedgenode。 如果腳本應該套用至數個節點類型,請以空格分隔角色。 例如: --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 腳本已在此叢集上執行。 此螢幕快照不會顯示任何持續性腳本。

    Portal script actions submit history.

  4. 從歷程記錄中選取文稿,以顯示此腳本的 [屬性 ] 區段。 從畫面頂端,您可以重新執行腳本或升級腳本。

    Script actions properties promote.

  5. 您也可以選取文稿動作區段上專案右邊的省略號 ...,以執行動作。

    Persisted script actions delete.

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 描述
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 取得指定文稿執行標識碼的腳本執行詳細數據。

HDInsight .NET SDK

如需使用 .NET SDK 從叢集擷取腳本歷程記錄、升階或降級腳本的範例,請參閱針對執行中以Linux為基礎的 HDInsight 叢集套用腳本動作。

注意

此範例也會示範如何使用 .NET SDK 來安裝 HDInsight 應用程式。

下一步