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

Docker Micro Services-an implementation case of Jenkins+Gitlab+Maven+Shell Automation Construction

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

* Environment:

CentOS7

Code repository: Gitlab

Continuous integration code update: Jenkins

Build Packaging: Maven

Image automatic delivery script: Shell

Basic environment software installation: Gitlab, Jenkins, Maven configuration, installation reference network, installation strategy here, focusing on project continuous integration and image delivery.

Combined with the previous blog post: https://blog.51cto.com/10874766/2353577

Here, we configure the automatic construction of two basic service containers, the automatic construction of a regular business service container, and the continuous integration of other container services for similar operations, which do not repeat operations.

* create a basic continuous integration project-config

1.1 create a free-style software project-Job, create config-tmp-release

1.2 create maven project-Job, create basic project config-tmp-ms

1.3Configuring config-tmp-ms basic projects

Based on jdk8, configure the config-tmp-ms project Git code repository source and select the release branch.

Configure the associated project config-tmp-release:

Build commands and post-build action scripts:

1.4 configuration script

Project jar package synchronization script: / home/jenkins/docker-tmp/script/rsync.sh config-tmp-ms config-service

Cat / homepool jenkinsAccording DockerMutur ip passwd $1 $2 $3 BINGUBING BASHIDEN 10.1.1.1 # docker tmp serverpasswd=*rsync-vzrtopg-- progress-- password-file=/etc/rsyncd.password6 / home/jenkins/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar root@$ip::$1expect / home/jenkins/docker-test/script/image.exp $script $script

Where config-tmp-ms must be the same as the project job name. $1 is the project job name "config-tmp-ms", and $2 is the project code git@gitlab.alaxiaoyou.com:xxx/alaxiaoyou-config-service.git, provider:config-service. The goal is to automatically replace individual provider configurations.

The expect script remotely triggers the script on the rancher server: / home/jenkins/docker-pro/script/image.exp

Cat / home/jenkins/docker-pro/script/image.exp #! / usr/bin/expect-fset ipaddress [lindex $argv 0] set passwd [lindex $argv 1] set msname [lindex $argv 2] set msnameb [lindex $argv 3] spawn ssh name@$ipaddress;expect "password:"; send "$passwd\ r"; expect "#" send "sudo nohup / data/docker/layout-script/pro-all.sh $msname $msnameb > > / data/docker/logs/$msname.log &\ r" expect eof {exit 1}

Script, build, and push push to Ali image repository on rancher server server: / data/docker/layout-script/pro-all.sh

#! / bin/bashnamems=$1namemsb=$2version=latestmsrepo=/data/docker/$1imgrepo=xmbabyimgname=$1if [!-d $msrepo] Thenmkdir-p $msreposcp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepoecho "[$namems]" > > / etc/rsyncd.confecho "path=/data/docker/$namems/" > > / etc/rsyncd.confecho "comment = updateignore errorsread only = nolist = 10.1.1.1/255.255.255.0auth users = rootuid = rootgid = rootsecrets file = / etc/rsyncd.secrets" > > / etc/rsyncd.confelseecho "on Build the docker image "fiecho" FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8 "> $msrepo/dockerfileecho" MAINTAINER $namems "name@alaxiaoyou.com" > > $msrepo/dockerfileecho "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar / data/httpd/" > > $msrepo/dockerfileecho "WORKDIR / data/httpd/" > > $msrepo/dockerfileecho "ENTRYPOINT java-Xmx512m-Xss512k-jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" > > $msrepo/dockerfilecd $msrepodocker build-t ms/$namems .if [$?-ne 0] Thenecho "$namems image construction failed. Please check dockerfile!" exitelseimageid= `docker images | grep "ms/$namems" | awk'{print $3} '`docker login-- username=name@alaxiaoyou.com-- password=* registry-internal.cn-hangzhou.aliyuncs.comdocker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$versiondocker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$versionfi

1.5 build operation

Build a config-tmp-release project. After the execution of this project, the associated subordinate project config-tmp-ms,config-tmp-ms project will automatically pull the latest code in Jenkins, trigger the rsync synchronization script, and trigger the docker image build and push operations in the rsync script. After the final construction of Jenkins is completed, you can pull the newly delivered image from the image repository.

* create a basic continuous integration project-gateway

2.1 create a free-style software project-Job, create gateway-tmp-release

2.2 create maven project-Job, create basic project gateway-tmp-ms

2.3 configure the gateway-tmp-ms basic project

Based on jdk8, configure the gateway-tmp-ms project Git code repository source and select the release branch.

Configure the associated project gateway-tmp-release:

Configure pre-build action scripts, build commands, and post-build action scripts:

2.4 configuration script

Before building, the configuration template is replaced and configured with the corresponding provider. Here is gateway provider:

Script: sh / home/jenkins/docker-tmp/script/configuration.sh gateway-tmp-ms api-gateway

Cat / homepool JenkinsAccording Docker talk tpGUBN scriptUniversation.ShowThroupBinder Basihsed s/module/alaxiaoyou-$2/g / home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.yml > / home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.ymlyes | cp-rfp / home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml / home/jenkins/.jenkins/workspace/$1/src/main/resources/

Where gateway-tmp-ms must be the same as the project job name. $1 is the project job name "gateway-tmp-ms", and $2 is the project code git@gitlab.alaxiaoyou.com:xxx.git name "api-gateway". The goal is to automatically replace each provider configuration, and then automatically replace each subsequent provider.

Template file:

Cat / home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.ymlspring: application: name: module cloud: zookeeper: connect-string: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181 config: uri: http://msconfig:20000

After the configuration is built, the project package (maven packaging) synchronization script rsync is used. After pulling the replacement code from the git warehouse server, the package is synchronized to the docker-tmp server (rancher server):

Project jar package synchronization script: sh / home/jenkins/docker-tmp/script/rsync.sh gateway-tmp-ms api-gateway

Cat / homepool jenkinsAccording DockerMutur ip passwd $1 $2 $3 BINGUBING BASHIDEN 10.1.1.1 # docker tmp serverpasswd=*rsync-vzrtopg-- progress-- password-file=/etc/rsyncd.password6 / home/jenkins/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar root@$ip::$1expect / home/jenkins/docker-tmp/script/image.exp $script $script

The expect script remotely triggers the script on the rancher server: / home/jenkins/docker-tmp/script/image.exp

Cat / homeback jenkinspedicure; send "$passwd\ r"; expect "#" send "send" sudo nohup / data/docker/layout-script/all.sh $msname $msname $jvm > / data/docker/logs/$msname.log &\ r "

Script, build, and push push to Ali image repository on rancher server server: / data/docker/layout-script/all.sh

#! / bin/bashnamems=$1namemsb=$2version=latestmsrepo=/data/docker/$1imgrepo=xmbabyimgname=$1if [!-d $msrepo] Thenmkdir-p $msreposcp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepoecho "[$namems]" > > / etc/rsyncd.confecho "path=/data/docker/$namems/" > > / etc/rsyncd.confecho "comment = updateignore errorsread only = nolist = 10.1.1.1/255.255.255.0auth users = rootuid = rootgid = rootsecrets file = / etc/rsyncd.secrets" > > / etc/rsyncd.confelseecho "on Build the docker image "fiecho" FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8 "> $msrepo/dockerfileecho" MAINTAINER $namems "name@alaxiaoyou.com" > > $msrepo/dockerfileecho "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar / data/httpd/" > > $msrepo/dockerfileecho "WORKDIR / data/httpd/" > > $msrepo/dockerfileecho "ENTRYPOINT java-Xmx512m-Xss512k-jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" > > $msrepo/dockerfilecd $msrepodocker build-t ms/$namems .if [$?-ne 0] Thenecho "$namems image construction failed. Please check dockerfile!" exitelseimageid= `docker images | grep "ms/$namems" | awk'{print $3} '`docker login-- username=name@alaxiaoyou.com-- password=* registry-internal.cn-hangzhou.aliyuncs.comdocker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$versiondocker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$versionfi

2.5 build operation

Build a gateway-tmp-release project. After the execution of this project, the associated subordinate project gateway-tmp-ms,gateway-tmp-ms project will automatically pull the latest code in Jenkins, trigger the rsync synchronization script, and trigger the docker image build and push operations in the rsync script. After the final construction of Jenkins is completed, you can pull the newly delivered image from the image repository.

* create a continuous integration project-sms

3.1Create maven project-Job, create sms-tmp-release

3.2Configuring ms-tmp-release projects

Based on jdk8, configure the sms project Git code repository source and select the release branch.

Build based on pom.xml

3.3.Create maven project-Job, create sms-tmp-ms

3.4.Configuring sms-tmp-ms projects

Based on jdk8, configure the sms project Git code repository source and select the release branch:

Configure the associated project sms-tmp-release:

Configure pre-build action scripts, build commands, and post-build action scripts:

3.5 configuration script

Before building, the configuration template is replaced and configured with the corresponding provider. Here is sms provider:

Script: sh / home/jenkins/docker-tmp/script/configuration.sh sms-tmp-ms sms-provider

Cat / home/jenkins/docker-tmp/script/configuration.sh sms-tmp-ms SMS talk providerboat home/jenkins/.jenkins/workspace/$1/src/main/resources/ binder Bash s/module/alaxiaoyou-$2/g / home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.yml > / home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.ymlyes | cp-rfp / home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml / home/jenkins/.jenkins/workspace/$1/src/main/resources/

Template file:

Cat / home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.ymlspring: application: name: module cloud: zookeeper: connect-string: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181 config: uri: http://msconfig:20000

Where sms-tmp-ms must be the same as the project job name. $1 is the project job name "sms-tmp-ms", and $2 is the project code git@gitlab.alaxiaoyou.com:xxx.git name "sms-provider". The goal is to automatically replace individual provider configurations.

After the configuration is built, the project package (maven packaging) synchronization script rsync is used. After pulling the replacement code from the git warehouse server, the package is synchronized to the docker-tmp server (rancher server):

Script: sh / home/jenkins/docker-tmp/script/rsync.sh sms-tmp-ms sms-provider 256m

Cat / home/jenkins/docker-tmp/script/rsync.sh sms-tmp-ms sms-provider 256mBASTERBING BASHUM 10.1.1.1 # docker tmp serverpasswd=*rsync-vzrtopg-- numeric-ids-- progress-- password-file=/etc/rsyncd.password6 / home/jenkins/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar root@$ip::$1expect / home/jenkins/docker-test/script/image.exp $ip $passwd $1 $2 $3

Jenkins and rancher are not on the same server line. Use the shell expect script to remotely trigger the script on the rancher server:

Cat / homepin argv set msnameb set msnameb [lindex $argv 3] set jvm [lindex $argv 4] spawn ssh name@$ipaddress;expect "password:"; send "$passwd\ r"; expect "#" send "sudo nohup / data/docker/layout-script/all.sh $msname $msnameb $jvm > > / data/docker/logs/$msname.log &\ r" expect eof {exit 1}

Script, build, and push push to Ali image repository on rancher server server: / data/docker/layout-script/all.sh

#! / bin/bashnamems=$1namemsb=$2version=latestmsrepo=/data/docker/$1imgrepo=xmbabyimgname=$1if [!-d $msrepo] Thenmkdir-p $msreposcp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepoecho "[$namems]" > > / etc/rsyncd.confecho "path=/data/docker/$namems/" > > / etc/rsyncd.confecho "comment = updateignore errorsread only = nolist = 10.1.1.1/255.255.255.0auth users = rootuid = rootgid = rootsecrets file = / etc/rsyncd.secrets" > > / etc/rsyncd.confelseecho "on Build the docker image "fiecho" FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8 "> $msrepo/dockerfileecho" MAINTAINER $namems "name@alaxiaoyou.com" > > $msrepo/dockerfileecho "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar / data/httpd/" > > $msrepo/dockerfileecho "WORKDIR / data/httpd/" > > $msrepo/dockerfileecho "ENTRYPOINT java-Xmx512m-Xss512k-jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" > > $msrepo/dockerfilecd $msrepodocker build-t ms/$namems .if [$?-ne 0] Thenecho "$namems image construction failed. Please check dockerfile!" exitelseimageid= `docker images | grep "ms/$namems" | awk'{print $3} '`docker login-- username=name@alaxiaoyou.com-- password=* registry-internal.cn-hangzhou.aliyuncs.comdocker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$versiondocker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$versionfi

3.6 build operation

Build a sms-tmp-release project. After the execution of this project, the associated subordinate project sms-tmp-ms,sms-tmp-ms project will automatically pull the latest code in Jenkins, trigger the rsync synchronization script, and trigger the docker image build and push operations in the rsync script. After the final construction of Jenkins is completed, you can pull the newly delivered image from the image repository.

3.7 release built image

Enter the rancher console

Add a service tmp-sms-ms:

Map volumes:

Click "create", select "always pull the image before creation", and rancher will distribute the task to agent to pull the latest image.

The image is already on the rancher host and is manually built and delivered to the Ali Cloud image repository. It is not automatically built and delivered to the Aliyun image repository through Jenkins.

3.8 Image upgrade and rollback operations

Upgrade steps:

Stop the container in which the service is running

Pull the latest image

Run the new container

Roll back:

If you do not click "Upgraded" in the upper-right corner of the rancher console, the service can be rolled back.

The container with "stopped" status above the rancher console is an old container. Click the "start" button to roll back.

The container with "Running" status below is the upgraded container. Click the "Upgraded" button in the upper right corner to roll back; the upgrade of the container image is completed, and the old container is destroyed.

3.9 Images delivered by Ali Cloud Image Warehouse

Note: the above configuration includes the continuous delivery of two basic service containers, the continuous delivery of one regular service container, and the continuous delivery of similar operations for other container services. There is no repetition. The corresponding IP in the script for building and pushing the image warehouse is configured as the address of the production environment. Since the test environment is a private network, there is no public network IP. Therefore, the push operation involving mirroring cannot be operated successfully. The packaging of each service provider cannot be completed if it is to be operated automatically in Jenkins. Therefore, they are copied from other packaged servers, and then built on the rancher host, and push is pushed to the Aliyun image warehouse.

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