簡體   English   中英

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

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

我正在使用來自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

然后運行幾個腳本(cp、chmod)將它們作為正確的文件移動到/home/jenkins/.ssh/id_rsa - jenkins 代理的問題是 - 由於 777 原因,我不能簡單地附加卷,並且我不能在代理上使用任何命令,因為主要的 ENTRYPOINT 命令是使它成為代理的命令

command:
args: "${computer.jnlpmac} ${computer.name}"
  • 嘗試使用mkdir /home/jenkins/.ssh && chmod... && <above args>或與; 使 pod go 變為0/1 Completed state 而不是1/1 Running

有誰知道如何將兩個 priv 和 pub 密鑰放入 jenkins 代理? 這是主要問題。 我剛才所說的其他一切都是我解決它的方法。

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:我在這里看到官方文檔描述了如何將密鑰附加到 pod https://kubernetes.io/docs/concepts/configuration/secret/#use-cases (ctrl+f Use-Case: Pod with ssh keys )但是我遇到上述問題 777

PS:要查看部署和值文件的外觀和測試,您可以安裝 helm3 和helm3 show values stable/jenkins > jenkins.yaml

正如@potatopotato 在評論中提到的那樣,他通過創建一個nfs 服務器並將其作為卷附加到代理解決了這個問題,並手動將密鑰放在那里。


我建議查看這篇中等文章,因為它很好地描述了應該如何完成,本文中提到的所有資源都可以在這個 git存儲庫中找到

這個 git 存儲庫包含運行演示所需的文件以進行部署

  • 一個 Jenkins 主控
  • 具有持久卷的 Jenkins 代理

更准確地檢查這部分。

master和agent之間的通信

Jenkins 主機和代理可以通過多種方式相互連接。 我們將從Jenkins 主節點通過 SSH 啟動此連接,使用 SSH 憑據來保護連接。 這種方法要求我們將 ssh-slaves 插件安裝到我們的 master 上,將 ssh 憑據安裝到 master 和 agent 中,並將我們的代理基於 ssh-slave 映像。

此外,Jenkins master 需要通過唯一的 static 主機名連接到每個代理,我們通過使用 StatefulSet 部署代理來獲得這些主機名。

代理的初始化

Jenkins 主機不會連接到基於 ssh 的代理,除非它被配置為這樣做。 我們可以通過給每個代理 pod 一個負責配置 Jenkins 主控的 Init Container 來引導這個配置。 代理啟動時,Jenkins 主站已經在嘗試連接。

Init Container 需要完成以下操作:

  • 從 master 下載 Jenkins CLI
  • 使用 CLI 檢查 master 是否已配置為使用此代理
  • 如果需要,使用 CLI 將 master 配置為使用此代理

這將需要在我們的代理 pod 中進行一些配置:

  • fsGroup 設置為“jenkins”用戶組。 這是必需的,因為默認情況下持久卷以 root 用戶身份掛載,這使得它們對“jenkins”用戶不可用。
  • Init Container 內的環境變量: JENKINS_URL: http Jenkins master 的地址 JENKINS_LABEL: label 為我們的代理
  • 主容器中的環境變量:JENKINS_SLAVE_SSH_PUBKEY:用於驗證連接嘗試的公鑰

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM