自定义引导操作 - Amazon ParallelCluster
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

自定义引导操作

Amazon ParallelCluster 可以在创建集群时的主引导操作之前(安装前)或之后(安装后)运行任意代码。在大多数情况下,此代码存储在 Amazon Simple Storage Service (Amazon S3) 中并通过 HTTPS 连接进行访问。此代码将以 root 用户身份运行,可以采用集群操作系统支持的任何脚本语言。代码通常采用 BashPython 语言。

在开始任何集群部署引导操作(例如配置 NAT、Amazon Elastic Block Store (Amazon EBS) 或调度器)之前调用预安装操作。某些预安装操作包括修改存储、添加额外的用户和添加程序包。

集群引导过程完成后调用安装后操作。安装后操作充当在实例可视为已完全配置并完成之前执行的最后操作。某些安装后操作包括更改调度器设置、修改存储和修改程序包。

您可以通过在配置期间指定参数,将参数传递到脚本。为此,需要将这些参数包含在双引号中并传递到安装前或安装后操作。

如果安装前或安装后操作失败,则该实例引导也将失败。退出代码为零 (0) 表示成功。任何其他退出代码都表示实例引导失败。

您可以区分正在运行的头节点和计算节点。获取 /etc/parallelcluster/cfnconfig 文件并评估对于头节点和计算节点分别具有“MasterServer”和“ComputeFleet”值的 cfn_node_type 环境变量。

#!/bin/bash . "/etc/parallelcluster/cfnconfig" case "${cfn_node_type}" in MasterServer) echo "I am the head node" >> /tmp/head.txt ;; ComputeFleet) echo "I am a compute node" >> /tmp/compute.txt ;; *) ;; esac

配置

以下配置设置用于定义安装前与安装后操作和参数。

# URL to a preinstall script. This is run before any of the boot_as_* scripts are run # (no default) pre_install = https://<bucket-name>.s3.amazonaws.com/my-pre-install-script.sh # Arguments to be passed to preinstall script # (no default) pre_install_args = argument-1 argument-2 # URL to a postinstall script. This is run after any of the boot_as_* scripts are run # (no default) post_install = https://<bucket-name>.s3.amazonaws.com/my-post-install-script.sh # Arguments to be passed to postinstall script # (no default) post_install_args = argument-3 argument-4

参数

前两个参数(即 $0$1)是为脚本名称和 URL 预留的。

$0 => the script name $1 => s3 url $n => args set by pre/post_install_args

示例

以下步骤创建一个简单的安装后脚本,此脚本用于在集群中安装 R 程序包。

  1. 创建脚本。

    #!/bin/bash echo "post-install script has $# arguments" for arg in "$@" do echo "arg: ${arg}" done yum -y install "${@:2}"
  2. 使用正确的权限将脚本上传到 Amazon S3。如果公共读取权限不适合您,可使用 s3_read_resources3_read_write_resource 参数来授予访问权限。有关更多信息,请参阅 使用 Amazon S3

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

    如果脚本是在 Windows 上编辑的,则必须将行结尾从 CRLF 更改为 LF,然后才能将脚本上传到 Amazon S3。

  3. 更新 Amazon ParallelCluster 配置以包含新的安装后操作。

    [cluster default] ... post_install = https://<bucket-name>.s3.amazonaws.com/myscript.sh post_install_args = 'R curl wget'

    如果存储桶没有公共读取权限,则使用 s3 作为 URL 协议。

    [cluster default] ... post_install = s3://<bucket-name>/myscript.sh post_install_args = 'R curl wget'
  4. 启动集群。

    $ pcluster create mycluster
  5. 验证输出。

    $ less /var/log/cfn-init.log 2019-04-11 10:43:54,588 [DEBUG] Command runpostinstall output: post-install script has 4 arguments arg: s3://<bucket-name>/test.sh 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