keepalived + LVS DR 构建高可用负载均衡
原博客很详细并正确,重新记录只为方便查找和搭建
一、环境简述
real server1: 192.168.50.207 -----------httpd服务器
real server2: 192.168.50.235 -----------httpd服务器
director server1:192.168.50.232--------lvs dr服务器+ keepalived服务器
director server2:192.168.50.231 -----lvs dr服务器+ keepalived服务器
测试服务器: 192.168.50.208
二、搭建
1 real server1和real server2服务器设置:
分别执行以下脚本
[ lvs_dir]#cat lvs_dr_rs.sh
#!/bin/bash
vip=192.168.50.252
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
注: httpd的安装和设置省略,参考前面的博客
- director server1服务器设置:
[ keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 111
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.50.252
}
}
virtual_server 192.168.50.252 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.50.235 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.50.207 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }
}
[[email protected] keepalived]# echo 1 > /proc/sys/net/ipv4/ip_forward
director server2的搭建类似
只需要将keepalived的配置文件中的内容稍作修改
state MASTER -> state BACKUP
priority 100 -> priority 90- 启动keepalved服务(director server1和director server2)
[[email protected] keepalived]# systemctl start keepalived
三、测试
1 测试负载均衡(208上):
[[email protected] ~]# curl 192.168.50.252:80
235
[[email protected] ~]# curl 192.168.50.252:80
207
[[email protected] ~]# curl 192.168.50.252:80
235
[[email protected] ~]# curl 192.168.50.252:80
207
2.测试real server的单点故障问题:
关掉235上的httpd服务,然后在208上测试:
[[email protected] ~]# curl 192.168.50.252:80
207
[[email protected] ~]# curl 192.168.50.252:80
207
3.测试director的高可用性
关掉231服务器的keepalived,或者关掉网卡等,再在208上测试
[[email protected] ~]# curl 192.168.50.252:80
235
[[email protected] ~]# curl 192.168.50.252:80
207
[[email protected] ~]# curl 192.168.50.252:80
235
[[email protected] ~]# curl 192.168.50.252:80
207