为使用jenkinsci kubernetes-plugin创建的Jenkins slave配置一个side car容器

iszxjhcz  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(99)

我有这个yaml文件,它通过插件在k8s上配置我的Jenkins从容器:

clouds:
  - kubernetes:
      ...
      name: "kubernetes"
      templates:
      - name: jenkins-slave
        containers:
          - image: "..."
            name: "jnlp"
            resourceLimitCpu: "1000m"
            resourceLimitMemory: "4000Mi"
            ...

字符串
如何配置另一个Sidecar容器?添加buildkit。像这样添加它不起作用,插件只是忽略此configMap配置:

clouds:
  - kubernetes:
      ...
      name: "kubernetes"
      templates:
      - name: jenkins-slave
        containers:
        - image: "..."
          name: "jnlp"
          resourceLimitCpu: "1000m"
          resourceLimitMemory: "4000Mi"
          ...
        - image: "moby/buildkit"
          name: "buildkit"


我在文档中看到我可以配置像additionalContainers这样的东西,但我不知道如何设置它。

ldxq2e6h

ldxq2e6h1#

如果使用CASC插件配置代理
这个例子可以帮助你

jenkins:
  clouds:
    - kubernetes:
        name: "advanced-k8s-config"
        serverUrl: "https://advanced-k8s-config:443"
        serverCertificate: "serverCertificate"
        skipTlsVerify: true
        credentialsId: "advanced-k8s-credentials"
        namespace: "default"
        jenkinsUrl: "http://jenkins/"
        jenkinsTunnel: "jenkinsTunnel"
        containerCapStr: 42
        maxRequestsPerHostStr: 64
        retentionTimeout: 5
        connectTimeout: 10
        readTimeout: 20

        templates:
          - name: "test"
            serviceAccount: "serviceAccount"
            instanceCap: 1234
            idleMinutes: 0
            label: "label"
            # Enable whether the POD Yaml is displayed in each build log or not, `true` by default.
            showRawYaml: true
            
            volumes:
              - hostPathVolume:
                  mountPath: "mountPath"
                  hostPath: "hostPath"

            containers:
              - name: "name"
                image: "image"
                privileged: true
                alwaysPullImage: true
                command: "command"
                args: "args"
                workingDir: "workingDir"
                ttyEnabled: true
                resourceRequestCpu: "resourceRequestCpu"
                resourceRequestMemory: "resourceRequestMemory"
                resourceLimitCpu: "resourceLimitCpu"
                resourceLimitMemory: "resourceLimitMemory"
            imagePullSecrets:
              - name: "imagePullSecrets"

            envVars:
              - envVar:
                  key: "FOO"
                  value: "BAR"

          - name: "k8s-agent"
            namespace: "default"
            label: "linux-x86_64"
            nodeUsageMode: EXCLUSIVE
            containers:
              - name: "jnlp"
                image: "jenkins/inbound-agent:latest"
                alwaysPullImage: true
                workingDir: "/home/jenkins"
                ttyEnabled: true
                resourceRequestCpu: "500m"
                resourceLimitCpu: "1000m"
                resourceRequestMemory: "1Gi"
                resourceLimitMemory: "2Gi"
            volumes:
              - emptyDirVolume:
                  memory: false
                  mountPath: "/tmp"
              # Mount the content of the ConfigMap `configmap-name` with the data `config`.
              - configMapVolume:
                  configMapName: configmap-name
                  mountPath: /home/jenkins/.aws/config
                  subPath: config
            idleMinutes: "1"
            activeDeadlineSeconds: "120"
            slaveConnectTimeout: "1000"

字符串
你也可以在这里查看文档https://plugins.jenkins.io/kubernetes/#plugin-content-pod-template
ps一些名称与K8s不同,插件 Package 了所有内容
所有代理都应该有“jnlp”容器-它由jenkins自己使用,并在上面有java客户端
你可以运行任何你想要的代理,并在管道文档中切换容器

container('your-second-container-name') {
  sh 'hostname'
}


模板示例中的2个容器:

jenkins:
  clouds:
    - kubernetes:
        name: "advanced-k8s-config"
        serverUrl: "https://advanced-k8s-config:443"
        serverCertificate: "serverCertificate"
        skipTlsVerify: true
        credentialsId: "advanced-k8s-credentials"
        namespace: "default"
        jenkinsUrl: "http://jenkins/"
        jenkinsTunnel: "jenkinsTunnel"
        containerCapStr: 42
        maxRequestsPerHostStr: 64
        retentionTimeout: 5
        connectTimeout: 10
        readTimeout: 20
        templates:
          - name: "templatename"
            label: "label"
            showRawYaml: true
            containers:
              - name: "name1"
                command: "cat"
                args: ''
                alwaysPullImage: true
                image: "anyrandomimage"
                workingDir: '/home/jenkins/agent'
                ttyEnabled: true
              - name: "name2"
                command: "cat"
                args: ''
                alwaysPullImage: true
                image: "anyrandomimage2"
                workingDir: '/home/jenkins/agent'
                ttyEnabled: true


流水线示例:

pipeline {
  agent {
    kubernetes {
      cloud 'YOUR PREDEFINED CLOUD NAME FROM PLUGIN'
      defaultContainer 'jnlp'
      yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
  app: myapp
spec:
  serviceAccountName: k8s-agent
  containers:
    - name: maven
      image: maven:latest
      command:
      - cat
      tty: true
    - name: docker
      image: docker:latest
      command:
      - cat
      tty: true
      volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-sock
  volumes:
    - name: docker-sock
      hostPath:
        path: /var/run/docker.sock
"""
}
  }
  stages {

    stage ('build java') {
      steps {
        container('maven') {
          sh 'mvn clean install'
        }
      }
    }
    stage ('build docker image'){
      steps {
        container('docker') {
          sh 'docker build -t image:v1 .'
        }
      }
    }
  }
}

相关问题