传统云环境下的CI/CD操作手册(三)配置keepalived和haproxy
更新:HHH   时间:2023-1-7


准备yum源
cd /etc/yum.repos.d/
rm -rf *
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
yum repolist

配置keepalived
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER #另一台服务器是SLAVE
interface ens33 #默认是eth0,一定要修改
virtual_router_id 51
priority 100 #另一台服务器优先级要低于100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.19
}
}
关掉master,测试slave是否能接替master
systemctl stop keepalived
在slave上查看
tail -f /var/log/messages
Jul 24 14:09:37 jenkins-slave Keepalived_vrrp[3340]: VRRP_Instance(VI_1) Entering MASTER STATE

配置haproxy
cd /usr/local/src/
将haproxy压缩包上传到该目录
yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y
tar xvf haproxy-1.8.13.tar.gz
cd haproxy-1.8.13/
make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
haproxy -v

准备启动脚本
vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /r
un/haproxy.pidExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

准备haproxy配置文件
mkdir -p /var/lib/haproxy #配置文件中需要该目录
mkdir -p /etc/haproxy/conf
cd /etc/haproxy/
vim haproxy.cfg
global
maxconn 100000
chroot /usr/local/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin #动态调整后端
uid 99
gid 99
daemon
nbproc 4
cpu-map 1 0
cpu-map 2 1
cpu-map 3 2
cpu-map 4 3
pidfile /usr/local/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth admin:mypassword
http://192.168.1.19:9999/haproxy-status

准备业务配置文件
cd conf/
vim tomcat-server.cfg
listen web_port
bind 192.168.1.19:80 #VIP
mode http
log global
server 192.168.1.17 192.168.1.17:8080 check inter 3000 fall 2 rise 5
server 192.168.1.18 192.168.1.18:8080 check inter 3000 fall 2 rise 5

sysctl -a |grep nonlocal
sysctl -a |grep forward
vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
sysctl -p
systemctl start haproxy
systemctl enable haproxy
systemctl status haproxy
ss -tnl |grep 80
LISTEN 0 128 192.168.1.19:80 :
http://192.168.1.19/web01/

返回云计算教程...