Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manually update the ECS service on AWS by Jenkins

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how Jenkins manually updates the ECS service on AWS. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

The above is the flowchart of the entire deployment update:

1. The developer type a tag to the online code, and then push the code with tag to AWS codecommit.

Git add-A *

Git commit-a-m "${tag}"

Git tag "${tag}"

Git push origin Branch ${tag}

5.jenkins does the code build job:

(first you need to install plug-ins: Amazon ECR plugin, Docker plugin)

The following is an example of a jenkins project configuration:

Here's how to build image and push image to ECR

The following are the settings for updating the ECS service (for updating interfaces, external interfaces, timing tasks, front end, H5 front end of app):

Example: (configuration of api)

#! / bin/bash

REGION= "ap-northeast-1" # area

Repository for REPOSITORY_NAME= "exchange-api" # ECR

CLUSTER= "online-exchange" # Cluster name

SERVICE_NAME= "online-exchange-api" # Service name

FAMILY= "online_exchange-api" # Task definition name

NAME= "online-exchange-api" # Container name

TASKDEFNAME= "online_exchange-api" # # Task definition name

# list the URL of the repository

REPOSITORY_URI= `aws ecr describe-repositories-- repository-names ${REPOSITORY_NAME}-- region ${REGION} | jq .repositories [] .repositoryUri | tr-d'"'`

# create a json file that generates the task definition according to the built tag tag

Sudo sed-e "s opt/update-exchange/update-api/online-ecs-exchange-api.json% tag% politics ${tag}; g"-e "s politics% REPOSITORYPROSITORYPROSTOYPROTURI% politics ${REPOSITORY_URI}; g" / opt/update-exchange/update-api/online-ecs-exchange-api.json > / opt/update-exchange/update-api/$ {NAME}-${tag} .json

# create a new task definition based on the URL in ECR

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 [] `

# get the latest version

REVISION= `aws ecs describe-task-definition-- task-definition ${TASKDEFNAME}-- region ${REGION} | jq .taskDefinition.construcsion`

# create or update services

If ["$SERVICES" = = ""]; then

Echo "entered existing service"

DESIRED_COUNT= `aws ecs describe-services-- services ${SERVICE_NAME}-- cluster ${CLUSTER}-- region ${REGION} | jq .services [] .accounredCount`

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

# after the update service is successful, clean the image and clean the files.

Docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-api:latest

Docker rmi 786381498352.dkr.ecr.apmurnortheastMush1.amazonaws.comAccorde exchangeLay ApiVel ${tag}

\ rm / opt/update-exchange/update-api/$ {NAME}-${tag} .json

The following is the configuration of ECS update (applicable to update matchmaking and settlement):

Example (settlement):

#! / 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 opt/update-exchange/update-deal/online-ecs-exchange-deal.json% tag% politics ${tag}; g"-e "s politics% REPOSITORYPROSITORYPROSTOYPROTURI% politics ${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.construcsion`

# first change the expected number to 0, then delete the service, and then create a new service.

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

# after the update service is successful, clean the image and clean the files.

Docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-deal:latest

Docker rmi 786381498352.dkr.ecr.apluto northeastLay 1.amazonaws.comAccorde exchangeMerchesDealoLuo ${tag}

\ rm / opt/update-exchange/update-deal/$ {NAME}-${tag} .json

The purpose of deleting the service first is to absolutely guarantee that only one service will exist, and finally to clean up the image and clean up the files in order to reduce the pressure on the disk.

The following is the configuration of the ECS update (for update push):

Example (push):

#! / 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 opt/update-exchange/update-ws/online-ecs-exchange-ws.json% tag% politics ${tag}; g"-e "s politics% REPOSITORYPROSITORYPROSTOYPROTURI% politics ${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.construcsion`

# first change the expected number to 0, wait for the task that is not running in the service, and then update the task.

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

# after the update service is successful, clean the image and clean the files.

Docker rmi 786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-ws:latest

Docker rmi 786381498352.dkr.ecr.apluto northeastMush1.amazonaws.comAccorde exchangeLeiwsLuo ${tag}

\ rm / opt/update-exchange/update-ws/$ {NAME}-${tag} .json

This is how Jenkins updates the ECS service on AWS manually. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report