共计 6141 个字符,预计需要花费 16 分钟才能阅读完成。
这期内容当中丸趣 TV 小编将会给大家带来有关 Jenkins 怎样手动更新 AWS 上面的 ECS 服务,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
以上是整个部署更新的流程图:
1. 开发人员对上线的代码打一个 tag,然后把带 tag 的代码推到 AWS codecommit 上面。
git add -A *
git commit -a -m ${tag}
git tag ${tag}
git push origin 分支 ${tag}
5.jenkins 进行代码构建作业:
(首先需要安装插件:Amazon ECR plugin、Docker plugin)
以下是 jenkins 项目配置的示例:
以下是构建 image 和把 image 推送到 ECR 上
以下是更新 ECS 服务的设置(适用于更新接口、对外接口、定时任务、前端、app 的 H5 前端):
例子:(api 的配置)
#!/bin/bash
REGION= ap-northeast-1 #区域
REPOSITORY_NAME= exchange-api #ECR 的存储库
CLUSTER= online-exchange #集群名称
SERVICE_NAME= online-exchange-api #服务名称
FAMILY= online_exchange-api # 任务定义名称
NAME= online-exchange-api # 容器名称
TASKDEFNAME= online_exchange-api ## 任务定义名称
# 列出存储库的 URL
REPOSITORY_URI=`aws ecr describe-repositories –repository-names ${REPOSITORY_NAME} –region ${REGION} | jq .repositories[].repositoryUri | tr -d `
# 根据构建的 tag 标签去创建一个生成任务定义的 json 文件
sudo sed -e %TAG%;${tag};g -e %REPOSITORY_URI%;${REPOSITORY_URI};g /opt/update-exchange/update-api/online-ecs-exchange-api.json /opt/update-exchange/update-api/${NAME}-${tag}.json
# 根据 ECR 里面的 URL, 创建一个新的任务定义
aws ecs register-task-definition –family ${FAMILY} –cli-input-json file:///opt/update-exchange/update-api/${NAME}-${tag}.json –region ${REGION}
SERVICES=`aws ecs describe-services –services ${SERVICE_NAME} –cluster ${CLUSTER} –region ${REGION} | jq .failures[]`
# 获取最新的版本
REVISION=`aws ecs describe-task-definition –task-definition ${TASKDEFNAME} –region ${REGION} | jq .taskDefinition.revision`
# 创建或者更新服务
if [$SERVICES ==]; then
echo entered existing service
DESIRED_COUNT=`aws ecs describe-services –services ${SERVICE_NAME} –cluster ${CLUSTER} –region ${REGION} | jq .services[].desiredCount`
if [${DESIRED_COUNT} = 0 ]; then
DESIRED_COUNT= 1
fi
aws ecs update-service –cluster ${CLUSTER} –region ${REGION} –service ${SERVICE_NAME} –task-definition ${FAMILY}:${REVISION}
else
echo entered new service
aws ecs create-service –service-name ${SERVICE_NAME} –desired-count 2 –task-definition ${FAMILY} –cluster ${CLUSTER} –region ${REGION}
fi
# 更新服务成功之后,执行清理镜像,清理文件。
docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-api:latest
docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-api:${tag}
\rm /opt/update-exchange/update-api/${NAME}-${tag}.json
以下是 ECS 更新的配置(适用于更新撮合、结算):
例子 (结算):
#!/bin/bash
REGION= ap-northeast-1
REPOSITORY_NAME= exchange-deal
CLUSTER= online-exchange
SERVICE_NAME= online-exchange-deal
FAMILY= online_exchange-deal
NAME= online-exchange-deal
TASKDEFNAME= online_exchange-deal
REPOSITORY_URI=`aws ecr describe-repositories –repository-names ${REPOSITORY_NAME} –region ${REGION} | jq .repositories[].repositoryUri | tr -d `
#Replace the build number and respository URI placeholders with the constants above
sudo sed -e %TAG%;${tag};g -e %REPOSITORY_URI%;${REPOSITORY_URI};g /opt/update-exchange/update-deal/online-ecs-exchange-deal.json /opt/update-exchange/update-deal/${NAME}-${tag}.json
#Register the task definition in the repository
aws ecs register-task-definition –family ${FAMILY} –cli-input-json file:///opt/update-exchange/update-deal/${NAME}-${tag}.json –region ${REGION}
SERVICES=`aws ecs describe-services –services ${SERVICE_NAME} –cluster ${CLUSTER} –region ${REGION} | jq .failures[]`
#Get latest revision
REVISION=`aws ecs describe-task-definition –task-definition ${TASKDEFNAME} –region ${REGION} | jq .taskDefinition.revision`
#首先把预期数改为 0,再删除服务,再创建新的服务。
if [$SERVICES ==]; then
echo entered existing service
aws ecs update-service –cluster ${CLUSTER} –service ${SERVICE_NAME} –desired-count 0
sleep 10
aws ecs delete-service –cluster ${CLUSTER} –service ${SERVICE_NAME}
sleep 100
aws ecs create-service –cluster ${CLUSTER} –service-name ${SERVICE_NAME} –task-definition ${FAMILY}:${REVISION} –desired-count 1 –launch-type FARGATE –network-configuration awsvpcConfiguration={subnets=[subnet-04a486cb94c9c9032,subnet-0d93a8c5452781c8f],securityGroups=[sg-0c66800c0bdd235fc],assignPublicIp=ENABLED}
else
echo service does not exist
echo create a new service
aws ecs create-service –cluster ${CLUSTER} –service-name ${SERVICE_NAME} –task-definition ${FAMILY}:${REVISION} –desired-count 1 –launch-type FARGATE –network-configuration awsvpcConfiguration={subnets=[subnet-04a486cb94c9c9032,subnet-0d93a8c5452781c8f],securityGroups=[sg-0c66800c0bdd235fc],assignPublicIp=ENABLED}
fi
# 更新服务成功之后,执行清理镜像,清理文件。
docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-deal:latest
docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-deal:${tag}
\rm /opt/update-exchange/update-deal/${NAME}-${tag}.json
先删除服务的目的是为了绝对的保证服务只会存在一个,最后有一个清理镜像,清理文件,是为了减少磁盘的压力。
以下是 ECS 更新的配置(适用于更新推送):
例子 (推送):
#!/bin/bash
REGION= ap-northeast-1
REPOSITORY_NAME= exchange-ws
CLUSTER= online-exchange
SERVICE_NAME= online-exchange-ws
FAMILY= online_exchange-ws
NAME= online-exchange-ws
TASKDEFNAME= online_exchange-ws
REPOSITORY_URI=`aws ecr describe-repositories –repository-names ${REPOSITORY_NAME} –region ${REGION} | jq .repositories[].repositoryUri | tr -d `
#Replace the build number and respository URI placeholders with the constants above
sudo sed -e %TAG%;${tag};g -e %REPOSITORY_URI%;${REPOSITORY_URI};g /opt/update-exchange/update-ws/online-ecs-exchange-ws.json /opt/update-exchange/update-ws/${NAME}-${tag}.json
#Register the task definition in the repository
aws ecs register-task-definition –family ${FAMILY} –cli-input-json file:///opt/update-exchange/update-ws/${NAME}-${tag}.json –region ${REGION}
SERVICES=`aws ecs describe-services –services ${SERVICE_NAME} –cluster ${CLUSTER} –region ${REGION} | jq .failures[]`
#Get latest revision
REVISION=`aws ecs describe-task-definition –task-definition ${TASKDEFNAME} –region ${REGION} | jq .taskDefinition.revision`
# 首先把预期数改为 0,等待服务中没有运行得任务,再去更新任务。
aws ecs update-service –cluster online-exchange –service online-exchange-ws –desired-count 0
while :
do
count=`aws ecs describe-services –cluster ${CLUSTER} –service ${SERVICE_NAME} | grep runningCount | tail -n1 | egrep -oE [0-9] `
sleep 30
if [$count -eq 0];then
aws ecs update-service –cluster ${CLUSTER} –region ${REGION} –service ${SERVICE_NAME} –task-definition ${FAMILY}:${REVISION} –desired-count 1
exit
fi
done
#更新服务成功之后,执行清理镜像,清理文件。
docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-ws:latest
docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-ws:${tag}
\rm /opt/update-exchange/update-ws/${NAME}-${tag}.json
上述就是丸趣 TV 小编为大家分享的 Jenkins 怎样手动更新 AWS 上面的 ECS 服务了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注丸趣 TV 行业资讯频道。