1)需求:html
Rancher 上部署 Jenkins-master,服務採用 Jenkins-slave 發佈,發佈完成後 Jenkins-slave 自動銷燬。git
2)環境:docker
Rancher 2.2.7 (部署Rancher參考以前文章:離線安裝 Rancher2.2.4 HA 集羣)瀏覽器
Jenkins 2.176.2 負載均衡
在 Rancher 2 上部署 Jenkins-mastermaven
名稱:jenkins-master Docker鏡像:jenkins/jenkins:lts 命名空間:jenkins 數據卷:jenkins-master-pvc,容器路徑:/var/jenkins_home
名稱:jenkins-master 命名空間:jenkins 自定義域名:jenkinscicd.xxxxxx.com 服務/工做負載:jenkins-master 容器端口:8080
jenkinscicd.xxxxxx.com 172.16.5.84(ingress lb 地址)
dig查看是否解析成功gitlab
dig jenkinscicd.xxxxxx.com
1、瀏覽器訪問鏈接:jenkinscicd.xxxxxx.com 2、在rancher上進入容器,查看密碼 cat /var/jenkins_home/secrets/initialAdminPassword 3、驗證後選擇安裝推薦的插件 4、重啓jenkins
Manage Jenkins -> Manage Plugins 安裝 Kubernetes 和 Kubernetes Cli 插件ui
Kubernetes
Kubernetes Cli
說明:Kubernetes插件是能夠增長Kubernetes雲,Kubernetes Cli插件是用於jenkinsfile中kubectl容器能夠使用withKubeConfig參數url
添加4個全局憑據spa
1)docker-registry:gitlab 鏡像倉庫憑據(類型:Username with password) 2)gitlab:gitlab 憑據(類型:Username with password) 3)kube:develop 集羣的憑據(類型:Secret text) 4)cicdkube:jenkins 所在k8s集羣的憑據(類型:Secret text)
其中 kube 和 cicdkube 的Secret 爲 rancher 對應的集羣配置文件中的 token 值。
系統設置,增長一個雲(kubernetes)
1)名稱:kubernetes 2)Kubernetes 地址:https://kubernetes.default 3)Kubernetes 命名空間:jenkins 4)憑據:cicd-kube (以前建立的cicdkube憑據,用於鏈接jenkins-master所在的集羣) 5)Jenkins 地址:http://jenkins-master:8080
把 KubernetesPod.yaml 文件放在代碼庫根目錄,內容以下:
metadata: labels: some-label: some-label-value spec: containers: - name: jnlp env: - name: CONTAINER_ENV_VAR value: jnlp resources: limits: cpu: 1 memory: 1Gi requests: cpu: 1 memory: 1Gi - name: maven image: maven:3.6-alpine command: - cat tty: true env: - name: CONTAINER_ENV_VAR value: maven volumeMounts: - name: repo-maven-cache mountPath: /root/.m2 - name: kubectl image: cnych/kubectl command: - cat tty: true env: - name: CONTAINER_ENV_VAR value: kubectl - name: docker image: docker command: - cat tty: true env: - name: CONTAINER_ENV_VAR value: docker volumeMounts: - name: repo-docker-sock mountPath: /var/run/docker.sock volumes: - name: repo-maven-cache persistentVolumeClaim: claimName: pvc-jenkins-maven-cache - name: repo-docker-sock hostPath: path: /var/run/docker.sock
把 Jenkinsfile 文件也放在代碼庫根目錄,內容以下:
pipeline { environment { IMAGE_TAG = sh (returnStdout: true, script: 'echo "build-${BRANCH_NAME//\\//_}-$BUILD_NUMBER"').trim() } agent { kubernetes { defaultContainer 'maven' yamlFile 'KubernetesPod.yaml' } } stages { stage('Run maven') { steps { container('maven') { echo "代碼編譯打包" sh 'mvn clean install' } } } stage('Build image') { steps { container('docker') { script { echo "構建Docker鏡像" def image = docker.build("reg.nexus.wmqhealth.com/tools/cicd-test:" + "$IMAGE_TAG", ".") withDockerRegistry([credentialsId:'docker-registry', url:"https://reg.nexus.wmqhealth.com"]){ image.push() } } } } } stage('Deploy') { steps { container('kubectl') { script { echo "部署項目" withKubeConfig(clusterName: 'develop', contextName: 'develop', credentialsId: 'kube', namespace: 'cicdtest', serverUrl: 'https://rancher.wmqhealth.com/k8s/clusters/c-xg99q') { sh 'kubectl set image deployment/cicd-test cicd-test=reg.nexus.wmqhealth.com/tools/cicd-test:$IMAGE_TAG --namespace cicdtest' } } } } } } }
在 develop 集羣建立 cicdtest 命名空間
名稱和命名空間與jenkinsfile中定義保持一致。鏡像隨便寫一個不存在的,部署pods數爲0
在jenkins 中點擊構建
一、在jenkins所在的集羣會動態生成 jenkins-slave pod
二、會打包上傳鏡像,並會更新develop集羣中cicd-test的鏡像信息
三、構建成功後,手動把pod數增長到1,這樣項目就構建完成了。