

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

# 创建具有启用 EFA 的 Lustre FSx 的集群
<a name="tutorial-efa-enabled-fsx-lustre"></a>

在本教程中，您将创建一个使用支持 EFA 的 FSx Lustre 文件系统作为共享存储的集群。使用启用 EFA 的 FSx Lustre 文件系统可以将性能提高多达 8 倍。*要验证是否需要启用 EFA 的文件系统，请查看 for Lustre 用户指南中的[使用启用 EFA 的文件系统](https://docs.aws.amazon.com/fsx/latest/LustreGuide/efa-file-systems.html)。FSx *

使用时 AWS ParallelCluster，您只需为创建或更新 AWS ParallelCluster 映像和集群时创建的 AWS 资源付费。有关更多信息，请参阅 [AWS 使用的服务 AWS ParallelCluster](aws-services-v3.md)。

## 要求
<a name="tutorial-efa-enabled-fsx-lustre-requirements"></a>
+ C AWS LI 已[安装并配置](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)完毕。
+ C ParallelCluster LI 已[安装并配置](install-v3-parallelcluster.md)完毕。
+ 用于登录集群[的 Amazon EC2 密钥对](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)。
+ 具有运行 ParallelCluster CLI 所需[权限](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)的 IAM 角色。

## 创建安全组
<a name="tutorial-efa-enabled-fsx-lustre-security-groups"></a>

在部署集群和文件系统的同一 VPC 中创建两个安全组：一个用于在群集节点上运行的客户端，另一个用于文件系统。

```
# Create security group for the FSx client
aws ec2 create-security-group \
    --group-name Fsx-Client-SecurityGroup \
    --description "Allow traffic for the FSx Lustre client" \
    --vpc-id vpc-cluster \
    --region region

# Create security group for the FSx file system
aws ec2 create-security-group \
    --group-name Fsx-FileSystem-SecurityGroup \
    --description "Allow traffic for the FSx Lustre File System" \
    --vpc-id vpc-cluster \
    --region region
```

在本教程的其余部分中，我们将假设`sg-client`和分别`sg-file-system`是客户端和文件系统的安全组 ID。

按照 EFA 的[要求，为客户端配置安全组以允许所有出站流量进入文件系统](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa-start.html#efa-start-security)。

```
# Allow all outbound traffic from the client to the file system
aws ec2 authorize-security-group-egress \
 --group-id sg-client \ 
 --protocol -1 \
 --port -1 \
 --source-group sg-file-system \
 --region region
```

按照 EFA 的[要求，为文件系统配置安全组，使其允许其内部的所有流量以及来自客户端的所有入站流量](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa-start.html#efa-start-security)。 inbound/outbound 

```
# Allow all inbound traffic within this security group
aws ec2 authorize-security-group-ingress \
    --group-id sg-file-system \
    --protocol -1 \
    --port -1 \
    --source-group sg-file-system \
    --region region

# Allow all outbound traffic within this security group
aws ec2 authorize-security-group-egress \
    --group-id sg-file-system \
    --protocol -1 \
    --port -1 \
    --source-group sg-file-system \
    --region region

# Allow all inbound traffic from the client
aws ec2 authorize-security-group-ingress \
    --group-id sg-file-system \
    --protocol -1 \
    --port -1 \
    --source-group sg-client \
    --region region

# Allow all outbound traffic to the client
aws ec2 authorize-security-group-egress \
    --group-id sg-file-system \
    --protocol -1 \
    --port -1 \
    --source-group sg-client \
    --region region
```

## 创建文件系统
<a name="tutorial-efa-enabled-fsx-lustre-create-filesystem"></a>

在计算节点所在的可用区 (AZ) 中创建文件系统；并在以下代码中`subnet-compute-nodes`替换为其 ID。这是允许 EFA 与您的文件系统配合使用所必需的。请注意，作为创建文件系统的一部分，我们使用 EfaEnable 属性启用 EFA。

```
aws fsx create-file-system \
    --file-system-type LUSTRE \
    --storage-capacity 38400 \
    --storage-type SSD \
    --subnet-ids subnet-compute-nodes \
    --security-group-ids sg-file-system \
    --lustre-configuration DeploymentType=PERSISTENT_2,PerUnitStorageThroughput=125,EfaEnabled=true,MetadataConfiguration={Mode=AUTOMATIC} \
    --region region
```

记下上一个命令返回的文件系统 ID。在本教程的其余部分中，`fs-id`用此文件系统 ID 替换。

## 创建集群
<a name="tutorial-efa-enabled-fsx-lustre-create-cluster"></a>

1. 使用 AWS ParallelCluster YAML 配置文件中设置的以下配置创建集群：

   1. AMI 基于支持的操作系统，例如 Ubuntu 22.04。

   1. 计算节点必须使用[支持 EFA 且具有 [Nitro v4\$1](https://docs.aws.amazon.com/ec2/latest/instancetypes/ec2-nitro-instances.html) 的实例类型](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa.html#efa-instance-types)，例如 g6.16xlarge。
      + 计算节点必须位于文件系统所在的同一个可用区中。
      + 计算节点必须将 [Efa/Enabled](Scheduling-v3.md#yaml-Scheduling-SlurmQueues-ComputeResources-Efa-Enabled) 设置为 true。
      + 计算节点必须将配置脚本`configure-efa-fsx-lustre-client.sh`作为[OnNodeStart](Scheduling-v3.md#yaml-Scheduling-SlurmQueues-CustomActions-OnNodeStart)自定义操作运行。为了方便起见，该脚本在[FSx 官方文档](https://docs.aws.amazon.com/fsx/latest/LustreGuide/configure-efa-clients.html)中提供，并在我们的公共存储桶中提供，旨在在计算节点上配置 FSx Lustre 客户端，让它们使用 EFA。

1. 创建集群配置文件`config.yaml`：

   ```
   Region: region
   Image:
     Os: ubuntu2204
   HeadNode:
     InstanceType: c5.xlarge
     Networking:
       SubnetId: subnet-xxxxxxxxxx
       AdditionalSecurityGroups:
           - sg-client
     Ssh:
       KeyName: my-ssh-key
   Scheduling:
     Scheduler: slurm
     SlurmQueues:
       - Name: q1
         ComputeResources:
           - Name: cr1
             Instances:
               - InstanceType: g6.16xlarge
             MinCount: 1
             MaxCount: 3
             Efa:
               Enabled: true
         Networking:
           SubnetIds:
             - subnet-xxxxxxxxxx # Subnet in the same AZ where the file system is
           AdditionalSecurityGroups:
             - sg-client
           PlacementGroup:
             Enabled: false
         CustomActions:
           OnNodeStart:
             Script: https://us-east-1-aws-parallelcluster.s3.us-east-1.amazonaws.com/scripts/fsx-lustre-efa/configure-efa-fsx-lustre-client.sh
   SharedStorage:
     - MountDir: /fsx
       Name: my-fsxlustre-efa-external
       StorageType: FsxLustre
       FsxLustreSettings:
         FileSystemId: fs-id
   ```

   然后使用该配置创建集群：

   ```
   pcluster create-cluster \
       --cluster-name fsx-efa-tutorial \
       --cluster-configuration config.yaml \
       --region region
   ```

## FSx 使用 EFA 进行验证正在起作用
<a name="tutorial-efa-enabled-fsx-lustre-validate"></a>

要验证 Lustre 网络流量是否在使用 EFA，请使用可以显示给定网络接口的网络流量的 Lustre `lnetctl` 工具。为此，请在计算节点中执行以下命令：

```
# Take note of the number of packets flowing through the interface, 
# which are specified in statistics:send_count and statistics:recv_count
sudo lnetctl net show --net efa -v

# Generate traffic to the file system
echo 'Hello World' > /fsx/hello-world.txt

# Take note of the number of packets flowing through the interface, 
# which are specified in statistics:send_count and statistics:recv_count
sudo lnetctl net show --net efa -v
```

如果该功能起作用，则流经该接口的数据包数量预计会增加。