本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
自定义引导操作
Amazon ParallelCluster可以在创建集群时在主引导操作之前(预安装)或之后(安装后)运行任意代码。在大多数情况下,此代码存储在 Amazon Simple Simple Storage Service (Amazon S3) 中并通过 HTTPS 连接进行访问。该代码以 root 身份运行,可以使用集群操作系统支持的任何脚本语言。代码通常在 Bash 或 Python 中使用。
预安装操作在启动任何集群部署引导操作之前调用,例如配置 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
Arguments
前两个参数(即 $0
和 $1
)是为脚本名称和 URL 预留的。
$0 => the script name $1 => s3 url $n => args set by pre/post_install_args
示例
以下步骤创建一个简单的安装后脚本,此脚本用于在集群中安装 R 程序包。
-
创建脚本。
#!/bin/bash echo "post-install script has $# arguments" for arg in "$@" do echo "arg: ${arg}" done yum -y install "${@:2}"
-
将具有正确权限的脚本上载到 Amazon S3。如果公共读取权限不适合您,请使用s3_read_resource或s3_read_write_resource参数授予访问权限。有关更多信息,请参阅使用 Amazon S3:
$
aws s3 cp --acl public-read
/path/to/myscript.sh
s3://<bucket-name>/myscript.sh
重要
如果在 Windows 上编辑脚本,则在将脚本上传到 Amazon S3 之前,必须将行结尾从 CRLF 更改为 LF。
-
更新 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' -
启动集群。
$
pcluster create mycluster
-
验证输出。
$
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