「JENKINS」- 在 Kubernetes 中,执行 Pipeline 任务

认识

官网:https://plugins.jenkins.io/kubernetes/
文档:https://www.jenkins.io/doc/pipeline/steps/kubernetes/
仓库:https://github.com/jenkinsci/kubernetes-plugin

在以往的构建中,我们需要为 Jenkin 添加 Agent 节点,以执行构建任务。但是当没有构建任务时,这些节点处于闲置状态,我们希望提高资源利用率。通过 Kubernetes 插件,实现在 Kubernetes 中动态创建 Agent 节点,来执行构建任务。

组成

WIP

性质

使用 Kubernetes Cluster 作为构建节点

通过 Kubernetes 插件,我们能够在 Kubernetes Pod 中运行构建任务。

添加 Kubernetes 节点:
1、Manage Jenkins ⇒ Manage Nodes and Clouds ⇒ Configure Clouds
2、Jenkins URL:填写 Jenkins Server 地址;
3、Container Cleanup Timeout:控制 Container 清理。默认在任务结束时将自动清理;

提供外部存储 | volumes | workspaceVolume

volumes Volumes that are defined for the pod and are mounted by ALL containers.

workspaceVolume The type of volume to use for the workspace.

应用

添加 Kubernetes 节点

WIP

创建 Pipeline 脚本

pipeline {

    agent {
        kubernetes {
            cloud 'gke-developing-120'
            yamlFile 'k8s/declarativeYamlFile.yml'
            serviceAccount 'foo-sa'
        }
    }

    stages {

        stage('Print Cli Version') {
            steps {
                container('cli') {
                    sh 'cldi -V'
                }
            }
        }

    }
    ...
}

改进

[DONE] 动态创建的 Agent 处于 ContainerCreating 状态 09/22/2025

通过该插件,我们能够动态创建 Agent 节点,但是,某天,动态创建 Agent 时,其均处于 ContainerCreating 状态。

我们复制 Agent Pod YAML 定义,并经过逐步排查(删除 YAML 字段)和测试,我们发现问题出在 NFS 上。

针对 Agent Pod 实例,其通过 POD.spec.volumes[].nfs 来访问 NFS 存储。但是 Pod 的 nfs 参数并不支持配置 mount 选项,所以默认使用 nfsv4 挂载。然而,云商提供的 NFS 存储不支持 nfsv4 挂载(或许是因为有问题,所以不支持),进而导致 Agent Pod 无法正常启动。

解决方案有多种:

  • 通过 /etc/nfsmount.conf 强制使用 NFSv3 挂载 ⇒ 我们未测试;
  • 我们通过创建额外的 PVC 来提供给 Jenikins Agent 使用 ⇒ 问题得以解决 09/24/2025

多个 Job 共享 Agent

使得多个 Job 能够共享 Agent 实例?等同于把 Node 迁移到 Kubernetes 中。

修改 jenkins/inbound-agent 镜像地址

Kubernetes | Jenkins plugin

问题描述

执行 Pipeline 失败,返回 Back-off pulling image “jenkins/inbound-agent:4.11-1-jdk11” 错误;

原因分析

默认使用 DockerHub 的 jenkins/inbound-agent 镜像,网络原因导致镜像无法成功拉取;

解决方案

通过 Jenkinfile / pipeline.agent.kubernetes 方式:

1)在 Jenkins / Manage Jenkins / Manage Nodes and Clouds 中,
选中需要进行配置的集群
创建 Pod Templates 配置
假如 Pod Templates 的 Name 为 global-override(根据需要自行命名);
在 Containers 中,Name: jnlp, Docker image: <Your jenkins/inbound-agent Image>

2)在特定作业中,配置 inheritFrom ‘global-override’ 来继承配置;

通过 Jenkinsfile / PodTemplate 方式:

通过
containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent', args: '${computer.jnlpmac} ${computer.name}') 修改;

… Invalid value: 600: must be a number between 0 and 0777 (octal) ..

问题描述

… Message: Pod “xxxxxx” is invalid: [spec.volumes[0].configMap.defaultMode: Invalid value: 600: must be a number between 0 and 0777 (octal), both inclusive, spec.containers[0].volumeMounts[0].name: Not found: “kube-config”]. Received status: Status(apiVersion=v1, code=422, details=StatusDetails(causes=[StatusCause(field=spec.volumes[0].configMap.defaultMode, message=Invalid value: 600: must be a number between 0 and 0777 (octal), both inclusive, reason=FieldValueInvalid, additionalProperties={}), StatusCause(field=spec.containers[0].volumeMounts[0].name, message=Not found: “kube-config”, reason=FieldValueNotFound, additionalProperties={})], group=null, kind=Pod, name=xxxxxx, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=Pod “xxxxxx” is invalid: [spec.volumes[0].configMap.defaultMode: Invalid value: 600: must be a number between 0 and 0777 (octal), both inclusive, spec.containers[0].volumeMounts[0].name: Not found: “kube-config”], metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid, status=Failure, additionalProperties={}). …

Jenkins 2.414, Kubernetes CLI Plugin Version1.12.1

原因分析

解决方案

修改 spec.volumes[0].configMap.defaultMode: 384 即可

参考

Kubernetes CLI | Jenkins plugin
kubernetes – Jenkins pipeline: kubectl: not found – Stack Overflow