常用搜索

更新服务并恢复之前的版本

AWSBeginner
立即练习

介绍

你的服务正在运行经过测试的版本。新版本应在能够处理请求后才替代旧任务。本实验将双任务 ECS 服务更新为正常的新镜像,诊断一个虽然运行但返回 HTTP 503 的后续版本,再手动恢复最初经过验证的任务定义。

请先完成前面的 ECS 服务实验。本实验在新的 VM 中独立开始,提供三个不可变应用镜像、应用源码、基础任务定义文件、基础执行角色、网络和 ALB。你将亲自注册修订并管理服务。无需之前 VM 的资源或个人 AWS 账号。使用下方 Terminal 执行命令,通过上方 AWS View 查看任务修订、部署状态、目标健康和真实应用响应。请保持同一终端打开,保留变量。

两个正常镜像分别返回 release-1 和 release-2,HTTP 状态为 200。故意提供的故障镜像返回 release-broken 和 HTTP 503,/health 也不例外。健康检查路径始终保持 /health;故障来自新应用版本。

认证考点

认证 考试技能 本实验实践
Developer – Associate(DVA-C02) 技能 3.2.1 和 3.4.8 测试已部署应用的响应,并使用已有任务定义修订手动恢复经过测试的版本。

本实验覆盖这些技能中的服务发布和恢复部分,不配置 CI/CD 流水线或自动部署熔断器。

实验概览

概念图:测试版本 1,发布正常版本 2,诊断故障修订 3,然后手动恢复经过验证的第一个修订。

准备经过测试的版本

本步骤中,你将检查已提供的镜像,并在自己的集群中注册第一个任务定义修订。

ECS 任务定义是具有版本的配置。在同一 family(系列)中注册另一份配置,会生成新的 revision(修订),而不会修改之前的修订。镜像 digest(摘要)固定确切镜像内容;标签是便于阅读的名称。已提供的三个标签不可变,分别指向不同摘要。

打印文件和镜像清单。基础定义通过摘要引用 release-1,并使用之前实验介绍的基础执行角色和日志设置:

cd /home/labex/project
cat task-definition.json

查看 network.json:

cat network.json

查看预置 ECR 镜像清单:

aws ecr describe-images \
  --repository-name ct05-app \
  --query 'imageDetails[].{Tags:imageTags,Digest:imageDigest}'

预期有 release-1、release-2 和 release-broken,每个都有不同的 sha256:... 摘要。不要重新构建或覆盖这些输入;本实验关注通过 ECS 发布它们。

保存目标组 ARN 和应用 URL。$(...) 捕获命令输出,--output text 返回不带引号的值:

TARGET_GROUP=$(aws elbv2 describe-target-groups \
  --names ct05-app --query 'TargetGroups[0].TargetGroupArn' --output text)
echo "$TARGET_GROUP"

保存并显示 APPLICATION_URL:

APPLICATION_URL=$(cat application-url.txt)
echo "$APPLICATION_URL"

创建自己的集群,并注册已提供定义。file:// 读取 JSON 文件。${DEFINITION_1##*/} 移除 ARN 最后一个斜杠及之前的前缀,得到标准的 family:revision 引用:

aws ecs create-cluster --cluster-name ct05-demo \
  --query 'cluster.{Name:clusterName,Status:status}'

保存并显示 DEFINITION_1:

DEFINITION_1=$(aws ecs register-task-definition \
  --cli-input-json file://task-definition.json \
  --query 'taskDefinition.taskDefinitionArn' --output text)
echo "$DEFINITION_1"

保存并显示 REVISION_1:

REVISION_1=${DEFINITION_1##*/}
printf '%s\n' "$REVISION_1"

预期为 ct05-demo:1。保留此引用:测试后,你可以返回相同配置和确切镜像。AWS View 显示你的活动集群及已提供的 ct05-reference,暂时没有服务任务。

建立正常工作的服务

本步骤中,你将从修订 1 创建两个任务,在更改前确认正常工作基线。

服务要求运行两个任务,并将它们的 8000 端口注册到已提供的 IP 目标组。300 秒健康检查宽限期让新任务有时间启动,之后 ECS 才根据不健康检查采取行动。ALB 在宽限期内仍检查 /health。本实验也利用这段时间让你稍后观察受控故障版本。

使用保存的修订引用及已提供网络创建服务:

aws ecs create-service \
  --cluster ct05-demo --service-name ct05-app \
  --task-definition "$REVISION_1" --desired-count 2 \
  --launch-type FARGATE --network-configuration file://network.json \
  --load-balancers "targetGroupArn=${TARGET_GROUP},containerName=app,containerPort=8000" \
  --health-check-grace-period-seconds 300 \
  --query 'service.{Name:serviceName,Desired:desiredCount,Definition:taskDefinition}'

创建仅记录请求。先等待服务计数,再单独等待目标健康;成功的 waiter 不输出内容:

aws ecs wait services-stable --cluster ct05-demo --services ct05-app

等待 elbv2 target-in-service:

aws elbv2 wait target-in-service --target-group-arn "$TARGET_GROUP"

查看ECS 服务配置和任务数量:

aws ecs describe-services --cluster ct05-demo --services ct05-app \
  --query 'services[0].{Desired:desiredCount,Running:runningCount,Pending:pendingCount,Definition:taskDefinition,Rollout:deployments[0].rolloutState}'

查看已注册目标及其健康状态:

aws elbv2 describe-target-health --target-group-arn "$TARGET_GROUP" \
  --query 'TargetHealthDescriptions[].{IP:Target.Id,State:TargetHealth.State}'

预期 desired/running 都为 2,pending 为 0,使用修订 1,rollout 为 COMPLETED,两个目标为 healthy。计数不能证明应用可用。发送四个请求;Shell 的 for 循环重复请求,curl -f 遇到 HTTP 错误响应会失败:

for request in 1 2 3 4; do
  curl -fsS "$APPLICATION_URL"
  printf '\n'
done

所有响应应显示 release-1,这四次请求中出现两个主机名。在 AWS View 点击 Request application 查看真实响应。这个版本现在成为经过测试的恢复点。

发布正常的新修订

本步骤中,你将通过新镜像摘要注册修订 2,并更新现有服务以使用它。

发布先改变任务定义中的镜像,再告诉服务运行哪个修订。更新这个服务参数会启动部署,不会原地修改运行中的容器。

获取仓库 URI 及正常新镜像的摘要,将摘要引用保存到 Shell 变量:

REPOSITORY_URI=$(aws ecr describe-repositories \
  --repository-names ct05-app --query 'repositories[0].repositoryUri' --output text)
echo "$REPOSITORY_URI"

保存并显示 DIGEST_2:

DIGEST_2=$(aws ecr describe-images --repository-name ct05-app \
  --image-ids imageTag=release-2 --query 'imageDetails[0].imageDigest' --output text)
echo "$DIGEST_2"

保存并显示 IMAGE_2:

IMAGE_2="${REPOSITORY_URI}@${DIGEST_2}"
echo "$IMAGE_2"

jq 编辑 JSON,而不改变 CPU、内存、角色、端口或日志配置。--arg 为表达式提供字符串,> 将编辑后的定义写入新文件:

jq --arg image "$IMAGE_2" '.containerDefinitions[0].image = $image' \
  task-definition.json > task-definition-2.json

保存并显示 DEFINITION_2:

DEFINITION_2=$(aws ecs register-task-definition \
  --cli-input-json file://task-definition-2.json \
  --query 'taskDefinition.taskDefinitionArn' --output text)
echo "$DEFINITION_2"

保存并显示 REVISION_2:

REVISION_2=${DEFINITION_2##*/}
echo "$REVISION_2"

更新ECS 服务配置和任务数量:

aws ecs update-service --cluster ct05-demo --service ct05-app \
  --task-definition "$REVISION_2" \
  --query 'service.{Requested:taskDefinition,Desired:desiredCount}'

预期请求的定义以 ct05-demo:2 结尾。部署过程中,新旧任务可能同时运行。新任务接受检查时,旧健康任务继续响应。等待并检查部署完成后的结果:

aws ecs wait services-stable --cluster ct05-demo --services ct05-app

等待 elbv2 target-in-service:

aws elbv2 wait target-in-service --target-group-arn "$TARGET_GROUP"

查看ECS 服务配置和任务数量:

aws ecs describe-services --cluster ct05-demo --services ct05-app \
  --query 'services[0].{Requested:taskDefinition,Running:runningCount,Pending:pendingCount,Rollout:deployments[0].rolloutState}'

保存并显示 CURRENT_TASKS:

CURRENT_TASKS=$(aws ecs list-tasks --cluster ct05-demo \
  --service-name ct05-app --desired-status RUNNING --query taskArns --output text)
echo "$CURRENT_TASKS"

查看所选 ECS 任务:

aws ecs describe-tasks --cluster ct05-demo --tasks $CURRENT_TASKS \
  --query 'tasks[].{Status:lastStatus,Definition:taskDefinitionArn,Digest:containers[0].imageDigest}'

重复请求应用,比较各任务的响应:

for request in 1 2 3 4; do
  curl -fsS "$APPLICATION_URL"
  printf '\n'
done

不加引号的 $CURRENT_TASKS 将空格分隔的两个 ARN 作为独立 --tasks 参数传入。预期两个运行任务使用修订 2 和 DIGEST_2,rollout 为 COMPLETED,响应仅包含 release-2。在 AWS View 再次请求:响应面板显示最近一次请求的结果,不会自动替换先前响应文本。

诊断无法变为健康的版本

本步骤中,你将部署已提供的故障镜像,区分服务请求的修订与仍在提供流量的健康版本。

故障镜像可以启动,但 /health 返回 HTTP 503。运行中的任务不一定是健康的 ALB 目标。保持相同角色、端口、网络和健康路径,才能将故障定位到应用版本。

获取摘要,编辑新定义并更新服务:

BROKEN_DIGEST=$(aws ecr describe-images --repository-name ct05-app \
  --image-ids imageTag=release-broken --query 'imageDetails[0].imageDigest' --output text)
echo "$BROKEN_DIGEST"

保存并显示 BROKEN_IMAGE:

BROKEN_IMAGE="${REPOSITORY_URI}@${BROKEN_DIGEST}"
echo "$BROKEN_IMAGE"

将配置写入 task-definition-broken.json:

jq --arg image "$BROKEN_IMAGE" '.containerDefinitions[0].image = $image' \
  task-definition.json > task-definition-broken.json

保存并显示 BROKEN_DEFINITION:

BROKEN_DEFINITION=$(aws ecs register-task-definition \
  --cli-input-json file://task-definition-broken.json \
  --query 'taskDefinition.taskDefinitionArn' --output text)
echo "$BROKEN_DEFINITION"

保存并显示 BROKEN_REVISION:

BROKEN_REVISION=${BROKEN_DEFINITION##*/}
echo "$BROKEN_REVISION"

更新ECS 服务配置和任务数量:

aws ecs update-service --cluster ct05-demo --service ct05-app \
  --task-definition "$BROKEN_REVISION" \
  --query 'service.taskDefinition'

等待两次间隔五秒的健康检查,然后查看部署及目标健康原因:

sleep 12
aws ecs describe-services --cluster ct05-demo --services ct05-app \
  --query 'services[0].{Requested:taskDefinition,Desired:desiredCount,Running:runningCount,Rollout:deployments[0].rolloutState,Reason:deployments[0].rolloutStateReason}'
aws elbv2 describe-target-health --target-group-arn "$TARGET_GROUP" \
  --query 'TargetHealthDescriptions[].{IP:Target.Id,State:TargetHealth.State,Reason:TargetHealth.Reason}'
CURRENT_TASKS=$(aws ecs list-tasks --cluster ct05-demo \
  --service-name ct05-app --desired-status RUNNING --query taskArns --output text)
echo "$CURRENT_TASKS"
aws ecs describe-tasks --cluster ct05-demo --tasks $CURRENT_TASKS \
  --query 'tasks[].{Status:lastStatus,Definition:taskDefinitionArn,Digest:containers[0].imageDigest}'

预期服务请求修订 3,rollout 为 IN_PROGRESS。两个健康的修订 2 任务仍在;两个修订 3 任务运行着,但目标变为 unhealthy,原因为 Target.ResponseCodeMismatch。这段重叠会使 running 达到 4,desired 仍为 2。若等待超过宽限期,ECS 可能替换不健康任务,短暂出现 initial 目标;此版本仍不能达到健康稳定状态。

同时检查已发送的日志和普通应用请求:

aws logs filter-log-events --log-group-name /ecs/ct05 \
  --filter-pattern '"503"' --query 'events[].message'

请求应用并查看响应:

curl -i "$APPLICATION_URL"

日志包含真实 /health 请求的 503 响应。由于旧健康目标仍可用,普通请求依然收到 release-2 的 HTTP 200。这证明旧版本可用,不证明修订 3 部署成功。AWS View 将每个目标关联到任务定义修订,便于对比请求修订、健康目标与响应。再次点击 Request application。

不要等待故障部署变为稳定:它无法满足健康要求。不要修改健康路径,也不要把 503 接受为健康。下一步将显式恢复经过测试的修订。

AWS View 示例:请求的修订 3 仍在部署;健康的修订 2 目标继续返回 release-2,修订 3 目标检查失败。你的任务 ID 和镜像摘要会不同。

恢复最初经过测试的修订

本步骤中,你将手动恢复修订 1,并证明恢复后仅由它的镜像提供服务。

手动回退选择较早的任务定义。你已经测试修订 1,并保留了不可变镜像。这能恢复之前的应用配置,但不能撤销数据库更改或外部副作用。

将同一个服务更新为保存的修订,无需注册新任务定义:

aws ecs update-service --cluster ct05-demo --service ct05-app \
  --task-definition "$REVISION_1" \
  --query 'service.taskDefinition'

等待 ecs services-stable:

aws ecs wait services-stable --cluster ct05-demo --services ct05-app

等待 elbv2 target-in-service:

aws elbv2 wait target-in-service --target-group-arn "$TARGET_GROUP"

ECS 从修订 1 启动替代任务。先退役已被取代的不健康任务,再在恢复任务通过检查后退役健康的修订 2 任务。检查请求的修订及实际运行任务:

aws ecs describe-services --cluster ct05-demo --services ct05-app \
  --query 'services[0].{Requested:taskDefinition,Desired:desiredCount,Running:runningCount,Pending:pendingCount,Rollout:deployments[0].rolloutState}'

保存并显示 CURRENT_TASKS:

CURRENT_TASKS=$(aws ecs list-tasks --cluster ct05-demo \
  --service-name ct05-app --desired-status RUNNING --query taskArns --output text)
echo "$CURRENT_TASKS"

查看所选 ECS 任务:

aws ecs describe-tasks --cluster ct05-demo --tasks $CURRENT_TASKS \
  --query 'tasks[].{Status:lastStatus,Definition:taskDefinitionArn,Digest:containers[0].imageDigest}'

查看已注册目标及其健康状态:

aws elbv2 describe-target-health --target-group-arn "$TARGET_GROUP" \
  --query 'TargetHealthDescriptions[].{IP:Target.Id,State:TargetHealth.State}'

重复请求应用,比较各任务的响应:

for request in 1 2 3 4; do
  curl -fsS "$APPLICATION_URL"
  printf '\n'
done

预期 desired/running 为 2,pending 为 0,rollout 为 COMPLETED,两个修订 1 目标健康,响应仅为 release-1。主机名是新的:回退根据保留的定义和镜像启动新容器,不会重启最初已停止的任务。

在 AWS View 再次请求应用。停止的修订 2 和故障修订 3 任务仍作为历史存在,但不再执行或接收流量。服务没有自动回退;是你的 update-service 选择了恢复点。

AWS View 示例:恢复后的修订 1 有两个健康目标,重新请求的响应为 release-1。你的任务 ID 和镜像摘要会不同。

删除自己的服务和修订

本步骤中,你将删除自己的服务配置及任务执行,同时保留已提供的镜像、ALB、角色、网络、日志和参考集群。

先把 desired count 设为零,否则逐个停止任务会触发替代任务。等待服务停止全部任务:

aws ecs update-service --cluster ct05-demo --service ct05-app \
  --desired-count 0 --query 'service.desiredCount'

等待 ecs services-stable:

aws ecs wait services-stable --cluster ct05-demo --services ct05-app

删除空服务,注销亲自注册的三个修订,再删除自己的集群。循环将注销范围限定为保存的定义 ARN:

aws ecs delete-service --cluster ct05-demo --service ct05-app \
  --query 'service.status'

仅注销保存的三个任务定义修订:

for definition in "$DEFINITION_1" "$DEFINITION_2" "$BROKEN_DEFINITION"; do
  aws ecs deregister-task-definition --task-definition "$definition" \
    --query 'taskDefinition.status'
done

仅删除练习 ECS 集群:

aws ecs delete-cluster --cluster ct05-demo --query 'cluster.status'

这些操作返回 INACTIVE。确认没有任务执行或目标注册,并保留参考集群:

aws ecs list-tasks --cluster ct05-demo --desired-status RUNNING

查看已注册目标及其健康状态:

aws elbv2 describe-target-health --target-group-arn "$TARGET_GROUP" \
  --query 'TargetHealthDescriptions'

查看练习和参照集群的状态:

aws ecs describe-clusters --clusters ct05-demo ct05-reference \
  --query 'clusters[].{Name:clusterName,Status:status}'

查看预置 ECR 镜像清单:

aws ecr describe-images --repository-name ct05-app \
  --query 'imageDetails[].imageTags'

预期没有运行任务 ARN、没有目标注册,ct05-demo 非活动,ct05-reference 活动,三个已提供镜像标签都还在。已发送日志和停止任务历史可以保留。没有运行任务后,AWS View 隐藏旧应用响应。不要删除已准备的 ALB 或参考资源来通过清理。

总结

你注册了基于不可变镜像的任务定义修订,验证了正常服务更新,并诊断了运行任务无法通过真实健康检查的后续版本。你区分了请求的修订与仍在响应的健康版本,手动恢复最初经过测试的修订,确认任务摘要、目标健康和响应后,删除了自己的服务和修订。