

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

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

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

使用时 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 的[要求，为客户端配置安全组以允许进出文件系统的所有 inbound/outbound 流量](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa-start.html#efa-start-security)。

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

# 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\+](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。
      + 计算节点必须按照《fsX for Lustre 用户指南》中的 “配置 EFA 客户端”，将 FSx *Lustre [客户端配置为使用 E](https://docs.aws.amazon.com/fsx/latest/LustreGuide/configure-efa-clients.html) F* A。该指南提供了您可以下载、解压缩和运行的`configure-efa-fsx-lustre-client`软件包`setup.sh`。要将其自动应用于每个计算节点，请通过[OnNodeStart](Scheduling-v3.md#yaml-Scheduling-SlurmQueues-CustomActions-OnNodeStart)自定义操作运行这些步骤，如下所示。
      + 例如`your-bucket`，创建文件`configure-efa-fsx-lustre-client-wrapper.sh`并将其上传到可从计算节点访问的存储桶。按照上面引用的 FSx 文档中的步骤，包装器执行的操作相当于：

        ```
        #!/bin/bash
        set -euo pipefail
        
        # Download the FSx Lustre EFA client configuration package.
        # See: https://docs.aws.amazon.com/fsx/latest/LustreGuide/configure-efa-clients.html
        # Replace the source below with a location reachable from your compute nodes
        # (for example, your own S3 bucket populated from the FSx documentation package).
        cd /tmp
        aws s3 cp {{s3://your-bucket/configure-efa-fsx-lustre-client.zip}} .
        unzip -o configure-efa-fsx-lustre-client.zip
        cd configure-efa-fsx-lustre-client
        
        # Configure the FSx Lustre client to use EFA.
        ./setup.sh
        ```

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
         Iam:
           S3Access:
             - BucketName: {{your-bucket}}
         CustomActions:
           OnNodeStart:
             # Point this at the wrapper script you created and hosted (see step above).
             Script: {{s3://your-bucket/configure-efa-fsx-lustre-client-wrapper.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}}
   ```

## 验证使用 EFA 的 FSx 是否正常工作
<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
```

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