

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

# 在多個佇列模式叢集中執行任務
<a name="multi-queue-tutorial-v3"></a>

本教學課程說明如何 AWS ParallelCluster 在具有[多個佇列模式](configuration-of-multiple-queues-v3.md)的 上執行您的第一個「Hello World」任務。

使用 AWS ParallelCluster 命令列界面 (CLI) 或 API 時，您只需為建立或更新 AWS ParallelCluster 映像和叢集時建立 AWS 的資源付費。如需詳細資訊，請參閱[AWS 使用的 服務 AWS ParallelCluster](aws-services-v3.md)。

**先決條件**
+ AWS ParallelCluster [已安裝 ](install-v3-parallelcluster.md)。
+  AWS CLI [已安裝並設定 。](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
+ 您有 [Amazon EC2 金鑰對](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)。
+ 您有一個 IAM 角色，具有執行 CLI [`pcluster`](pcluster-v3.md) 所需的[許可](iam-roles-in-parallelcluster-v3.md#iam-roles-in-parallelcluster-v3-example-user-policies)。

## 設定您的叢集
<a name="multi-queue-tutorial-v3-configure-cluster"></a>

首先，執行下列命令，確認 AWS ParallelCluster 已正確安裝。

```
$ pcluster version
```

如需 `pcluster version` 的相關資訊，請參閱 [`pcluster version`](pcluster.version-v3.md)。

此命令會傳回 的執行版本 AWS ParallelCluster。

接著，執行 `pcluster configure` 以產生基本組態檔案。遵循遵循此命令的所有提示。

```
$ pcluster configure --config multi-queue-mode.yaml
```

如需 `pcluster configure` 命令的詳細資訊，請參閱[`pcluster configure`](pcluster.configure-v3.md)。

完成此步驟後，`multi-queue-mode.yaml`會顯示名為 的基本組態檔案。此檔案包含基本叢集組態。

在下一個步驟中，您會修改新的組態檔案，並啟動具有多個佇列的叢集。

**注意**  
此教學課程使用的某些執行個體不符合免費方案資格。

在本教學課程中，請修改您的組態檔案以符合下列組態。以紅色反白顯示的項目代表您的組態檔案值。保留您自己的值。

```
Region: region-id
Image:
 Os: alinux2
HeadNode:
 InstanceType: c5.xlarge
 Networking:
   SubnetId: subnet-abcdef01234567890
 Ssh:
   KeyName: yourkeypair
Scheduling:
 Scheduler: slurm
 SlurmQueues:
 - Name: spot
   ComputeResources:
   - Name: c5xlarge
     InstanceType: c5.xlarge
     MinCount: 1
     MaxCount: 10
   - Name: t2micro
     InstanceType: t2.micro
     MinCount: 1
     MaxCount: 10
   Networking:
     SubnetIds:
     - subnet-abcdef01234567890
 - Name: ondemand
   ComputeResources:
   - Name: c52xlarge
     InstanceType: c5.2xlarge
     MinCount: 0
     MaxCount: 10
   Networking:
     SubnetIds:
     - subnet-021345abcdef6789
```

## 建立 叢集
<a name="multi-queue-tutorial-v3-create-cluster"></a>

`multi-queue-cluster` 根據您的組態檔案建立名為 的叢集。

```
$ pcluster create-cluster --cluster-name multi-queue-cluster --cluster-configuration multi-queue-mode.yaml
{
 "cluster": {
   "clusterName": "multi-queue-cluster",
   "cloudformationStackStatus": "CREATE_IN_PROGRESS",
   "cloudformationStackArn": "arn:aws:cloudformation:eu-west-1:123456789012:stack/multi-queue-cluster/1234567-abcd-0123-def0-abcdef0123456",
   "region": "eu-west-1",
   "version": "3.15.0",
   "clusterStatus": "CREATE_IN_PROGRESS"
 }
}
```

如需 `pcluster create-cluster` 命令的詳細資訊，請參閱[`pcluster create-cluster`](pcluster.create-cluster-v3.md)。

若要檢查叢集的狀態，請執行下列命令。

```
$ pcluster list-clusters
{
 "cluster": {
   "clusterName": "multi-queue-cluster",
   "cloudformationStackStatus": "CREATE_IN_PROGRESS",
   "cloudformationStackArn": "arn:aws:cloudformation:eu-west-1:123456789012:stack/multi-queue-cluster/1234567-abcd-0123-def0-abcdef0123456",
   "region": "eu-west-1",
   "version": "3.15.0",
   "clusterStatus": "CREATE_IN_PROGRESS"
 }
}
```

建立叢集時， `clusterStatus` 欄位會顯示 `CREATE_COMPLETE`。

## 登入前端節點
<a name="multi-queue-tutorial-v3-log-into-head-node"></a>

使用您的私有 SSH 金鑰檔案登入前端節點。

```
$ pcluster ssh --cluster-name multi-queue-cluster -i ~/path/to/yourkeyfile.pem
```

如需 `pcluster ssh` 的相關資訊，請參閱 [`pcluster ssh`](pcluster.ssh-v3.md)。

登入後，請執行 `sinfo`命令來驗證您的排程器佇列是否已設定。

如需 的詳細資訊`sinfo`，請參閱 *Slurm 文件*中的 [sinfo](https://slurm.schedmd.com/sinfo.html)。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
spot*        up   infinite     18  idle~ spot-dy-c5xlarge-[1-9],spot-dy-t2micro-[1-9]
spot*        up   infinite      2  idle  spot-st-c5xlarge-1,spot-st-t2micro-1
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
```

輸出顯示您的叢集中有一個和`t2.micro`一個處於可用`idle`狀態的`c5.xlarge`運算節點。

其他節點都處於省電狀態，以節點狀態的`~`尾碼表示，沒有支援它們的 Amazon EC2 執行個體。預設佇列會以佇列名稱後面的尾碼表示。 `*` `spot`是您的預設任務佇列。

## 在多個佇列模式中執行任務
<a name="multi-queue-tutorial-v3-running-job-mqm"></a>

接下來，嘗試執行任務以睡眠一段時間。任務稍後會輸出自己的主機名稱。請確定目前使用者可執行此指令碼。

```
$ tee <<EOF hellojob.sh
#!/bin/bash
sleep 30
echo "Hello World from \$(hostname)"
EOF

$ chmod +x hellojob.sh
$ ls -l hellojob.sh
-rwxrwxr-x 1 ec2-user ec2-user 57 Sep 23 21:57 hellojob.sh
```

使用 `sbatch`命令提交任務。使用 `-N 2`選項為此任務請求兩個節點，並確認任務已成功提交。如需 的詳細資訊`sbatch`，請參閱 *Slurm 文件*[https://slurm.schedmd.com/sbatch.html](https://slurm.schedmd.com/sbatch.html)中的 。

```
$ sbatch -N 2 --wrap "srun hellojob.sh"
Submitted batch job 1
```

您可以使用 `squeue`命令檢視佇列並檢查任務的狀態。由於您未指定特定佇列，因此會使用預設佇列 (`spot`)。如需 的詳細資訊`squeue`，請參閱 *Slurm 文件*[https://slurm.schedmd.com/squeue.html](https://slurm.schedmd.com/squeue.html)中的 。

```
$ squeue
JOBID PARTITION     NAME     USER  ST      TIME  NODES NODELIST(REASON)
   1      spot     wrap ec2-user  R       0:10      2 spot-st-c5xlarge-1,spot-st-t2micro-1
```

輸出顯示任務目前處於執行中狀態。等待任務完成。這大約需要 30 秒。然後，`squeue`再次執行。

```
$ squeue
JOBID PARTITION     NAME     USER          ST       TIME  NODES NODELIST(REASON)
```

現在佇列中的任務都已完成，請尋找`slurm-1.out`目前目錄中名為 的輸出檔案。

```
$ cat slurm-1.out
Hello World from spot-st-t2micro-1
Hello World from spot-st-c5xlarge-1
```

輸出顯示任務在 `spot-st-t2micro-1`和 `spot-st-c5xlarge-1`節點上成功執行。

現在使用以下命令為特定執行個體指定限制條件，以提交相同的任務。

```
$ sbatch -N 3 -p spot -C "[c5.xlarge*1&t2.micro*2]" --wrap "srun hellojob.sh"
Submitted batch job 2
```

您針對 使用這些參數`sbatch`：
+ `-N 3`– 請求三個節點。
+ `-p spot`– 將任務提交至`spot`佇列。您也可以指定 ，將任務提交至`ondemand`佇列`-p ondemand`。
+ `-C "[c5.xlarge*1&t2.micro*2]"`– 指定此任務的特定節點限制條件。這會請求一個`c5.xlarge`節點和兩個`t2.micro`節點用於此任務。

執行 `sinfo`命令以檢視節點和佇列。中的佇列 AWS ParallelCluster 在 中稱為分割區Slurm。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
spot*        up   infinite      1  alloc# spot-dy-t2micro-1
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[2-10],spot-dy-t2micro-[2-9]
spot*        up   infinite      1  mix   spot-st-c5xlarge-1
spot*        up   infinite      1  alloc spot-st-t2micro-1
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
```

節點正在啟動。這由節點狀態的`#`尾碼表示。執行 squeue命令以檢視叢集中任務的相關資訊。

```
$ squeue
JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
   2      spot     wrap ec2-user CF       0:04      3 spot-dy-c5xlarge-1,spot-dy-t2micro-1,spot-st-t2micro-1
```

您的任務處於 `CF`(CONFIGURING) 狀態，正在等待執行個體擴展並加入叢集。

大約三分鐘後，節點可用，任務進入 `R`(RUNNING) 狀態。

```
$ squeue
JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
   2      spot     wrap ec2-user  R       0:07      3 spot-dy-t2micro-1,spot-st-c5xlarge-1,spot-st-t2micro-1
```

任務完成，且所有三個節點都處於 `idle` 狀態。

```
$ squeue
JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[1-9],spot-dy-t2micro-[2-9]
spot*        up   infinite      3  idle  spot-dy-t2micro-1,spot-st-c5xlarge-1,spot-st-t2micro-1
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
```

然後，在佇列中沒有任何任務後，請檢查`slurm-2.out`本機目錄中的 。

```
$ cat slurm-2.out 
Hello World from spot-st-t2micro-1
Hello World from spot-dy-t2micro-1
Hello World from spot-st-c5xlarge-1
```

這是叢集的最終狀態。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[1-9],spot-dy-t2micro-[2-9]
spot*        up   infinite      3  idle  spot-dy-t2micro-1,spot-st-c5xlarge-1,spot-st-t2micro-1
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
```

登出叢集後，您可以執行 來清除 `pcluster delete-cluster`。如需詳細資訊，請參閱[`pcluster list-clusters`](pcluster.list-clusters-v3.md)及[`pcluster delete-cluster`](pcluster.delete-cluster-v3.md)。

```
$ pcluster list-clusters
{
 "clusters": [
   {
     "clusterName": "multi-queue-cluster",
     "cloudformationStackStatus": "CREATE_COMPLETE",
     "cloudformationStackArn": "arn:aws:cloudformation:eu-west-1:123456789012:stack/multi-queue-cluster/1234567-abcd-0123-def0-abcdef0123456",
     "region": "eu-west-1",
     "version": "3.1.4",
     "clusterStatus": "CREATE_COMPLETE"
   }
 ]
}
$ pcluster delete-cluster -n multi-queue-cluster
{
 "cluster": {
   "clusterName": "multi-queue-cluster",
   "cloudformationStackStatus": "DELETE_IN_PROGRESS",
   "cloudformationStackArn": "arn:aws:cloudformation:eu-west-1:123456789012:stack/multi-queue-cluster/1234567-abcd-0123-def0-abcdef0123456",
   "region": "eu-west-1",
   "version": "3.1.4",
   "clusterStatus": "DELETE_IN_PROGRESS"
 }
}
```