环境准备
IP | 配置 | VIP | |
node1 | 192.168.134.170 | LVS+Keepalived | 192.168.134.100 |
node3 | 192.168.134.172 | LVS+Keepalived | 192.168.134.100 |
node2 | 192.168.134.171 | 做web服务器使用 | |
node4 | 192.168.134.173 | 做web服务器使用 |
1、准备node1与node3环境(安装LVS与Keepalived)==>由于只是简单的模拟测试,故环境安装使用yum即可。(LVS使用DR模式)
yum install ipvsadm
2、准备web服务器环境(两台)
yum install httpd -y
3、写测试实例(web服务器上)
echo "web test page ,ip is `hostname -I`" > /var/www/html/index.html
systemctl restart httpd
4、写脚本准备web环境
cat /etc/init.d/lvs_rs#!/bin/sh
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR-RIP
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.95.10
. /etc/rc.d/init.d/functions
start() {
PID=`ifconfig | grep lo:10 | wc -l`
if [ $PID -ne 0 ];
then
echo "The LVS-DR-RIP Server is already running !"
else
/sbin/ifconfig lo:10 $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev lo:10
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/eth0/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/eth0/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
/bin/touch $LOCK
echo "starting LVS-DR-RIP server is ok !"
fi
}
stop() {
/sbin/route del -host $VIP dev lo:10
/sbin/ifconfig lo:10 down >/dev/null
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/eth0/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/eth0/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
rm -rf $LOCK
echo "stopping LVS-DR-RIP server is ok !"
}
status() {
if [ -e $LOCK ];
then
echo "The LVS-DR-RIP Server is already running !"
else
echo "The LVS-DR-RIP Server is not running !"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $1 {start|stop|restart|status}"
exit 1
esac
exit 0chomd +x /etc/init.d/lvs_rs # 添加执行权限
/etc/init.d/lvs_rs start # 启动脚本
route -n # 查看路由
5、修改keeplaived服务器配置文件(实现健康检查和故障切换功能)
! Configuration File for keepalived # 主配置文件global_defs { # 全局配置notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVEL1
}vrrp_instance VI_1 { # 实例一state MASTER # 指定为主节点# nopreempt 非抢占模式interface ens33 # 指定VIP绑定网卡virtual_router_id 51 # 指定实例的idpriority 100 # 指定主节点的优先级,主节点要高于备节点advert_int 1 # 指定发送vrrp通告的间隔authentication {auth_type PASS # 指定认证方式auth_pass 1111 # 指定使用密码}virtual_ipaddress { # 指定vip(需要与真实ip在同一个网段)192.168.134.100}
}# 以下是做LVS配置
virtual_server 192.168.134.100 80 { # 指定所绑定的VIP及端口delay_loop 6 # 健康检查的时间间隔lb_algo rr # 指定LVS调度算法lb_kind DR # 指定LVS模式,此处我们选取的是DR模式,还有NAT和TUN模式protocol TCP # 指定四层协议为TCPreal_server 192.168.134.171 80 { # 指定真实服务器地址weight 1 # 设定真实主机的权重TCP_CHECK { # 选中健康检查方式为tcp-checkconnect_timeout 3 # 连接超时时间retry 3 # 重试次数delay_before_retry 3 # 在重试之前延迟多少秒connect_port 80 # 指定连接端口}}real_server 192.168.134.173 80 { # 指定另一台真实服务器weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3connect_port 80}}
}备节点配置
修改以下三处即可router_id LVS_DEVEL2state BACKUP priority 80
6、重启Keepalived
systemctl restart keepalived.service # 先启动主,再启动备ip a|grep 192.168.134.100 # 此时只有主机有VIP,备机,没有
7、测试(找客户端写以下测试来访问,得到如下结果即正确)
for ((i=1;i<=6;i++)); do curl http://192.168.134.100; done结果:
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
8、测试健康检查与故障转移
健康检查:
停掉一台web服务器
systemctl stop httpd此时客户端访问结果: 由于时间设置,需要一段时间后才可以出结果,虽然访问结果少了一台web服务器,但是可以被另一台顶上,同样不影响访问。
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1 故障转移:
停掉一台keepalived服务器
systemctl stop keepalived此时VIP转移至另一台
[root@localhost keepalived]# ip a|grep 192.168.134.100inet 192.168.134.100/32 scope global ens33
客户端访问结果正常(1:1轮询)
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1 在重新启动主keepalived后,vip又回到主机,访问仍旧正常。