深圳幻海软件技术有限公司 欢迎您!

Jenkins 业务发版平滑上线实战

2023-02-28

背景简介:原来发版必须等到晚上10点访问量最少的时候,期间服务中断产生大量告警。为了不影响业务,现在需要修改jenkins使其平滑上线。具体操作步骤如下:第一步:拉取git指定分支Jenkins默认一个项目只能对应一个git分支来构建,通过gitParameter插件可以从项目中读取GITSCM配置

背景简介:

原来发版必须等到晚上10点访问量最少的时候,期间服务中断产生大量告警。为了不影响业务,现在需要修改jenkins使其平滑上线。具体操作步骤如下:

第一步:拉取git指定分支

Jenkins默认一个项目只能对应一个git分支来构建,通过git Parameter插件可以从项目中读取GIT SCM 配置实现选择分支或tag来构建项目。

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

图1-1 拉取指定分支

第二步:编译

通过mvn编译java代码,将编译后得到的jar包推送至各个服务器,其中servers_ips为各个服务器ip地址(如: servers_ips = ” 172.1.1.100 172.1.1.101”)。

script {
  sh " mvn clean package -DskipTests "   #编译java项目代码
  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包推送至服务器
}}}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

第三步:下线指定服务器

批量修改各个服务器上nginx配置,注释upstream中指定的服务器,使其下线不提供服务,为之后新代码上线做准备,在不影响业务的情况下平滑上线。

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/.*'${指定服务器ip}'/#&/' /etc/nginx/conf.d/xxx.conf
        nginx -s reload
      """
}}}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

第四步:重启服务运行新代码

在刚下线的服务器上重启java项目使其运行新的代码。立即重启会导致原有的连接返回5xx,所以在重启之前sleep几秒。

sleep 30
def remote = [:]
remote.name = root
remote.host = 指定服务器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"
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

第五步:健康检查

访问该服务器后端提供的接口判断是否可以提供服务。状态码为200即为健康即可上线提供服务,否则访问3次之后抛出异常中断Jenkins。服务启动过程中进行检查,jenkins会抛异常并中断,这里需要加上异常处理继续运行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 "服务没起来" }
  if ( health_status == '200' ){
    echo "健康"
    break
}else if ( health_times < 2) {
    sleep 30
    echo "不健康,再次尝试检测..."
}else{
    error "不健康" }
health_times = health_times +1 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

第六步:上线指定服务器

通过健康检查后,即可批量修改各个服务器上的nginx配置,使得该服务器上线提供服务。

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
       """
}}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

第七步:检查

在构建过程中循环curl服务,检查是否会出现服务中断的情况。下图7-1为构建过程中的日志。

图7-1 平滑上线过程

至此,Jenkins平滑上线完成。