排查 Amazon ECS 蓝绿部署问题 - Amazon Elastic Container Service

排查 Amazon ECS 蓝绿部署问题

以下内容为您提供在使用 Amazon ECS 进行蓝绿部署时可能出现的常见问题的解决方案。以下阶段可能会发生蓝绿部署错误:

  • 同步路径:响应 CreateServiceUpdateService API 调用时立即出现的错误。

  • 异步路径:出现在 DescribeServiceDeploymentsstatusReason 字段中并导致部署回滚的错误

负载均衡器配置问题

负载均衡器配置是 Amazon ECS 中蓝绿部署的关键组成部分。适当配置侦听器规则、目标组和负载均衡器类型对于成功部署至关重要。本部分介绍可能导致蓝绿部署失败的常见负载均衡器配置问题。

排查负载均衡器问题时,了解侦听器规则与目标组之间的关系十分重要。在蓝绿部署中:

  • 生产监听器规则会将流量定向到当前处于活动状态(蓝色)的服务版本

  • 可以使用测试侦听器规则在转移生产流量之前验证新的(绿色)服务版本

  • 目标组用于从每个服务版本中注册容器实例

  • 部署过程中,可以通过调整侦听器规则中目标组的权重,将流量逐渐从蓝色服务版本转移到绿色服务版本

侦听器规则配置错误

以下问题与蓝绿部署的侦听器规则配置不正确有关。

使用应用程序负载均衡器侦听器 ARN 而不是侦听器规则 ARN

错误消息productionListenerRule has an invalid ARN format. Must be RuleArn for ALB or ListenerArn for NLB. Got: arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-alb/abc123/def456

解决方案:使用应用程序负载均衡器时,必须为 productionListenerRuletestListenerRule 指定侦听器规则 ARN,而不是监听器 ARN。对于网络负载均衡器,则必须使用侦听器 ARN。

有关如何查找侦听器 ARN 的更多信息,请参阅《应用程序负载均衡器用户指南》中的应用程序负载均衡器的侦听器。规则的 ARN 格式为 arn:aws:elasticloadbalancing:region:account-id:listener-rule/app/...

为生产和测试侦听器使用相同的规则

错误消息The following rules cannot be used as both production and test listener rules: arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-alb/abc123/def456/ghi789

解决方案:您必须为生产和测试流量使用不同的侦听器规则。为路由到测试目标组的测试流量创建单独的侦听器规则。

网络负载均衡器缺少测试侦听器规则

错误消息TestListenerRule is required for NLB with arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/nlb-targetgroup/abc123

解决方案:使用网络负载均衡器时,必须同时指定 productionListenerRuletestListenerRule。在您的配置中添加具有有效侦听器 ARN 的 testListenerRule。有关更多信息,请参阅《网络负载均衡器用户指南》中的创建网络负载均衡器的侦听器

不与侦听器规则关联的目标组

错误消息Service deployment rolled back because of invalid networking configuration: Target group arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myAlternateTG/abc123 is not associated with either productionListenerRule or testListenerRule.

解决方案:主要目标组和替代目标组都必须与生产侦听器规则或测试侦听器规则相关联。更新您的负载均衡器配置,以确保两个目标组都与您的侦听器规则适当关联。

应用程序负载均衡器缺少测试侦听器规则

错误消息For Application LoadBalancer, testListenerRule is required when productionListenerRule is not associated with both targetGroup and alternateTargetGroup

解决方案:使用应用程序负载均衡器时,如果两个目标组都不与生产侦听器规则关联,则您必须指定测试侦听器规则。在配置中添加 testListenerRule,并确保两个目标组都与生产或测试侦听器规则相关联。有关更多信息,请参阅《应用程序负载均衡器用户指南》中的应用程序负载均衡器的侦听器

目标组配置错误

以下问题与蓝绿部署的目标组配置不正确有关。

侦听器规则中有流量的多个目标组

错误消息Service deployment rolled back because of invalid networking configuration. productionListenerRule arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-alb/abc123/def456/ghi789 should have exactly one target group serving traffic but found 2 target groups which are serving traffic

解决方案:在开始蓝绿部署之前,请确保侦听器规则中只有一个目标组在接收流量(权重不为零)。更新您的侦听器规则配置,将任何不应接收流量的目标组的权重设置为零。

在负载均衡器条目间复制目标组

错误消息Duplicate targetGroupArn found: arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myecs-targetgroup/abc123

解决方案:每个目标组 ARN 在服务定义的所有负载均衡器条目中必须是唯一的。查看您的配置,并确保为每个负载均衡器条目使用不同的目标组。

生产侦听器规则中出现意外的目标组

错误消息Service deployment rolled back because of invalid networking configuration. Production listener rule is forwarding traffic to unexpected target group arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/random-nlb-tg/abc123. Expected traffic to be forwarded to either targetGroupArn: arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/nlb-targetgroup/def456 or alternateTargetGroupArn: arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/nlb-tg-alternate/ghi789

解决方案:生产侦听器规则正在将流量转发到未在服务定义中指定的目标组。确保将侦听器规则配置为,仅将流量转发到服务定义中指定的目标组。

有关更多信息,请参阅《应用程序负载均衡器用户指南》中的转发操作

负载均衡器类型配置错误

以下问题与蓝绿部署的负载均衡器类型配置不正确有关。

经典负载均衡器和应用程序负载均衡器或网络负载均衡器的混合配置

错误消息All loadBalancers must be strictly either ELBv1 (defining loadBalancerName) or ELBv2 (defining targetGroupArn)

注意

经典负载均衡器是 Elastic Load Balancing 的上一代负载均衡器。建议您迁移到当前一代负载均衡器。有关更多信息,请参阅迁移您的经典负载均衡器

解决方案:或是使用所有的经典负载均衡器,或是使用所有的应用程序负载均衡器和网络负载均衡器。

对于应用程序负载均衡器和网络负载均衡器,请仅指定 targetGroupArn 字段。

将高级配置与经典负载均衡器结合使用

错误消息advancedConfiguration field is not allowed with ELBv1 loadBalancers

解决方案:仅在应用程序负载均衡器和网络负载均衡器中支持蓝绿部署高级配置。如果您使用经典负载均衡器(使用 loadBalancerName 指定),则无法使用 advancedConfiguration 字段。要么切换到应用程序负载均衡器,要么移除 advancedConfiguration 字段。

负载均衡器间的高级配置不一致

错误消息Either all or none of the provided loadBalancers must have advancedConfiguration defined

解决方案:如果您使用多个负载均衡器,则必须为所有负载均衡器定义 advancedConfiguration,或者不为其中的任何一个定义。更新您的配置,以确保所有负载均衡器条目的一致性。

蓝绿部署缺少高级配置

错误消息advancedConfiguration field is required for all loadBalancers when using a non-ROLLING deployment strategy

解决方案:将应用程序负载均衡器与蓝绿部署策略结合使用时,必须为所有负载均衡器条目指定 advancedConfiguration 字段。将所需的 advancedConfiguration 添加到您的负载均衡器配置中。

权限问题

以下问题与蓝绿部署权限不足有关。

缺少关于基础设施角色的信任策略

错误消息Service deployment rolled back because of invalid networking configuration. ECS was unable to manage the ELB resources due to missing permissions on ECS Infrastructure Role 'arn:aws:iam::123456789012:role/Admin'.

解决方案:为管理负载均衡器资源而指定的 IAM 角色不具有正确的信任策略。更新角色的信任策略,以允许服务担任角色。信任策略必须包括:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ecs.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
负载均衡器角色缺少读取权限

错误消息service myService failed to describe target health on target-group myTargetGroup with (error User: arn:aws:sts::123456789012:assumed-role/myELBRole/ecs-service-scheduler is not authorized to perform: elasticloadbalancing:DescribeTargetHealth because no identity-based policy allows the elasticloadbalancing:DescribeTargetHealth action)

解决方案:用于管理负载均衡器资源的 IAM 角色不具有读取目标运行状况信息的权限。将 elasticloadbalancing:DescribeTargetHealth 权限添加到角色的策略。有关 Elastic Load Balancing 权限的信息,请参阅适用于负载均衡器的 Amazon ECS 基础设施 IAM 角色

负载均衡器角色缺少写入权限

错误消息service myService failed to register targets in target-group myTargetGroup with (error User: arn:aws:sts::123456789012:assumed-role/myELBRole/ecs-service-scheduler is not authorized to perform: elasticloadbalancing:RegisterTargets on resource: arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/myTargetGroup/abc123 because no identity-based policy allows the elasticloadbalancing:RegisterTargets action)

解决方案:用于管理负载均衡器资源的 IAM 角色不具有注册目标的权限。将 elasticloadbalancing:RegisterTargets 权限添加到角色的策略。有关 Elastic Load Balancing 权限的信息,请参阅适用于负载均衡器的 Amazon ECS 基础设施 IAM 角色

缺少修改侦听器规则的权限

错误消息Service deployment rolled back because TEST_TRAFFIC_SHIFT lifecycle hook(s) failed. User: arn:aws:sts::123456789012:assumed-role/myELBRole/ECSNetworkingWithELB is not authorized to perform: elasticloadbalancing:ModifyListener on resource: arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-alb/abc123/def456 because no identity-based policy allows the elasticloadbalancing:ModifyListener action

解决方案:用于管理负载均衡器资源的 IAM 角色不具有修改侦听器的权限。将 elasticloadbalancing:ModifyListener 权限添加到角色的策略。有关 Elastic Load Balancing 权限的信息,请参阅适用于负载均衡器的 Amazon ECS 基础设施 IAM 角色

对于蓝绿部署,建议将 AmazonECS-ServiceLinkedRolePolicy 托管策略附加到您的基础设施角色,其中包括管理负载均衡器资源的所有必要权限。

生命周期挂钩问题

以下问题与蓝绿部署中的生命周期挂钩有关。

Lambda 挂钩角色的信任策略不正确

错误消息Service deployment rolled back because TEST_TRAFFIC_SHIFT lifecycle hook(s) failed. ECS was unable to assume role arn:aws:iam::123456789012:role/Admin

解决方案:为 Lambda 生命周期挂钩指定的 IAM 角色不具有正确的信任策略。更新角色的信任策略,以允许服务担任角色。信任策略必须包括:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ecs.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
Lambda 挂钩返回 FAILED 状态

错误消息Service deployment rolled back because TEST_TRAFFIC_SHIFT lifecycle hook(s) failed. Lifecycle hook target arn:aws:lambda:us-west-2:123456789012:function:myHook returned FAILED status.

解决方案:指定为生命周期挂钩的 Lambda 函数返回 FAILED 状态。查看 Amazon CloudWatch Logs 中的 Lambda 函数日志,以确定失败原因,然后更新函数以正确处理部署事件。

缺少调用 Lambda 函数的权限

错误消息Service deployment rolled back because TEST_TRAFFIC_SHIFT lifecycle hook(s) failed. ECS was unable to invoke hook target arn:aws:lambda:us-west-2:123456789012:function:myHook due to User: arn:aws:sts::123456789012:assumed-role/myLambdaRole/ECS-Lambda-Execution is not authorized to perform: lambda:InvokeFunction on resource: arn:aws:lambda:us-west-2:123456789012:function:myHook because no identity-based policy allows the lambda:InvokeFunction action

解决方案:用于 Lambda 生命周期挂钩的 IAM 角色不具有调用 Lambda 函数的权限。为特定的 Lambda 函数 ARN 向角色的策略添加 lambda:InvokeFunction 权限。有关最低权限的更多信息,请参阅 Amazon ECS 蓝绿部署中 Lambda 函数所需的权限

Lambda 函数超时或响应无效

错误消息Service deployment rolled back because TEST_TRAFFIC_SHIFT lifecycle hook(s) failed. ECS was unable to parse the response from arn:aws:lambda:us-west-2:123456789012:function:myHook due to HookStatus must not be null

解决方案:Lambda 函数超时,或返回了无效的响应。请确保您的 Lambda 函数返回 hookStatus 字段被设置为 SUCCEEDEDFAILED 的有效响应。此外,请检查 Lambda 函数超时是否设置得适合您的验证逻辑。有关更多信息,请参阅 适用于 Amazon ECS 服务部署的生命周期挂钩

有效的 Lambda 响应示例:

{ "hookStatus": "SUCCEEDED", "reason": "Validation passed" }