认识
官网: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 镜像地址
问题描述
原因分析
解决方案
containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent', args: '${computer.jnlpmac} ${computer.name}') 修改;
… Invalid value: 600: must be a number between 0 and 0777 (octal) ..
问题描述
原因分析
解决方案
参考
Kubernetes CLI | Jenkins plugin
kubernetes – Jenkins pipeline: kubectl: not found – Stack Overflow