自訂引導動作 - AWS ParallelCluster

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

自訂引導動作

如果您定義 HeadNode/CustomActions/OnNodeStart組態設定,請在節點啟動後立即 AWS ParallelCluster 執行任意程式碼。如果您定義 HeadNode/CustomActions/OnNodeConfigured配置設置,則在節點配置正確完成後 AWS ParallelCluster 運行代碼。

從 AWS ParallelCluster 版本 3.4.0 開始,如果您定義 HeadNode/CustomActions/OnNodeUpdated配置設置,則可以在頭節點更新後運行代碼。

在大多數情況下,此程式碼會儲存在 Amazon Simple Storage Service (Amazon S3) 中,並透過 HTTPS 連線存取。程式碼會以叢集作業系統支援的任何指令碼語言執行,root而且可以使用任何指令碼語言執行。通常代碼是在 BashPython 中。

注意

從 AWS ParallelCluster 版本 3.7.0 開始,叢集 Imds/ImdsSupport設定預設值為v2.0

當您建立要升級至 3.7.0 版及更新版本的新叢集時,請將自訂啟動程序動作指令碼更新為與 IMDSv2 相容,或在叢集配置檔v1.0ImdsSupportImds/設定為。

警告

您必須負責設定共用職責模型中所述的自訂指令集與引數。確認您的自訂啟動程序指令碼和引數來自您信任的來源,可完整存取叢集節點。

警告

AWS ParallelCluster 不支持使用通過/etc/parallelcluster/cfnconfig文件提供的內部變量。此檔案可能會在 future 版本中移除。

OnNodeStart在啟動任何節點部署啟動程序動作 (例如設定 NAT、Amazon Elastic Block Store (Amazon EBS) 或排程器之前,都會呼叫動作。 OnNodeStart引導操作可能包括修改存儲,添加額外的用戶和添加軟件包。

注意

如果您為叢集設定 DirectoryServiceHeadNodeCustomActions//OnNodeStart指令碼,請在執行 AWS ParallelCluster 指令碼之前設定DirectoryServicesssd並重新啟動。OnNodeStart

OnNodeConfigured動作會在節點啟動程序完成後呼叫。 OnNodeConfigured動作會在將執行個體視為完全設定並完成之前發生的最後一個動作。某些OnNodeConfigured動作包括變更排程器設定、修改儲存區和修改套件。您可以通過在配置過程中指定參數來將參數傳遞給腳本。

OnNodeUpdated在頭節點更新完成,並且排程器和共用儲存裝置與最新的叢集配置變更一致之後,會呼叫動作。

OnNodeConfigured自訂動作成功OnNodeStart或自訂動作成功時,會以結束代碼零 (0) 表示成功。任何其他結束代碼表示執行個體啟動程序失敗。

OnNodeUpdated自定義操作成功時,成功的信號與退出代碼零 (0)。任何其他結束代碼都表示更新失敗。

注意

如果您進行設定 OnNodeUpdated,則必須在更新失敗時手OnNodeUpdated動將動作還原到先前的狀態。

如果自OnNodeUpdated訂動作失敗,更新會復原至先前的狀態。不過,OnNodeUpdated動作只會在更新時執行,而不會在堆疊回復時執行。

您可以在/和 HeadNode//CustomActionsCustomActions配置部分為頭節點和每個隊列指定不同的腳本。SchedulingSlurmQueues OnNodeUpdated只能在HeadNode區段中設定。

注意

在 3.0 AWS ParallelCluster 版之前,無法為頭節點和計算節點指定不同的腳本。請參閱 從 AWS ParallelCluster 2.x 移動到 3.x

組態

以下配置設置用於定義 HeadNodeCustomActions//OnNodeStartOnNodeConfigured& OnNodeUpdatedScheduling/CustomActions/OnNodeStart& OnNodeConfigured操作和參數。

HeadNode: [...] CustomActions: OnNodeStart: # Script URL. This is run before any of the bootstrap scripts are run Script: s3://bucket-name/on-node-start.sh Args: - arg1 OnNodeConfigured: # Script URL. This is run after all the bootstrap scripts are run Script: s3://bucket-name/on-node-configured.sh Args: - arg1 OnNodeUpdated: # Script URL. This is run after the head node update is completed. Script: s3://bucket-name/on-node-updated.sh Args: - arg1 # Bucket permissions Iam: S3Access: - BucketName: bucket_name EnableWriteAccess: false Scheduling: Scheduler: slurm [...] SlurmQueues: - Name: queue1 [...] CustomActions: OnNodeStart: Script: s3://bucket-name/on-node-start.sh Args: - arg1 OnNodeConfigured: Script: s3://bucket-name/on-node-configured.sh Args: - arg1 Iam: S3Access: - BucketName: bucket_name EnableWriteAccess: false

使用Sequence設置(在 AWS ParallelCluster 版本 3.6.0 中添加):

HeadNode: [...] CustomActions: OnNodeStart: # Script URLs. The scripts are run in the same order as listed in the configuration, before any of the bootstrap scripts are run. Sequence: - Script: s3://bucket-name/on-node-start1.sh Args: - arg1 - Script: s3://bucket-name/on-node-start2.sh Args: - arg1 [...] OnNodeConfigured: # Script URLs. The scripts are run in the same order as listed in the configuration, after all the bootstrap scripts are run. Sequence: - Script: s3://bucket-name/on-node-configured1.sh Args: - arg1 - Script: s3://bucket-name/on-node-configured2.sh Args: - arg1 [...] OnNodeUpdated: # Script URLs. The scripts are run in the same order as listed in the configuration, after the head node update is completed. Sequence: - Script: s3://bucket-name/on-node-updated1.sh Args: - arg1 - Script: s3://bucket-name/on-node-updated2.sh Args: - arg1 [...] # Bucket permissions Iam: S3Access: - BucketName: bucket_name EnableWriteAccess: false Scheduling: Scheduler: slurm [...] SlurmQueues: - Name: queue1 [...] CustomActions: OnNodeStart: # Script URLs. The scripts are run in the same order as listed in the configuration, before any of the bootstrap scripts are run Sequence: - Script: s3://bucket-name/on-node-start1.sh Args: - arg1 - Script: s3://bucket-name/on-node-start2.sh Args: - arg1 [...] OnNodeConfigured: # Script URLs. The scripts are run in the same order as listed in the configuration, after all the bootstrap scripts are run Sequence: - Script: s3://bucket-name/on-node-configured1.sh Args: - arg1 - Script: s3://bucket-name/on-node-configured2.sh Args: - arg1 [...] Iam: S3Access: - BucketName: bucket_name EnableWriteAccess: false

從 AWS ParallelCluster 版本 3.6.0 開始新增此Sequence設定。當您指定時Sequence,您可以列出自訂動作的多個指令碼。 AWS ParallelCluster 繼續支援使用單一指令碼設定自訂動作,但不包含Sequence

AWS ParallelCluster 不支持包括單個腳本和相同Sequence的自定義操作。例如,如 AWS ParallelCluster 果您指定下列組態,就會失敗。

[...] CustomActions: OnNodeStart: # Script URL. This is run before any of the bootstrap scripts are run Script: s3://bucket-name/on-node-start.sh Args: - arg1 # Script URLs. The scripts are run in the same order as listed in the configuration, before any of the bootstrap scripts are run. Sequence: - Script: s3://bucket-name/on-node-start1.sh Args: - arg1 - Script: s3://bucket-name/on-node-start2.sh Args: - arg1 [...]

引數

注意

在 AWS ParallelCluster 2.x 中,$1參數是一個保留的,用於存儲自定義腳本的 URL。如果要重新使用為 AWS ParallelCluster 2.x 和 AWS ParallelCluster 3.x 創建的自定義引導腳本,則需要通過考慮參數的移位來調整它們。請參閱 從 AWS ParallelCluster 2.x 移動到 3.x

包含自訂引導動作的範例叢集

下列步驟會建立在設定節點後執行的簡單指令碼,以便在叢集的節點中安裝R,curlwget套件。

  1. 建立指令碼。

    #!/bin/bash echo "The script has $# arguments" for arg in "$@" do echo "arg: ${arg}" done yum -y install "${@:1}"
  2. 將具有正確許可的指令碼上傳到 Amazon S3。如果公共讀取權限不適合您,請使用 HeadNode/Iam/S3AccessScheduling/SlurmQueues配置部分。如需詳細資訊,請參閱 使用 Amazon S3

    $ aws s3 cp --acl public-read /path/to/myscript.sh s3://<bucket-name>/myscript.sh
    重要

    如果指令碼是在 Windows 上編輯的,則必須先將行尾從 CRLF 變更為 LF,才能將指令碼上傳到 Amazon S3。

  3. 更新組 AWS ParallelCluster 態以包含新OnNodeConfigured動作。

    CustomActions: OnNodeConfigured: Script: https://<bucket-name>.s3.<region>.amazonaws.com/myscript.sh Args: - "R" - "curl" - "wget"

    如果值區沒有公開讀取權限,請s3做為 URL 通訊協定使用。

    CustomActions: OnNodeConfigured: Script: s3://<bucket-name>/myscript.sh Args: - "R" - "curl" - "wget"
  4. 啟動叢集。

    $ pcluster create-cluster --cluster-name mycluster \ --region <region> --cluster-configuration config-file.yaml
  5. 驗證輸出。

    • 如果您已將自訂動作新增至HeadNode組態,請登入 head 節點,並/var/log/cfn-init.log透過執行下列命令來檢查位於的cfn-init.log檔案:

      $ less /var/log/cfn-init.log 2021-09-03 10:43:54,588 [DEBUG] Command run postinstall output: The script has 3 arguments arg: R arg: curl arg: wget Loaded plugins: dkms-build-requires, priorities, update-motd, upgrade-helper Package R-3.4.1-1.52.amzn1.x86_64 already installed and latest version Package curl-7.61.1-7.91.amzn1.x86_64 already installed and latest version Package wget-1.18-4.29.amzn1.x86_64 already installed and latest version Nothing to do
    • 如果您已將自訂動作新增至SlurmQueues設定,請勾選cloud-init.log/var/log/cloud-init.log於計算節點中的。用 CloudWatch 於檢視這些記錄檔。

    您可以在 Amazon CloudWatch 主控台中檢視這兩個日誌。如需詳細資訊,請參閱 與 Amazon Amazon Amazon CloudWatch 的整合

更新 IMDSv2 的自訂啟動程序指令碼範例

在下列範例中,我們更新了與 IMDSv1 搭配使用的自訂啟動程序動作指令碼,以搭配 IMDSv2 使用。IMDSv1 指令碼會擷取亞馬遜 EC2 執行個體 AMI ID 中繼資料。

#!/bin/bash AMI_ID=$(curl http://169.254.169.254/latest/meta-data/ami-id) echo $AMI_ID >> /home/ami_id.txt

以下顯示修改為與 IMDSv2 相容的自訂啟動程序動作指令碼。

#!/bin/bash AMI_ID=$(TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/ami-id) echo $AMI_ID >> /home/ami_id.txt

如需詳細資訊,請參閱《Amazon EC2 Linux 執行個體使用者指南》中的擷取執行個體中繼資料

更新 IMDSv1 組態的範例

以下是使用 3.7.0 及更 AWS ParallelCluster 舊版本時支援 IMDSv1 的叢集配置範例。

Region: us-east-1 Imds: ImdsSupport: v1.0 Image: Os: alinux2 HeadNode: InstanceType: t2.micro Networking: SubnetId: subnet-abcdef01234567890 Ssh KeyName: key-name CustomActions: OnNodeConfigured: Script: Script-path Scheduling: Scheduler: slurm SlurmQueues: - Name: queue1 CustomActions: OnNodeConfigured: Script: Script-path ComputeResources: - Name: t2micro Instances: - InstanceType: t2.micro MinCount: 11 Networking: SubnetIds: - subnet-abcdef01234567890