root@jenkins:~# netstat -antp|grep 50000
tcp6 0 0 :::50000 ::😗 LISTEN 5139/java
1.52 安装Jenkins
root@ubuntu20:~# dpkg -i jenkins_2.414.3_all.deb
配置各种类型的Agent的关键之处在于启动Agent的方式
◼ JNLP Agent对应着"通过Java Web启动代理"这种方式
◼ Master与Slave之间建立一个基于HTTPS协力的Web连接
◼ Slave首次接入Master时,需要提供用于认证的Secret以完成
Bootstrap
配置JNLP Agent创建分布式构建环境的简要步骤
◼ 准备Agent节点,部署好Jenkins
◼ 在Master节点上,添加Agent节点的定义
◼ 在Master节点上,获取将Agent节点连接至Master时需要执行的
命令
◼ 在Agent节点上,打开命令终端,运行Master提示的命令
◼ 在Master节点上验证添加的结果
/home/jenkins/agent
点击创建的
agent 1.52 运行命令
root@ubuntu20:~# mkdir /home/jenkins/agent -pv
root@ubuntu20:~# chown -R jenkins.jenkins /home/jenkins/agent/
curl -sO http://192.168.1.51:8080/jnlpJars/agent.jar
java -jar agent.jar -jnlpUrl http://192.168.1.51:8080/computer/jenkins01-192.168.1.52/jenkins-agent.jnlp -secret db32e82e85dddf69f96f0f50bd13b5380f5f6de1789f0535c01bb650843c844a -workDir "/home/jenkins/agent"
编辑流水线填写agent的标签
查看流水线会在agent1.52中部署
ssh方式连接agent
root@ubuntu20:/var/lib/jenkins/plugins# passwd jenkins
New password: 123456
Retype new password:
passwd: password updated successfully
使用ssh连接成功
报错,授权即可
bash
Building remotely on jenkins01-1.52 (linux-agent) in workspace /home/jenkins/agent/workspace/spring-boot-helloword2
The recommended git tool is: NONE
java.lang.ClassNotFoundException: org.jenkinsci.plugins.gitclient.Git$GitAPIMasterToSlaveFileCallable
chown -R jenkins:jenkins /var/lib/jenkins/workspace/
使用docker agent
bash
root@jenkins:~/learning-jenkins-cicd/07-jenkins-agents# cat docker-compose-inbound-agent.yml
version: '3.6'
volumes:
agent01_data: {}
agent02_data: {}
networks:
jenkins_net:
driver: bridge
ipam:
config:
- subnet: 172.31.9.0/24
services:
agent01:
image: jenkins/inbound-agent:alpine-jdk11
hostname: agent01.magedu.com
user: root
environment:
TZ: Asia/Shanghai
JENKINS_URL: http://192.168.1.51:8080 #jenkinsmaster地址
JENKINS_AGENT_NAME: agent01.magedu.com #agent名字
JENKINS_AGENT_WORKDIR: /home/jenkins/agent
JENKINS_SECRET: 7eecbd2daa66612cac412d4fc484faf80fcee0fc0e8502b12abec11605744074 #jenkins添加节点后的密钥,保持一致
# SECRET is automatically generated by the master when adding agent01.
volumes:
- agent01_data:/appdata/jenkins/
networks:
jenkins_net:
ipv4_address: 172.31.9.11
aliases:
- slave01
- agent01
#extra_hosts:
# - "jenkins.magedu.com:172.29.7.7"
#restart: always
agent02:
image: jenkins/inbound-agent:alpine-jdk11
hostname: agent02.magedu.com
user: root
environment:
TZ: Asia/Shanghai
JENKINS_URL: http://192.168.1.51:8080
JENKINS_AGENT_NAME: agent02.magedu.com
JENKINS_AGENT_WORKDIR: /home/jenkins/agent
JENKINS_SECRET: be0e53b93558553ee9694fced68d6443138ee13c5eeaf5d07bfad026d2fd36a6
# SECRET is automatically generated by the master when adding agent02.
volumes:
- agent02_data:/appdata/jenkins/
networks:
jenkins_net:
ipv4_address: 172.31.9.12
aliases:
- slave02
- agent02
#extra_hosts:
# - "jenkins.magedu.com:172.29.7.7"
#restart: always
yaml文件 JENKINS_SECRET和jenkins中添加的密钥要一致
运行
docker-compose -f docker-compose-inbound-agent.yml up
运行成功后
添加流水线可以使用
通过docker ssh 连接
ssh-keygen -t rsa -b 2048 -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): .jenkins-ssh-agent-rsa
Your identification has been saved in .jenkins-ssh-agent-rsa
Your public key has been saved in .jenkins-ssh-agent-rsa.pub
查看并替换yanl
bash
root@jenkins:~# cat .jenkins-ssh-agent-rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPIv6Hjrl0r4QroJe++/xx3wMJesjoKW7Or16ilVTEXLP5PahwJyNlRZIJN5JdmFwu6QV/izQw0JvyceOo5Gebo1LZW6qZmhMJAJB537U38l1Wex7iIpnTlugrLkq2CtaOPKNEYEXBXH0SOaZuTosMoSLI83CBHlWWzAKcd/WRWQOMB48eABGvxC8LZDzrsA/uA3kIRaJS56PeWkBWK/OU/inCfzmSUVbN5uY3eRV/WHZ32R2dk9ELbmBjcxt+w2h+a0aWCuWj4wSQd8Oy0LfFTuuP+PwCpWasJqhIf3r+nms72oXk1qS9P7peFGybSDGePbW8BD209gJ3UzERif/j root@jenkins
bash
cat docker-compose-ssh-agent.yml
version: '3.6'
volumes:
ssh_agent01_data: {}
ssh_agent02_data: {}
networks:
jenkins_net:
driver: bridge
ipam:
config:
- subnet: 172.31.9.0/24
services:
ssh-agent01:
image: jenkins/ssh-agent:jdk11
hostname: ssh-agent01.magedu.com
#user: jenkins
environment:
TZ: Asia/Shanghai
JENKINS_AGENT_HOME: /home/jenkins
JENKINS_AGENT_SSH_PUBKEY: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPIv6Hjrl0r4QroJe++/xx3wMJesjoKW7Or16ilVTEXLP5PahwJyNlRZIJN5JdmFwu6QV/izQw0JvyceOo5Gebo1LZW6qZmhMJAJB537U38l1Wex7iIpnTlugrLkq2CtaOPKNEYEXBXH0SOaZuTosMoSLI83CBHlWWzAKcd/WRWQOMB48eABGvxC8LZDzrsA/uA3kIRaJS56PeWkBWK/OU/inCfzmSUVbN5uY3eRV/WHZ32R2dk9ELbmBjcxt+w2h+a0aWCuWj4wSQd8Oy0LfFTuuP+PwCpWasJqhIf3r+nms72oXk1qS9P7peFGybSDGePbW8BD209gJ3UzERif/j root@jenkins
# SSH PRIVATE KEY and PUBLIC KEY is generated by you.
networks:
jenkins_net:
ipv4_address: 172.31.9.21
aliases:
- ssh-slave01
- ssh-agent01
ports:
- "22022:22"
#restart: always
ssh-agent02:
image: jenkins/ssh-agent:jdk11
hostname: ssh-agent02.magedu.com
#user: jenkins
environment:
TZ: Asia/Shanghai
#JENKINS_AGENT_HOME: /home/jenkins
JENKINS_AGENT_SSH_PUBKEY: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPIv6Hjrl0r4QroJe++/xx3wMJesjoKW7Or16ilVTEXLP5PahwJyNlRZIJN5JdmFwu6QV/izQw0JvyceOo5Gebo1LZW6qZmhMJAJB537U38l1Wex7iIpnTlugrLkq2CtaOPKNEYEXBXH0SOaZuTosMoSLI83CBHlWWzAKcd/WRWQOMB48eABGvxC8LZDzrsA/uA3kIRaJS56PeWkBWK/OU/inCfzmSUVbN5uY3eRV/WHZ32R2dk9ELbmBjcxt+w2h+a0aWCuWj4wSQd8Oy0LfFTuuP+PwCpWasJqhIf3r+nms72oXk1qS9P7peFGybSDGePbW8BD209gJ3UzERif/j root@jenkins
# SSH PRIVATE KEY and PUBLIC KEY is generated by you.
networks:
jenkins_net:
ipv4_address: 172.31.9.22
aliases:
- ssh-slave02
- ssh-agent02
ports:
- "22122:22"
#restart: always
运行容器
root@jenkins:~/learning-jenkins-cicd/07-jenkins-agents# docker-compose -f docker-compose-ssh-agent.yml up
在jenkins创建,私钥保存为认证凭证
bash
cat .jenkins-ssh-agent-rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEAzyL+h465dK+EK6CXvvv8cd8DCXrI6Cluzq9eopVUxFyz+T2ocCcj
ZUWSCTeSXZhcLukFf4s0MNCb8nHjqORnm6NS2VuqmZoTCQCQed+1N/JdVnse4iKZ05boKy
5KtgrWjjyjRGBFwVx9Ejmmbk6LDKEiyPNwgR5VlswCnHf1kVkDjAePHgARr8QvC2Q867AP
7gN5CEWiUuej3lpAVivzlP4pwn85klFWzebmN3kVf1h2d9kdnZPRC25gY3MbfsNofmtGlg
rlo+MEkHfDstC3xU7rj/j8AqVmrCaoSH96/p5rO9qF5NakvT+6XhRsm0gxnj21vAQ9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W1pS0WbGqeCDx100xxxuCJCe4FB/YMpO5zyClAZUCRjqqwAAAIEA5GDj7zXXwNb1viJO
fMLCcK28Qhho2ueIOZIZF2TqcgElQaPUHVrBHQUtr/wEPqFYGLRP2qAiBNEQCL0QmuA94P
FxaKbQThtW/MZxSweitRxl5brua8jVofx2+6w/CNTco4DdmSceV0mZEKm+LPPaZsdM9g/b
huHbDn6nuAsFP6kAAAAMcm9vdEBqZW5raW5zAQIDBAUGBw==
-----END OPENSSH PRIVATE KEY-----
jenkins添加从节点
192.168.1.51 是jenkins容器运行所在的宿主机
Host Key Verification Strategy 选non
/opt/java/openjdk/bin/java 是容器里面的java路径
查看正常
基于Docker容器的动态Agent
#192.168.1.52
docker service 添加
vim /usr/lib/systemd/system/docker.service
-H tcp://0.0.0.0:2376
root@ubuntu20:~/.ssh# systemctl daemon-reload
root@ubuntu20:~/.ssh# systemctl restart docker.service
jenkins 服务器测试
bash
root@jenkins:~# curl http://192.168.1.52:2376/version
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"24.0.7","Details":{"ApiVersion":"1.43","Arch":"amd64","BuildTime":"2023-10-26T09:08:01.000000000+00:00","Experimental":"false","GitCommit":"311b9ff","GoVersion":"go1.20.10","KernelVersion":"5.4.0-81-generic","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.6.24","Details":{"GitCommit":"61f9fd88f79f081d64d6fa3bb1a0dc71ec870523"}},{"Name":"runc","Version":"1.1.9","Details":{"GitCommit":"v1.1.9-0-gccaecfc"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"Version":"24.0.7","ApiVersion":"1.43","MinAPIVersion":"1.12","GitCommit":"311b9ff","GoVersion":"go1.20.10","Os":"linux","Arch":"amd64","KernelVersion":"5.4.0-81-generic","BuildTime":"2023-10-26T09:08:01.000000000+00:00"}
安装插件docker-plugin
tcp://192.168.1.52:2376 #docker所在的地址
docker-agent2 docker-dyn-agent-2 #标签 ,流水线要配置标签才能使用
jenkins/inbound-agent:jdk11 #镜像
构建提示失败重启jenkins后成功:
在Docker Cloud上添加ssh Agent模板
系统管理-clounds
编辑修改流水线
查看使用了