根据Jenkins开源版系统高可用架构设计 - 云+社区 - 腾讯云这篇文章所写的内容,在本地实现了jenkins高可用架构搭建。
准备机器3台,操作系统centos7:
10.10.10.1 haproxy
10.10.10.2 jenkins01
10.10.10.3 jenkins03
一、安装nfs
参考文章Linux 环境下 NFS 服务安装及配置使用_哎_小羊的博客-CSDN博客_nfs安装
1、首先,确认下服务端系统10.3是否已安装 NFS。
$ rpm -qa nfs-utils rpcbind
nfs-utils-1.3.0-0.54.el7.x86_64
rpcbind-0.2.0-38.el7.x86_64
注意:这里我已经安装完毕,若为空,则说明未安装。
2、然后,安装 NFS 服务
# 服务端 10.3
$ yum install -y nfs-utils rpcbind
# 客户端 10.2
$ yum install -y nfs-utils
3、NFS 配置及使用
我们在服务端创建一个共享目录 /data/share ,作为客户端挂载的远端入口,然后设置权限。
$ mkdir -p /data/share
$ chmod 666 /data/share
4、然后,修改 NFS 配置文件 /etc/exports
$ vim /etc/exports
/data/share 10.10.10.0/24(rw,sync,insecure,no_subtree_check,no_root_squash)
5、接下来,我们先启动 RPC 服务。
$ service rpcbind start
# 或者使用如下命令亦可
$ /bin/systemctl start rpcbind.service
# 查看 NFS 服务项 rpc 服务器注册的端口列表
$ rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
注意:此时我们还没有启动 NFS 服务,只监听了 111 端口,接着我们来启动 NFS 服务,再来看下注册的端口列表。
# 启动 NFS 服务
$ service nfs start
# 或者使用如下命令亦可
/bin/systemctl start nfs.service
# 启动 NFS 服务后 rpc 服务已经启用了对 NFS 的端口映射列表
# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 33745 status
100024 1 tcp 36980 status
100005 1 udp 20048 mountd
100005 1 tcp 20048 mountd
100005 2 udp 20048 mountd
100005 2 tcp 20048 mountd
100005 3 udp 20048 mountd
100005 3 tcp 20048 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 3 udp 2049 nfs_acl
100021 1 udp 38960 nlockmgr
100021 3 udp 38960 nlockmgr
100021 4 udp 38960 nlockmgr
100021 1 tcp 38362 nlockmgr
100021 3 tcp 38362 nlockmgr
100021 4 tcp 38362 nlockmgr
我们发现,启动了 NFS 服务后,rpc 注册的端口列表明显增多。OK 现在服务端都启动起来了,在服务端看下是否正确加载了设置的 /etc/exports 配置。
$ showmount -e localhost
Export list for localhost:
/data/share 10.10.10.0/24
6、NFS 测试
最后,在另一台 Linux 虚拟机上测试一下,是否能够正确挂载吧。首先,我们可以在客户端查看下 NFS 服务端 (上边服务端 IP 为:10.10.10.3) 设置可共享的目录信息。
$ showmount -e 10.10.10.3
Export list for 10.10.10.3:
/data/share 10.10.10.0/24
然后,在客户端创建挂在目录 /share
$ mkdir -p /share
最后,挂载远端目录到本地 /share 目录。
$ mount 10.10.10.3:/data/share /share
$ df -h | grep 10.10.10.3
Filesystem Size Used Avail Use% Mounted on
10.10.10.3:/data/share 27G 11G 17G 40% /share
可以看到,可以正确将远端 NFS 目录挂载到本地。注意:挂载点 /share 目录必须已经存在,而且目录中没有文件或子目录。
最后,我们在 NFS 服务端 /data/share 目录下创建一个文件,看下客户端是否能够正确读取并修改。
# 服务端10.3写入
$ echo "This is NFS server." > /data/share/nfs.txt
# ll /data/share/
total 4
-rw-r--r-- 1 root root 20 Nov 5 16:49 nfs.txt
# 客户端10.2读取
$ ll /share/
total 4
-rw-r--r-- 1 root root 20 Nov 5 16:49 nfs.txt
$ cat /share/nfs.txt
This is NFS server.
# 客户端10.2写入
$ echo "This is NFS client." >> /share/nfs.txt
# 服务端10.3读取
$ cat /data/share/nfs.txt
This is NFS server.
This is NFS client.
二:在服务器10.2,10.3安装jenkins
1、下载tomcat tar -xvf apache-tomcat-9.0.31.tar.gz,下载jenkins.war
mkdir /data/share/jenkins
# 解压tomcat
tar -xvf apache-tomcat-9.0.31.tar.gz
# 把jenkins.war放进tomcat
mv jenkins.war apache-tomcat-9.0.31/webapps
# 指定环境变量,有两种方法
# a、指定全局变量
export JENKINS_HOME=/data/share/jenkins
export PATH=$PATH:$JENKINS_HOME
# b、在tomcat设置
vim apache-tomcat-9.0.31/conf/context.xml
...
# 增加以下配置,优先获取该配置路径。
# 启动jenkins
apache-tomcat-9.0.31/bin/startup.sh
启动jenkins后,更改jenkins服务器地址为HA地址
三、在服务器10.1安装haproxy
访问 HAProxy - The Reliable, High Performance TCP/HTTP Load Balancer,下载版本2.0.25
#编译安装
tar zxf haproxy-2.0.25.tar.gz
cd haproxy-2.0.25
make TARGET=linux-glibc PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cp ./examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
useradd -r haproxy
mkdir /etc/haproxy
# 创建/etc/haproxy/haproxy.cfg
global
maxconn 20000
ulimit-n 16384
log 127.0.0.1 local0
uid 200
node http
gid 200
chroot /var/empty
nbproc 4
daemon
listen admin_stats #开启haproxy监控界面
bind *:8001 #绑定8001端口
mode http
option httplog
stats enable #开启统计
stats refresh 5s
stats uri /haproxy?stats #监控界面url为:http://ip:80/haproxy/stats
stats auth admin:123456
stats realm welcome\ Haproxy
stats admin if TRUE
frontend jenkins_proxy
bind *:80
log global
option redispatch
option forwardfor
option http-server-close
maxconn 8000
timeout client 30s
default_backend jenkins_proxy
reqadd X-Forwarded-Proto:\ http
backend jenkins_proxy
balance roundrobin
option httpchk GET /jenkins/login
server jenkins1 10.10.10.2:8080 check inter 1000 rise 1 fall 1 weight 30
server jenkins2 10.10.10.3:8080 check inter 1000 rise 1 fall 1 weight 30
# 启动服务
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
Haproxy 管理后台
http://10.10.10.1:8001/haproxy?stats 账号admin 密码:123456
这时,访问http:10.10.10.1就能访问到jekins。
实验总结:为了实现高可用,两台(10.2,10.3)jenkins 应该都启动,可是发现都启动时,创建job会出现不明错误,就把其中一台停掉,等到如果访问有问题,再启动另外一台。