

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

# 使用 Amazon EC2 竞价型实例
<a name="working-with-ec2-spot-instances"></a>

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

在 Amazon EC2 中断竞价型实例时，将提供竞价型实例中断通知，这会在 Amazon EC2 终止该实例之前为其提供两分钟的警告。有关更多信息，请参阅 *Amazon EC2 用户指南*中的[竞价型实例](https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/using-spot-instances.html)。

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

## 处理 Amazon EC2 竞价型实例中断事件
<a name="handling-ec2-spot-interruptions-in-fsx"></a>

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

 如果在没有完全卸载文件系统的情况下关闭Lustre客户端，则使用该文件系统的其他客户机可能会遇到延迟时间暂时增加、操作挂起或 I/O 出错的情况。

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

**Example – 彻底卸载终止 EC2 竞价型实例的脚本**  
此示例脚本通过执行以下操作彻底卸载终止 EC2 竞价型实例：  
+ 关注竞价型实例终止通知。
+ 当它收到终止通知时，会：
  + 停止运行正在访问文件系统的应用程序。
  + 在实例终止前卸载文件系统。
您可以根据需要调整脚本，尤其是在正常关闭应用程序时。有关处理竞价型实例中断的最佳实践的更多信息，请参阅 [Best practices for handling EC2 Spot Instance interruptions](https://www.amazonaws.cn/blogs//compute/best-practices-for-handling-ec2-spot-instance-interruptions/)。  
Lustre客户机卸载文件系统可能需要一段时间。如果您在同一 Amazon EC2 竞价型实例上挂载多个文件系统，则可能需要很长时间才能在两分钟的 Spot 终止通知窗口内卸载所有文件系统。如果您需要在一台主机上挂载大量文件系统，我们建议您使用按需实例，以避免由于 Amazon EC2 竞价型实例中断而导致卸载不干净的问题。

```
#!/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/spot/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/spot/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 unmounting '$FSXPATH'. Processes accessing it:" >&2
        lsof "${FSXPATH}"

        echo "Retrying..."
        continue
    fi

    # Start a graceful shutdown of the host
    shutdown now

done
```