Jenkins - 在Rocky Linux系統下安裝Jenkins和配置Agent節點
安裝Jenkins
主節點下載和安裝Jenkins LTS版本
本機信息
[root@localhost ~]# cat /etc/system-release
Rocky Linux release 9.4 (Blue Onyx)
[root@localhost ~]#
[root@localhost ~]# uname -a
Linux localhost.localdomain 5.14.0-427.33.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 28 17:34:59 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#
官方安裝文檔
https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos
下載和安裝Jenkins LTS版本
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
yum upgrade
yum install fontconfig java-17-openjdk -y
yum install jenkins
systemctl daemon-reload
確認版本
[root@localhost ~]# java -version
openjdk version "17.0.12" 2024-07-16 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.12.0.7-1) (build 17.0.12+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.12.0.7-1) (build 17.0.12+7-LTS, mixed mode, sharing)
[root@localhost ~]#
[root@localhost jenkins]# java -jar /usr/share/java/jenkins.war --version
2.462.1
[root@localhost jenkins]#
一些網絡配置
關閉防火墻
systemctl stop firewalld
systemctl disable firewalld
關閉SELINUX
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
[root@localhost ~]#
[root@localhost ~]# vim /etc/sysconfig/selinux
[root@localhost ~]# cat /etc/sysconfig/selinux |grep "SELINUX=" |grep -v "#"
SELINUX=disabled
[root@localhost ~]#
[root@localhost ~]# reboot
[root@localhost ~]#
[root@localhost ~]# sestatus
SELinux status: disabled
[root@localhost ~]#
啟動和初始化Jenkins
啟動Jenkins
[root@localhost ~]# systemctl enable jenkins
Created symlink /etc/systemd/system/multi-user.target.wants/jenkins.service → /usr/lib/systemd/system/jenkins.service.
[root@localhost ~]# systemctl start jenkins
[root@localhost ~]# systemctl status jenkins
● jenkins.service - Jenkins Continuous Integration Server
Loaded: loaded (/usr/lib/systemd/system/jenkins.service; enabled; preset: disabled)
Active: active (running) since Mon 2024-09-02 10:36:06 CST; 8s ago
Main PID: 42838 (java)
Tasks: 51 (limit: 48820)
Memory: 551.8M
CPU: 28.391s
CGroup: /system.slice/jenkins.service
└─42838 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080
Sep 02 10:35:56 localhost.localdomain jenkins[42838]: 502ee818dd62450f9f0a4156a90f5e95
Sep 02 10:35:56 localhost.localdomain jenkins[42838]: This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword
Sep 02 10:35:56 localhost.localdomain jenkins[42838]: *************************************************************
Sep 02 10:35:56 localhost.localdomain jenkins[42838]: *************************************************************
Sep 02 10:35:56 localhost.localdomain jenkins[42838]: *************************************************************
Sep 02 10:36:06 localhost.localdomain jenkins[42838]: 2024-09-02 02:36:06.367+0000 [id=34] INFO jenkins.InitReactorRunner$1#on>
Sep 02 10:36:06 localhost.localdomain jenkins[42838]: 2024-09-02 02:36:06.388+0000 [id=24] INFO hudson.lifecycle.Lifecycle#onR>
Sep 02 10:36:06 localhost.localdomain systemd[1]: Started Jenkins Continuous Integration Server.
Sep 02 10:36:07 localhost.localdomain jenkins[42838]: 2024-09-02 02:36:07.459+0000 [id=49] INFO h.m.DownloadService$Downloadab>
Sep 02 10:36:07 localhost.localdomain jenkins[42838]: 2024-09-02 02:36:07.460+0000 [id=49] INFO hudson.util.Retrier#start: Per>
[root@localhost ~]#
初始配置Jenkins
- 瀏覽器訪問頁面 http://192.168.16.200:8080/
- 根據提示查看初始登錄密碼:
cat /var/lib/jenkins/secrets/initialAdminPassword - 配置插件:默認不選,需要時再安裝
- 創建第一個管理員用戶
- 完成初始化
安裝和配置Jenkins Agent節點
在主節點配置Node
登錄Jenkins頁面:
Dashboard --》Manage Jenkins --》Nodes --》New node --》輸入名稱,并選擇類型 Permanent Agent --》依次進行選項配置
- Name: JenkinsAgent-Build01
- Description: Build agent node - 01
- Number of executors: 5
- Remote root directory: /opt/JenkinsAgent
- Labels: build maven linux
- Usage: Only build jobs with label expressions matching this node
- Launch method: Launch agent by connecting it to the controller
- Availability: Keep this agent online as much as possible
- Node Properties: null

主節點開啟TCP端口
Dashboard --> Manage Jenkins --> Security --> Agents --> TCP port for inbound agents --> 選擇 Fixed --> 添加端口 12345
主節點執行器數量修改
Dashboard --》Nodes --》Built-In Node --》Configure --》 Number of executors,修改為0
Note:主節點一般不執行流水線任務
在Node主機上配置Agent節點
在主節點上可以查看到需要在Node主機執行的命令
Dashboard --》Manage Jenkins --》Nodes --》JenkinsAgent-Build01
# Run from agent command line: (Unix)
mkdir /opt/JenkinsAgent
cd /opt
curl -sO http://192.168.16.200:8080/jnlpJars/agent.jar
java -jar /opt/agent.jar -url http://192.168.16.200:8080/ -secret 4e1e31e059bbb8248e90e07acace1d084b04756a62c253c1a0680c0b3513b75a -name "JenkinsAgent-Build01" -workDir "/opt/JenkinsAgent"
Note:每次主節點重啟后,對應的-secret的值都會發生變化
關閉防火墻
systemctl stop firewalld
systemctl disable firewalld
關閉SELINUX
[root@localhost ~]# vim /etc/sysconfig/selinux
[root@localhost ~]# cat /etc/sysconfig/selinux |grep "SELINUX=" |grep -v "#"
SELINUX=disabled
[root@localhost ~]# reboot
[root@localhost ~]# sestatus
SELinux status: disabled
[root@localhost ~]#
推薦以nohup方式啟動Jenkins Agent
nohup java -jar /opt/agent.jar -url http://192.168.16.200:8080/ -secret 4e1e31e059bbb8248e90e07acace1d084b04756a62c253c1a0680c0b3513b75a -name "JenkinsAgent-Build01" -workDir "/opt/JenkinsAgent" &
Agent啟動日志
[root@localhost opt]# java -jar agent.jar -url http://192.168.16.200:8080/ -secret 4e1e31e059bbb8248e90e07acace1d084b04756a62c253c1a0680c0b3513b75a -name "JenkinsAgent-Build01" -workDir "/opt/JenkinsAgent"
Sep 02, 2024 12:57:31 PM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /opt/JenkinsAgent/remoting as a remoting work directory
Sep 02, 2024 12:57:31 PM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /opt/JenkinsAgent/remoting
Sep 02, 2024 12:57:31 PM hudson.remoting.Launcher createEngine
INFO: Setting up agent: JenkinsAgent-Build01
Sep 02, 2024 12:57:31 PM hudson.remoting.Engine startEngine
INFO: Using Remoting version: 3248.3250.v3277a_8e88c9b_
Sep 02, 2024 12:57:31 PM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /opt/JenkinsAgent/remoting as a remoting work directory
Sep 02, 2024 12:57:31 PM hudson.remoting.Launcher$CuiListener status
INFO: Locating server among [http://192.168.16.200:8080/]
Sep 02, 2024 12:57:32 PM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolve
INFO: Remoting server accepts the following protocols: [JNLP4-connect, Ping]
Sep 02, 2024 12:57:32 PM hudson.remoting.Launcher$CuiListener status
INFO: Agent discovery successful
Agent address: 192.168.16.200
Agent port: 12345
Identity: b9:05:31:af:9d:c1:bd:81:f6:97:f1:76:4e:9d:60:5f
Sep 02, 2024 12:57:32 PM hudson.remoting.Launcher$CuiListener status
INFO: Handshaking
Sep 02, 2024 12:57:32 PM hudson.remoting.Launcher$CuiListener status
INFO: Connecting to 192.168.16.200:12345
Sep 02, 2024 12:57:32 PM hudson.remoting.Launcher$CuiListener status
INFO: Server reports protocol JNLP4-connect-proxy not supported, skipping
Sep 02, 2024 12:57:32 PM hudson.remoting.Launcher$CuiListener status
INFO: Trying protocol: JNLP4-connect
Sep 02, 2024 12:57:34 PM org.jenkinsci.remoting.protocol.impl.BIONetworkLayer$Reader run
INFO: Waiting for ProtocolStack to start.
Sep 02, 2024 12:57:36 PM hudson.remoting.Launcher$CuiListener status
INFO: Remote identity confirmed: b9:05:31:af:9d:c1:bd:81:f6:97:f1:76:4e:9d:60:5f
Sep 02, 2024 12:57:36 PM hudson.remoting.Launcher$CuiListener status
INFO: Connected
確認狀態
查看節點管理界面,現實狀態正常
Dashboard --》Manage Jenkins --》Nodes

錯誤排查
問題現象:agent節點啟動報錯
Sep 02, 2024 12:50:16 PM hudson.remoting.Launcher$CuiListener status
INFO: Could not locate server among [http://192.168.16.200:8080/]; waiting 10 seconds before retry
java.io.IOException: http://192.168.16.200:8080/tcpSlaveAgentListener/ is invalid: 404 Not Found
at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:229)
at hudson.remoting.Engine.innerRun(Engine.java:829)
at hudson.remoting.Engine.run(Engine.java:574)
需要在管理節點開啟TCP端口
Dashboard --> Manage Jenkins --> Security --> Agents --> TCP port for inbound agents --> 選擇 Fixed --> 添加端口 12345
正常情況下,瀏覽器頁面訪問 http://192.168.16.200:8080/tcpSlaveAgentListener/ 會顯示“Jenkins”結果
行動是絕望的解藥!
歡迎轉載和引用,但請在明顯處保留原文鏈接和原作者信息!
本博客內容多為個人工作與學習的記錄,少數內容來自于網絡并略有修改,已盡力標明原文鏈接和轉載說明。如有冒犯,即刻刪除!
以所舍,求所得,有所獲,方所成。

浙公網安備 33010602011771號