【问题标题】:How to avoid downtime when restarting ECS Fargate services?重启 ECS Fargate 服务时如何避免宕机?
【发布时间】:2020-10-14 07:39:21
【问题描述】:

我有这个 bash 脚本:

#!/bin/bash    
myClusterId="myCluster"
for service in $(aws ecs list-services --cluster $myClusterId --query "serviceArns[*]" | jq -r 'to_entries[] | .value | sub(".*/";"")'); do
    for task in $( aws ecs list-tasks --cluster $myClusterId --service-name $service --desired-status 'RUNNING' --no-paginate --output text --query 'taskArns[*]' ) ; do 
        aws ecs stop-task --cluster $myClusterId --task $task --reason "Restarted using bash script" > /dev/null 2>&1
    done
done

简而言之,它将重新启动我在 myCluster 下的所有 ECS Fargate 任务(不包括由 CloudWatch 规则触发的计划任务)。到目前为止一切正常。

我所有的服务都将minHealthyPercent 设置为100,maxHealthyPercent 设置为200。但是,我注意到它在重启过程中没有保持任何健康的任务。当新任务处于挂起/配置状态时,所有任务都会立即终止,并且我的负载均衡器会抛出 503 Service Temporarily Unavailable 错误。

我的脚本中是否缺少某些内容?如何使用 AWS CLI 正确执行无停机服务重启过程?

【问题讨论】:

    标签: amazon-web-services aws-fargate aws-cdk


    【解决方案1】:

    maximumPercentminimumHealthyPercent 参数仅在您的 ECS 服务的rolling updates 期间使用:

    Amazon ECS 在滚动更新期间从服务中添加或删除的任务数量由部署配置控制。部署配置由服务部署期间允许的最小和最大任务数组成。

    重新启动任务不被视为新部署。

    要纠正这个问题,有几个选择:

    • 在你的 for 循环中包含一个 sleep。它是最粗略的方式,但实施起来最快。

    • 在 for 循环中使用describe-tasks拉取刚刚终止的任务的状态。仅当最近重新启动的任务的状态为RUNNING 时,才继续重新启动下一个任务。

    【讨论】:

      【解决方案2】:

      假设您使用弹性负载均衡器,我认为您最好的选择是通过 CodeDeploy 进行蓝/绿部署。蓝/绿部署将自动检测任何错误并在需要时停止部署。

      https://aws.amazon.com/blogs/devops/use-aws-codedeploy-to-implement-blue-green-deployments-for-aws-fargate-and-amazon-ecs/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-11
        • 2017-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-07
        • 1970-01-01
        • 2019-09-10
        相关资源
        最近更新 更多