这期内容当中小编将会给大家带来有关Jenkins怎样手动更新AWS上面的ECS服务,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
以上是整个部署更新的流程图:
1.开发人员对上线的代码打一个tag,然后把带tag的代码推到AWS codecommit上面。
git add -A *
git commit -a -m "${tag}"
git tag "${tag}"
git push origin 分支 ${tag}
(首先需要安装插件: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 "s;%TAG%;${tag};g" -e "s;%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 "s;%TAG%;${tag};g" -e "s;%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 "s;%TAG%;${tag};g" -e "s;%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
上述就是小编为大家分享的Jenkins怎样手动更新AWS上面的ECS服务了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。