date
related_level
slug
type
relate_date
summary
status
tags
category
last_updated
Jan 4, 2026 03:53 PM
是否已更新
orginal_page
是否推荐
helm 部署 jenkins
参考资料
- helm 部署已经包含 Kubernetes 插件和当前环境作为初始 Cloud
- 如果
添加 jenkins repo
helm repo add jenkinsci https://charts.jenkins.io
helm repo update
helm search repo jenkins
kustomize 创建 secrets,PVC 和服务账户等必要资源
mkdir -p ~/.config/k3s/helm/devops/jenkins/kustomize/init
vim ~/.config/k3s/helm/devops/jenkins/kustomize/init/admin-secret.env
- 自行修改 password
vim ~/.config/k3s/helm/devops/jenkins/kustomize/init/jenkins-ns.yaml
vim ~/.config/k3s/helm/devops/jenkins/kustomize/init/jenkins-rbac.yaml
wget -O ~/.config/k3s/helm/devops/jenkins/kustomize/init/jenkins-rbac-2.yaml https://raw.githubusercontent.com/jenkins-infra/jenkins.io/master/content/doc/tutorials/kubernetes/installing-jenkins-on-kubernetes/jenkins-02-sa.yaml- 详见对应文件
vim ~/.config/k3s/helm/devops/jenkins/kustomize/init/jenkins-pvc.yaml
- 使用 longhorn 存储
vim ~/.config/k3s/helm/devops/jenkins/kustomize/init/jenkins-agent-pvc.yaml
- 使用 longhorn 存储
- accessModes 设置为 ReadWriteMany
vim ~/.config/k3s/helm/devops/jenkins/kustomize/init/kustomization.yaml
kubectl create -k ~/.config/k3s/helm/devops/jenkins/kustomize/init
配置 jenkins helm values
cd ~/.config/k3s/helm/devops/jenkins
helm search repo jenkins -l | grep jenkins/jenkins | head -n 20- 查看要安装哪个版本
helm show values jenkinsci/jenkins --version 5.8.101 > value.default.yaml- 以 2.516.3 版本最后一个 chart version 为例
cp value.default.yaml value-5.8.101.yaml
vim value-5.8.101.yaml
- 默认的插件版本依赖有冲突,修改了下
controller.installPlugins
- 用到之前的 Secrets, SA 和 PVC
- 添加了 prometheus 的 ServiceMonitor
- agent 启用了 garbageCollection 和 idleMinutes 并使用之前创建的PVC
给 node 打上标签以部署调度到对应 node
k label nodes node1 jenkins-server=v1
k label nodes node2 node3 jenkins-agent=v1
helm 部署 jenkins
cd ~/.config/k3s/helm/devops/jenkins
helm upgrade --install jenkins jenkinsci/jenkins \ -n jenkins --version 5.8.101 \ --values value-5.8.101.yaml
检查 jenkins 运行情况
kubectl get -n jenkins sts
kubectl logs --since 1h -n jenkins sts/jenkins
kubectl get -n jenkins svc- server 端口在 jenkins 服务的 8080 端口
kubectl get pods -n jenkins --field-selector=status.phase!=Running- 查看不可用
部署 jenkins 对应 traefik 路由
vim ~/.config/k3s/helm/devops/jenkins/kustomize/network/jenkins-ingressroute.yaml
vim ~/.config/k3s/helm/devops/jenkins/kustomize/network/kustomization.yaml
kubectl apply -k ~/.config/k3s/helm/devops/jenkins/kustomize/network
kubectl describe -n kube-system deploy/traefik | grep namespace- 检查 traefik 是否已经为 jenkins 命名空间提供路由
添加 DNS 记录到 /etc/hosts
- 获取 traefik 的 external-ip
kubectl get svc -n kube-system traefik -o wide
- curl 测试首页
curl -vk https://jenkins.traefik.cluster.local/login- ok 表示访问成功
使用插件 kubernetes-credentials-provider 配置凭据
参考资料
helm 添加插件 kubernetes-credentials-provider
- helm 使用对应 value 更新部署即可
需要 jenkins 的服务账户可以管理 Secret,建议单独配置命名空间
- 之前配置的 SA 已经满足要求
jenkins 一些可用的凭据类型
secretText
secretFile
usernamePassword
basicSSHUserPrivateKey
certificate
x509ClientCert
aws
openstackCredentialv3
gitHubApp
配置 usernamePassword 类型 harbor 凭据 Secret
- 假设已经创建了对应 harbor robot 账号
robot$jenkins
echo -n "harbor passwd: " && read -s HARBOR_ROBOT_PASSWORD; echo- 可以从文件创建也可以终端输入,这里采取终端输入
kubectl create secret generic harbor-regcred \ -n jenkins \ --type=kubernetes.io/basic-auth \ --from-literal=username='robot$jenkins' \ --from-literal=password=$HARBOR_ROBOT_PASSWORD- 名称
harbor-regcred会作为 jenkins 凭据 ID
kubectl label secret harbor-regcred \ -n jenkins \ jenkins.io/credentials-type=usernamePassword- labels 设置 jenkins 凭据类型
kubectl annotate secret harbor-regcred \ -n jenkins \ jenkins.io/credentials-description="credentials from Kubernetes" \ jenkins.io/credentials-store-locations="['thisIsJobA', 'thisIsJobB', 'thisIsFolderA/thisIsJobC']"- annotation 可以设置可选的凭据注释和范围
kubectl get -n jenkins secret harbor-regcred -o yaml
kubectl get -n jenkins secret harbor-regcred -o jsonpath='{.data.password}' | base64 --decode
unset HARBOR_ROBOT_PASSWORD
配置 usernamePassword 类型 gitea 凭据 Secret
- 假设已经创建了对应 gitea 账号 token
echo -n "gitea passwd: " && read -s GITEA_TOKEN; echo- 可以从文件创建也可以终端输入,这里采取终端输入
kubectl create secret generic gitea-token-regcred \ -n jenkins \ --type=kubernetes.io/basic-auth \ --from-literal=username=<username> \ --from-literal=password=$GITEA_TOKEN_PASSWORD- 名称
gitea-token-regcred会作为 jenkins 凭据 ID
kubectl label secret gitea-token-regcred \ -n jenkins \ jenkins.io/credentials-type=usernamePassword- labels 设置 jenkins 凭据类型
kubectl annotate secret gitea-token-regcred \ -n jenkins \ jenkins.io/credentials-description="credentials from Kubernetes" \ jenkins.io/credentials-store-locations="['thisIsJobA', 'thisIsJobB', 'thisIsFolderA/thisIsJobC']"- annotation 可以设置可选的凭据注释和范围
kubectl get -n jenkins secret gitea-token-regcred -o yaml
kubectl get -n jenkins secret gitea-token-regcred -o jsonpath='{.data.password}' | base64 --decode
unset GITEA_TOKEN
配置 basicSSHUserPrivateKey 类型 gitea ssh 凭据 Secret
prikey=$(cat $HOME/.ssh/your-gitea-private-key)
kubectl create secret generic gitea-ssh-prikey-regcred \ -n jenkins \ --type=Opaque \ --from-literal=username=git \ --from-literal=privateKey=$prikey- 名称
gitea-ssh-prikey-regcred会作为 jenkins 凭据 ID - key 必须要为
username和privateKey - 如果需要为私钥添加密码可以添加字段
passphrase --from-literal=passphrase=mypassphrase
kubectl label secret gitea-ssh-prikey-regcred \ -n jenkins \ jenkins.io/credentials-type=basicSSHUserPrivateKey- labels 设置 jenkins 凭据类型
kubectl annotate secret gitea-ssh-prikey-regcred \ -n jenkins \ jenkins.io/credentials-description="credentials from Kubernetes" \ jenkins.io/credentials-store-locations="['thisIsJobA', 'thisIsJobB', 'thisIsFolderA/thisIsJobC']"- annotation 可以设置可选的凭据注释和范围
kubectl get -n jenkins secret gitea-ssh-prikey-regcred -o yaml
kubectl get -n jenkins secret gitea-ssh-prikey-regcred -o jsonpath='{.data.privateKey}' | base64 --decode
unset prikey
配置 certificate 类型 buildkit 凭据 Secret
- 通过 cert-manager step-issuer 签发,配置详见相关文章
创建 p12 证书的 password
kubectl create secret generic buildkit-pkcs12-cred \ -n jenkins \ --type=Opaque \ --from-literal=password=nomoresecret
vim ~/.step/cert.buildctl.jenkins.yaml
- dns 名称随意
- secretTemplate 中的 jenkins.io/credentials-keybinding 用于转换键 keystore.p12 为 certificate 以便 jenkins 能正确识别
kubectl apply -f ~/.step/cert.buildctl.jenkins.yaml
- 签发之后需要添加 password 字段以便 jenkins 解码 p12 证书
password_b64=$(echo -n "nomoresecret" | base64 -w 0)kubectl patch secret buildkit-client-mtls -n jenkins -p "{\"data\":{\"password\": \"$password_b64\"}}"
kubectl get -n jenkins secret buildkit-client-mtls -o yaml
查看 keystore.p12 证书
kubectl get -n jenkins secret buildkit-client-mtls -o yaml -o jsonpath='{.data.keystore\.p12}' | base64 -d > keystore.p12- 导出 keystore.p12
openssl pkcs12 -info -in keystore.p12 -nokeys -provider legacy -provider default- 需要输入之前的 password
- OpenSSL 3 默认禁用了 RC2-40-CBC 这种弱算法
-provider legacy -provider default启用
配置 x509ClientCert类型 buildkit 凭据 Secret
- 通过 cert-manager step-issuer 签发,配置详见相关文章
vim ~/.step/cert.buildctl.jenkins.x509.yaml
- dns 名称随意
- secretTemplate 中的 jenkins.io/credentials-* 用于转换键 tls.crt, tls.key, ca.crt 以便 jenkins 能正确识别
kubectl apply -f ~/.step/cert.buildctl.jenkins.x509.yaml
kubectl get -n jenkins secret buildkit-x509client-mtls -o yaml
- 配置后在 manage/credentials 里的 Kubernetes 存储里可以看到对应 Secret
其他可选:使用 JCasC (代码即配置) 配置凭据
参考资料
- 容器内 JCasC 配置脚本存放路径
/var/jenkins_home/casc_configs/
- 输出现在的 JCasC 配置脚本
k get -n jenkins cm jenkins-jenkins-jcasc-config -ojsonpath="{.data.jcasc-default-config\.yaml}" > jcasc-default-config.yaml
配置 gitea ssh 凭据 Secret
prikey=$(cat $HOME/.ssh/your-gitea-private-key)
kubectl create secret generic gitea-ssh-secret \ -n jenkins \ --type=Opaque \ --from-literal=username=gitea \ --from-literal=prikey=$prikey \ --from-literal=password=nomoresecret- password 用于加密存储的私钥
kubectl get -n jenkins secret gitea-ssh-secret -o yaml
kubectl get -n jenkins secret gitea-ssh-secret -o jsonpath='{.data.prikey}' | base64 --decode
unset prikey
helm 使用对应 value 更新部署
vim value.yaml
- controller 添加 secret 和 JCasC
其中,controller.JCasC.configScripts 的 ssh-credentials 添加 credentials 如下
- configScripts 配置后会部署在 ConfigMap 上
jenkins-jenkins-config-<configscripts-name> k get -n jenkins cm jenkins-jenkins-config-ssh-credentials -o yaml
使用 JCasC (代码即配置) 配置 gitea 主机的 known_host
参考资料
- 获取对应主机的公钥信息
ssh-keyscan -p 22 github.com
手动修改办法如下
- Security - Security - Git Host Key Verification Configuration
- Host Key Verification Strategy 修改为 Manually Provided keys
- 然后填入 Approved Host Keys,会生成对应 known_hosts
- 容器内 JCasC 配置脚本存放路径
/var/jenkins_home/casc_configs/
- 输出现在的 JCasC 配置脚本
k get -n jenkins cm jenkins-jenkins-jcasc-config -ojsonpath="{.data.jcasc-default-config\.yaml}" > jcasc-default-config.yaml
helm 使用对应 value 更新部署
vim value.yaml
获取对应主机的公钥信息填入 known_hosts
ssh-keyscan -p 22 github.com
- configScripts 配置后会部署在 ConfigMap 上
jenkins-jenkins-config-<configscripts-name> k get -n jenkins cm jenkins-jenkins-config-ssh-credentials -o yaml
dashboard 登录后在 Settings 中显示 It appears that your reverse proxy set up is broken.
- Jenkins URL 和当前访问地址不一致
- System configuration - System - Jenkins Location - Jenkins URL
- 调整为当前访问的 URL,如
https://jenkins.traefik.cluster.local/
jenkins Kubernetes 插件提供四个步骤:kubeconfig, podTemplate 和 contianer, containerLog
- kubeconfig 配置
kubectl命令行工具,使其能在 Pipeline 中操作 Kubernetes 集群
- podTemplate 定义运行 Pod 模板,动态创建临时 Pod 作为 Agent
- contianer 在已启动的 Agent Pod 中,切换到指定容器执行命令
- containerLog 获取 Pod 中某个容器的日志
jenkins agent 运作流程和配置 Kubernetes Pod 模板
- 参考资料
- Jenkins Controller 动态地在 Kubernetes 集群中创建一个临时 Pod 作为构建代理(Agent),任务完成后自动销毁该 Pod
- 首个容器默认为使用官方
jenkins/inbound-agent镜像的 JNLP 容器 - 启动后,会主动连接 Jenkins Controller(通过
JENKINS_URL和JENKINS_SECRET) - 连接成功后,Controller 将构建任务下发给 Agent 执行
配置 Kubernetes Pod 模板参数
- 不是全部参数都有,没有的参数可以通过 yaml 自定义,比如说
- 可以配置的参数在 helm 里都有了
通过 Pod 原生 yaml 语法和 override 融合策略配置 hostAliases
vim values.yaml
- 多个不同 Agent 可以通过
additionalAgents: {}配置 - 额外的 Agent 默认集成 agent 配置项,因此只需要配置其他需要的选项
- Author:白鸟3
- URL:https://blog.kun2peng.top/operation/k8s_jenkins_deploy
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
