简体   繁体   中英

Using k8s to deploy jenkins and make slave Pods to perform tasks, how to put files or installation packages generated in the pod into jenkins pvc

I deployed jenkins and used the slave pod to run it. I used the local pv mode of openebs, deployed jenkins and volume on the same node. I used the volume mode to transfer the data generated by the pod. It is shared in the jenkins volume of the host computer. My task is to download the code and add some installation packages to it, but it takes a long time to download the packages. I hope that the slave pod will not be downloaded every time it is deployed.

#!/usr/bin/env groovy
// groovy公共变量
def PROJECT = "CI-code"
def WORKDIR_PATH = "/opt/status"
def DOWNLOAD_KUBE_DOWNLOAD_URL = "xxx/kube-1.19.0-v2.2.0-amd64.tar.gz"
def PVC_PATH = "/var/openebs/local/pvc-8e8f9830-9bdc-494d-ac45-19310cbda035/cloudybase"

pipeline {
   agent {
   kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  name: jenkins-slave
  namespace: devops-tools
spec:
  containers:
  - name: jnlp
    image: "xxx/google_containers/jenkins-slave-jdk11-wget:latest"
    imagePullPolicy: Always
    securityContext:
      privileged: true
      runAsUser: 0
    volumeMounts:
      - name: docker-cmd
        mountPath: /usr/bin/docker
      - name: docker-sock
        mountPath: /var/run/docker.sock
      - name: code
        mountPath: /home/jenkins/agent/workspace/${PROJECT}
  volumes:
    - name: docker-cmd
      hostPath:
        path: /usr/bin/docker
    - name: docker-sock
      hostPath:
        path: /var/run/docker.sock
    - name: code
      hostPath:
        path: ${PVC_PATH}
"""
    }
  }

   stages {
      stage('拉取代码') {
         steps {
            git branch: 'release-2.2.0', credentialsId: 'e17ba069-aa8b-4bfd-9c9e-3f3956914f09', url: 'xxx/deployworker.git'
         }
      }

   stage('下载依赖包到组件包对应目录') {
      steps {
            sh """
               logging() {
                 echo -e "\033[32m $(/bin/date)\033[0m" - $@
                }
             
               main () {
                 logging Check ${DOWNLOAD_KUBE_DOWNLOAD_URL} installation package if download ...
                    if [ ! -f "${PVC_PATH}/kube_status_code" ];then
                     download_kube
                    fi
                }

                download_kube () {
                DOWNLOAD_KUBE_NAME=$(echo ${DOWNLOAD_KUBE_DOWNLOAD_URL} | /bin/sed 's|.*/||')
                
                cd / && { /bin/curl -O ${DOWNLOAD_KUBE_DOWNLOAD_URL} ; cd -; }
                if [ "$?" -ne "0" ]; then
                  echo "Failed"
                  exit 1
                fi
 
                echo 'true' > ${W_PATH}/kube_status_code
                mkdir -p /home/jenkins/agent/workspace/deploywork/Middleware-choreography/kubeQ/kubeQ/
                tar xf /\${DOWNLOAD_KUBE_NAME} -C /home/jenkins/agent/workspace/deploywork/Middleware-choreography/kubeQ/kubeQ/

                if [ "$?" -ne "0" ]; then
                  echo "Failed"
                  exit 1
                fi
                }
            """
         }
      }
      

Hi zccharts from the above explanation I can get that you are trying to create your container from scratch every time and it is consuming time. This can be solved by creating your base container image with all the packages installed and you can use it in your pipeline for deploying your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM