k8s 上使用 Jenkins 遇到的坑
2023-12-14 23:22:43
大家好,我是技术博客创作专家。今天,我想和大家分享一下我在 k8s 上使用 Jenkins 时遇到的各种问题和解决方案。希望我的经验能帮助你们避免陷入同样的陷阱。
问题 1:两个 stage 之间切换需要 1 分钟左右
这是我在使用 Jenkins pipeline 进行简单测试时遇到的第一个问题。两个 stage 之间需要 1 分钟左右的切换时间,这实在是太慢了。
经过一番调查,我发现问题出在 Jenkins 的配置上。在 Jenkins 的全局配置中,有一个选项叫做 "Stage View Timeout",默认值为 60 秒。这意味着,如果一个 stage 在 60 秒内没有完成,Jenkins 就会认为这个 stage 已经失败了,并会自动切换到下一个 stage。
为了解决这个问题,我将 "Stage View Timeout" 的值增加到了 300 秒。这样,Jenkins 就不会在 60 秒内自动切换到下一个 stage 了。
问题 2:Pod 无法启动
这是我在使用 Jenkins pipeline 部署 Pod 时遇到的另一个问题。Pod 总是无法启动,报错信息如下:
Error: failed to start container "my-container": Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "process_linux.go:428: container init caused \"process_linux.go:454: running pre-start hook 0 caused \\\"exec: \\\"/usr/bin/env\\\": executable file not found in $PATH\\\"\"": unknown
经过一番调查,我发现问题出在 Jenkins 的 Pod 配置上。在 Jenkins 的 Pod 配置中,有一个选项叫做 "Image Pull Policy",默认值为 "IfNotPresent"。这意味着,如果 Pod 的镜像不存在,Jenkins 会自动从镜像仓库中拉取该镜像。
但是,我的镜像仓库中并没有这个镜像。因此,Jenkins 无法拉取镜像,Pod 也无法启动。
为了解决这个问题,我将 "Image Pull Policy" 的值改为 "Always"。这样,Jenkins 就会在每次启动 Pod 时都从镜像仓库中拉取镜像,即使镜像已经存在。
问题 3:Pod 无法访问外部网络
这是我在使用 Jenkins pipeline 部署 Pod 时遇到的又一个问题。Pod 无法访问外部网络,报错信息如下:
Error: failed to start container "my-container": Error response from daemon: networkPlugin cni failed to set up pod "my-pod" network: error getting kubenet cni config: no networks found in /etc/cni/net.d
经过一番调查,我发现问题出在 Jenkins 的网络配置上。在 Jenkins 的网络配置中,有一个选项叫做 "Pod Network",默认值为 "bridge"。这意味着,Jenkins 会使用桥接网络来连接 Pod。
但是,我的集群中并没有使用桥接网络。因此,Pod 无法连接到外部网络。
为了解决这个问题,我将 "Pod Network" 的值改为 "host"。这样,Jenkins 就会使用主机网络来连接 Pod。Pod 就可以直接访问外部网络了。
总结
以上就是我在 k8s 上使用 Jenkins 时遇到的各种问题和解决方案。希望我的经验能帮助你们避免陷入同样的陷阱。
建议和技巧
以下是使用 Jenkins pipeline 时的一些建议和技巧:
- 使用 "Stage View Timeout" 选项来控制 stage 之间的切换时间。
- 使用 "Image Pull Policy" 选项来控制 Jenkins 拉取镜像的策略。
- 使用 "Pod Network" 选项来控制 Pod 的网络配置。
- 使用 "secret" 命令来管理敏感数据。
- 使用 "sh" 命令来执行 shell 命令。
- 使用 "retry" 命令来重试失败的 stage。
我希望这些建议和技巧能帮助你们使用 Jenkins pipeline 更加高效。