、 兩篇文章實現了K8S+Jenkins+Maven基礎環境,通過此環境我們進一步實現SpringBoot項目的CI/CD。
K8S+Jenkins+maven基礎環境目前能夠實現:
剩下的工作需在Jenkins流水線中調用kubectl 命令實現SpringBoot項目在K8S中部署了。
要想Jenkins能夠調用kubectl命令,需要安裝相關插件,下面我們來介紹下。
Jenkins中有兩個插件可以實現與Kubernetes進行交互。
一、Kubernetes CLI
此插件允許在Jenkins Job中通過kubectl與k8s集群進行交互,主要是通過kubectl執行各種命令。
1.安裝Kubernetes CLI插件
2.通過&34;生成指令
通過&34;來使用插件:
3.添加需要執行的命令
withKubeConfig(caCertificate: &39;, clusterName: &39;, contextName: &39;, credentialsId: &39;, namespace: &39;, serverUrl: &39;) { // 自定義指定塊 sh &34; }
4.執行流水線
pipeline { agent { label &39; } stages { stage(&39;) { steps{ git credentialsId: &39;, url: &39; } } stage(&39;) { steps { container(&39;) { sh &34;&34;&34; } } } stage(&39;) { steps { withKubeConfig(caCertificate: &39;, clusterName: &39;, contextName: &39;, credentialsId: &39;, namespace: &39;, serverUrl: &39;) { // 自定義指令塊 sh &34; } } } }}
由此可見,通過此插件可以在k8s中執行任何命令。
注意:
二、Kubernetes Continuous Deploy
此插件用於將資源配置文件部署到k8s集群,主要是應用yaml配置文件。
1.安裝Kubernetes Continuous Deploy插件
2.添加憑據
&34;中的內容為&34;內容,直接拷貝即可。
3.通過&34;生成指令
通過&34;來具體使用插件:
注意:pod中的容器鏡像存在於自建的遠程harbor倉庫,拉取鏡像時需要進行認證,在k8s中我們將此密碼在k8s以secret形式存放。
4.添加需要執行的命令
kubernetesDeploy configs: &39;, kubeconfigId: &39;, secretName: &39;, secretNamespace: &39;, ssh: [sshCredentialsId: &39;, sshServer: &39;], textCredentials: [certificateAuthorityData: &39;, clientCertificateData: &39;, clientKeyData: &39;, serverUrl: &39;]39;helloworld.yaml&39;k8s-cd-plugin&39;harbor&39;test&34;Kubernetes Continuous Deploy&34;Kubernetes CLI&FF0000; --tt-darkmode-color: 生產環境helloworld-prod.yaml 以測試模板配置文件為例 hosts解析 hostAliases: - ip: &34; hostnames: - &34; - &34; - ip: &34; hostnames: - &34; containers: - name: helloworld 鏡像tag image: harbor.test.cn/helloworld/helloworld:{tag} imagePullPolicy: IfNotPresent livenessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 readinessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 ports: - containerPort: 8080 resources: limits: cpu: &34; memory: &34; requests: cpu: &34; memory: &34; volumeMounts: - name: logdir mountPath: /logs - name: localtime mountPath: /etc/localtime - name: timezone mountPath: /etc/timezone imagePullSecrets: - name: harbor volumes: - name: logdir emptyDir: {} - name: localtime hostPath: path: /etc/localtime - name: timezone hostPath: path: /etc/timezone---apiVersion: v1kind: Servicemetadata: name: helloworld namespace: testspec: type: NodePort selector: app: helloworld ports: - port: 8080 targetPort: 8080---apiVersion: extensions/v1beta1kind: Ingressmetadata: name: helloworld namespace: testspec: rules: - host: hello.test.cn http: paths: - path: / backend: serviceName: helloworld servicePort: 8080
模板文件中我們設置了JAVA_OPTS、鏡像tag變量,經流水線傳遞具體參數後,將生成最終的helloworld-test.yaml文件,以便&34;插件調用。
二、流水線配置
Jenkins新建流水線項目並添加以下流水線:
pipeline { agent { label &39; } environment { branch = &34; JAVA_OPTS = &34; } stages { stage(&39;) { steps{ git branch: &39;, credentialsId: &39;, url: &39; } } stage(&39;) { steps { container(&39;) { sh &34;&獲取git版本作為鏡像tag tag=`git rev-parse HEAD` echo \$tag mvn clean package docker:build -DdockerImageTags=\$tag -Dmaven.test.skip=true -DpushImageTag 34;s\$tag{JAVA_OPTS}g&34;&34; } } } stage(&39;) { when { environment name: &39;, value: &39; } steps { kubernetesDeploy configs: &39;, kubeconfigId: &39;, secretName: &39;, secretNamespace: &39; } } }}
要點:
最終部署結果如下:
如圖:Jenkins的&34;插件直接應用的yaml,如果換成&34;則需要執行&34;,大家可根據自己的習慣選擇不同的插件。
三、回滾
&34; 是一個滾動升級的過程,即當readinessProbe探針檢查正常後才會加入到service的Endpoints提供服務;最終原來的pod將會被終止、刪除。
既然滾動升級可以了,我們再來嘗試下回滾:
kubectl get deploy -n testNAME READY UP-TO-DATE AVAILABLE AGEhelloworld 1/1 1 1 4d18h kubectl rollout history deploy helloworld -n testdeployment.apps/helloworld REVISION CHANGE-CAUSE2 <none>3 <none>4 <none>5 <none>6 <none>7 <none>8 <none>9 <none> kubectl rollout undo deploy/hellworlddeployment.apps/helloworld rolled back kubectl rollout undo deploy/hellworld --to-revision=3
為實現整個SpringBoot項目CI/CD的整個過程,因文章篇幅問題,我們從基礎環境的準備到本文分別總結了4篇文章,另外3篇為: