Keepalived+LVS实战案例( 双主架构实现WEB和MariaDB负载均衡及高可用)

莫道桑榆晚,为霞尚满天。这篇文章主要讲述Keepalived+LVS实战案例: 双主架构实现WEB和MariaDB负载均衡及高可用相关的知识,希望能为你提供帮助。
?概要说明:双主是指两台Keepalived / LVS服务器,互为冗余,在其中一台上(IP:192.168.250.18)以master方式 VIP 192.168.250.100 代理转发后端WEB服务,同时这台以slave方式代理转发mariadb的服务请求;在另外一台上(IP:192.168.250.28)以master方式 VIP 192.168.250.200 代理转发mariadb的服务请求,同时这台以slave方式代理转发WEB;当任何一台发生故障都会将全部转发请求转移到另外一台上,这样充分地利用了运算资源,也提供了高可靠性。?
?本文以比较简要的方式进行描述,详细解读可以参考前文《?Keepalived+LVS实战案例(一): 单主架构实现WEB负载均衡及高可用?》?
1.拓扑结构及主机环境

# 七台主机
12台web服务器 :
主机名:WebServer-IP17
CentOS 7.9
IP:192.168.250.17

主机名:WebServer-IP27
CentOS 7.9
IP:192.168.250.27

22台 MariaDB 数据库服务器 :
主机名:MariaDB-IP37
CentOS 7.9
IP:192.168.250.37

主机名:MariaDB-IP47
CentOS 7.9
IP:192.168.250.47


32台 keepalived 服务器 :
主机名: KA-IP18
CentOS 8.4
IP:192.168.250.18/24
Keepalived v2.1.5 (07/13,2020)

主机名: KA-IP28
CentOS 8.4
IP:192.168.250.28/24
Keepalived v2.1.5 (07/13,2020)

41台client主机 :
主机名:Client-IP172-8
CentOS 8.4
IP:172.16.0.8/24NAT成192.168.250.254 访问192.168.250.X网段

2.配置后端WEB服务器及数据库RS服务?简要说明:按照架构图,需要IP17 / IP27 两台WEB服务器,安装好httpd及定义好首页内容,并按照LVS-DR对后端RS服务器的配置要求(关闭ARP宣告和接受;绑定VIP地址等)完成配置。同样需要准备两台 IP37 /IP47 两台 MariaDB 数据库服务器。?
2.1配置WEB服务器
# 基础环境包括CentOS操作系统的优化、防火墙关闭、同步时间等都要做好,我们按照规划的架构图对四台服务器进行分组并重新命名
# 修改服务器名称
[root@centos79 < sub> ]# hostnamectl set-hostname WebServer-IP17
[root@centos79 < /sub> ]# exit
# 修改NTP服务器地址为阿里云的NTP启用时钟同步服务
[root@webserver-ip17 < sub> ]#timedatectl set-timezone Asia/Shanghai
[root@webserver-ip17 < /sub> ]#sed -i /^server/cserver ntp.aliyun.com iburst /etc/chrony.conf
[root@webserver-ip17 < sub> ]#systemctl enable --nowchronyd.service
# 安装Apache
[root@webserver-ip17 < /sub> ]#yum -y install httpd
# 定义web主页文件
[root@webserver-ip17 < sub> ]#yum -y install httpd; hostname > /var/www/html/indexTmp.html; hostname -I > > /var/www/html/indexTmp.html; cat /var/www/html/indexTmp.html | xargs > /var/www/html/index.html; rm -rf /var/www/html/indexTmp.html; systemctlenable --now httpd

# 启动Apache服务,并开机自启
[root@webserver-ip17 < /sub> ]#systemctlenable --now httpd

# 验证
[root@webserver-ip17 < sub> ]# curl 192.168.250.17
webserver-ip17 192.168.250.17
[root@webserver-ip17 < /sub> ]#

################################################################################
#### 同样的方式完成另外一台 webserver-ip27 192.168.250.27 的Apache的配置和调试

####################################################################################
#### 用脚本修改于LVS相关的配置lvs_dr_rs.sh 内容,在VS-Code内修改好上传到两个WEB-RS服务器上运行

#!/bin/bash
vip=192.168.250.100
mask=255.255.255.255
dev=lo:1

case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask
echo "The RS Server is Ready!"
; ;
stop)
ifconfig $dev down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "The RS Server is Canceled!"
; ;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
; ;
esac

## IP192.168.250.17上的配置过程
[root@webserver-ip17 < sub> ]# rz
rz waiting to receive.
Starting zmodem transfer.Press Ctrl+C to cancel.
Transferring lvs_dr_rs.sh...
100%728 bytes728 bytes/sec 00:00:010 Errors

[root@webserver-ip17 < /sub> ]# bash
[root@webserver-ip17 < sub> ]# bash lvs_dr_rs.sh
Usage: lvs_dr_rs.sh start|stop
[root@webserver-ip17 < /sub> ]# bash lvs_dr_rs.sh start
The RS Server is Ready!
# 查看绑定是否成功
[root@webserver-ip17 < sub> ]# ip a


## 同样的方式在 IP192.168.250.27上的配置
[root@webserver-ip27 < /sub> ]# bash lvs_dr_rs.sh start
The RS Server is Ready!
# 查看绑定是否成功
[root@webserver-ip27 ~]# ip a

2.2配置MariaDB数据库服务器
#### 准备好基础的环境、按照拓扑修改主机名、
[root@centos79 < sub> ]# hostnamectl set-hostname MariaDB-IP37
[root@centos79 < /sub> ]# exit
# 安装mariadb,并启动、开机自启、授权
[root@mariadb-ip37 < sub> ]# yum -y install mariadb-server
[root@mariadb-ip37 < /sub> ]# mysql -e grant all on *.* to test@"%.%.%.%" identified by "shone8888"
[root@mariadb-ip37 < sub> ]# systemctl enable --now mariadb.service

# 用脚本修改和LVS相关的内容,这个VIP是 192.168.250.200WEB绑定的是192.168.250.100这就是我们说的双主
[root@mariadb-ip37 < /sub> ]#vim lvs_dr_rs.sh
#!/bin/bash
vip=192.168.250.200
mask=255.255.255.255
dev=lo:1

case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask
echo "The RS Server is Ready!"
; ;
stop)
ifconfig $dev down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "The RS Server is Canceled!"
; ;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
; ;
esac

[root@mariadb-ip37 < sub> ]# bash lvs_dr_rs.sh start
The RS Server is Ready!
[root@mariadb-ip37 < /sub> ]# ip a
1: lo: < LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.250.200/32 scope global lo:1
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: < BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:a3:ef:ca brd ff:ff:ff:ff:ff:ff
inet 192.168.250.37/24 brd 192.168.250.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fea3:efca/64 scope link
valid_lft forever preferred_lft forever

# 在IP192.168.250.47 上重复上面的步骤,完成LVS-DR的lo-IP地址设置,并关闭arp宣告和接收

# 配置完成后再本机上测试访问
[root@CentOS84-IP172-08 ]#mysql -utest -pshone8888 -h192.168.250.37 -e select @@hostname
+--------------------------+
| @@hostname|
+--------------------------+
| mariadb-ip192.168.250.37 |
+--------------------------+
[root@CentOS84-IP172-08 ]#mysql -utest -pshone8888 -h192.168.250.47 -e select @@hostname
+--------------------------+
| @@hostname|
+--------------------------+
| mariadb-ip192.168.250.47 |
+--------------------------+
[root@CentOS84-IP172-08 ]#

2.3keepalived 基础配置
【Keepalived+LVS实战案例( 双主架构实现WEB和MariaDB负载均衡及高可用)】?简要说明:配置好keepalived 地全局配置、vrrp的配置。本次我们沿用的是单播方式,如果需要启用多播方式,只需要注释掉单播的配置,开启多播的配置行(在下面的配置文件中也有,只需要去掉 # 注释行头)。?
2.3.1Keepalived-IP18的配置
[root@Keepalived-IP18 ]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs
notification_email
root@shone.cn

notification_email_from admin@shone.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA-IP18
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
#vrrp_mcast_group4 224.0.0.18



vrrp_instance VI_IP100
state MASTER
interface eth0
virtual_router_id 100
priority 100
advert_int 1
authentication
auth_type PASS
auth_pass shone888

virtual_ipaddress
192.168.250.100 dev eth0 label eth0:1

unicast_src_ip 192.168.250.18
unicast_peer
192.168.250.28



vrrp_instance VI_IP200
state BACKUP
interface eth0
virtual_router_id 200
priority 80
advert_int 1
authentication
auth_type PASS
auth_pass shone888

virtual_ipaddress
192.168.250.200 dev eth0 label eth0:2

unicast_src_ip 192.168.250.18
unicast_peer
192.168.250.28






[root@Keepalived-IP18 ]#systemctl restart keepalived
[root@Keepalived-IP18 ]#

2.3.2Keepalived-IP28的配置
[root@Keepalived-IP28 ]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs
notification_email
root@shone.cn

notification_email_from admin@shone.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id KA-IP28
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
#vrrp_mcast_group4 224.0.0.18



vrrp_instance VI_IP100
state

    推荐阅读