20230306--Jenkins远程部署方法之配置Slave节点
1.Jenkins远程部署方法Publish over SSH和slave的区别
-
publish over ssh
- jenkins运行时,获取的代码是放在jenkins所在主机的工作空间(默认的工作空间目录是:用户名/.jenkins/workspace/下,如果想要修改工作空间,最好是在启动jenkins之前添加环境变量:JENKINS_HOME=你想要指定的目录),再通过ssh登录远程主机将代码发送到远程主机上(需要配置远程主机存放这些文件的目录),包括shell脚本或者执行命令,都是从本地发送到远程上去的(或者这些shell脚本已经在远程主机上,使用命令执行即可。
- 需要安装插件:publish over ssh;如果git拉去项目需要在jenkins服务器上安装git,并配置git登录管理
-
slave
- slave即是创建一个分身节点,用于支持操作多个节点来支持分布式。
- 在jenkins构建时:先登录远程主机,通过远程主机这台电脑去拉取代码存放在远程主机的工作空间,再执行shell脚本或者执行命令(也就是说文件本身就是在远程主机上的,不需要从jenkins本地发送到配置的远程主机上)。
- 需要安装插件:SSH agent;
2.SSH方法添加Slave节点
1)Jenkins安装或确认必要插件
jenkins安装或确认必要插件SSH agent。
Dashboard--Manage Jenkins--Plugin Manager
2)SSH Slave Node配置
Dashboard--Manage Jenkins--Configure Global Security;指定代理端口为50000,默认为禁用;SSH Server 设置为启用,默认是禁用;保存Save。
添加节点:Dashboard--Manage Jenkins--Nodes-NewNode;填写Node name,选择Permanent Agent,Create。
配置节点:Dashboard--Manage Jenkins--Nodes,选择节点--选择Configuration
设置启动方式Launch agents via SSH,Host Key Verification Strategy 选择Non verifying Verification Strategy
SSH节点安装和jenkins相同版本的Java.
# yum install java-11-openjdk -y
# java -version
openjdk version "11.0.18" 2023-01-17 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.18.0.10-2.el8_7) (build 11.0.18+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.18.0.10-2.el8_7) (build 11.0.18+10-LTS, mixed mode, sharing)
确认Slave Node状态
3.Docker添加Slave节点
1)Jenkins安装或确认必要插件
jenkins安装或确认必要插件SSH agent。
Dashboard--Manage Jenkins--Plugin Manager
2)Docker服务器允许远程连接
修改docker服务启动文件「/usr/lib/systemd/system/docker.service」,并重启docker服务
# cp -p /usr/lib/systemd/system/docker.service /root/docker.service.bak
# vi -H /usr/lib/systemd/system/docker.service
# diff /usr/lib/systemd/system/docker.service /root/docker.service.bak
13,14c13
< #ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=172.26.37.126:80
< ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=172.26.37.126:80
---
> ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=172.26.37.126:80
# systemctl daemon-reload
# systemctl restart docker
3)Docker下载一个jenkins slave镜像
通过docker search slave 找到需要的镜像,然后pull镜像
# docker search slave
# docker pull jenkins/slave
4)Docker Slave Node配置
Dashboard--Manage Jenkins--Nodes,选择Configure Clouds;Add a new cloud,选择Docker,填写Docker Host URI,测试连通性。
点击Docker Agent templates;填写Labels,选择Enabled,填写docker Image,也就是刚才下载镜像
5)测试
新建一个pipeline来测试这个docker agent是否可用
pipeline {
agent {
label 'docker_slave'
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
sh 'pwd'
sh 'sleep 20'
echo 'Finish'
}
}
}
}
要指定agent.label文件docker-slave,这个名字是上面配置master时的Docker Agent templates一致
执行Job,一直报如下错误
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Still waiting to schedule task
All nodes of label ‘docker-slave’ are offline
Aborted by admin
[Pipeline] // node
[Pipeline] End of Pipeline
查看jenkins log
2023-03-06 09:08:12.457+0000 [id=344] INFO c.n.j.p.docker.DockerTemplate#doProvisionNode: Started container ID 7734ae758bfbccdf7b668494a7d15fa06e0db974ca9794205365e3deb302af71 for node docker-slave-1-0000kg3e51g2z from image: jenkins/slave:latest
2023-03-06 09:08:13.600+0000 [id=37] INFO hudson.slaves.NodeProvisioner#update: Image of jenkins/slave:latest provisioning successfully completed. We have now 3 computer(s)
2023-03-06 09:08:13.771+0000 [id=342] INFO i.j.d.c.DockerMultiplexedInputStream#readInternal: stderr from docker-slave-1-0000kg3e51g2z (7734ae758bfbccdf7b668494a7d15fa06e0db974ca9794205365e3deb302af71): Error: A JNI error has occurred, please check your installation and try again
2023-03-06 09:08:13.774+0000 [id=342] INFO i.j.d.c.DockerMultiplexedInputStream#readInternal: stderr from docker-slave-1-0000kg3e51g2z (7734ae758bfbccdf7b668494a7d15fa06e0db974ca9794205365e3deb302af71):
Exception in thread "main"
2023-03-06 09:08:13.776+0000 [id=342] INFO i.j.d.c.DockerMultiplexedInputStream#readInternal: stderr from docker-slave-1-0000kg3e51g2z (7734ae758bfbccdf7b668494a7d15fa06e0db974ca9794205365e3deb302af71): java.lang.UnsupportedClassVersionError: hudson/remoting/Launcher has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
有最后一句基本判断原因是由于jenkins服务器的java版本和docker环境中jenkins/slave的java不一致引起;
Jenkins服务器Java版本是openjdk 11.0.17 2022-10-18 LTS
jenkins/slave的Java版本是openjdk version "1.8.0_292"
jenkins@9ee753738ba6:~$ java -version
openjdk version "1.8.0_292"
此处暂不解决。基本上可以确定整个过程没问题。
参考URL
https://www.shuzhiduo.com/A/xl56QXm7Jr/
https://www.jianshu.com/p/7cf4dc902e88
https://blog.csdn.net/weixin_39658716/article/details/111257698
未完待续。。。
计划还有Kubernetes添加Slave节点(Jenkins Slave Pod)
网友评论