1、环境准备
172.18.62.38 haproxy+keeplived master
172.18.62.39 haproxy+keeplived backup
172.18.62.40 apache
172.18.62.41 apache
172.18.62.42 vip
2、启动容器
[root@iZuf612i9bshiuw3zzlfe9Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
289ccced5587 kad "/usr/sbin/init" 38 minutes ago Up 38 minutes keepalived_apache2_1
44e30a4a733c kad "/usr/sbin/init" 38 minutes ago Up 38 minutes 0.0.0.0:80->80/tcp keepalived_haproxy-master_1
d85f5ec88462 kad "/usr/sbin/init" 38 minutes ago Up 38 minutes keepalived_apache1_1
72f8eef52e47 kad "/usr/sbin/init" 38 minutes ago Up 38 minutes 0.0.0.0:90->80/tcp keepalived_haproxy-slave_1
6f27741df3e2 kad "/usr/sbin/init" 38 minutes ago Up 38 minutes 0.0.0.0:100->80/tcp keepalived_vip_1
3、安装服务apache
#更新软件包
apt update
apt upgrade -y
#安装apache
apt install apache2 -y
#方便测试,修改首页
vim /var/www/html/index.html
#启动apache服务
/etc/init.d/apache2 start
访问如下:
root@apache1:~# curl http://172.18.62.40
<h1>harpxoy-keepalived-apache1 172.18.62.40</h1>
root@apache2:~# curl http://172.18.62.41
<h1>haproxy-keepalived-apache2 172.18.62.41</h1>
4、安装haproxy
#更新安装包
apt update
apt upgrade -y
#安装服务
apt install haproxy -y
5、安装keepalived
#更新安装包
apt update
apt upgrade -y
#安装服务
apt install keepalived -y
6、配置haproxy负载均衡
vim /etc/haproxy/haproxy.cfg
#master
#----------global---------------------
global
maxconn 2000
nbproc 1
user haproxy
group haproxy
log 127.0.0.1 local0 info
daemon
pidfile /var/run/haproxy.pid
#-------------default---------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#-------------frontend------------------
frontend test
bind *:80
default_backend test_server
#------------backend-------------------
backend test_server
server apache1 172.18.62.40:80 check
server apache2 172.18.62.41:80 check
#slave
#----------global---------------------
global
maxconn 2000
nbproc 1
user haproxy
group haproxy
log 127.0.0.1 local0 info
daemon
pidfile /var/run/haproxy.pid
#-------------default---------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#-------------frontend------------------
frontend test
bind *:80
default_backend test_server
#------------backend-------------------
backend test_server
server apache1 172.18.62.40:80 check
server apache2 172.18.62.41:80 check
7、配置keepalived
vim /etc/keepalived/keepalived.conf
#master
global_defs {
router_id 172.18.62.38
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance vrrptest {
state master
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass ufsoft
}
track_script {
check_haproxy
}
virtual_ipaddress {
172.18.62.43
}
}
#backup
global_defs {
router_id 172.18.62.39
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance vrrptest {
state master
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass ufsoft
}
track_script {
check_haproxy
}
virtual_ipaddress {
172.18.62.43
}
}
#启动
/etc/init.d/keepalived start
8、检查haproxy脚本
if [ "$(ps -ef | grep "haproxy:master process"| grep -v grep )" == "" ];then
/etc/init.d/keepalived stop
else
echo "haproxy is running"
fi
9、访问测试
#master
http://Ip:80
#backup
http://IP:90
#VIP
http://IP:100
10、故障测试
#停掉master
root@haproxy-master:~# /etc/init.d/keepalived stop
Stopping keepalived (via systemctl): keepalived.service.
root@haproxy-master:~# /etc/init.d/keepalived status
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2021-05-21 03:15:53 CEST; 5s ago
Process: 1555 ExecStart=/usr/sbin/keepalived --dont-fork $DAEMON_ARGS (code=exited, status=0/SUCCESS)
Main PID: 1555 (code=exited, status=0/SUCCESS)
http://IP:100依旧正常访问
#停掉主haproxy
root@haproxy-master:~# /etc/init.d/haproxy stop
Stopping haproxy (via systemctl): haproxy.service.
root@haproxy-master:~# /etc/init.d/haproxy status
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2021-05-21 03:58:10 CEST; 1min 5s ago
Docs: man:haproxy(1)
file:/usr/share/doc/haproxy/configuration.txt.gz
Process: 1478 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS (code=exited, status=0/SUCCESS)
Process: 1479 ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS (code=exited, status=0/SUCCESS)
Main PID: 1479 (code=exited, status=0/SUCCESS)
http://IP:100依旧正常访问