为使用jenkinsci kubernetes-plugin创建的Jenkins从节点配置一个旁路容器。

huangapple go评论76阅读模式
英文:

Configure a side car container for Jenkins slave created with jenkinsci kubernetes-plugin

问题

我有这个YAML文件,用于配置我的Jenkins从节点容器在K8s上通过插件:

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的东西,但我不知道如何设置它。

英文:

I have this yaml file that configures my Jenkins slave container on k8s via the plugin:

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

How can I configure another sidecar container? I want to add buildkit. Adding it like this doesn't work, the plugin just ignores this configMap configuration:

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

I saw that in the docs I can configure something like additionalContainers but I can't understand how I can set it.

答案1

得分: 0

如果您使用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"
            # 是否在每个构建日志中显示POD Yaml,默认为`true`。
            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"
              # 使用ConfigMap `configmap-name` 的数据 `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](https://plugins.jenkins.io/kubernetes/#plugin-content-pod-template)

PS:某些名称与K8s的名称不同,插件会封装所有内容,并且所有代理都应该有 "jnlp" 容器,它由Jenkins自身使用并且其中有Java客户端。您可以运行任何您想要的代理并在流水线中切换容器。

在模板示例中有2个容器:

```yaml
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 .'
        }
      }
    }
  }
}

文档链接

英文:

if you use CASC plugin to configure you agents

this example can help you

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"

also you can check documentation here https://plugins.jenkins.io/kubernetes/#plugin-content-pod-template

ps some names differs to what K8s has, plugin wraps everything

and all agent should have "jnlp" container - it used by jenkins itself and has java client on it

you can run any agent you want and switch container in pipeline documentation

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

2 containers in template example:

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 example:

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 .'
}
}
}
}
}

huangapple
  • 本文由 发表于 2023年7月27日 21:38:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定