centos7.8主备keepalived+haproxy环境搭建以及systemd守护服务进程

 

参考:https://my.oschina.net/u/4415887/blog/3325099

centos7.8安装haproxy主备

1.

#!/bin/bash

yum install gcc libtermcap-devel ncurses-devel libevent-devel readline-devel -y

wget http://www.lua.org/ftp/lua-5.3.5.tar.gz

tar xf lua-5.3.5.tar.gz -C /usr/local/src/

cd /usr/local/src/lua-5.3.5/

make linux test

src/lua -v

cd

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 #安装编译haproxy所需要的环境

wget http://www.haproxy.org/download/2.0/src/haproxy-2.0.12.tar.gz

tar xf haproxy-2.0.12.tar.gz -C /usr/local/src/

cd /usr/local/src/haproxy-2.0.12/

make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src LUA_LIB=/usr/local/src/lua-5.3.5/src PREFIX=/usr/local/haproxy

--

ARCH=x86_64 #CPU架构 T

ARGET=linux-glibc #通用linux内核

USE_PCRE=1 #PCRE支持正则表达式,用于用户请求的uri

USE_OPENSSL=1 #https,证书

USE_ZLIB=1 #开启压缩

USE_SYSTEMD=1 #使用systemd启动haproxy主进程

USE_CPU_AFFINITY=1 #CPU亲和性,让haproxy指定的进程工作在指定的CPU核心上

USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src LUA_LIB=/usr/local/src/lua-5.3.5/src #开启lua,及lua和lua库所在路径 PREFIX=/apps/haproxy #指定安装路径

--

make install PREFIX=/usr/local/haproxy

--

守护进程:

vim /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

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 /var/lib/haproxy/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 haadmin:q1w2e3r4ys

#测试阶段可以先不加真实后端

listen swift-80
bind 0.0.0.0:80
server proxy_1 100.131.200.50:8080 maxconn 100 check weight 100
server proxy_2 100.131.200.51:8080 maxconn 100 check weight 100
server proxy_3 100.131.200.52:8080 maxconn 100 check weight 100
server proxy_4 100.131.200.53:8080 maxconn 100 check weight 100
server proxy_5 100.131.200.54:8080 maxconn 100 check weight 100
server proxy_6 100.131.200.55:8080 maxconn 100 check weight 100
server proxy_7 100.131.200.56:8080 maxconn 100 check weight 100
server proxy_8 100.131.200.57:8080 maxconn 100 check weight 100
server proxy_9 100.131.200.58:8080 maxconn 100 check weight 100
server proxy_10 100.131.200.59:8080 maxconn 100 check weight 100
server proxy_11 100.131.200.60:8080 maxconn 100 check weight 100
server proxy_12 100.131.200.61:8080 maxconn 100 check weight 100
mode http
timeout connect 60s
timeout client 60s
timeout server 60s
balance roundrobin

 

hapoyx -c -f /etc/haproxy/haproxy.cfg

systemctl restart haproxy

/etc/init.d/haproxy reload

2#安装配置keepalived

yum install keepalived -y

 

vi /etc/keepalived/check_haproxy.sh

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export $PATH
counts=$(ps -ef|grep -w "haproxy"|grep -v grep|wc -l)
if [ "${counts}" = "0" ]; then
systemctl restart keepalived
sleep 2
counts=$(ps -ef|grep -w "haproxy"|grep -v grep|wc -l)
if [ "${counts}" = "0" ]; then
systemctl stop keepalived
fi
fi

///////////////////////////////////////////


vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
! By zisefeizhu

vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight -5
fall 2
rise 1
}

vrrp_instance swift {
state MASTER
interface ens224
virtual_router_id 1
priority 150
advert_int 1
unicast_src_ip 100.131.200.221
unicast_peer {
100.131.200.222
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
100.131.200.223 dev ens224 label ens224:1
}
track_script {
check_haproxy
}
}


! Configuration File for keepalived
! By zisefeizhu

vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight -5
fall 2
rise 1
}

vrrp_instance swift {
state BACKUP
interface ens224
virtual_router_id 1
priority 70
advert_int 1
unicast_src_ip 100.131.200.222
unicast_peer {
100.131.200.221
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
100.131.200.223 dev ens224 label ens224:1
}
track_script {
check_haproxy
}
}

 

 

----------

一些常用的调试命令:

/etc/sysconfig/haproxy --vv

which haproxyx
haproxy -vv

haproxy -v查看版本

systemctl status haproxy
journalctl -b -0 -u haproxy --查看haproxy的日志

posted @ 2021-11-08 16:30  frantzz  阅读(54)  评论(0编辑  收藏  举报