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

  1. 浏览器访问页面 http://192.168.16.200:8080/
  2. 根据提示查看初始登录密码:cat /var/lib/jenkins/secrets/initialAdminPassword
  3. 配置插件:默认不选,需要时再安装
  4. 创建第一个管理员用户
  5. 完成初始化

安装和配置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”结果


posted @ 2024-09-02 22:40  Anliven  阅读(141)  评论(0编辑  收藏  举报