

# 将外部实例注册到 Amazon ECS 集群
<a name="ecs-anywhere-registration"></a>

对于您向 Amazon ECS 集群注册的每个外部实例，必须安装 SSM Agent、Amazon ECS 容器代理和 Docker。要将外部实例注册到 Amazon ECS 集群，必须首先将其注册为 Amazon Systems Manager 托管实例。您可以在 Amazon ECS 控制台上单击几下即可创建安装脚本。安装脚本包括一个 Systems Manager 激活密钥和命令，用于安装每个所需代理和 Docker。必须在本地部署服务器或 VM 上运行安装脚本，才能完成安装和注册步骤。

**注意**  
在将 Linux 外部实例注册到集群之前，请在您的外部实例上创建 `/etc/ecs/ecs.config` 文件，然后添加所需的任何容器代理配置参数。在将外部实例注册到集群后，您无法执行此操作。有关更多信息，请参阅 [Amazon ECS 容器代理配置](ecs-agent-config.md)。

------
#### [ Amazon Web Services 管理控制台 ]

1. 在 [https://console.aws.amazon.com/ecs/v2](https://console.amazonaws.cn/ecs/v2) 打开控制台。

1. 从导航栏中，选择要使用的区域。

1. 在导航窗格中，选择**集群**。

1. 在**集群**页面上，选择要将外部实例注册到的集群。

1. 在 **Cluster : *name***（集群：名称）页面上，选择 **Infrastructure**（基础设施）选项卡。

1. 在 **Register external instances**（注册外部实例）页面上，完成以下步骤。

   1. 对于**激活密钥持续时间（以天为单位）**，输入激活密钥保持活动状态的天数。在您输入的天数过后，注册外部实例时，密钥将不再有效。

   1. 对于**实例的数量**，输入要使用激活密钥注册到集群的外部实例数量。

   1. 对于**实例角色**，选择要与外部实例关联的 IAM 角色。如果尚未创建角色，请选择**创建新角色**让 Amazon ECS 代表您创建角色。有关外部实例需要哪些 IAM 权限的更多信息，请参阅 [Amazon ECS Anywhere IAM 角色](iam-role-ecsanywhere.md)。

   1.  复制注册命令。应在要注册到集群的每个外部实例上运行此命令。
**重要**  
脚本的 bash 部分必须以根用户的身份运行。如果未以根用户身份运行命令，则返回错误。

   1. 选择**关闭**。

------
#### [ Amazon CLI for Linux operating systems ]

1. 创建 Systems Manager 激活对。这用于 Systems Manager 托管实例激活。输出包括 `ActivationId` 和 `ActivationCode`。您将在后面的步骤中用到它。确保您指定了创建的 ECS Anywhere IAM 角色。有关更多信息，请参阅 [Amazon ECS Anywhere IAM 角色](iam-role-ecsanywhere.md)。

   ```
   aws ssm create-activation --iam-role ecsAnywhereRole | tee ssm-activation.json
   ```

1. 在本地部署服务器或虚拟机（VM）上，下载安装脚本。

   ```
   curl --proto "https" -o "/tmp/ecs-anywhere-install.sh" "https://amazon-ecs-agent.s3.amazonaws.com/ecs-anywhere-install-latest.sh"
   ```

1. （可选）在本地部署服务器或虚拟机（VM）上，使用脚本签名文件验证安装脚本。

   1. 下载并安装 GnuPG。有关 GnuPG 的更多信息，请参阅 [GnuPG 网站](https://www.gnupg.org)。对于 Linux 系统，使用您的 Linux 风格的程序包管理器安装 `gpg`。

   1. 检索 Amazon ECS PGP 公钥。

      ```
      gpg --keyserver hkp://keys.gnupg.net:80 --recv BCE9D9A42D51784F
      ```

   1. 下载安装脚本签名。签名是存储在扩展名为 `.asc` 的文件中的 ascii 分离 PGP 签名。

      ```
      curl --proto "https" -o "/tmp/ecs-anywhere-install.sh.asc" "https://amazon-ecs-agent.s3.amazonaws.com/ecs-anywhere-install-latest.sh.asc"
      ```

   1. 使用密钥验证安装脚本文件。

      ```
      gpg --verify /tmp/ecs-anywhere-install.sh.asc /tmp/ecs-anywhere-install.sh
      ```

      预期的输出如下所示：

      ```
      gpg: Signature made Tue 25 May 2021 07:16:29 PM UTC
      gpg:                using RSA key 50DECCC4710E61AF
      gpg: Good signature from "Amazon ECS <ecs-security@amazon.com>" [unknown]
      gpg: WARNING: This key is not certified with a trusted signature!
      gpg:          There is no indication that the signature belongs to the owner.
      Primary key fingerprint: F34C 3DDA E729 26B0 79BE  AEC6 BCE9 D9A4 2D51 784F
           Subkey fingerprint: D64B B6F9 0CF3 77E9 B5FB  346F 50DE CCC4 710E 61AF
      ```

1. 在本地部署服务器或虚拟机（VM）上，运行安装脚本。从第一步指定集群名称、区域和 Systems Manager 激活 ID 和激活代码。

   ```
   sudo bash /tmp/ecs-anywhere-install.sh \
       --region $REGION \
       --cluster $CLUSTER_NAME \
       --activation-id $ACTIVATION_ID \
       --activation-code $ACTIVATION_CODE
   ```

   对于为 GPU 工作负载安装 NVIDIA 驱动程序的本地服务器或虚拟机（VM），您必须添加 `--enable-gpu` 标记到安装脚本。指定此标记后，安装脚本将验证 NVIDIA 驱动程序是否正在运行，然后添加运行 Amazon ECS 任务所需的配置变量。有关在任务定义中运行 GPU 工作负载和指定 GPU 要求的更多信息，请参阅 [在 Amazon ECS 任务定义中指定 GPU 数](ecs-gpu-specifying.md)。

   ```
   sudo bash /tmp/ecs-anywhere-install.sh \
       --region $REGION \
       --cluster $CLUSTER_NAME \
       --activation-id $ACTIVATION_ID \
       --activation-code $ACTIVATION_CODE \
       --enable-gpu
   ```

使用以下步骤将现有外部实例注册到其他集群。

**将现有外部实例注册到其他集群**

1. 停止 Amazon ECS 容器代理。

   ```
   sudo systemctl stop ecs.service
   ```

1. 编辑 `/etc/ecs/ecs.config` 文件，并在 `ECS_CLUSTER` 行中确保集群名称与要向其注册外部实例的集群名称相匹配。

1. 删除现有的 Amazon ECS 代理数据。

   ```
   sudo rm /var/lib/ecs/data/agent.db
   ```

1. 开始 Amazon ECS 容器代理

   ```
   sudo systemctl start ecs.service
   ```

------
#### [ Amazon CLI for Windows operating systems ]

1. 创建 Systems Manager 激活对。这用于 Systems Manager 托管实例激活。输出包括 `ActivationId` 和 `ActivationCode`。您将在后面的步骤中用到它。确保您指定了创建的 ECS Anywhere IAM 角色。有关更多信息，请参阅 [Amazon ECS Anywhere IAM 角色](iam-role-ecsanywhere.md)。

   ```
   aws ssm create-activation --iam-role ecsAnywhereRole | tee ssm-activation.json
   ```

1. 在本地部署服务器或虚拟机（VM）上，下载安装脚本。

   ```
   Invoke-RestMethod -URI "https://amazon-ecs-agent.s3.amazonaws.com/ecs-anywhere-install.ps1" -OutFile “ecs-anywhere-install.ps1”
   ```

1. （可选）Powershell 脚本由 Amazon 签名，因此，Windows 会自动对同一脚本执行证书验证。您无需执行任何手动验证。

   要手动验证证书，请右键单击该文件，导航到属性，然后使用 Digital Signatures（数字签名）选项卡获取更多详细信息。

   仅当主机在证书存储中有证书时，此选项才可用。

   此验证应返回类似于以下内容的信息：

   ```
   # Verification (PowerShell)
   Get-AuthenticodeSignature -FilePath .\ecs-anywhere-install.ps1
   
   SignerCertificate                         Status      Path
   -----------------                         ------      ----
   EXAMPLECERTIFICATE                        Valid       ecs-anywhere-install.ps1
   
   ...
   
   Subject              : CN="Amazon Web Services, Inc.",...
   
   ----
   ```

1. 在本地部署服务器或虚拟机（VM）上，运行安装脚本。从第一步指定集群名称、区域和 Systems Manager 激活 ID 和激活代码。

   ```
   .\ecs-anywhere-install.ps1 -Region $Region -Cluster $Cluster -ActivationID $ActivationID -ActivationCode $ActivationCode
   ```

1. 验证 Amazon ECS 容器代理正在运行。

   ```
   Get-Service AmazonECS
   
   Status   Name               DisplayName
   ------   ----               -----------
   Running  AmazonECS          Amazon ECS
   ```

使用以下步骤将现有外部实例注册到其他集群。

**将现有外部实例注册到其他集群**

1. 停止 Amazon ECS 容器代理。

   ```
   Stop-Service AmazonECS
   ```

1. 修改 `ECS_CLUSTER` 参数，以便集群名称与要向其注册外部实例的集群名称相匹配。

   ```
   [Environment]::SetEnvironmentVariable("ECS_CLUSTER", $ECSCluster, [System.EnvironmentVariableTarget]::Machine)
   ```

1. 删除现有的 Amazon ECS 代理数据。

   ```
   Remove-Item -Recurse -Force $env:ProgramData\Amazon\ECS\data\*
   ```

1. 开始 Amazon ECS 容器代理

   ```
   Start-Service AmazonECS
   ```

------

在运行安装脚本以完成外部实例注册过程之前，可以使用 Amazon CLI 来创建 Systems Manager 激活。