转载

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

* 环境:

CentOS 7

代码仓库:Gitlab

持续集成代码更新:Jenkins

构建打包:Maven

镜像自动交付脚本:Shell

基础环境软件安装:Gitlab、Jenkins、Maven配置,安装参考网上,这里安装略,重点介绍项目持续集成和镜像交付。

结合上一博文: https://www.linuxidc.com/Linux/2019-02/157106.htm

这里配置了两个基础服务容器的自动化构建,一个常规业务服务容器的自动化构建,其他的容器服务持续集成类似操作,这不做重复操作。

* 创建基础持续集成项目-config

1.1 创建自由风格的软件项目-Job,创建config-tmp-release

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

1.2 创建maven项目-Job,创建基础项目config-tmp-ms

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

1.3 配置config-tmp-ms基础项目

基于jdk8,配置config-tmp-ms项目Git代码仓库源,并选取release分支。

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

配置关联项目config-tmp-release:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

构建命令及构建后操作脚本:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

1.4配置脚本

项目jar包同步脚本:/home/jenkins/docker-tmp/script/rsync.sh config-tmp-ms config-service

cat /home/jenkins/docker-tmp/script/rsync.sh
#!/bin/bash
ip=10.1.1.1  #docker tmp server
passwd=******
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::$1
expect /home/jenkins/docker-test/script/image.exp $ip $passwd $1 $2 $3

其中config-tmp-ms必须跟项目job名称一样。$1为此项目job名称“config-tmp-ms”,$2为此项目代码git@gitlab.alaxiaoyou.com:xxx/alaxiaoyou-config-service.git, provider:config-service。目的是自动替换各个provider配置。

expect脚本远程触发rancher服务器上脚本: /home/jenkins/docker-pro/script/image.exp

cat /home/jenkins/docker-pro/script/image.exp 
#!/usr/bin/expect -f
set 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}

rancher server服务器上脚本,构建,推送push到阿里镜像仓库:/data/docker/layout-script/pro-all.sh

#!/bin/bash
namems=$1
namemsb=$2
version=latest
msrepo=/data/docker/$1
imgrepo=xmbaby
imgname=$1

if [ ! -d $msrepo ];then

mkdir -p $msrepo

scp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepo
echo "[$namems]" >> /etc/rsyncd.conf
echo "path=/data/docker/$namems/" >> /etc/rsyncd.conf
echo "comment = update
ignore errors
read only = no
list = no
hosts allow = 10.1.1.1/255.255.255.0
auth users = root
uid = root
gid = root
secrets file = /etc/rsyncd.secrets" >> /etc/rsyncd.conf

else

echo "开始构建docker镜像"

fi

echo "FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8" > $msrepo/dockerfile
echo "MAINTAINER $namems "name@alaxiaoyou.com"" >> $msrepo/dockerfile
echo "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar /data/httpd/" >> $msrepo/dockerfile
echo "WORKDIR /data/httpd/" >> $msrepo/dockerfile
echo "ENTRYPOINT java -Xmx512m -Xss512k -jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" >> $msrepo/dockerfile

cd $msrepo

docker build -t ms/$namems .

if [ $? -ne 0 ];then

echo "$namems 镜像构建失败,请检查dockerfile !"
exit

else

imageid=`docker images |grep "ms/$namems" |awk '{print $3}'`
docker login --username=name@alaxiaoyou.com --password=****** registry-internal.cn-hangzhou.aliyuncs.com
docker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version
docker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version

fi

1.5 构建操作

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

构建config-tmp-release项目,此项目执行完成后,自动构建关联的下级项目config-tmp-ms,config-tmp-ms项目会Jenkins中自动拉取最新代码,触发rsync同步脚本,rsync脚本里触发docker镜像build和push操作。Jenkins最终构建完成后,即可将镜像仓库拉取最新交付的镜像。

* 创建基础持续集成项目-gateway

2.1 创建自由风格的软件项目-Job,创建gateway-tmp-release

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

2.2 创建maven项目-Job,创建基础项目gateway-tmp-ms

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

2.3 配置gateway-tmp-ms基础项目

基于jdk8,配置gateway-tmp-ms项目Git代码仓库源,并选取release分支。

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

配置关联项目gateway-tmp-release:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

配置构建前操作脚本,构建命令,构建后操作脚本:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

2.4配置脚本

构建前配置模板替换,配置成相应的provider,这里为gateway provider:

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

cat /home/jenkins/docker-tmp/script/configuration.sh
#!/bin/bash
sed s/module/alaxiaoyou-$2/g  /home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.yml > /home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml
yes | cp -rfp /home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml /home/jenkins/.jenkins/workspace/$1/src/main/resources/

其中gateway-tmp-ms必须跟项目job名称一样。$1为此项目job名称“gateway-tmp-ms”,$2为此项目代码git@gitlab.alaxiaoyou.com:xxx.git名称"api-gateway"。目的是自动替换各个provider配置,接下来后面各个provider自动替换。

模板文件:

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

构建后配置,项目包(maven打包)同步脚本rsync,从git仓库服务器拉取代码打包后,包同步到docker-tmp服务器(rancher服务器)中:

项目jar包同步脚本:sh /home/jenkins/docker-tmp/script/rsync.sh gateway-tmp-ms api-gateway

cat /home/jenkins/docker-tmp/script/rsync.sh
#!/bin/bash
ip=10.1.1.1  #docker tmp server
passwd=******
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::$1
expect /home/jenkins/docker-tmp/script/image.exp $ip $passwd $1 $2 $3

expect脚本远程触发rancher服务器上脚本: /home/jenkins/docker-tmp/script/image.exp

cat /home/jenkins/docker-tmp/script/image.exp
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set msname [lindex $argv 2]
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"

rancher server服务器上脚本,构建,推送push到阿里镜像仓库:/data/docker/layout-script/all.sh

#!/bin/bash
namems=$1
namemsb=$2
version=latest
msrepo=/data/docker/$1
imgrepo=xmbaby
imgname=$1

if [ ! -d $msrepo ];then

mkdir -p $msrepo

scp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepo

echo "[$namems]" >> /etc/rsyncd.conf
echo "path=/data/docker/$namems/" >> /etc/rsyncd.conf
echo "comment = update
ignore errors
read only = no
list = no
hosts allow = 10.1.1.1/255.255.255.0
auth users = root
uid = root
gid = root
secrets file = /etc/rsyncd.secrets" >> /etc/rsyncd.conf

else

echo "开始构建docker镜像"

fi

echo "FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8" > $msrepo/dockerfile
echo "MAINTAINER $namems "name@alaxiaoyou.com"" >> $msrepo/dockerfile
echo "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar /data/httpd/" >> $msrepo/dockerfile
echo "WORKDIR /data/httpd/" >> $msrepo/dockerfile
echo "ENTRYPOINT java -Xmx512m -Xss512k -jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" >> $msrepo/dockerfile

cd $msrepo

docker build -t ms/$namems .

if [ $? -ne 0 ];then

echo "$namems 镜像构建失败,请检查dockerfile !"
exit

else

imageid=`docker images |grep "ms/$namems" |awk '{print $3}'`
docker login --username=name@alaxiaoyou.com --password=****** registry-internal.cn-hangzhou.aliyuncs.com
docker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version
docker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version

fi

2.5 构建操作

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

构建gateway-tmp-release项目,此项目执行完成后,自动构建关联的下级项目gateway-tmp-ms,gateway-tmp-ms项目会Jenkins中自动拉取最新代码,触发rsync同步脚本,rsync脚本里触发docker镜像build和push操作。Jenkins最终构建完成后,即可将镜像仓库拉取最新交付的镜像。

* 创建持续集成项目-sms

3.1 创建maven项目-Job,创建sms-tmp-release

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

3.2 配置ms-tmp-release项目

基于jdk8,配置sms项目Git代码仓库源,并选取release分支。

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

构建基于pom.xml进行构建

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

3.3 创建maven项目-Job,创建sms-tmp-ms

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

3.4 配置sms-tmp-ms项目

基于jdk8,配置sms项目Git代码仓库源,并选取release分支:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

配置关联项目sms-tmp-release:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

配置构建前操作脚本,构建命令,构建后操作脚本:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

3.5配置脚本

构建前配置模板替换,配置成相应的provider,这里为sms provider:

脚本: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-provider
#!/bin/bash
sed s/module/alaxiaoyou-$2/g  /home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.yml > /home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml
yes | cp -rfp /home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml /home/jenkins/.jenkins/workspace/$1/src/main/resources/

模板文件:

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

其中sms-tmp-ms必须跟项目job名称一样。$1为此项目job名称“sms-tmp-ms”,$2为此项目代码git@gitlab.alaxiaoyou.com:xxx.git名称"sms-provider"。目的是自动替换各个provider配置。

构建后配置,项目包(maven打包)同步脚本rsync,从git仓库服务器拉取代码打包后,包同步到docker-tmp服务器(rancher服务器)中:

脚本: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 256m
#!/bin/bash
ip=10.1.1.1  #docker tmp server
passwd=******
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::$1
expect /home/jenkins/docker-test/script/image.exp $ip $passwd $1 $2 $3

Jenkins和rancher不在同一台服务器行,用shell expect脚本远程触发rancher服务器上脚本:

cat /home/jenkins/docker-test/script/image.exp
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set msname [lindex $argv 2]
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}

rancher server服务器上脚本,构建,推送push到阿里镜像仓库:/data/docker/layout-script/all.sh

#!/bin/bash
namems=$1
namemsb=$2
version=latest
msrepo=/data/docker/$1
imgrepo=xmbaby
imgname=$1

if [ ! -d $msrepo ];then

mkdir -p $msrepo

scp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepo

echo "[$namems]" >> /etc/rsyncd.conf
echo "path=/data/docker/$namems/" >> /etc/rsyncd.conf
echo "comment = update
ignore errors
read only = no
list = no
hosts allow = 10.1.1.1/255.255.255.0
auth users = root
uid = root
gid = root
secrets file = /etc/rsyncd.secrets" >> /etc/rsyncd.conf

else

echo "开始构建docker镜像"

fi

echo "FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8" > $msrepo/dockerfile
echo "MAINTAINER $namems "name@alaxiaoyou.com"" >> $msrepo/dockerfile
echo "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar /data/httpd/" >> $msrepo/dockerfile
echo "WORKDIR /data/httpd/" >> $msrepo/dockerfile
echo "ENTRYPOINT java -Xmx512m -Xss512k -jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" >> $msrepo/dockerfile

cd $msrepo

docker build -t ms/$namems .

if [ $? -ne 0 ];then

echo "$namems 镜像构建失败,请检查dockerfile !"
exit

else

imageid=`docker images |grep "ms/$namems" |awk '{print $3}'`
docker login --username=name@alaxiaoyou.com --password=****** registry-internal.cn-hangzhou.aliyuncs.com
docker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version
docker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version

fi

3.6 构建操作

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

构建sms-tmp-release项目,此项目执行完成后,自动构建关联的下级项目sms-tmp-ms,sms-tmp-ms项目会Jenkins中自动拉取最新代码,触发rsync同步脚本,rsync脚本里触发docker镜像build和push操作。Jenkins最终构建完成后,即可将镜像仓库拉取最新交付的镜像。

3.7 发布已构建镜像

进入rancher控制台

添加服务tmp-sms-ms:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

映射卷:

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

点击“创建”,选中“创建前总是拉取镜像”,rancher便会分发任务至agent,进行最新镜像的拉取。

其中镜像为已在rancher宿主机上,进行手动镜像构建交付到阿里云镜像仓库的。并非通过Jenkins自动构建交付到阿里云镜像仓库。

3.8 镜像升级、回滚操作

升级步骤:

停止该服务正在运行的容器

拉取最新镜像

运行新容器

回滚:

如果不单击rancher控制台右上角“Upgraded”,该服务可进行回滚操作。

rancher控制台上方为“stopped”状态的容器为旧容器,单击“启动”按钮可进行回滚;

下方为“Running”状态的容器为升级的容器,单击右上角“Upgraded”按钮可进行回滚;容器镜像升级完成,旧容器销毁。

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

3.9 阿里云镜像仓库交付的镜像

Docker微服务-Jenkins+GitLab+Maven+Shell自动化构建实施案例

说明:以上配置了两个基础服务容器的持续交付,一个常规服务容器的持续交付,其他的容器服务持续交付类似操作,这不做重复操作。其中,构建、推送镜像仓库的脚本里对应的IP配置为生产环境地址,由于此次测试环境为内网,无外网IP。所以,涉及到镜像的push操作无法操作成功。各个服务provider的打包,如要在Jenkins中自动操作,无法完成。所以,均从其他已打好包的服务器上拷贝过来,然后在rancher宿主机进行构建、推送push到阿里云镜像仓库。

Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-02/157107.htm

原文  https://www.linuxidc.com/Linux/2019-02/157107.htm
正文到此结束
Loading...