

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用啟用 EFA 的 FSx Lustre 建立叢集
<a name="tutorial-efa-enabled-fsx-lustre"></a>

在本教學課程中，您將建立使用啟用 EFA 的 FSx Lustre 檔案系統做為共用儲存體的叢集。在啟用 EFA 的情況下使用 FSx Lustre 檔案系統，可將效能提升高達 8 倍。若要驗證已啟用 EFA 的檔案系統是否為您需要的檔案系統，請參閱《*FSx for Lustre 使用者指南*》中的[使用已啟用 EFA 的檔案系統](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>
+ [已安裝並設定](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) AWS CLI。
+ [已安裝並設定](install-v3-parallelcluster.md) ParallelCluster CLI。
+ 用於登入叢集的 [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)的所有來自用戶端的傳入流量。

```
# 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. 使用 YAML 組態檔案中設定的 AWS ParallelCluster 下列組態建立叢集：

   1. 以支援的作業系統為基礎的 AMI，例如 Ubuntu 22.04。

   1. 運算節點必須使用具有 [Nitro v4\$1](https://docs.aws.amazon.com/ec2/latest/instancetypes/ec2-nitro-instances.html) 的 [EFA 支援的執行個體類型](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa.html#efa-instance-types)，例如 g6.16xlarge。
      + 運算節點必須位於檔案系統所在的相同 AZ 中。
      + 運算節點必須將 [Efa/Enabled](Scheduling-v3.md#yaml-Scheduling-SlurmQueues-ComputeResources-Efa-Enabled) 設定為 true。
      + 運算節點必須以 [OnNodeStart](Scheduling-v3.md#yaml-Scheduling-SlurmQueues-CustomActions-OnNodeStart) 自訂動作`configure-efa-fsx-lustre-client.sh`的形式執行組態指令碼。為了方便起見，[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
   ```

## 使用 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
```

如果功能正常運作，流經界面的封包數量預期會增加。