本节详细讲述使用SSH连接以Docker方式启动的agents。
一、环境
假设Jenkins master已经安装好了。agent机器上docker环境也安装好了。
二、安装步骤
2.1 生成ssh key pair
在Jenkins master机器上执行以下命令,如果是以docker方式安装就在容器里面执行
$ ssh-keygen
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:XqxxjqsLlvDD0ZHm9Y2iR7zC6IbsUlMEHo3ffy8TzGs
The key's randomart image is:
+---[RSA 3072]----+
| o+ |
| ...o . |
| .o .+ . |
| o+.+ o o |
| ... o.So* . |
| o+ = +.X= |
| o oO + *..+ |
|. oo.o o .E . |
| o... oo.. o |
+----[SHA256]-----+
2.2 创建Jenkins SSH credential
- 打开Jenkins dashboard
- 在主菜单上点击
Manage Jenkins
,然后点击credentials 按钮
-
在global下拉框点击 Add Credentials
- 填写表单
- Kind: SSH Username with private key
- id: jenkins
- description: The jenkins ssh key
- username: jenkins(xia'magent容器的用户)
- Private Key: 从
~/.ssh/id_rsa
拷贝 -
Passphrase: 如果私钥有密码保护就需要填入
2.3 创建docker agent
使用 docker-ssh-agent image创建agent:
docker run -d --rm --name=agent1 -p 22:22 \
-e "JENKINS_AGENT_SSH_PUBKEY=[your-public-key]" \
jenkins/ssh-agent:alpine
[your-public-key]是ssh public key,可以从~/.ssh/id_rsa.pub获取
2.4 在Jenkins master建立agent1
在Jenkins dashboard一次点击Manage Jenkins
---> Manage Nodes and clouds
---> New Node
输入agent的名称和类型(比如Name: agent1, Type: Permanent Agent),点击OK,填写以下表单
- Remote root directory; (e.g.: /home/jenkins )
- label; (e.g.: agent1 )
- usage; (e.g.: only build jobs with label expression…)
- Launch method; (e.g.: Launch agents by SSH )
- Host; (e.g.: localhost or your IP address )
- Credentials; (e.g.: jenkins )
- Host Key verification Strategy; (e.g.: Manually trusted key verification … )
- JavaPath;这个选项必填
/opt/java/openjdk/bin/java
然后点击保存,此时agent1已经注册到master,但是还是offline,点进去
然后点击
Launch agent
,如果不出问题,日志最后会有如下的输出