简体   繁体   English

Helm 中 Kubernetes 上的 Jenkins - 如何将 ssh 密钥附加到代理

[英]Jenkins on Kubernetes in Helm - how to attach ssh keys to the agent

I'm using Jenkins from stable/jenkins helm chart - all is fine I attached secrets with id_rsa/.pub key as volume to the jenkins master (in /opt/sshkeys/ let's say, why there? because the created files are a soft-link with 777 permissions so cannot go directly to $HOME/.ssh )我正在使用来自stable/jenkins helm 图表的 Jenkins - 一切都很好我将带有 id_rsa/.pub 密钥的秘密作为卷附加到 jenkins 主控器(在/opt/sshkeys/中,比如说,为什么会有?因为创建的文件是软文件- 具有 777 权限的链接,因此不能将 go 直接链接到$HOME/.ssh

root@agent-pod:/opt/config# ls -alh
total 12K
drwxrwxrwx 3 root root 4.0K Apr 20 13:00 .
drwxr-xr-x 1 root root 4.0K Apr 20 13:02 ..
drwxr-xr-x 2 root root 4.0K Apr 20 13:00 ..2020_04_20_13_00_29.601158101
lrwxrwxrwx 1 root root   31 Apr 20 13:00 ..data -> ..2020_04_20_13_00_29.601158101
lrwxrwxrwx 1 root root   13 Apr 20 13:00 id_rsa -> ..data/id_rsa

and then run few scripts (cp, chmod) to move them as proper files to /home/jenkins/.ssh/id_rsa - issues with the jenkins agent is - I can't simply attach volume as described above for the 777 reason, and I cannot use any command on the agent as the main ENTRYPOINT command is the one that makes it THE agent然后运行几个脚本(cp、chmod)将它们作为正确的文件移动到/home/jenkins/.ssh/id_rsa - jenkins 代理的问题是 - 由于 777 原因,我不能简单地附加卷,并且我不能在代理上使用任何命令,因为主要的 ENTRYPOINT 命令是使它成为代理的命令

command:
args: "${computer.jnlpmac} ${computer.name}"
  • trying to do any combination of mkdir /home/jenkins/.ssh && chmod... && <above args> or with ;尝试使用mkdir /home/jenkins/.ssh && chmod... && <above args>或与; makes the pod go into 0/1 Completed state rather than 1/1 Running使 pod go 变为0/1 Completed state 而不是1/1 Running

Does anyone have any idea how to put the two priv and pub keys into jenkins agent?有谁知道如何将两个 priv 和 pub 密钥放入 jenkins 代理? That's the main question.这是主要问题。 All else I just said is my way of solving it.我刚才所说的其他一切都是我解决它的方法。

Part of the Kubernetes plugin for Jenkins (which is required when running Jenkins on Kubernetes) shows this: Jenkins(在 Kubernetes 上运行 Jenkins 时需要)的 Kubernetes 插件的一部分显示了这一点:

Constraints
Multiple containers can be defined in a pod. One of them is automatically created with name
jnlp, and runs the Jenkins JNLP agent service, with args ${computer.jnlpmac} ${computer.name},
and will be the container acting as Jenkins agent.

Other containers must run a long running process, so the container does not exit. If the default 
entrypoint or command just runs something and exit then it should be overridden with something
like cat with ttyEnabled: true.

WARNING If you want to provide your own Docker image for the JNLP slave, you must name the
container jnlp so it overrides the default one. Failing to do so will result in two slaves 
trying to concurrently connect to the master.

PS: I see the official docs describing how to attach keys to pod here https://kubernetes.io/docs/concepts/configuration/secret/#use-cases (ctrl+f Use-Case: Pod with ssh keys ) but I met the issue as above with 777 PS:我在这里看到官方文档描述了如何将密钥附加到 pod https://kubernetes.io/docs/concepts/configuration/secret/#use-cases (ctrl+f Use-Case: Pod with ssh keys )但是我遇到上述问题 777

PS: To see how the deployment and values files looks like and test you can install helm3 and helm3 show values stable/jenkins > jenkins.yaml PS:要查看部署和值文件的外观和测试,您可以安装 helm3 和helm3 show values stable/jenkins > jenkins.yaml

As @potatopotato mentioned in comments, he solved this issue with creating a nfs server and attaching it as a volume to agent, with manually putting keys there.正如@potatopotato 在评论中提到的那样,他通过创建一个nfs 服务器并将其作为卷附加到代理解决了这个问题,并手动将密钥放在那里。


I would recommend to check this medium article as it describe well how it should be done, all resources mentioned in this article can be found in this git repository我建议查看这篇中等文章,因为它很好地描述了应该如何完成,本文中提到的所有资源都可以在这个 git存储库中找到

This git repository contains files needed to run through the demo for deploying这个 git 存储库包含运行演示所需的文件以进行部署

  • a Jenkins master一个 Jenkins 主控
  • a Jenkins agent with a persistent volume具有持久卷的 Jenkins 代理

More precisely check this part.更准确地检查这部分。

Communication between master and agent master和agent之间的通信

There are a number of ways a Jenkins master and agent can connect to each other. Jenkins 主机和代理可以通过多种方式相互连接。 We will initiate this connection from the Jenkins master over SSH , using SSH credentials to secure the connection.我们将从Jenkins 主节点通过 SSH 启动此连接,使用 SSH 凭据来保护连接。 This approach requires that we install the ssh-slaves plugin to our master, mount ssh credentials into the master and agent, and base our agent off of an ssh-slave image.这种方法要求我们将 ssh-slaves 插件安装到我们的 master 上,将 ssh 凭据安装到 master 和 agent 中,并将我们的代理基于 ssh-slave 映像。

Additionally, the Jenkins master needs to connect to each agent via unique static hostnames, which we get by deploying our agents using a StatefulSet.此外,Jenkins master 需要通过唯一的 static 主机名连接到每个代理,我们通过使用 StatefulSet 部署代理来获得这些主机名。

Initialization of agent(s)代理的初始化

Jenkins master will not connect to an ssh-based agent unless it is configured to do so. Jenkins 主机不会连接到基于 ssh 的代理,除非它被配置为这样做。 We can bootstrap this configuration by giving each agent pod an Init Container in charge of configuring Jenkins master.我们可以通过给每个代理 pod 一个负责配置 Jenkins 主控的 Init Container 来引导这个配置。 By the time the agent starts up, Jenkins master will already be trying to connect.代理启动时,Jenkins 主站已经在尝试连接。

The Init Container will need to complete the following: Init Container 需要完成以下操作:

  • Download the Jenkins CLI from master从 master 下载 Jenkins CLI
  • Use the CLI to check if master is already configured to use this agent使用 CLI 检查 master 是否已配置为使用此代理
  • If needed, use the CLI to configure master to use this agent如果需要,使用 CLI 将 master 配置为使用此代理

This will require some configuration within our agent pod:这将需要在我们的代理 pod 中进行一些配置:

  • fsGroup set to the “jenkins” user group. fsGroup 设置为“jenkins”用户组。 This is needed because persistent volumes mount as the root user by default, making them unusable for the “jenkins” user.这是必需的,因为默认情况下持久卷以 root 用户身份挂载,这使得它们对“jenkins”用户不可用。
  • Environment variables within the Init Container: JENKINS_URL: http address of Jenkins master JENKINS_LABEL: label for our agent Init Container 内的环境变量: JENKINS_URL: http Jenkins master 的地址 JENKINS_LABEL: label 为我们的代理
  • Environment variables within the main container: JENKINS_SLAVE_SSH_PUBKEY: public key to authenticate connection attempts主容器中的环境变量:JENKINS_SLAVE_SSH_PUBKEY:用于验证连接尝试的公钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 kubernetes 安装带有 helm 的 Moloch? - how to install Moloch with helm for kubernetes? ssh-agent 在 jenkins 管道上不起作用 - ssh-agent not working on jenkins pipeline 使用自定义代理映像配置 Kubernetes 插件 Jenkins - Configure Kubernetes plugin Jenkins with custom agent image 为什么 Kubernetes 中的 jenkins 代理有多个容器? - Why multiple containers for a jenkins agent in Kubernetes? 如何仅针对 Jenkins 管道后期条件指定要在 Kubernetes 集群上执行的容器/代理 - How to specify a container / agent to execute on Kubernetes cluster for Jenkins pipeline post condition only 如何为jenkins用户创建使用提供的ssh密钥的Jenkins Docker镜像? - How can I create a Jenkins Docker image that uses provided ssh keys for jenkins user? 码头工人的詹金斯奴隶拒绝SSH密钥 - Jenkins slave in docker is denying SSH keys 如何使用秘密中的ssh密钥将私有git存储库克隆到kubernetes pod? - How to clone a private git repository into a kubernetes pod using ssh keys in secrets? Docker jenkins ssh-agent“不在容器内运行”在 Ubuntu 22.04 - Docker jenkins ssh-agent "not running inside container" on Ubuntu 22.04 Jenkins 无法通过 ssh-agent 执行 docker login - Jenkins can not execute docker login via ssh-agent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM