您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Jenkins怎樣手動(dòng)更新AWS上面的ECS服務(wù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
以上是整個(gè)部署更新的流程圖:
1.開發(fā)人員對(duì)上線的代碼打一個(gè)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項(xiàng)目配置的示例:
以下是構(gòu)建image和把image推送到ECR上
以下是更新ECS服務(wù)的設(shè)置(適用于更新接口、對(duì)外接口、定時(shí)任務(wù)、前端、app的H5前端):
例子:(api的配置)
#!/bin/bash
REGION="ap-northeast-1" #區(qū)域
REPOSITORY_NAME="exchange-api" #ECR的存儲(chǔ)庫(kù)
CLUSTER="online-exchange" #集群名稱
SERVICE_NAME="online-exchange-api" #服務(wù)名稱
FAMILY="online_exchange-api" #任務(wù)定義名稱
NAME="online-exchange-api" #容器名稱
TASKDEFNAME="online_exchange-api" ##任務(wù)定義名稱
#列出存儲(chǔ)庫(kù)的URL
REPOSITORY_URI=`aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d '"'`
#根據(jù)構(gòu)建的tag標(biāo)簽去創(chuàng)建一個(gè)生成任務(wù)定義的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
#根據(jù)ECR里面的URL,創(chuàng)建一個(gè)新的任務(wù)定義
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`
#創(chuàng)建或者更新服務(wù)
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
#更新服務(wù)成功之后,執(zhí)行清理鏡像,清理文件。
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更新的配置(適用于更新撮合、結(jié)算):
例子(結(jié)算):
#!/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`
#首先把預(yù)期數(shù)改為0,再刪除服務(wù),再創(chuàng)建新的服務(wù)。
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
#更新服務(wù)成功之后,執(zhí)行清理鏡像,清理文件。
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
先刪除服務(wù)的目的是為了絕對(duì)的保證服務(wù)只會(huì)存在一個(gè),最后有一個(gè)清理鏡像,清理文件,是為了減少磁盤的壓力。
以下是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`
#首先把預(yù)期數(shù)改為0,等待服務(wù)中沒有運(yùn)行得任務(wù),再去更新任務(wù)。
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
#更新服務(wù)成功之后,執(zhí)行清理鏡像,清理文件。
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怎樣手動(dòng)更新AWS上面的ECS服務(wù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。