简体   繁体   English

AWS ECS - 部署容器的方法

[英]AWS ECS - Ways to deploy containers

The use case is like - developer makes some code changes and the below things happen automatically - build runs, application artifact created, docker image generated with the artifact, image pushed to Docker registry, AWS ECS tasks and ECS services updated.用例就像 - 开发人员进行了一些代码更改,以下事情会自动发生 - 构建运行、创建应用程序工件、使用工件生成 docker 镜像、将镜像推送到 Docker 注册表、AWS ECS 任务和 ECS 服务更新。

I want to know what are the ways to achieve the above automation of update of AWS ECS services.我想知道有什么方法可以实现上述AWS ECS服务更新的自动化。 Till now I have implemented AWS ECS update from Jenkins build using -到目前为止,我已经使用 Jenkins 构建实现了 AWS ECS 更新 -
1>run post build AWS CLi scripts from Jenkins to update ECS 1>从 Jenkins 运行 post build AWS CLi 脚本来更新 ECS
2>post build action or pipeline step to invoke AWS Lambda function. 2> 发布构建操作或管道步骤以调用 AWS Lambda 函数。 I have created one Lambda function in Java to implement that.我在 Java 中创建了一个 Lambda 函数来实现它。

Please let me the other ways we can achieve the above.请让我知道我们可以通过其他方式实现上述目标。 Thanks.谢谢。

I'm continuously deploying Docker containers from CircleCI to AWS ECS. 我一直在从CircleCI到AWS ECS不断部署Docker容器。

The outline of the deployment flow is as follows: 部署流程的概要如下:

  1. Build and tag a new Docker image 构建并标记新的Docker映像
  2. Login to AWS ECR and push the image 登录到AWS ECR并推送图像
  3. Update task definitions and services of ECS with ecs-deploy 使用ecs-deploy更新ECS的任务定义和服务

ecs-deploy is a useful script that updates Docker images in ECS. ecs-deploy是一个有用的脚本,用于更新ECS中的Docker映像。

https://github.com/silinternational/ecs-deploy https://github.com/silinternational/ecs-deploy

You could use a shell script that calls aws cli commands to create cloudformation stacks or directly call the create commands in the aws cli for the ECR repository, Task Definition, Events rule and target(for scheduling). 您可以使用shell脚本来调用aws cli命令来创建cloudformation堆栈,也可以直接在aws cli中为ECR存储库,任务定义,事件规则和target(用于调度)调用create命令。

then you just call this script on your terminal using this command: ./setup.sh and it should execute all your commands at once. 那么您只需使用以下命令在您的终端上调用此脚本: ./setup.sh ,它将立即执行所有命令。

aws ecr create-repository \
    --repository-name tasks-${TASK_NAME}-${TASK_ENV} \
;

or if you want to set up your resources via cloudformation templates, you can launch them using this command as long as the template exists at file://name.yml : 或者如果您想通过cloudformation模板设置资源,则可以使用此命令启动它们,只要模板位于file://name.yml

aws cloudformation create-stack \
    --stack-name stack-name \
    --capabilities CAPABILITY_IAM \
    --template-body file://name.yml \
    --parameters
        ParameterKey=ParamName,ParameterValue=${PARAM_NAME} \
;

Take a look at Codefresh - https://docs.codefresh.io/docs/amazon-ecs 看看Codefresh- https ://docs.codefresh.io/docs/amazon-ecs

You can build your pipeline 您可以建立管道

  1. Build Step 建立步骤
  2. Push to Registry 推送到注册表
  3. Deply to ECS 前往ECS

That easy 这么简单

While there are a ton of CI/CD tools out there, since I am early in my rollout, I decided to write a small script instead of having CI/CD pipelines do it. 尽管那里有大量的CI / CD工具,但由于我还处于开发初期,所以我决定编写一个小脚本,而不要使用CI / CD管道来完成。

Here is a one-click deploy script I wrote using the ecs-deploy script as a dependency to achieve a rolling deploy of a docker image to ECS . 这是一个一键式部署脚本我用写ECS部署脚本的依赖,实现了码头工人形象 ECS的滚动部署。

You can run this locally from your dev or build/deployment box or use Jenkins or some local build tool. 您可以在开发人员或构建/部署框中本地运行此命令,也可以使用Jenkins或某些本地构建工具。

#!/bin/bash

# automatically login to AWS
eval $(aws ecr get-login)

# build local docker image and push repo to AWS
docker build -t <yourlocaldockerimagetag> .
docker tag <yourlocaldockerimagetag>:latest <yourECSRepoURL>:latest
docker -D -l debug push <yourECSRepoURL>:latest

# deploy to ECS 
ecs-deploy/ecs-deploy -m 50 -k <access-key> -s <secret-key> -r <aws-region> -c <cluster-name> -n <service-name> -i <yourECSRepoURL>:latest

Parameters: 参数:

  • cluster-name : Your cluster name in ECS cluster-name :您在ECS中的集群名称
  • service-name : Your service name that you had created in ECS service-name :您在ECS中创建的服务名称
  • yourECSRepoURL : ECS Repository URL yourECSRepoURL :ECS存储库URL
  • yourlocaldockerimagetag : Any local image tag name yourlocaldockerimagetag :任何本地图像标签名称
  • access-key : your AWS access key for deployments access-key :用于部署的AWS访问密钥
  • secret-key : your AWS secret key 密钥 :您的AWS密钥

Make sure you install ecs-deploy before this script. 确保在此脚本之前安装ecs-deploy

The -m 50 tells it that it can deploy even if the number of nodes drops to 50%. -m 50告诉它即使节点数下降到50%也可以部署。 Ideally you would have an extra node to do deployments, but if you can't afford that setting this would ensure that deployments continue to happen. 理想情况下,您将有一个额外的节点来进行部署,但是如果您负担不起该设置,则可以确保部署继续进行。

If you are also using an ELB (load balancer), then the default deregistration delay for target groups is 5 minutes which is a bit excessive. 如果您还使用ELB(负载平衡器),则目标组的默认注销延迟为5分钟,这有点过分。 The deregistration delay is the time to wait for existing requests to complete BEFORE ECS sends a SIGTERM or SIGINT to your docker container. 注销延迟是在ECS向您的Docker容器发送SIGTERM或SIGINT之前等待现有请求完成的时间。 You should lower this by going to the Target Groups in EC2 dashboard and click the Edit Attributes to edit it. 您应通过移至EC2仪表板中的“目标组”并单击“编辑属性”进行编辑来降低它。 Otherwise your deployments may take forever. 否则,您的部署可能会花费很长时间。

I think nobody has mentioned CodePipeline from AWS, it really integrates easilly with many AWS Services including ECS and CodeCommit:我想没有人提到过 AWS 的 CodePipeline,它真的很容易与许多 AWS 服务集成,包括 ECS 和 CodeCommit:

  1. Push commit to CodeCommit Repo, triggering the pipeline execution.将提交推送到 CodeCommit Repo,触发管道执行。
  2. (Optional) Configure a Manual Approval step that needs you to take an action before Build. (可选)配置需要您在构建之前执行操作的手动批准步骤。
  3. Run a CodeBuild Project that builds your Dockerfile and push the image to an ECR Repo.运行构建 Dockerfile 的 CodeBuild 项目并将映像推送到 ECR 存储库。
  4. Run a "Deploy" step that deploys to a specific ECS Service.运行部署到特定 ECS 服务的“部署”步骤。 It updates the services with a new Task Definition that points to the new ECR Image.它使用指向新 ECR 映像的新任务定义更新服务。

I have used this flow with BitBucket also, just configure a BitBucket pipeline that pushes all new code to a CodeCommit Repo as a previous step.我也将此流程与 BitBucket 一起使用,只需配置一个 BitBucket 管道,将所有新代码推送到 CodeCommit Repo 作为上一步。

Exactly as @minamiyojo and @astav answers, we ended up glueing ecs-deploy with a template engine to power up our CD pipeline with some reusable component, we just open-sourced as well:正如@minamiyojo 和@astav 的回答一样,我们最终将 ecs-deploy 与模板引擎粘合在一起,以使用一些可重用的组件来增强我们的 CD 管道,我们也只是开源了:

https://github.com/GuccioGucci/yoke https://github.com/GuccioGucci/yoke

Please refer to Motivation section in README, hope this would help your scenario too.请参阅自述文件中的动机部分,希望这也能对您的场景有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM