使用Jenkins进行CI/CD-续2(8)

在前面的章节中已经完成了从Jenkins构建,上传镜像到harbor,最后在通过kubectl进行部署,即CI的工作已经实现了自动化,但是CD部分还没有。这边文章就将后续的CD部分进行完成,相比前面的CI部分也会有所变化,将通过在源码中集成Jenkinsfile来完成项目的构建和发布。

CD

1、 项目修改

  1. 将上节使用到的deployment.yaml文件复制到项目的doc目录,并进行修改(主要是对版本号的控制)

       template:
         metadata:
           labels:
             app: tlh-server
         spec:
           containers:
           - image: harbor.tlh.com/tlhhup/jks: # 主要修改地方,用于版本控制
             name: tlh
    
  2. 在doc目录下添加kubernetesPod.yaml文件,用于对构建时使用的容器进行定义

     apiVersion: "v1"
     kind: "Pod"
     metadata:
       labels:
         jenkins: "slave"
     spec:
       containers:
         - name: "maven"
           image: "maven:3.5-jdk-8"
           imagePullPolicy: "IfNotPresent"
           resources:
             limits: {}
             requests: {}
           securityContext:
             privileged: false
           tty: true
           volumeMounts:
             - mountPath: "/root/.m2"
               name: "volume-0"
               readOnly: false
             - mountPath: "/home/jenkins/agent"
               name: "workspace-volume"
               readOnly: false
           command:
             - "cat"
         - name: "kubectl"
           image: "lachlanevenson/k8s-kubectl:latest"    # 该容器可以用于执行kubectl命令
           imagePullPolicy: "IfNotPresent"
           resources:
             limits: {}
             requests: {}
           securityContext:
             privileged: true
           tty: true
           volumeMounts:
             - mountPath: "/home/jenkins/agent"
               name: "workspace-volume"
               readOnly: false
           command:
             - "cat"
         - name: "docker"
           image: "docker:latest"
           imagePullPolicy: "IfNotPresent"
           resources:
             limits: {}
             requests: {}
           securityContext:
             privileged: false
           tty: true
           volumeMounts:
             - mountPath: "/var/run/docker.sock"
               name: "volume-1"
               readOnly: false
             - mountPath: "/home/jenkins/agent"
               name: "workspace-volume"
               readOnly: false
         - name: "jnlp"
           image: "jenkins/jnlp-slave:alpine"
           volumeMounts:
             - mountPath: "/home/jenkins/agent"
               name: "workspace-volume"
               readOnly: false
       nodeSelector: {}
       restartPolicy: "Never"
       serviceAccount: "jenkins"
       volumes:
         - name: "volume-0"
           persistentVolumeClaim:    # 这里挂载的是在部署Jenkins是创建的pvc
             claimName: opspvc
         - hostPath:
             path: "/var/run/docker.sock"
           name: "volume-1"
         - emptyDir: {}
           name: "workspace-volume"
    
  3. 在doc目录下添加Jenkinsfile文件,对构建的流程进行定义

     pipeline {
       agent {
         kubernetes {
           defaultContainer 'jnlp'
           yamlFile 'doc/kubernetesPod.yaml'
         }
       }
     
       stages {
         stage('Get a Maven project') {
             steps{
                 container('maven') {
                     sh 'mvn -DskipTests=true clean package'
                 }
             }
         }
         stage('Build Docker Image'){
             steps{
                 container('docker'){
                     sh '''
                         mkdir context
                         cp target/tlhhup-1.0-SNAPSHOT.jar context
                         cp doc/Dockerfile context
                     '''
                     sh 'docker build -t harbor.tlh.com/tlhhup/jks:${BUILD_NUMBER} context'
                 }
             }
         }
         stage('Push Docker Image'){
             steps{
                 container('docker'){
                     sh 'echo Push Docker Image'
                     withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
                         sh '''
                             docker login harbor.tlh.com -u ${dockerHubUser} -p ${dockerHubPassword}
                             docker push harbor.tlh.com/tlhhup/jks:${BUILD_NUMBER}
                         '''
                     }
                 }
             }
         }
         stage('Deploy'){
             steps{
                 input "Does the staging environment look ok?"
                 container('kubectl'){
                     sh 'echo Deploy'
                     sh '''
                         mkdir deploy
                         cp doc/deployment.yaml deploy
                     '''
                     sh "sed -i 's//${BUILD_NUMBER}/' deploy/deployment.yaml" # 修改部署的版本号
                     sh 'kubectl apply -f deploy/deployment.yaml -n tlh'
                 }
             }
         }
       }
     }
    

2、创建Jenkins项目

  1. 选择创建流水线类型的项目,取名为tlhhup_pipeline

  2. 添加构建参数,取名为BUILD_NUMBER


    1.png
  3. 项目配置


    2.png
  4. 选择构建,这里输入的版本号为1.4

    3.png

  5. 查看dashboard


    4.png

总结

  1. 整个架构图


    流程图
  2. k8s中对象关系


    k8s-objects.png

你可能感兴趣的:(使用Jenkins进行CI/CD-续2(8))