一、设置路由服务器(IP:192.168.1.4)
1、路由服务器开启路由转发
vi /etc/sysctl.conf
net.ipv4.ip_forward=1
2、路由生效:
sysctl -p
3、返回如下,则表示生效成功
net.ipv4.ip_forward = 1
二、配置RS1服务器(RIP:192.168.1.7)
1、设置网关为路由服务器IP地址
vi /etc/sysconfig/network-scripts/ifcfg-ens33
GATEWAY=192.168.1.4
2、安装httpd包
yum install httpd -y
3、开启httpd服务,并设置为开机自启
systemctl start httpd
systemctl enable httpd
4、新建测试页面
echo 192.168.1.7---- RS1 > /var/www/html/index.html
5、本地测试能否访问
[10:55:47 root@rs1 ~]#curl 192.168.1.7
192.168.1.7---- RS1
6、执行以下脚本
bash RS.sh start
脚本内容如下:适当修改可以使用
#/bin/bash
#Date:2021-5-18
vip=192.168.1.100
mask=255.255.255.255
#The subnet mask is not important here
dev=lo:1
test=192.168.1.100
#rpm q- http &> /dev/null || yum -y install httpd &>/dev/null
#service httpd start &> /dev/null && echo "The httpd Server is Ready!"
#echo "<h1>$test `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 #broadcast $vip up
#route add -host $vip dev $dev
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 Caneled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
三、配置RS2服务器(IP:192.168.1.8)
1、设置网关为路由服务器IP地址
vi /etc/sysconfig/network-scripts/ifcfg-ens33
GATEWAY=192.168.1.4
2、安装httpd包
yum install httpd -y
3、开启httpd服务,并设置为开机自启
systemctl start httpd
systemctl enable httpd
4、新建测试页面
echo 192.168.1.8____RS2 > /var/www/html/index.html
5、本地测试能否访问
[10:58:09 root@rs2 ~]#curl 192.168.1.8
192.168.1.8____RS2
6、执行以下脚本
bash RS.sh start
脚本内容如下:适当修改可以使用
#/bin/bash
#Date:2021-5-18
vip=192.168.1.100
mask=255.255.255.255
#The subnet mask is not important here
dev=lo:1
test=192.168.1.100
#rpm q- http &> /dev/null || yum -y install httpd &>/dev/null
#service httpd start &> /dev/null && echo "The httpd Server is Ready!"
#echo "<h1>$test `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 #broadcast $vip up
#route add -host $vip dev $dev
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 Caneled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
四、配置LVS1服务器
1、安装keepalived服务
yum install keepalived -y
2、进入keepalived目录
cd /etc/keepalived/
3、备份keepalived配置文件
cp keepalived.conf{,.bak}
4、安装httpd服务
yum install httpd -y
5、开启httpd服务,并设置为开机自启动
systemctl start httpd
systemctl enable httpd
6、生成道歉页面
echo 'The server is down temporarily, please contact the administrator' > /var/www/html/index.html
(用于后端RS服务器全部宕机的时候,由LVS服务器接替对外服务,提供对外道歉页面)
7、测试访问本机是否能访问道歉页面
[11:18:55 root@ka1 keepalived]#curl 192.168.1.5
The server is down temporarily, please contact the administrator
8、安装ipvsadm服务,不需要手动通过ipvsadm设置,但可以使用这个查看规则
yum install ipvsadm -y
五、配置LVS2服务器
1、步骤同第四步-略
六、修改主LVS配置文件(192.168.1.5)
vim keepalived.conf
配置文件如下:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 88
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.100/24 dev ens33 label ens33:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.7 80 {
weight 1
HTTP_GET
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.8 80 {
weight 1
HTTP_GET
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
七、配置从LVS服务(192.168.1.6)
vim keepalived.conf
配置文件如下:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 88
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.100/24 dev ens33 label ens33:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.7 80 {
weight 1
HTTP_GET
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.8 80 {
weight 1
HTTP_GET
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
八、测试访问VIP地址(VIP:192.168.1.100)
[12:08:27 root@route ~]#while :; do curl 192.168.1.100; sleep 0.5; done;
192.168.1.7---- RS1
192.168.1.8____RS2
192.168.1.7---- RS1
192.168.1.8____RS2
192.168.1.7---- RS1
目前192.168.1.5这台服务器优先级是100,所有现在VIP在这台机器上,
九、停止优先级高的这台主服务器的keepalived服务(IP:192.168.1.5)
systemctl stop keepalived
十、查看优先级低的这台服务器IP地址(IP:192.168.1.6)
[11:46:37 root@ka2 keepalived]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c4:30:ea brd ff:ff:ff:ff:ff:ff
inet 192.168.1.6/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.100/24 scope global secondary ens33:1
valid_lft forever preferred_lft forever
inet6 fe80::8951:f7f2:a66d:106/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::6b0a:dbe0:253c:dd85/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
当优先级高于自己的服务器宕机的时候,优先级低的服务器会自动或去到浮动IP,接替主服务器来提供服务,同时当主服务器正常停止了对外服务,会自动清除ipvsadm规则,异常则不会,如物理断网,当主服务器连接上网络,会自动来提供服务
十一、将后端RS1服务器宕机
systemctl stop httpd
1、测试访问
[12:16:48 root@route ~]#while :; do curl 192.168.1.100; sleep 0.5; done;
192.168.1.8____RS2
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
192.168.1.8____RS2
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
192.168.1.8____RS2
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
从这里可以看出,当后端一台RS服务器宕机之后,keepalived需要略微的等待时间才会将宕机的服务地址从规则中剔除,
十二、将后端另外一台RS服务器宕机
systemctl stop httpd
1、测试访问
[12:21:01 root@route ~]#while :; do curl 192.168.1.100; sleep 1; done;
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
curl: (7) Failed connect to 192.168.1.100:80; Connection refused
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrato
由此可以看出,当后端所有RS服务器无法对外提供服务,LVS服务器会对外提供一个道歉页面,一样也需要略微的等待时间
十三、开启一台服务器
systemctl start httpd
1、测试访问
[12:24:17 root@route ~]#while :; do curl 192.168.1.100; sleep 1; done;
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
The server is down temporarily, please contact the administrator
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
192.168.1.8____RS2
由此可以看出,当后端服务器恢复之后,keepalived 对外提供的道歉页面会自动停止,同时后端对外业务,可以正常访问
以上简单演示了keepalived服务实现了LVS的高可用的步骤,当其中任何一台LVS服务器宕机或RS宕机,其他服务器会自动接替服务,中间有略微的延迟,但延迟的幅度不大。
------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------- 博客均为个人笔记,无所追求,仅供参考~~~