您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。