Jenkins 業(yè)務(wù)發(fā)版平滑上線實(shí)戰(zhàn)
背景簡(jiǎn)介:
原來發(fā)版必須等到晚上10點(diǎn)訪問量最少的時(shí)候,期間服務(wù)中斷產(chǎn)生大量告警。為了不影響業(yè)務(wù),現(xiàn)在需要修改jenkins使其平滑上線。具體操作步驟如下:
第一步:拉取git指定分支
Jenkins默認(rèn)一個(gè)項(xiàng)目只能對(duì)應(yīng)一個(gè)git分支來構(gòu)建,通過git Parameter插件可以從項(xiàng)目中讀取GIT SCM 配置實(shí)現(xiàn)選擇分支或tag來構(gòu)建項(xiàng)目。
pipeline {
parameters{
gitParameter(name: 'BRANCH_TAG',type: 'PT_BRANCH_TAG',defaultValue: 'master')
}
stage('gitpull') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],gitTool: 'Default',submoduleCfg: [],
userRemoteConfigs: [[url: 'http://xxx.git',credentialsId: 'xxx',]]
])
}}}
圖1-1 拉取指定分支
第二步:編譯
通過mvn編譯java代碼,將編譯后得到的jar包推送至各個(gè)服務(wù)器,其中servers_ips為各個(gè)服務(wù)器ip地址(如: servers_ips = ” 172.1.1.100 172.1.1.101”)。
script {
sh " mvn clean package -DskipTests " #編譯java項(xiàng)目代碼
for (server_ip in servers_ips.tokenize(' ')){
def remote = [:]
remote.name = root
remote.host = server_ip
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity',passphr aseVariable:'',usernameVariable:'userName')])
{
remote.user = userName
remote.identityFile = identity
sshPut remote: remote, from: "./target/xxx.jar", into: "/data/" #
將jar包推送至服務(wù)器
}}}
第三步:下線指定服務(wù)器
批量修改各個(gè)服務(wù)器上nginx配置,注釋upstream中指定的服務(wù)器,使其下線不提供服務(wù),為之后新代碼上線做準(zhǔn)備,在不影響業(yè)務(wù)的情況下平滑上線。
script {
for (server_ip2 in servers_ips.tokenize(' ')){
def remote = [:]
remote.name = root
remote.host = server_ip2
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"""
sed -i 's/.*'${指定服務(wù)器ip}'/#&/' /etc/nginx/conf.d/xxx.conf
nginx -s reload
"""
}}}
第四步:重啟服務(wù)運(yùn)行新代碼
在剛下線的服務(wù)器上重啟java項(xiàng)目使其運(yùn)行新的代碼。立即重啟會(huì)導(dǎo)致原有的連接返回5xx,所以在重啟之前sleep幾秒。
sleep 30
def remote = [:]
remote.name = root
remote.host = 指定服務(wù)器ip
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: xxx, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"./prodservice.sh restart"
}
第五步:健康檢查
訪問該服務(wù)器后端提供的接口判斷是否可以提供服務(wù)。狀態(tài)碼為200即為健康即可上線提供服務(wù),否則訪問3次之后拋出異常中斷Jenkins。服務(wù)啟動(dòng)過程中進(jìn)行檢查,jenkins會(huì)拋異常并中斷,這里需要加上異常處理繼續(xù)運(yùn)行jenkins。
health_times=0
health_status = ""
while(health_times < 3){
try {
health_status = sh(script: "curl -I -m 10 -o /dev/null -s -w %{http_code} -d '' http://${server_ip}:8080/xxx", returnStdout: true).trim()
} catch(Exception e1) {
echo "服務(wù)沒起來" }
if ( health_status == '200' ){
echo "健康"
break
}else if ( health_times < 2) {
sleep 30
echo "不健康,再次嘗試檢測(cè)..."
}else{
error "不健康" }
health_times = health_times +1 }
第六步:上線指定服務(wù)器
通過健康檢查后,即可批量修改各個(gè)服務(wù)器上的nginx配置,使得該服務(wù)器上線提供服務(wù)。
for (server_ip2 in servers_ips.tokenize(' ')){
remote.name = root
remote.host = server_ip2
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: ssh_credentialsId, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"""
sed -i 's/^#*\\(.*'${server_ip}'\\)/\\1/' /etc/nginx/conf.d/xxx.conf
nginx -s reload
"""
}}
第七步:檢查
在構(gòu)建過程中循環(huán)curl服務(wù),檢查是否會(huì)出現(xiàn)服務(wù)中斷的情況。下圖7-1為構(gòu)建過程中的日志。
圖7-1 平滑上線過程
至此,Jenkins平滑上線完成。