

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

# 在作为 Docker 容器 AWS IoT Greengrass V2 运行时部署容器化应用程序
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container"></a>

*亚马逊 Web Services 的 Salih Bakir、Giuseppe Di Bella 和 Gustav Svalander*

## Summary
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-summary"></a>

AWS IoT Greengrass Version 2，当部署为 Docker 容器时，本机不支持运行 Docker 应用程序容器。此模式向您展示如何基于启用 Docker-in-Docker (DinD) 功能的最新版本创建自定义容器镜像。 AWS IoT Greengrass V2 借助 DinD，您可以在环境中运行容器化应用程序。 AWS IoT Greengrass V2 

您可以将此模式部署为独立解决方案，也可以将其与 Amazon ECS Anywhere 等容器编排平台集成。在这两种部署模式中，您都可以保持包括 AWS IoT SiteWise 边缘处理 AWS IoT Greengrass V2 功能在内的全部功能，同时支持基于容器的可扩展部署。

## 先决条件和限制
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-prereqs"></a>

**先决条件**
+ 活跃 AWS 账户的.
+ 有关一般 AWS IoT Greengrass Version 2 先决条件，请参阅 AWS IoT Greengrass Version 2 文档中的[先决条件](https://docs.aws.amazon.com/greengrass/v2/developerguide/getting-started-prerequisites.html)。
+ Docker Engine，在 Linux、macOS 或 Windows 上安装和配置。
+ Docker Compose（如果你使用 Docker Compose 命令行界面 (CLI) 来运行 Docker 镜像）。
+ 一个 Linux 操作系统。
+ 带有支持虚拟化的主机服务器的虚拟机管理程序。
+ 系统要求：
  + 2 GB 内存（最小值）
  + 5 GB 可用磁盘空间（最小值）
  + 对于 AWS IoT SiteWise Edge，这是一款具有 16 GB 内存和 50 GB 可用磁盘空间的 x86\_64 四核 CPU。有关 AWS IoT SiteWise 数据处理的更多信息，请参阅 AWS IoT SiteWise 文档中的[数据处理包要求](https://docs.aws.amazon.com/iot-sitewise/latest/userguide/configure-gateway-ggv2.html#w2aac17c19c13b7)。

**产品版本**
+ AWS IoT Greengrass Version 2 版本 2.5.3 或更高版本
+ Docker-in-Docker 版本 1.0.0 或更高版本
+ Docker Compose 版本 1.22 或更高版本
+ Docker Engine 版本 20.10.12 或更高版本

**限制**
+ 有些 AWS 服务 并非全部可用 AWS 区域。有关区域可用性，请参阅[按区域划分的AWS 服务](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。有关特定端点，请参阅[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然后选择相应服务的链接。

## 架构
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-architecture"></a>

**目标技术堆栈**
+ **数据源** — 生成数据进行处理的物联网设备、传感器或工业设备
+ **AWS IoT Greengrass V2**— 作为具有 D-in-D 功能的 Docker 容器运行，部署在边缘基础架构上
+ **容器化应用程序** — 作为嵌套 Docker 容器在 AWS IoT Greengrass V2 环境中运行的自定义应用程序
+ **（可选）Amazon ECS Anywhere** — 管理容器部署的 AWS IoT Greengrass V2 容器编排
+ **其他 AWS 服务** — AWS IoT Core AWS IoT SiteWise、以及其他 AWS 服务 用于数据处理和管理

**目标架构 **

下图显示了使用 Amazon ECS Anywhere（一种容器管理工具）的目标部署架构示例。

![使用 Amazon ECS Anywhere 部署架构。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/2ecf5354-40e0-4fd9-9798-086719059784/images/5ed2652e-9604-4809-8962-b167e1991658.png)


下图显示了如下工作流：

**1：容器映像存储** — Amazon ECR 存储边缘处理所需的容器映像和任何自定义应用程序容器。 AWS IoT Greengrass 

**2** 和 **3：容器部署** — Amazon ECS Anywhere 将 AWS IoT Greengrass 容器映像从 Amazon ECR 部署到边缘站点，管理容器生命周期和部署过程。

**4：组件部署** — 已部署的 AWS IoT Greengrass 核心会根据其配置自动部署其相关组件。组件包括 AWS IoT SiteWise 容器化环境中的 Edge 和其他必要的边缘处理组件。

**5：数据摄取** — 完全配置完毕后， AWS IoT Greengrass 开始从边缘位置的各种 IoT 数据源提取遥测和传感器数据。

**6：数据处理和云集成** — 容器化 AWS IoT Greengrass 核心使用其部署的组件（包括用于工业数据的 AWS IoT SiteWise 边缘）在本地处理数据。然后，它将处理后的数据发送到 AWS Cloud 服务部门进行进一步分析和存储。

## 工具
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-tools"></a>

**AWS 服务**
+ [Amazon ECS Anywhere](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch-type-external.html) 可帮助您在自己的基础设施上部署、使用和管理亚马逊 ECS 任务和服务。
+ [Amazon Elastic Compute Cloud（Amazon EC2）](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)在 AWS Cloud中提供可扩展的计算容量。您可以根据需要启动任意数量的虚拟服务器，并快速纵向扩展或缩减这些服务器。
+ [Amazon Elastic Container Registry（Amazon ECR）](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)是一项安全、可扩展且可靠的托管容器映像注册表服务。
+ [AWS IoT Greengrass](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html) 是一项开源物联网（IoT）边缘运行时和云服务，可帮助您在设备上构建、部署和管理 IoT 应用程序。
+ [AWS IoT SiteWise](https://docs.aws.amazon.com/iot-sitewise/latest/userguide/what-is-sitewise.html) 可帮您大规模采集、建模、分析和可视化显示来自工业设备的数据。

**其他工具**
+ [Docker](https://www.docker.com/) 是一组平台即服务（PaaS）产品，它们利用操作系统级的虚拟化技术在容器中提供软件。
+ [Docker Compose](https://docs.docker.com/compose/) 是一款工具，用于定义和运行多容器应用程序。
+ [Docker Engin](https://docs.docker.com/engine/) e 是一种开源容器化技术，用于构建和容器化应用程序。

**代码存储库**

此模式的代码可在 GitHub [AWS IoT Greengrass v2 Docker-in-Docker](https://github.com/aws-samples/aws-iot-greengrass-docker-in-docker) 存储库中找到。

## 操作说明
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-epics"></a>

### 构建 AWS IoT Greengrass V2 Docker-in-Docker 镜像
<a name="build-the-gg2-docker-in-docker-image"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆并导航到存储库。 | 要克隆存储库，请使用以下命令：<br />`git clone https://github.com/aws-samples/aws-iot-greengrass-docker-in-docker.git`<br />要导航到该`docker`目录，请使用以下命令：<br />`cd aws-iot-greengrass-docker-in-docker/docker` | DevOps 工程师，AWS DevOps | 
| 构建 Docker 映像。 | 要使用默认（最新）版本构建 Docker 镜像，请运行以下命令：<br />`docker build -t x86_64/aws-iot-greengrass:latest .`<br />或者，要使用特定版本构建 Docker 镜像，请运行以下命令：<br />`docker build --build-arg GREENGRASS_RELEASE_VERSION=2.12.0 -t x86_64/aws-iot-greengrass:2.12.0 .`<br />要验证构建，请运行以下命令：<br />`docker images \| grep aws-iot-greengrass`<br />  | AWS DevOps， DevOps 工程师，应用程序开发人员 | 
| （可选）推送到 Amazon ECR。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 应用程序开发人员、AWS DevOps、 DevOps 工程师 | 

### 配置 AWS 凭证
<a name="configure-aws-credentials"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 选择身份验证方法。 | 请选择以下选项之一：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理员 | 
| 配置身份验证方法。 | 对于您选择的身份验证方法，请使用以下配置指南：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理员 | 

### 使用 Docker Compose 运行
<a name="run-with-docker-compose"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| Configure `docker-compose.yml`。 | 使用环境变量更新`docker-compose.yml`文件，如下所示：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程师 | 
| 启动并验证容器。 | 要从前台开始，请运行以下命令：<br />`docker-compose up --build`<br />或者，要在后台启动，请运行以下命令：<br />`docker-compose up --build -d`<br />要验证状态，请运行以下命令：<br />`docker-compose ps`<br />要监控日志，请运行以下命令：<br />`docker-compose logs -f` | DevOps 工程师 | 

### 使用 Docker CLI 运行
<a name="run-with-docker-cli"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 使用 Docker CLI 运行容器。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程师 | 
| 验证容器。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程师 | 

### 管理容器化应用程序
<a name="manage-containerized-applications"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 部署应用程序。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 应用程序开发人员 | 
| 访问和测试 Docker-in-Docker。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程师 | 

### （可选）与 Amazon ECS Anywhere 集成
<a name="optional-integrate-with-ecs-anywhere"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 设置 Amazon ECS 集群。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理员 | 
| 部署 Amazon ECS 任务。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理员 | 

### 停下来清理
<a name="stop-and-cleanup"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 停止集装箱。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程师 | 

## 问题排查
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 容器启动失败，出现权限错误。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)`--privileged`向容器授予扩展权限。 | 
| 由于凭据错误，置备失败。 | 要验证凭据配置是否正确，请使用以下步骤：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)<br />确保 IAM 权限包括`iot:CreateThing``iot:CreatePolicy`、`iot:AttachPolicy`、`iam:CreateRole`、和`iam:AttachRolePolicy`。 | 
| 无法连接到容器内的 Docker 守护程序。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 
| 容器的磁盘空间不足。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)确保最小磁盘空间：5 GB 用于基本操作，50 GB 用于 AWS IoT SiteWise 边缘 | 
| 构建问题。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 
| 网络连接问题。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)确认防火墙允许出站 HTTPS (443) 和 MQTT (8883) 流量。 | 
| Greengrass 组件无法部署。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)检查目录中特定于组件的`/greengrass/v2/logs/`日志。 | 
| 容器启动后立即退出。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)验证是否正确设置了所有必需的环境变量`PROVISION=true`。确保在启动容器时使用该`--init`标志。 | 

## 相关资源
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-resources"></a>

**AWS resources**
+ [Amazon Elastic Container Service](https://aws.amazon.com/ecs/)
+ [为 AWS IoT SiteWise 模型和资产配置边缘数据处理](https://docs.aws.amazon.com/iot-sitewise/latest/userguide/edge-processing.html)
+ [什么是 AWS IoT Greengrass](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html)

**其他资源**
+ [Docker 文档](https://docs.docker.com/)

## 附加信息
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-additional"></a>
+ 要进行 AWS IoT SiteWise 边缘数据处理，Docker 必须在 AWS IoT Greengrass 环境中可用。
+ 要运行嵌套容器，必须使用管理员级别的凭 AWS IoT Greengrass 据运行该容器。