Jenkins 平台搭建 | 为 Jenkins 配置 nginx 反向代理

以 Centos7 系统为例,详细记录一下 Jenkins 搭建流程。

参考官网:https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos


Install Jenkins

从 redhat-stable yum 存储库中安装 LTS(长期支持) 版本,该版本较为稳定。

sudo wget -O /etc/yum.repos.d/jenkins.repo \
    https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum upgrade
# Add required dependencies for the jenkins package
sudo yum install java-11-openjdk
# Verify 
java -version
sudo yum install jenkins
sudo systemctl daemon-reload

Start Jenkins

systemctl enable jenkins
systemctl start jenkins
systemctl status jenkins

Configuring the Jenkins platform

Unlocking Jenkins

当首次访问新的 Jenkins 实例时,系统会要求您使用自动生成的密码解锁它。

浏览到 http://localhost:8080(或在安装 Jenkins 时为它配置的任何端口)并等待解锁 Jenkins 页面出现。

在安装 Jenkins 的机器中查看  /var/lib/jenkins/secrets/initialAdminPassword 目录文件下的内容填入改框即可。  

Customizing Jenkins with plugins

在这里插入图片描述

解锁 Jenkins 后,会出现 Customize Jenkins 页面。作为初始设置的一部分,可以在此处安装任意数量的有用插件。

如果不确定需要什么插件,请选择安装建议的插件。您可以稍后通过 Jenkins 中的 Manage Jenkins > Plugins 页面安装(或删除)其他 Jenkins 插件。

Creating the first administrator user

 使用 admin 账号继续即可,也可以输入想创建的用户详细信息以创建新的管理员用户。

实例配置保存并完成即可。

以上,Jenkins 就安装成功可以正常使用了。

TroubeShooting

安装完成 Jenkins 后,通过 systemctl start Jenkins 启动,报错:

Job for jenkins.service failed because the control process exited with error code. See 
"systemctl status jenkins.service" and "journalctl -xe" for details.

如果直接通过 systemctl status jenkins 查看报错,报错会很模糊,找不到具体原因

[root@master init.d]# systemctl status jenkins
● jenkins.service - Jenkins Continuous Integration Server
   Loaded: loaded (/usr/lib/systemd/system/jenkins.service; disabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Tue 2023-03-21 09:56:52 CST; 35s ago
  Process: 11715 ExecStart=/usr/bin/jenkins (code=exited,status=1/FAILURE)
 Main PID: 11715 (code=exited,status=1/FAILURE)

Mar 21 09:56:52 master.cn systemd[1]: jenkins.service: main process exited,code=exited,status=1/FAILURE
Mar 21 09:56:52 master.cn systemd[1]: Failed to start Jenkins Continuous Integration Server.
Mar 21 09:56:52 master.cn systemd[1]: Unit jenkins.service entered failed state.
Mar 21 09:56:52 master.cn systemd[1]: jenkins.service failed.
Mar 21 09:56:52 master.cn systemd[1]: jenkins.service holdoff time over,scheduling restart.
Mar 21 09:56:52 master.cn systemd[1]: Stopped Jenkins Continuous Integration Server.
Mar 21 09:56:52 master.cn systemd[1]: start request repeated too quickly for jenkins.service
Mar 21 09:56:52 master.cn systemd[1]: Failed to start Jenkins Continuous Integration Server.
Mar 21 09:56:52 master.cn systemd[1]: Unit jenkins.service entered failed state.
Mar 21 09:56:52 master.cn systemd[1]: jenkins.service failed.

我们换一种启动方式查看报错,进入 /etc/init.d,通过 ./jenkins start 启动

[root@master init.d]# cd /etc/init.d

[root@master init.d]# ./jenkins restart
Starting Jenkins Running with Java 19 from /usr/lib/jvm/jdk-19-oracle-x64,which is not yet fully supported.
Run the command again with the --enable-future-java flag to enable preview support for future Java versions.
Supported Java versions are: [11,17]

从这里能看出来,是我们安装的版本不对,支持的Java版本只有:[11,17],所以我们需要通过 rpm -e --nodeps 强制卸载已安装的版本,安装另外的版本。

[root@master init.d]# rpm -qa|grep jdk
java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64
copy-jdk-configs-3.3-11.el7_9.noarch
jdk-19-19.0.2-7.x86_64

[root@master init.d]# rpm -e --nodeps java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64
[root@master init.d]# rpm -e --nodeps jdk-19-19.0.2-7.x86_64

 在官网找到合适版本的链接:Java Downloads | Oracle 中国

[root@master ~]# wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
--2023-03-21 09:44:24--  https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
Resolving download.oracle.com (download.oracle.com)... 23.192.208.88
Connecting to download.oracle.com (download.oracle.com)|23.192.208.88|:443... connected.
HTTP request sent,awaiting response... 200 OK
Length: 181343596 (173M) [application/x-redhat-package-manager]
Saving to: ‘jdk-17_linux-x64_bin.rpm’

100%[==========================================================================================>] 181,343,596 15.2MB/s   in 13s

2023-03-21 09:44:37 (13.3 MB/s) - ‘jdk-17_linux-x64_bin.rpm’ saved [181343596/181343596]

[root@master ~]# rpm -ivh jdk-17_linux-x64_bin.rpm
warning: jdk-17_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature,key ID ec551f03: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk-17-2000:17.0.6-9             ################################# [100%]


[root@master ~]# java -version
java version "17.0.6" 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190,mixed mode,sharing)

然后在 /etc/init.d 下重新启动 Jenkins 即可

[root@master ~]# cd /etc/init.d

[root@master init.d]# ./jenkins start
Starting Jenkins Running from: /usr/share/java/jenkins.war
                                                           [  OK  ]

为 Jenkins 配置 nginx 反向代理

参考:Reverse proxy - Nginx

nginx 配置:只需在监听 80 端口的 server 块中添加一个 location 块

location /jenkins {
    proxy_pass http://127.0.0.1:8080;
    access_log /var/log/nginx/jenkins-access.log;
}

根据以上内容正常安装好 Jenkins 后,我们直接访问 http:ip:8080 即可访问到 Jenkins 网站,即 Jenkins 的根目录直接是端口,没有统一的一个目录。

而配置该 nginx 后,会将 本机 ip/jenkins 的访问代理到 http://ip:8080/jenkins 上,但我们 Jenkins 网站默认首页直接就是端口,并没有/jenkins 这个 url,所以当我们直接访问 ip/jenkins 时就会返回 404。

所以 Jenkins 控制器和反向代理必须使用相同的上下文路径,需要修改 Jenkins 的 URL,即如果 Jenkins 控制器的 URL  为https://www.example.com/jenkins/,那么 --prefix=/jenkins 参数必须包含在 jenkins 控制器命令行参数中。

通过运行 systemctl edit jenkins 并添加以下内容来设置使用 Linux 包时的上下文路径:

systemctl edit jenkins 

[Service]
Environment="JENKINS_PREFIX=/jenkins"

然后重启 Jenkins 服务,可以通过 systemctl status jenkins 查看到我们新加的内容 

这时再通过 http://ip/jenkins 访问即可正常访问~

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...