使用 Amazon EC2 竞价型实例 - FSx for Lustre
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 Amazon EC2 竞价型实例

FSx for Lustre 可以与 EC2 竞价型实例一起使用,从而显著降低 Amazon EC2 的成本。Spot 实例是一种未使用的 EC2 实例,以低于按需价格提供。在竞价型实例价格超过您的最高价、竞价型实例需求增加、竞价型实例供应减少时,Amazon EC2 可能会中断您的竞价型实例。

在 Amazon EC2 中断竞价型实例时,将提供竞价型实例中断通知,这会在 Amazon EC2 终止该实例之前为其提供两分钟的警告。有关更多信息,请参阅《适用于 Linux 实例的 Amazon EC2 用户指南》中的竞价型实例

为确保 Amazon FSx 文件系统不受 EC2 竞价型实例中断的影响,我们建议在 EC2 竞价型实例终止或休眠之前卸载 Amazon FSx 文件系统。有关更多信息,请参阅 卸载文件系统

处理 Amazon EC2 竞价型实例中断事件

FSx for Lustre 是一个分布式文件系统,其中服务器和客户端实例相互协作,提供可靠的高性能文件系统。它们在客户端和服务器实例之间保持一致的分布式状态。FSx for Lustre 服务器在客户端主动执行 I/O 和缓存文件系统数据时会向其委派临时访问权限。当服务器请求客户端撤销其临时访问权限时,客户端应在短时间内做出回复。为了保护文件系统免受操作不良的客户端的侵害,服务器可以几分钟后驱逐没有响应的 Lustre 客户端。为了避免无响应的客户端等待数分钟才回复服务器请求,请务必彻底卸载 Lustre 客户端,尤其是在终止 EC2 竞价型实例之前。

EC2 竞价型实例会在关闭实例前提前 2 分钟发送终止通知。我们建议您在终止 EC2 竞价型实例之前,自动执行彻底卸载 Lustre 客户端的过程。

例 – 彻底卸载终止 EC2 竞价型实例的脚本

此示例脚本通过执行以下操作彻底卸载终止 EC2 竞价型实例:

  • 关注竞价型实例终止通知。

  • 当它收到终止通知时,会:

    • 停止运行正在访问文件系统的应用程序。

    • 在实例终止前卸载文件系统。

您可以根据需要调整脚本,尤其是在正常关闭应用程序时。有关处理竞价型实例中断的最佳实践的更多信息,请参阅 Best practices for handling EC2 Spot Instance interruptions

#!/bin/bash # TODO: Specify below the FSx mount point you are using *FSXPATH=/fsx* cd / TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") if [ "$?" -ne 0 ]; then echo "Error running 'curl' command" >&2 exit 1 fi # Periodically check for termination while sleep 5 do HTTP_CODE=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s -w %{http_code} -o /dev/null http://169.254.169.254/latest/meta-data/instance-action) if [[ "$HTTP_CODE" -eq 401 ]] ; then # Refreshing Authentication Token TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30") continue elif [[ "$HTTP_CODE" -ne 200 ]] ; then # If the return code is not 200, the instance is not going to be interrupted continue fi echo "Instance is getting terminated. Clean and unmount '$FSXPATH' ..." curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/instance-action echo # Gracefully stop applications accessing the filesystem # # TODO*: Replace with the proper command to stop your application if possible* # Kill every process still accessing Lustre filesystem echo "Kill every process still accessing Lustre filesystem..." fuser -kMm -TERM "${FSXPATH}"; sleep 2 fuser -kMm -KILL "${FSXPATH}"; sleep 2 # Unmount FSx For Lustre filesystem if ! umount -c "${FSXPATH}"; then echo "Error unmouting '$FSXPATH'. Processes accessing it:" >&2 lsof "${FSXPATH}" echo "Retrying..." continue fi # Start a graceful shutdown of the host shutdown now done