重新启动ECS Fargate服务时如何避免停机?

时间:2020-06-24 05:37:28

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

我有这个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 Rules触发的计划任务)。到目前为止,一切正常。

我所有的服务都将minHealthyPercent设置为100,并将maxHealthyPercent设置为200。但是,我注意到在重新启动过程中它没有保留任何正常的任务。当新任务处于待处理/预配置状态时,所有任务都会立即被杀死,并且我的负载均衡器会抛出 503服务暂时不可用错误。

enter image description here

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

2 个答案:

答案 0 :(得分:1)

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

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

不认为是重新部署任务。

要纠正此问题,有几种选择:

  • 在您的for循环中包含一个sleep。这是最原始的方式,但实施起来却最快以进行测试。

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

答案 1 :(得分:0)

我认为您最好的选择是通过CodeDeploy进行蓝/绿部署,假设您使用的是Elastic Load Balancer。蓝色/绿色部署将自动检测到任何错误,并在需要时停止部署。

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