通过部署 AWS Copilot 应用程序开始使用Amazon ECS - Amazon Elastic Container Service
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

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

通过部署 AWS Copilot 应用程序开始使用Amazon ECS

了解如何使用 Amazon ECS 部署 AWS Copilot 应用程序。

Prerequisites

在开始之前,请确保您满足以下先决条件:

  • 设置 AWS 账户。有关更多信息,请参阅使用 进行设置Amazon ECS

  • 安装 AWS Copilot CLI。版本当前支持 Linux 和 macOS 系统。有关更多信息,请参阅 安装 AWS Copilot CLI.

  • 安装和配置 AWS CLI. 有关更多信息,请参阅 AWS 命令行界面.

  • 运行 aws configure 以设置 AWS Copilot CLI 用于管理您的应用程序和服务的默认配置文件。

  • 安装并运行 Docker。有关更多信息,请参阅 Docker 入门

使用一条命令部署应用程序

在开始之前,请确保您已安装 AWS 命令行工具并且已运行 aws configure

使用以下命令部署应用程序。

git clone git@github.com:aws-samples/amazon-ecs-cli-sample-app.git demo-app && \ cd demo-app && \ copilot init --app demo \ --name api \ --type 'Load Balanced Web Service' \ --dockerfile './Dockerfile' \ --port 80 \ --deploy

分步部署您的应用程序

步骤 1:配置凭证

运行 aws configure 以设置 AWS Copilot CLI 用于管理您的应用程序和服务的默认配置文件。

aws configure

步骤 2:克隆演示应用程序

克隆简单的 Flask 应用程序和 Dockerfile。

git clone git@github.com:aws-samples/amazon-ecs-cli-sample-app.git demo-app

步骤 3:设置应用程序

  1. 在 demo-app 目录中,运行 init 命令。

    copilot init

    AWS Copilot 指导您完成第一个应用程序和服务的设置以及一系列终端提示,从下一步开始。如果您已使用 AWS Copilot 部署应用程序,系统将提示您从应用程序名称列表中选择一个应用程序。

  2. 为您的应用程序命名。

    What would you like to name your application? [? for help]

    Enter demo.

步骤 4:在“demo”应用程序中设置 ECS 服务

  1. 系统将提示您选择服务类型。您正在构建一个简单的 Flask 应用程序,提供小型 API。

    Which service type best represents your service's architecture? [Use arrows to move, type to filter, ? for more help] > Load Balanced Web Service Backend Service Scheduled Job

    Choose Load Balanced Web Service .

  2. 为您的服务提供一个名称。

    What do you want to name this Load Balanced Web Service? [? for help]

    Enter api 替换为您的服务名称。

  3. 选择一个 Dockerfile。

    Which Dockerfile would you like to use for api? [Use arrows to move, type to filter, ? for more help] > ./Dockerfile Use an existing image instead

    Choose Dockerfile.

  4. 定义端口。

    Which port do you want customer traffic sent to? [? for help] (80)

    Enter 80 或者接受默认值。

  5. 您将看到一个显示正在创建的应用程序资源的日志。

    Creating the infrastructure to manage services under application demo.
  6. 创建应用程序资源后,部署测试环境。

    Would you like to deploy a test environment? [? for help] (y/N)

    Enter y.

    Proposing infrastructure changes for the test environment.
  7. 您将看到一个 日志,其中显示了应用程序部署的状态。

    Note: It's best to run this command in the root of your Git repository. Welcome to the Copilot CLI! We're going to walk you through some questions to help you get set up with an application on ECS. An application is a collection of containerized services that operate together. Use existing application: No Application name: demo Workload type: Load Balanced Web Service Service name: api Dockerfile: ./Dockerfile no EXPOSE statements in Dockerfile ./Dockerfile Port: 80 Ok great, we'll set up a Load Balanced Web Service named api in application demo listening on port 80. ✔ Created the infrastructure to manage services under application demo. ✔ Wrote the manifest for service api at copilot/api/manifest.yml Your manifest contains configurations like your container size and port (:80). ✔ Created ECR repositories for service api. All right, you're all set for local development. Deploy: Yes ✔ Created the infrastructure for the test environment. - Virtual private cloud on 2 availability zones to hold your services [Complete] - Virtual private cloud on 2 availability zones to hold your services [Complete] - Internet gateway to connect the network to the internet [Complete] - Public subnets for internet facing services [Complete] - Private subnets for services that can't be reached from the internet [Complete] - Routing tables for services to talk with each other [Complete] - ECS Cluster to hold your services [Complete] ✔ Linked account aws_account_id and region region to application demo. ✔ Created environment test in region region under application demo. Environment test is already on the latest version v1.0.0, skip upgrade. [+] Building 0.8s (7/7) FINISHED => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 37B 0.0s => [internal] load metadata for docker.io/library/nginx:latest 0.7s => [internal] load build context 0.0s => => transferring context: 32B 0.0s => [1/2] FROM docker.io/library/nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b 0.0s => CACHED [2/2] COPY index.html /usr/share/nginx/html 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:3ee02fd4c0f67d7bd808ed7fc73263880649834cbb05d5ca62380f539f4884c4 0.0s => => naming to aws_account_id.dkr.ecr.region.amazonaws.com/demo/api:cee7709 0.0s WARNING! Your password will be stored unencrypted in /home/user/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded The push refers to repository [aws_account_id.dkr.ecr.region.amazonaws.com/demo/api] 592a5c0c47f1: Pushed 6c7de695ede3: Pushed 2f4accd375d9: Pushed ffc9b21953f4: Pushed cee7709: digest: sha_digest ✔ Deployed api, you can access it at http://demo-Publi-1OQ8VMS2VC2WG-561733989.region.elb.amazonaws.com.

步骤 5:验证您的应用程序是否正在运行

使用以下命令查看应用程序的状态。

列出所有 AWS Copilot 应用程序。

copilot app ls

显示有关应用程序中的环境和服务的信息。

copilot app show

显示有关您的环境的信息。

copilot env ls

显示有关服务的信息,包括终端节点、容量和相关资源。

copilot svc show

应用程序中的所有服务的列表。

copilot svc ls

显示已部署服务的日志。

copilot svc logs

显示服务状态。

copilot svc status

列出可用的命令和选项。

copilot --help
copilot init --help

步骤 6. 了解如何创建 CI/CD 管道

ECS Workshop 中可以找到详细说明如何使用 AWS Copilot 完全自动执行 CI/CD 管道和 Git 工作流程的说明。

步骤 7:清除

运行以下命令以删除并清除所有资源。

copilot app delete