基于K8S配置Jenkins主从节点实例
- 1.配置Jenkins主节点
- 1.确认 Jenkins Pod 名称
- 2.进入 Jenkins Pod:
- 3.生成SSH密钥对
- 4.将公钥复制到目标节点:
- 2.配置Jenkins的node1节点
- 1.安装java
- 2.配置 Jenkins node1节点的 Java 路径
- 1.添加Java环境变量
- 2.生效Java环境变量
- 3.Jenkins网页页面添加node1节点
- 1.创建node1节点
- 2.设置node1节点
1.配置Jenkins主节点
主节点就是按照Jenkins节点
在Jenkins主节点中生成SSH密钥对,并将公钥添加到目标节点的authorized_keys文件中。
1.确认 Jenkins Pod 名称
[root@master var]# kubectl get pods -n devops-tools
NAME READY STATUS RESTARTS AGE
jenkins-b96f7764f-drtz2 1/1 Running 0 52m
2.进入 Jenkins Pod:
[root@master var]# kubectl exec -it jenkins-b96f7764f-drtz2 -n devops-tools -- /bin/bash
jenkins@jenkins-b96f7764f-drtz2:/$ ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
3.生成SSH密钥对
jenkins@jenkins-b96f7764f-drtz2:~$ ssh-keygen -t rsa -b 4096 -C "jenkins" -f /var/jenkins_home/.ssh/id_rsa -N ""
Generating public/private rsa key pair.
/var/jenkins_home/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Your identification has been saved in /var/jenkins_home/.ssh/id_rsa
Your public key has been saved in /var/jenkins_home/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:1cqf53fIpx1kkDTUvFQ3ARMw75RbhBUdOozweab6mhI jenkins
The key's randomart image is:
+---[RSA 4096]----+
| . ooBBBO|
| o.B.B++|
| .+.%...|
| o .* =. |
| S o. o o |
| E .. .o |
| .. o..o |
| . o oo.=|
| .o.. o+o|
+----[SHA256]-----+
4.将公钥复制到目标节点:
jenkins@jenkins-b96f7764f-drtz2:~$ ssh-copy-id -i /var/jenkins_home/.ssh/id_rsa.pub root@172.16.103.197
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/jenkins_home/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.103.197's password:
Number of key(s) added:
Now try logging into the machine, with: "ssh 'root@172.16.103.197'"
and check to make sure that only the key(s) you wanted were added.
2.配置Jenkins的node1节点
1.安装java
在目标节点(172.16.103.197)上安装 Java 11 或更高版本:
root@node1 bin]# sudo yum install java-11-openjdk -y
上次元数据过期检查:1:27:01 前,执行于 2024年08月05日 星期一 04时00分42秒。
依赖关系解决。
===============================================================================================================================================
软件包 架构 版本 仓库 大小
===============================================================================================================================================
安装:
java-11-openjdk x86_64 1:11.0.23.0.9-3.oc8 AppStream 475 k
安装依赖关系:
java-11-openjdk-headless x86_64 1:11.0.23.0.9-3.oc8 AppStream 42 M
事务概要
===============================================================================================================================================
安装 2 软件包
总下载:42 M
安装大小:179 M
下载软件包:
(1/2): java-11-openjdk-11.0.23.0.9-3.oc8.x86_64.rpm 417 kB/s | 475 kB 00:01
(2/2): java-11-openjdk-headless-11.0.23.0.9-3.oc8.x86_64.rpm 5.1 MB/s | 42 MB 00:08
-----------------------------------------------------------------------------------------------------------------------------------------------
总计 5.1 MB/s | 42 MB 00:08
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
运行脚本: java-11-openjdk-headless-1:11.0.23.0.9-3.oc8.x86_64 1/1
准备中 : 1/1
安装 : java-11-openjdk-headless-1:11.0.23.0.9-3.oc8.x86_64 1/2
运行脚本: java-11-openjdk-headless-1:11.0.23.0.9-3.oc8.x86_64 1/2
安装 : java-11-openjdk-1:11.0.23.0.9-3.oc8.x86_64 2/2
运行脚本: java-11-openjdk-1:11.0.23.0.9-3.oc8.x86_64 2/2
运行脚本: java-11-openjdk-headless-1:11.0.23.0.9-3.oc8.x86_64 2/2
运行脚本: java-11-openjdk-1:11.0.23.0.9-3.oc8.x86_64 2/2
验证 : java-11-openjdk-1:11.0.23.0.9-3.oc8.x86_64 1/2
验证 : java-11-openjdk-headless-1:11.0.23.0.9-3.oc8.x86_64 2/2
已安装:
java-11-openjdk-1:11.0.23.0.9-3.oc8.x86_64 java-11-openjdk-headless-1:11.0.23.0.9-3.oc8.x86_64
完毕!
2.配置 Jenkins node1节点的 Java 路径
1.添加Java环境变量
[root@node1 bin]# vim /etc/profile
#行尾添加
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.23.0.9-3.oc8.x86_64
export PATH=$JAVA_HOME/bin:$PATH
2.生效Java环境变量
[root@node1 bin]# source /etc/profile
[root@node1 bin]# java -version
openjdk version "11.0.23" 2024-04-16 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.23.0.9-2) (build 11.0.23+9-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.23.0.9-2) (build 11.0.23+9-LTS, mixed mode, sharing)
[root@node1 bin]#
3.Jenkins网页页面添加node1节点
1.创建node1节点
2.设置node1节点
生成的密钥:
复制到你添加的Key里
jenkins@jenkins-b96f7764f-drtz2:~$ cat /var/jenkins_home/.ssh/id_rsa
保存,然后刷新当前页面