自定义引导操作 - 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 兼容,或者在集群配置文件中将 Imds/ImdsSupport 设置为 v1.0

警告

按照责任共担模式所述,您负责配置自定义脚本和参数。验证您的自定义引导脚本和参数是否来自您信任的可以完全访问集群节点的来源。

警告

AWS ParallelCluster 不支持使用通过 /etc/parallelcluster/cfnconfig 文件提供的内部变量。此文件可能会在未来版本中删除。

在开始任何节点部署引导操作(例如配置 NAT、Amazon Elastic Block Store (Amazon EBS) 或调度器)之前,将会调用 OnNodeStart 操作。OnNodeStart 引导操作可能包括修改存储、添加额外的用户和添加程序包。

注意

如果您为集群配置 DirectoryServiceHeadNode/CustomActions/OnNodeStart 脚本,则 AWS ParallelCluster 会在运行 OnNodeStart 脚本之前配置 DirectoryService 并重启 sssd

节点引导过程完成后将会调用 OnNodeConfigured 操作。OnNodeConfigured 操作是实例被视为完全配置并已完成之前执行的最后操作。某些 OnNodeConfigured 操作包括更改调度器设置、修改存储和修改程序包。您可以通过在配置期间指定参数,将参数传递到脚本。

在头节点更新完成并且调度器和共享存储与最新的集群配置更改保持一致之后,将会调用 OnNodeUpdated 操作。

OnNodeStartOnNodeConfigured 自定义操作成功时,将使用退出代码零 (0) 来表示成功。任何其他退出代码都表示实例引导失败。

OnNodeUpdated 自定义操作成功时,将使用退出代码零 (0) 来表示成功。任何其他退出代码都表示更新失败。

注意

如果配置 OnNodeUpdated,则在更新失败时,必须将 OnNodeUpdated 操作手动恢复到先前的状态。

如果 OnNodeUpdated 自定义操作失败,则更新将回滚到之前的状态。但 OnNodeUpdated 操作仅在更新时运行,而不是在堆栈回滚时运行。

您可以在 HeadNode/CustomActionsScheduling/SlurmQueues/CustomActions 配置部分中为头节点和每个队列指定不同的脚本。OnNodeUpdated 只能在 HeadNode 部分中进行配置。

注意

在 AWS ParallelCluster 版本 3.0 之前,无法为头节点和计算节点指定不同的脚本。请参阅 从 AWS ParallelCluster 2.x 迁移到 3.x

配置

以下配置设置用于定义 HeadNode/CustomActions/OnNodeStartOnNodeConfiguredOnNodeUpdated 以及 Scheduling/CustomActions/OnNodeStartOnNodeConfigured 操作和参数。

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 3.x 重复使用为 AWS ParallelCluster 2.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 配置中,请通过运行以下命令,登录到头节点并检查位于 /var/log/cfn-init.logcfn-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 设置中,请检查位于计算节点中 /var/log/cloud-init.log 处的 cloud-init.log。用于 CloudWatch 查看这些日志。

    您可以在 Amazon CloudWatch 控制台中查看这两个日志。有关更多信息,请参阅与 Amazon CloudWatch Logs 集成

更新 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

有关更多信息,请参阅 EC2 用户指南(适用于 Linux 实例)中的检索实例元数据

更新 IMDSv1 配置的示例

下面是使用 AWS ParallelCluster 3.7.0 及更早版本时支持 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