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

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

通过 AWS Copilot 开始使用 Amazon ECS

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

Prerequisites

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

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

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

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

  • 运行aws configure来设置默认配置文件,AWS CopilotCLI 将用于管理您的应用程序和服务。

  • 安装并运行码头。有关更多信息,请参阅 。开始使用 Docker

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

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

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

git clone https://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 CopilotCLI 用于管理您的应用程序和服务。

aws configure

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

克隆一个简单的烧瓶应用程序和码头文件。

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

第 3 步:设置应用程序

  1. 在演示应用程序目录中,运行init命令。

    copilot init

    AWS Copilot将引导您完成您的第一个应用程序和服务带有一系列终端提示,从下一步。如果您已经使用AWS Copilot部署应用程序时,系统会提示您从应用程序名称列表中选择一个应用程序。

  2. 命名应用程序。

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

    Enter demo.

第 4 步:在 “演示” 应用程序中设置弹性云服务器服务

  1. 系统会提示您选择服务类型。您正在构建一个简单的 Flask 应用程序that提供一个小 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 研讨会详细说明了如何使用AWS Copilot。

步骤 7:清除

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

copilot app delete