Jenkins - DevOps【结合Kubernetes部署】

Jenkins结合Kubernetes

配置Jenkins 2.401.1上Kubernetes配置 (连接1.20版本k8s成功)

#添加时报错 可以参考:https://blog.csdn.net/u011143903/article/details/120457866
先导出密钥 做成pfx 连接成功

配置集群

#解码配置文件
admin.conf中有三个值certificate-authority-data 、client-certificate-data 、 client-key-data,需要进行解码

$ echo <certificate-authority-data值> | base64 -d > ca.crt
$ echo <client-certificate-data值> | base64 -d > client.crt
$ echo <client-key-data值> | base64 -d > client.key

#根据这三个文件生成一个PKCS12格式的客户端证书文件
$ openssl pkcs12 -export -out cert.pfx -inkey client.key -in client.crt -certfile ca.crt
#生成证书的时候,需要填写密码,后面会用到。

#添加凭据
Dashboard->凭据->系统->全局凭据 (unrestricted)

选择证书,填入密码,证书为之前生成的cert.pfx文件。

点击连接测试

案例一:配置Pod Templates(jenkins-slave)

#Jenkins会自动拉取jenkins/inbound-agent:3148.v532a_7e715ee3-1 来作为agent

#但是可能服务器无法获取此容器可以自己拉取重命名

#node上 拉去镜像 因为可能无法获取到此镜像
#这个镜像jenkins自动回去拉取
docker login --username=夏恺晟 registry.cn-hangzhou.aliyuncs.com
docker pull registry.cn-hangzhou.aliyuncs.com/birkhoff/jenkins-inbound-agent:3148.v532a_7e715ee3-1
docker tag registry.cn-hangzhou.aliyuncs.com/birkhoff/jenkins-inbound-agent:3148.v532a_7e715ee3-1 jenkins/inbound-agent:3148.v532a_7e715ee3-1

创建一个流水线

pipeline {
	agent {
		kubernetes {
			inheritFrom 'jenkins-slave'
		}
	}
	stages {
		stage("Hello") {
			steps{
				sh 'echo HelloWorld'
			}
		}
	}
}

立即构建

#如果立即构建 一直卡着后台报错
2024-08-07 05:59:41.391+0000 [id=99]    INFO    o.j.p.w.s.concurrent.Timeout#lambda$ping$0: Running CpsFlowExecution[Owner[pipeline-run/14:pipeline-run #14]] unresponsive for 5 min 45 sec
#执行以下命令
kill -9 `ps -ef |grep jenkins | grep -Ev 'grep' | awk -F ' ' '{ print $2 }'`
java -jar jenkins.war
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: kubernetes default/pipeling-k8s-4-ksl86-t8kf9-kq8hb
Agent pipeling-k8s-4-ksl86-t8kf9-kq8hb is provisioned from template pipeling-k8s_4-ksl86-t8kf9
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://10.30.17.167:8080/job/pipeling-k8s/4/"
    runUrl: "job/pipeling-k8s/4/"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "c2fdf1366ad5ae0f78209989d5258130a01705b6"
    jenkins/label: "pipeling-k8s_4-ksl86"
  name: "pipeling-k8s-4-ksl86-t8kf9-kq8hb"
spec:
  containers:
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "pipeling-k8s-4-ksl86-t8kf9-kq8hb"
    - name: "JENKINS_WEB_SOCKET"
      value: "true"
    - name: "JENKINS_NAME"
      value: "pipeling-k8s-4-ksl86-t8kf9-kq8hb"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://10.30.17.167:8080/"
    image: "jenkins/inbound-agent:3148.v532a_7e715ee3-1"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on pipeling-k8s-4-ksl86-t8kf9-kq8hb in /home/jenkins/agent/workspace/pipeling-k8s
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] sh
+ echo HelloWorld
HelloWorld
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

案例二:配置Pod Templates(maven-3.6)

配置Dashboard-系统管理-配置集群 Pod Template(maven-3.6)

添加maven Pod Templates

#为了可以缓存添加pv、pvc

#添加NFS 服务
#配置PV、PVC
cat maven-persistentvolume.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: maven-datadir-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /data/maven
    server: 10.30.17.166
cat maven-persistentvolumeclaim.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: maven-datadir-pvc
spec:
  volumeName: maven-datadir-pv
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

配置流水线

pipeline {
	agent {
		kubernetes {
			inheritFrom 'maven-3.6'
		}
	}
	stages {
		stage("Hello") {
			steps{
				container('maven') {
					sh 'mvn -version'
				}
			}
		}
	}
}

立即构建

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Still waiting to schedule task
‘Jenkins’ doesn’t have label ‘k8s-maven_1-z2lrj’
Created Pod: kubernetes default/k8s-maven-1-z2lrj-0brnq-v32ss
Agent k8s-maven-1-z2lrj-0brnq-v32ss is provisioned from template k8s-maven_1-z2lrj-0brnq
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://10.30.17.167:8080/job/k8s-maven/1/"
    runUrl: "job/k8s-maven/1/"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "fc7abf08651341ea75819591758b65644233377d"
    jenkins/label: "k8s-maven_1-z2lrj"
  name: "k8s-maven-1-z2lrj-0brnq-v32ss"
spec:
  containers:
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "maven:3.6-openjdk-11-slim"
    imagePullPolicy: "IfNotPresent"
    name: "maven"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "k8s-maven-1-z2lrj-0brnq-v32ss"
    - name: "JENKINS_WEB_SOCKET"
      value: "true"
    - name: "JENKINS_NAME"
      value: "k8s-maven-1-z2lrj-0brnq-v32ss"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://10.30.17.167:8080/"
    image: "jenkins/inbound-agent:3148.v532a_7e715ee3-1"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - name: "volume-0"
    persistentVolumeClaim:
      claimName: "maven-datadir-pvc"
      readOnly: false
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on k8s-maven-1-z2lrj-0brnq-v32ss in /home/jenkins/agent/workspace/k8s-maven
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: Oracle Corporation, runtime: /usr/local/openjdk-11
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

案例三:配置Pod Templates(maven-and-docker)

#Git clone & maven &  building

安装Docker  Pipeline插件

配置Dashboard-系统管理-配置集群 Pod Template(maven-and-docker)

配置Pipeline

#这里的inheritFrom 'maven-and-docker' 是上面配置Pod Templates名字 
#代码Clone的时候 需要证书 设置fals (sh 'git config --global http.sslVerify false')   

pipeline {
    agent {
        kubernetes {
            inheritFrom 'maven-and-docker'
        }
    }    
    environment {
        appName = "spring-boot-helloworld"
        appVersion = "0.9.6"
    }  
    stages {
        stage('Source') {
            steps {
                sh 'git config --global http.sslVerify false'   
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://10.30.17.166:8929/root/springboot-helloworld.git']])
                echo '[1]-拉去Git仓库代码 - SUCCESS'
            }
        }      
        stage('Build') {
            steps {
            	container('maven') {
                    sh 'mvn clean package -DskipTests'
                }
            }
        }
        stage('Build App Images') {
            steps {
                container('docker') {
                    script {
                        dockerImage = docker.build appName + ":" + appVersion
                    }
                }
            }
        } 

        
    }
}

 Pipeline Console Output

最终编译为mytest.jar 因为再pom.xml文件指定了

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: kubernetes default/k8s-maven-testing-22-6rqrq-d5kzv-6cft0
Agent k8s-maven-testing-22-6rqrq-d5kzv-6cft0 is provisioned from template k8s-maven-testing_22-6rqrq-d5kzv
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://10.30.17.167:8080/job/k8s-maven-testing/22/"
    runUrl: "job/k8s-maven-testing/22/"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "3cbcdaed25c6ab486c7d4e8e0de88036ba51cd9e"
    jenkins/label: "k8s-maven-testing_22-6rqrq"
  name: "k8s-maven-testing-22-6rqrq-d5kzv-6cft0"
spec:
  containers:
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "maven:3.6-openjdk-11-slim"
    imagePullPolicy: "IfNotPresent"
    name: "maven"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "docker:19.03-git"
    imagePullPolicy: "IfNotPresent"
    name: "docker"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "k8s-maven-testing-22-6rqrq-d5kzv-6cft0"
    - name: "JENKINS_WEB_SOCKET"
      value: "true"
    - name: "JENKINS_NAME"
      value: "k8s-maven-testing-22-6rqrq-d5kzv-6cft0"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://10.30.17.167:8080/"
    image: "jenkins/inbound-agent:3148.v532a_7e715ee3-1"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - name: "volume-0"
    persistentVolumeClaim:
      claimName: "maven-datadir-pvc"
      readOnly: false
  - hostPath:
      path: "/run/docker.sock"
    name: "volume-1"
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on k8s-maven-testing-22-6rqrq-d5kzv-6cft0 in /home/jenkins/agent/workspace/k8s-maven-testing
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Source)
[Pipeline] sh
+ git config --global http.sslVerify false
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Avoid second fetch
Cloning repository https://10.30.17.166:8929/root/springboot-helloworld.git
 > git init /home/jenkins/agent/workspace/k8s-maven-testing # timeout=10
Fetching upstream changes from https://10.30.17.166:8929/root/springboot-helloworld.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- https://10.30.17.166:8929/root/springboot-helloworld.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://10.30.17.166:8929/root/springboot-helloworld.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision a0721e08dd0abeee73b402a1c4766e60adf4be12 (refs/remotes/origin/master)
Commit message: "Update Dockerfile"
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a0721e08dd0abeee73b402a1c4766e60adf4be12 # timeout=10
 > git rev-list --no-walk 9a117c23379a8ef616cb9dad821a6c2e112296d3 # timeout=10
[Pipeline] echo
[1]-拉去Git仓库代码 - SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.neo:spring-boot-helloworld >-------------------
[INFO] Building spring-boot-helloworld 0.9.6-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ spring-boot-helloworld ---
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ spring-boot-helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/jenkins/agent/workspace/k8s-maven-testing/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-boot-helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/jenkins/agent/workspace/k8s-maven-testing/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ spring-boot-helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/jenkins/agent/workspace/k8s-maven-testing/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ spring-boot-helloworld ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ spring-boot-helloworld ---
[INFO] Building jar: /home/jenkins/agent/workspace/k8s-maven-testing/target/mytest.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ spring-boot-helloworld ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.387 s
[INFO] Finished at: 2024-08-07T01:22:03Z
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build App Images)
[Pipeline] container
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker build -t spring-boot-helloworld:0.9.6 .
Sending build context to Docker daemon  16.89MB

Step 1/4 : FROM eclipse-temurin:11-jdk-alpine
11-jdk-alpine: Pulling from library/eclipse-temurin
c6a83fedfae6: Pulling fs layer
6dff7dc6fc28: Pulling fs layer
ef2a3080f55a: Pulling fs layer
157f0a6d035e: Pulling fs layer
8fb69009c15d: Pulling fs layer
157f0a6d035e: Waiting
8fb69009c15d: Waiting
c6a83fedfae6: Verifying Checksum
c6a83fedfae6: Download complete
6dff7dc6fc28: Verifying Checksum
6dff7dc6fc28: Download complete
c6a83fedfae6: Pull complete
6dff7dc6fc28: Pull complete
ef2a3080f55a: Download complete
157f0a6d035e: Verifying Checksum
157f0a6d035e: Download complete
8fb69009c15d: Verifying Checksum
8fb69009c15d: Download complete
ef2a3080f55a: Pull complete
157f0a6d035e: Pull complete
8fb69009c15d: Pull complete
Digest: sha256:d6f714a6dcfcd35813630f1640c408befcd8f86215b6508ec43cc662567d2755
Status: Downloaded newer image for eclipse-temurin:11-jdk-alpine
 ---> d232ea92e043
Step 2/4 : LABEL maintainer="MageEdu <mage@magedu.com>"
 ---> Running in 4f2789c7fd47
Removing intermediate container 4f2789c7fd47
 ---> 28dbaea10cc2
Step 3/4 : ADD target/mytest.jar /applications/spring-boot-helloworld.jar
 ---> d7241a72c1b1
Step 4/4 : ENTRYPOINT ["/bin/sh","-c","/opt/java/openjdk/bin/java -jar /applications/spring-boot-helloworld.jar --server.port=80"]
 ---> Running in f5c236c6a443
Removing intermediate container f5c236c6a443
 ---> 5b2d9d1a6274
Successfully built 5b2d9d1a6274
Successfully tagged spring-boot-helloworld:0.9.6
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

 检查node节点是否生成镜像以及镜像是否正常

docker images |grep spring-boot-helloworld
spring-boot-helloworld                                             0.9.6                   5b2d9d1a6274   18 minutes ago   308MB

docker run --name test --rm spring-boot-helloworld:0.9.6

docker inspect test

curl 172.17.0.2
Hello Spring Boot v3.0.1!

curl 172.17.0.2/hello
Hello World from MageEdu.com v3.0.1!

curl 172.17.0.2/version
version v3.0.1!

案例四:将镜像同步到Harbor中(或Nexus)

#前提搭载好Harbor、并且做好登陆互信

#Git clone & maven &  building & Harbor

配置Jenkins 访问 Harbor 凭据

Pipeline

#

pipeline {
    agent {
        kubernetes {
            inheritFrom 'maven-and-docker'
        }
    }    
    environment {
        appName = "spring-boot-helloworld"
        appVersion = "0.9.6"
        RegistryCredential='harbor-access-password'
        HarborServer='harbor.shunwang.com'
        RegistryUrl="https://${HarborServer}"
        ImageUrl="paibo/spring-boot-helloworld"
        ImageTag="latest"
    }  
    stages {
        stage('Source') {
            steps {
                sh 'git config --global http.sslVerify false'   
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://10.30.17.166:8929/root/springboot-helloworld.git']])
                echo '[1]-拉去Git仓库代码 - SUCCESS'
            }
        }      
        stage('Build') {
            steps {
            	container('maven') {
                    sh 'mvn clean package -DskipTests'
                }
            }
        }
        stage('Build App Images') {
            steps {
                container('docker') {
                    script {
                        dockerImage = docker.build("${HarborServer}/${ImageUrl}:${ImageTag}")  
                    }
                }
            }
        } 
        stage('Push Image') {
            steps {
                container('docker') {
                    script {
                        docker.withRegistry( RegistryUrl, RegistryCredential ) {
                            dockerImage.push()
                        }
                    }
                }
            }
        }

        
    }
}

立即构建-Console Output

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: kubernetes default/k8s-maven-testing-24-6fq5b-d15zq-wl5m0
Agent k8s-maven-testing-24-6fq5b-d15zq-wl5m0 is provisioned from template k8s-maven-testing_24-6fq5b-d15zq
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://10.30.17.167:8080/job/k8s-maven-testing/24/"
    runUrl: "job/k8s-maven-testing/24/"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "2d517e46fa35cd5df7899320626987c8df13a863"
    jenkins/label: "k8s-maven-testing_24-6fq5b"
  name: "k8s-maven-testing-24-6fq5b-d15zq-wl5m0"
spec:
  containers:
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "maven:3.6-openjdk-11-slim"
    imagePullPolicy: "IfNotPresent"
    name: "maven"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "docker:19.03-git"
    imagePullPolicy: "IfNotPresent"
    name: "docker"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "k8s-maven-testing-24-6fq5b-d15zq-wl5m0"
    - name: "JENKINS_WEB_SOCKET"
      value: "true"
    - name: "JENKINS_NAME"
      value: "k8s-maven-testing-24-6fq5b-d15zq-wl5m0"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://10.30.17.167:8080/"
    image: "jenkins/inbound-agent:3148.v532a_7e715ee3-1"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/root/.m2"
      name: "volume-0"
      readOnly: false
    - mountPath: "/run/docker.sock"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - name: "volume-0"
    persistentVolumeClaim:
      claimName: "maven-datadir-pvc"
      readOnly: false
  - hostPath:
      path: "/run/docker.sock"
    name: "volume-1"
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on k8s-maven-testing-24-6fq5b-d15zq-wl5m0 in /home/jenkins/agent/workspace/k8s-maven-testing
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Source)
[Pipeline] sh
+ git config --global http.sslVerify false
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning repository https://10.30.17.166:8929/root/springboot-helloworld.git
 > git init /home/jenkins/agent/workspace/k8s-maven-testing # timeout=10
Fetching upstream changes from https://10.30.17.166:8929/root/springboot-helloworld.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- https://10.30.17.166:8929/root/springboot-helloworld.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision a0721e08dd0abeee73b402a1c4766e60adf4be12 (refs/remotes/origin/master)
Commit message: "Update Dockerfile"
 > git config remote.origin.url https://10.30.17.166:8929/root/springboot-helloworld.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a0721e08dd0abeee73b402a1c4766e60adf4be12 # timeout=10
 > git rev-list --no-walk a0721e08dd0abeee73b402a1c4766e60adf4be12 # timeout=10
[Pipeline] echo
[1]-拉去Git仓库代码 - SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] container
[Pipeline] {
[Pipeline] sh
+ mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.neo:spring-boot-helloworld >-------------------
[INFO] Building spring-boot-helloworld 0.9.6-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ spring-boot-helloworld ---
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ spring-boot-helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/jenkins/agent/workspace/k8s-maven-testing/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-boot-helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/jenkins/agent/workspace/k8s-maven-testing/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ spring-boot-helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/jenkins/agent/workspace/k8s-maven-testing/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ spring-boot-helloworld ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ spring-boot-helloworld ---
[INFO] Building jar: /home/jenkins/agent/workspace/k8s-maven-testing/target/mytest.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ spring-boot-helloworld ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.947 s
[INFO] Finished at: 2024-08-07T03:41:22Z
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build App Images)
[Pipeline] container
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker build -t harbor.shunwang.com/paibo/spring-boot-helloworld:latest .
Sending build context to Docker daemon  16.89MB

Step 1/4 : FROM eclipse-temurin:11-jdk-alpine
 ---> d232ea92e043
Step 2/4 : LABEL maintainer="MageEdu <mage@magedu.com>"
 ---> Using cache
 ---> 28dbaea10cc2
Step 3/4 : ADD target/mytest.jar /applications/spring-boot-helloworld.jar
 ---> 04485f85cb2c
Step 4/4 : ENTRYPOINT ["/bin/sh","-c","/opt/java/openjdk/bin/java -jar /applications/spring-boot-helloworld.jar --server.port=80"]
 ---> Running in 8c8ee1c2e990
Removing intermediate container 8c8ee1c2e990
 ---> 61c4d2b0b3da
Successfully built 61c4d2b0b3da
Successfully tagged harbor.shunwang.com/paibo/spring-boot-helloworld:latest
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Push Image)
[Pipeline] container
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
Executing sh script inside container docker of pod k8s-maven-testing-24-6fq5b-d15zq-wl5m0
Executing command: "docker" "login" "-u" "admin" "-p" ******** "https://harbor.shunwang.com" 
exit
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
WARNING! Your password will be stored unencrypted in /home/jenkins/agent/workspace/k8s-maven-testing@tmp/f33b8b97-c5ae-4f41-a8fe-395acf41d987/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker tag harbor.shunwang.com/paibo/spring-boot-helloworld:latest harbor.shunwang.com/paibo/spring-boot-helloworld:latest
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker push harbor.shunwang.com/paibo/spring-boot-helloworld:latest
The push refers to repository [harbor.shunwang.com/paibo/spring-boot-helloworld]
d6008dd95b25: Preparing
8216c3e4d56f: Preparing
268d6cbe625e: Preparing
9c414807733f: Preparing
a713ad894cdf: Preparing
78561cef0761: Preparing
78561cef0761: Waiting
8216c3e4d56f: Pushed
268d6cbe625e: Pushed
d6008dd95b25: Pushed
78561cef0761: Pushed
a713ad894cdf: Pushed
9c414807733f: Pushed
latest: digest: sha256:94cbd6f6f8cca5e79ddd5083b0697ffdd74522dcf239c6a442c9918ac7cd4733 size: 1579
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

案例五:Gitlab通知自动触发构建

参考:https://www.cnblogs.com/birkhoffxia/articles/18303719 中全自动触发流水线-GitLab通知出发自动构建章节

案例六:配置Kubernetes Config仓库 结合Jenkins 实现CD

配置插件(Kubernetes CLI)

配置Pipeline

#流水线名字:sprint-boot-helloworld-deployment

pipeline {
    agent {
        kubernetes {
            inheritFrom 'kubectl'
        }
    }     
    stages {
        stage('Source') {
            steps {
                sh 'git config --global http.sslVerify false'   
                checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://10.30.17.166:8929/root/spring-boot-helloworld-deployment.git']])
                echo '[1]-拉去Git仓库代码 - SUCCESS'
            }
        }      
        stage('Deployment') {
            steps {
                container('kubectl') {
                    withKubeConfig([credentialsld: 'k8s-cluster-admin-kubeconfig']) {
                    	sh 'kubectl apply -f kubernetes/'
                    }
                }
            }
        }         
    }
}

配置credentials - secret file

kubectl  config view --raw > cluster-admin.kubeconfig

cat cluster-admin.kubeconfig

#把数据配置Jenkins 的Secret

配置Pod Template

#提前在node上 下载kubectl镜像
└──╼ # docker pull kubesphere/kubectl:v1.19.0
v1.19.0: Pulling from kubesphere/kubectl
8464c5956bbe: Pull complete 
e6c85eeb8fc4: Pull complete 
992bdb21de14: Pull complete 
Digest: sha256:f6851eae93529f70b3a1e7d4fee00b3a49cf548700a91457a4b8096a466d8efe
Status: Downloaded newer image for kubesphere/kubectl:v1.19.0

立即构建

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Created Pod: kubernetes default/sprint-boot-helloworld-deployment-3-65f5n-kjgjj-bf6l6
Agent sprint-boot-helloworld-deployment-3-65f5n-kjgjj-bf6l6 is provisioned from template sprint-boot-helloworld-deployment_3-65f5n-kjgjj
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://10.30.17.167:8080/job/sprint-boot-helloworld-deployment/3/"
    runUrl: "job/sprint-boot-helloworld-deployment/3/"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "94f1d060b6e0daff87fe981bee7d1b3078b01fae"
    jenkins/label: "sprint-boot-helloworld-deployment_3-65f5n"
  name: "sprint-boot-helloworld-deployment-3-65f5n-kjgjj-bf6l6"
spec:
  containers:
  - args:
    - "9999999"
    command:
    - "sleep"
    image: "kubesphere/kubectl:v1.19.0"
    imagePullPolicy: "IfNotPresent"
    name: "kubectl"
    resources: {}
    tty: false
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
    workingDir: "/home/jenkins/agent"
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_AGENT_NAME"
      value: "sprint-boot-helloworld-deployment-3-65f5n-kjgjj-bf6l6"
    - name: "JENKINS_WEB_SOCKET"
      value: "true"
    - name: "JENKINS_NAME"
      value: "sprint-boot-helloworld-deployment-3-65f5n-kjgjj-bf6l6"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://10.30.17.167:8080/"
    image: "jenkins/inbound-agent:3148.v532a_7e715ee3-1"
    name: "jnlp"
    resources:
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  hostNetwork: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on sprint-boot-helloworld-deployment-3-65f5n-kjgjj-bf6l6 in /home/jenkins/agent/workspace/sprint-boot-helloworld-deployment
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Source)
[Pipeline] sh
+ git config --global http.sslVerify false
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning repository https://10.30.17.166:8929/root/spring-boot-helloworld-deployment.git
 > git init /home/jenkins/agent/workspace/sprint-boot-helloworld-deployment # timeout=10
Fetching upstream changes from https://10.30.17.166:8929/root/spring-boot-helloworld-deployment.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- https://10.30.17.166:8929/root/spring-boot-helloworld-deployment.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://10.30.17.166:8929/root/spring-boot-helloworld-deployment.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision f758a710b15f023b9dede0639bb6403f5d514988 (refs/remotes/origin/main)
 > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f f758a710b15f023b9dede0639bb6403f5d514988 # timeout=10
Commit message: "Init First Commit"
 > git rev-list --no-walk f758a710b15f023b9dede0639bb6403f5d514988 # timeout=10
[Pipeline] echo
[1]-拉去Git仓库代码 - SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deployment)
[Pipeline] container
[Pipeline] {
[Pipeline] withKubeConfig
[Pipeline] {
[Pipeline] sh
+ kubectl apply -f kubernetes/
namespace/hello created
service/spring-boot-helloworld created
deployment.apps/spring-boot-helloworld created
[Pipeline] }
[kubernetes-cli] kubectl configuration cleaned up
[Pipeline] // withKubeConfig
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

验证是否正常运行Pod

└──╼ # kubectl get pods -n hello -o wide
NAME                                      READY   STATUS    RESTARTS   AGE    IP            NODE   NOMINATED NODE   READINESS GATES
spring-boot-helloworld-648cb859c7-bzsj5   1/1     Running   0          109s   10.244.3.28   k8s5   <none>           <none>
┌─[16:23:39 root@k8s1]─[~]
└──╼ # curl 10.244.3.28 && echo
Hello Spring Boot K8S version 1!

配置自动触发

 

GitLab上配置 Webhooks

点击测试Push events 自动触发流水线

Jenkinsfile入库

此实验缺乏 自动更改标签 修改代码触发流水线 但是无法触发配置仓库进行下发任务 自动部署 可以继续研究 GitOps Knative-ArgoCD 之类技术

posted @ 2024-08-06 15:17  しみずよしだ  阅读(133)  评论(0)    收藏  举报