k8s发布-jenkins流水线构建

一、需求:使用jenkins流水线构建应用,发布至k8s

二、发布流程:

从gitlab仓库拉取代码 --》代码编译、打包 --》制作镜像并上传至镜像仓库 --》发布至k8s

三、安装所需插件

jenkins上安装kubernetes cli,安装方法请见官方地址

插件官方地址:https://plugins.jenkins.io/kubernetes-cli/

 服务器上安装kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client

四、生成连接k8s集群token

创建一个sa jenkins-admin

kubectl create serviceaccount jenkins-admin -n kube-system

绑定集群管理员角色

kubectl create clusterrolebinding jenkins-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:jenkins-admin

通过sa获取token

kubectl get secret -n kube-system | grep jenkins
kubectl describe secret jenkins-admin-token-m77tm -n kube-system

五、jenkins上使用连接k8s集群的token创建连接k8s的credentials ID,使用镜像仓库账号密码创建连接镜像仓库的credentials ID.

 

 

六、jenkins上新建流水线项目、配置流水线项目

创建流水线项目

 

 

Dockerfile

FROM openjdk:8-jre-alpine
MAINTAINER megalab
ARG ACTIVE
ENV LC_ALL zh_CN.UTF-8
ENV JAVA_OPT="-Xms128m -Xmx256m"
ENV JAVA_ARG="--spring.profiles.active=$ACTIVE"
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && echo 'Asia/Shanghai' > /etc/timezone
ADD target/*.jar /opt/app.jar
CMD ["sh","-c","exec java $JAVA_OPT -jar /opt/app.jar $JAVA_ARG"]

流水线声明式脚本

pipeline {
    agent any
    environment {
       ACTIVE = "dev"
       image_url = ""
       docker_workdir = ""
       docker_file = ""
       HARBOR_KEY = credentials('')
       
       code_url = ""
       git_cid = ""
       branches_name = "*/dev"
       
       k8s_url = ""
       k8s_cid = ""
       deployment_file = ""
    }
    parameters {
        choice choices: ['deploy', 'rollback'], name: 'status'
        string 'image_version'
    }
    tools {
      maven 'mvn363'
    }
    stages{
        stage('拉取代码') {
            when {
                expression { params.status == 'deploy' }
            }
            steps {
                checkout([$class: 'GitSCM', branches: [[name: "${branches_name}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_cid}", url: "${code_url}"]]])
            }
        }
        
        stage('编译打包') {
             when {
                expression { params.status == 'deploy' }
            }
            steps {
                sh 'mvn clean package -pl megalab-data-collect -am -Dmaven.test.skip=true'
            }
        }    
        
        stage('制作镜像并上传harbord仓库') {
             when {
                expression { params.status == 'deploy' }
            }
            steps {
                sh '''
                    docker build -f ${docker_workdir}/${docker_file} -t ${image_url}:latest --build-arg ACTIVE=${ACTIVE} ${docker_workdir} 
                    docker tag  ${image_url}:latest ${image_url}:0.1-${BUILD_NUMBER}
                    
                    docker login -u ${HARBOR_KEY_USR} -p ${HARBOR_KEY_PSW} ${image_url}
                    docker push ${image_url}:0.1-${BUILD_NUMBER}
                    
                    docker rmi ${image_url}:0.1-${BUILD_NUMBER}
                    docker rmi ${image_url}:latest
                    docker logout
                    
                '''
            }
        }    
        
        stage('发布至k8s') {
            when {
                    expression { params.status == 'deploy' }
            }
            steps {
                withKubeConfig([credentialsId: "${k8s_cid}", serverUrl: "${k8s_url}"]) {
                    sh 'envsubst < ${WORKSPACE}/${docker_workdir}/${deployment_file} | kubectl apply -f -'
                }
            }
        }

        stage('回滚') {
            when {
                    expression { params.status == 'rollback' }
            }
            steps {
                withKubeConfig([credentialsId: "${k8s_cid}", serverUrl: "${k8s_url}"]) {
                    sh '''
                    sed -i 's/$BUILD_NUMBER/$image_version/g' ${deployment_file}
                    envsubst <  ${WORKSPACE}/${docker_workdir}/${deployment_file} | kubectl apply -f -
                    '''
                }
            }
        }         
                           
    }
}

 

 

 

posted @ 2022-09-30 18:32  屠夫2022  阅读(507)  评论(0编辑  收藏  举报