准备web服务器并使用脚本绑定VIP至web服务器lo网卡
#准备两台后端RS主机
[root@rs1 ~]# cat lvs_dr_rs.sh
#!/bin/bash
vip=10.0.0.100
mask='255.255.255.255'
dev=lo:1
rpm -q httpd $> /dev/null || yum -y install httpd &> /dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html
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@rs1 ~]# bash lvs_dr_rs.sh start
The httpd Server is Ready!
The RS Server is Ready!
[root@rs1 ~]# 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 10.0.0.100/32 scope global lo:1
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:ff:d7:7d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.40/24 brd 10.0.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::fbf3:9a70:f1bb:88ed/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::63d5:8547:bef8:27bc/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::2097:51de:521a:1cdb/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
[root@rs1 ~]#
#测试直接访问两台RS
[root@client ~]# curl 10.0.0.40
<h1>rs1.example.com</h1>
[root@client ~]# curl 10.0.0.50
<h1>rs2.example.com</h1>
[root@client ~]#
配置keepalived
#ka1节点的配置
[root@ka1 ~]# cat /etc/keepalived/conf.d/m1.conf
vrrp_instance m1 {
state MASTER
interface ens33
virtual_router_id 66
priority 100
advert_int 1
preempt_delay 10
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.100/24 dev ens33 label ens33:1
}
}
virtual_server 10.0.0.100 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 10.0.0.40 80 {
weight 1
HTTP_GET { #应用层检测
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 10.0.0.50 80 {
weight 1
TCP_CHECK { #另一台主机使用TCP检测
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@ka1 ~]#
#ka2节点的配置,配置和ka1基本相同,只需修改
route_id ka2.example.com
state BACKUP
priority 80
访问测试结果
[root@client ~]# curl 10.0.0.100
<h1>rs2.example.com</h1>
[root@client ~]# curl 10.0.0.100
<h1>rs1.example.com</h1>
[root@ka1 ~]# yum install -y ipvsadm
[root@ka1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
-> 10.0.0.40:80 Route 1 0 2
-> 10.0.0.50:80 Route 1 0 2
[root@ka1 ~]#
模拟故障
#第一台rs1故障,自动切换rs2
[root@rs1 ~]# chmod 0 /var/www/html/index.html
[root@client ~]# curl 10.0.0.100
<h1>rs2.example.com</h1>
[root@client ~]# curl 10.0.0.100
<h1>rs2.example.com</h1>
[root@client ~]#
[root@ka1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
-> 10.0.0.50:80 Route 1 0 2
[root@ka1 ~]#
#后端rs服务器都故障,启动Sorry Server
[root@ka1 ~]# yum install -y nginx
[root@ka1 ~]# systemctl start nginx
[root@ka1 ~]# echo 'Sorry Server on ka1' > /usr/share/nginx/html/index.html
[root@ka1 ~]# cat /usr/share/nginx/html/index.html
Sorry Server on ka1
[root@rs2 ~]# systemctl stop httpd
[root@client ~]# curl 10.0.0.100
Sorry Server on ka1
[root@ka1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
-> 127.0.0.1:80 Route 1 0 1
[root@ka1 ~]#
#ka1故障,自动切换至ka2
[root@ka1 ~]# killall keepalived
[root@client ~]# curl 10.0.0.100
Sorry Server on ka2
#恢复都有后端RS
[root@rs1 ~]# chmod 644 /var/www/html/index.html
[root@rs2 ~]# systemctl start httpd
[root@client ~]# curl 10.0.0.100
<h1>rs2.example.com</h1>
[root@client ~]# curl 10.0.0.100
<h1>rs1.example.com</h1>
#恢复ka1服务器,又抢回原先的VIP
[root@ka1 ~]# systemctl start keepalived
[root@ka1 ~]# hostname -I
10.0.0.10 10.0.0.100
[root@ka2 ~]# hostname -I
10.0.0.20