Keepalived+Lvs高可用高性能负载配置

环境准备

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 0

chomd +x /etc/init.d/lvs_rs # 添加执行权限
/etc/init.d/lvs_rs start # 启动脚本
route -n # 查看路由

5、修改keeplaived服务器配置文件(实现健康检查和故障切换功能)

! Configuration File for keepalived  # 主配置文件


global_defs {  # 全局配置
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL1
}

vrrp_instance VI_1 {  # 实例一
    state MASTER  # 指定为主节点
    # nopreempt 非抢占模式
    interface ens33  # 指定VIP绑定网卡
    virtual_router_id 51  # 指定实例的id
    priority 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  # 指定四层协议为TCP

    real_server 192.168.134.171 80 {  # 指定真实服务器地址
        weight 1  # 设定真实主机的权重
        TCP_CHECK {  # 选中健康检查方式为tcp-check
            connect_timeout 3  # 连接超时时间
            retry 3  # 重试次数
            delay_before_retry 3  # 在重试之前延迟多少秒
            connect_port 80  # 指定连接端口
        }
    }


    real_server 192.168.134.173 80 {  # 指定另一台真实服务器
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}


备节点配置
修改以下三处即可
 router_id LVS_DEVEL2
   state 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.100
    inet 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又回到主机,访问仍旧正常。

你可能感兴趣的:(lvs,服务器,linux)