您好,登錄后才能下訂單哦!
* 環(huán)境:
CentOS7
代碼倉(cāng)庫(kù):Gitlab
持續(xù)集成代碼更新:Jenkins
構(gòu)建打包:Maven
鏡像自動(dòng)交付腳本:Shell
基礎(chǔ)環(huán)境軟件安裝:Gitlab、Jenkins、Maven配置,安裝參考網(wǎng)上,這里安裝略,重點(diǎn)介紹項(xiàng)目持續(xù)集成和鏡像交付。
結(jié)合上一博文:https://blog.51cto.com/10874766/2353577
這里配置了兩個(gè)基礎(chǔ)服務(wù)容器的自動(dòng)化構(gòu)建,一個(gè)常規(guī)業(yè)務(wù)服務(wù)容器的自動(dòng)化構(gòu)建,其他的容器服務(wù)持續(xù)集成類似操作,這不做重復(fù)操作。
* 創(chuàng)建基礎(chǔ)持續(xù)集成項(xiàng)目-config
1.1 創(chuàng)建自由風(fēng)格的軟件項(xiàng)目-Job,創(chuàng)建config-tmp-release
1.2 創(chuàng)建maven項(xiàng)目-Job,創(chuàng)建基礎(chǔ)項(xiàng)目config-tmp-ms
1.3 配置config-tmp-ms基礎(chǔ)項(xiàng)目
基于jdk8,配置config-tmp-ms項(xiàng)目Git代碼倉(cāng)庫(kù)源,并選取release分支。
配置關(guān)聯(lián)項(xiàng)目config-tmp-release:
構(gòu)建命令及構(gòu)建后操作腳本:
1.4配置腳本
項(xiàng)目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必須跟項(xiàng)目job名稱一樣。$1為此項(xiàng)目job名稱“config-tmp-ms”,$2為此項(xiàng)目代碼git@gitlab.alaxiaoyou.com:xxx/alaxiaoyou-config-service.git, provider:config-service。目的是自動(dòng)替換各個(gè)provider配置。
expect腳本遠(yuǎn)程觸發(fā)rancher服務(wù)器上腳本: /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服務(wù)器上腳本,構(gòu)建,推送push到阿里鏡像倉(cāng)庫(kù):/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 "開(kāi)始構(gòu)建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 鏡像構(gòu)建失敗,請(qǐng)檢查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 構(gòu)建操作
構(gòu)建config-tmp-release項(xiàng)目,此項(xiàng)目執(zhí)行完成后,自動(dòng)構(gòu)建關(guān)聯(lián)的下級(jí)項(xiàng)目config-tmp-ms,config-tmp-ms項(xiàng)目會(huì)Jenkins中自動(dòng)拉取最新代碼,觸發(fā)rsync同步腳本,rsync腳本里觸發(fā)docker鏡像build和push操作。Jenkins最終構(gòu)建完成后,即可將鏡像倉(cāng)庫(kù)拉取最新交付的鏡像。
* 創(chuàng)建基礎(chǔ)持續(xù)集成項(xiàng)目-gateway
2.1 創(chuàng)建自由風(fēng)格的軟件項(xiàng)目-Job,創(chuàng)建gateway-tmp-release
2.2 創(chuàng)建maven項(xiàng)目-Job,創(chuàng)建基礎(chǔ)項(xiàng)目gateway-tmp-ms
2.3 配置gateway-tmp-ms基礎(chǔ)項(xiàng)目
基于jdk8,配置gateway-tmp-ms項(xiàng)目Git代碼倉(cāng)庫(kù)源,并選取release分支。
配置關(guān)聯(lián)項(xiàng)目gateway-tmp-release:
配置構(gòu)建前操作腳本,構(gòu)建命令,構(gòu)建后操作腳本:
2.4配置腳本
構(gòu)建前配置模板替換,配置成相應(yīng)的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/
其中g(shù)ateway-tmp-ms必須跟項(xiàng)目job名稱一樣。$1為此項(xiàng)目job名稱“gateway-tmp-ms”,$2為此項(xiàng)目代碼git@gitlab.alaxiaoyou.com:xxx.git名稱"api-gateway"。目的是自動(dòng)替換各個(gè)provider配置,接下來(lái)后面各個(gè)provider自動(dòng)替換。
模板文件:
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
構(gòu)建后配置,項(xiàng)目包(maven打包)同步腳本rsync,從git倉(cāng)庫(kù)服務(wù)器拉取代碼打包后,包同步到docker-tmp服務(wù)器(rancher服務(wù)器)中:
項(xiàng)目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腳本遠(yuǎn)程觸發(fā)rancher服務(wù)器上腳本: /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服務(wù)器上腳本,構(gòu)建,推送push到阿里鏡像倉(cāng)庫(kù):/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 "開(kāi)始構(gòu)建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 鏡像構(gòu)建失敗,請(qǐng)檢查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 構(gòu)建操作
構(gòu)建gateway-tmp-release項(xiàng)目,此項(xiàng)目執(zhí)行完成后,自動(dòng)構(gòu)建關(guān)聯(lián)的下級(jí)項(xiàng)目gateway-tmp-ms,gateway-tmp-ms項(xiàng)目會(huì)Jenkins中自動(dòng)拉取最新代碼,觸發(fā)rsync同步腳本,rsync腳本里觸發(fā)docker鏡像build和push操作。Jenkins最終構(gòu)建完成后,即可將鏡像倉(cāng)庫(kù)拉取最新交付的鏡像。
* 創(chuàng)建持續(xù)集成項(xiàng)目-sms
3.1 創(chuàng)建maven項(xiàng)目-Job,創(chuàng)建sms-tmp-release
3.2 配置ms-tmp-release項(xiàng)目
基于jdk8,配置sms項(xiàng)目Git代碼倉(cāng)庫(kù)源,并選取release分支。
構(gòu)建基于pom.xml進(jìn)行構(gòu)建
3.3 創(chuàng)建maven項(xiàng)目-Job,創(chuàng)建sms-tmp-ms
3.4 配置sms-tmp-ms項(xiàng)目
基于jdk8,配置sms項(xiàng)目Git代碼倉(cāng)庫(kù)源,并選取release分支:
配置關(guān)聯(lián)項(xiàng)目sms-tmp-release:
配置構(gòu)建前操作腳本,構(gòu)建命令,構(gòu)建后操作腳本:
3.5配置腳本
構(gòu)建前配置模板替換,配置成相應(yīng)的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必須跟項(xiàng)目job名稱一樣。$1為此項(xiàng)目job名稱“sms-tmp-ms”,$2為此項(xiàng)目代碼git@gitlab.alaxiaoyou.com:xxx.git名稱"sms-provider"。目的是自動(dòng)替換各個(gè)provider配置。
構(gòu)建后配置,項(xiàng)目包(maven打包)同步腳本rsync,從git倉(cāng)庫(kù)服務(wù)器拉取代碼打包后,包同步到docker-tmp服務(wù)器(rancher服務(wù)器)中:
腳本: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不在同一臺(tái)服務(wù)器行,用shell expect腳本遠(yuǎn)程觸發(fā)rancher服務(wù)器上腳本:
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服務(wù)器上腳本,構(gòu)建,推送push到阿里鏡像倉(cāng)庫(kù):/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 "開(kāi)始構(gòu)建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 鏡像構(gòu)建失敗,請(qǐng)檢查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 構(gòu)建操作
構(gòu)建sms-tmp-release項(xiàng)目,此項(xiàng)目執(zhí)行完成后,自動(dòng)構(gòu)建關(guān)聯(lián)的下級(jí)項(xiàng)目sms-tmp-ms,sms-tmp-ms項(xiàng)目會(huì)Jenkins中自動(dòng)拉取最新代碼,觸發(fā)rsync同步腳本,rsync腳本里觸發(fā)docker鏡像build和push操作。Jenkins最終構(gòu)建完成后,即可將鏡像倉(cāng)庫(kù)拉取最新交付的鏡像。
3.7 發(fā)布已構(gòu)建鏡像
進(jìn)入rancher控制臺(tái)
添加服務(wù)tmp-sms-ms:
映射卷:
點(diǎn)擊“創(chuàng)建”,選中“創(chuàng)建前總是拉取鏡像”,rancher便會(huì)分發(fā)任務(wù)至agent,進(jìn)行最新鏡像的拉取。
其中鏡像為已在rancher宿主機(jī)上,進(jìn)行手動(dòng)鏡像構(gòu)建交付到阿里云鏡像倉(cāng)庫(kù)的。并非通過(guò)Jenkins自動(dòng)構(gòu)建交付到阿里云鏡像倉(cāng)庫(kù)。
3.8 鏡像升級(jí)、回滾操作
升級(jí)步驟:
停止該服務(wù)正在運(yùn)行的容器
拉取最新鏡像
運(yùn)行新容器
回滾:
如果不單擊rancher控制臺(tái)右上角“Upgraded”,該服務(wù)可進(jìn)行回滾操作。
rancher控制臺(tái)上方為“stopped”狀態(tài)的容器為舊容器,單擊“啟動(dòng)”按鈕可進(jìn)行回滾;
下方為“Running”狀態(tài)的容器為升級(jí)的容器,單擊右上角“Upgraded”按鈕可進(jìn)行回滾;容器鏡像升級(jí)完成,舊容器銷毀。
3.9 阿里云鏡像倉(cāng)庫(kù)交付的鏡像
說(shuō)明:以上配置了兩個(gè)基礎(chǔ)服務(wù)容器的持續(xù)交付,一個(gè)常規(guī)服務(wù)容器的持續(xù)交付,其他的容器服務(wù)持續(xù)交付類似操作,這不做重復(fù)操作。其中,構(gòu)建、推送鏡像倉(cāng)庫(kù)的腳本里對(duì)應(yīng)的IP配置為生產(chǎn)環(huán)境地址,由于此次測(cè)試環(huán)境為內(nèi)網(wǎng),無(wú)外網(wǎng)IP。所以,涉及到鏡像的push操作無(wú)法操作成功。各個(gè)服務(wù)provider的打包,如要在Jenkins中自動(dòng)操作,無(wú)法完成。所以,均從其他已打好包的服務(wù)器上拷貝過(guò)來(lái),然后在rancher宿主機(jī)進(jìn)行構(gòu)建、推送push到阿里云鏡像倉(cāng)庫(kù)。
免責(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)容。