LVS(DR) + Keepalived 实现负载均衡高可用


一、为什么要使 用负载均衡技术?

1、  系统高可用性

2、  系统可扩展性

3、  负载均衡能力

    LVS+keepalived能很好的实现以上的要求,LVS提 供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后 很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的 配置文件,并能实现无缝配置变更!

 

二、LVS+Keepalived介绍

1、  LVS

    LVS是一个开源的软件,可以实现LINUX平台下的简单负载均衡。LVS是Linux Virtual Server的缩写,意思是Linux虚拟服务器。目前有三种IP负 载均衡技术(VS/NAT、VS/TUN和VS/DR);八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。

2、  keepalived

    Keepalived 是运行在lvs 之上,它的主要功能是实现真实机的故障隔离及负载均衡器间的失败 切换,提高系统的可用性。


一、环境说明

    本次试验使用的是2台前端代理服务器(LVS) 加上 keepalived,后端为了方便,使用apache来发布网页,达到测试的目的。


操作系统:centos7 64位

软件源:阿里云

2台服务器(LVS1、LVS2)安装 keepalived 和安装LVS 来反向代理

2台服务器(web1、web2)安装 apache 来提供服务

服务器的防火墙和selinux全部关闭


LVS1  IP:192.168.163.158

LVS2  IP:192.168.163.161

web1  IP:192.168.163.159

web2  IP:192.168.163.160

虚拟IP: 192.168.163.200


拓扑图如下:

二、环境安装


首先配置web服务器

1、首先为后端的2台web服务器安装apache

[root@web1 ~]# yum install -y httpd

[root@web2 ~]# yum install -y httpd


2、创建各自的网页

[root@web1 ~]# echo "web1:192.168.163.159" > /var/www/html/index.html

[root@web2 ~]# echo "web2:192.168.163.160" > /var/www/html/index.html


3、启动服务

[root@web1 ~]# systemctl start httpd

[root@web2 ~]# systemctl start httpd


4、编写脚本,为web服务器设置虚拟IP以及设置抑制ARP包

[root@web1 ~]# vim /root/VIP.sh 

#!/bin/bash
VIP=192.168.163.200   #这个为你要设置的VIP
 
case $1 in
start)
    ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
    /sbin/route add -host $VIP dev 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
    sysctl -p >/dev/null 2>&1
    echo "RealServer Start OK"
    ;;
stop)
    ifconfig lo:0 down
    route del $IP >/dev/null 2>&1
    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/all/arp_ignore
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
    echo "RealServer Stoped"
    ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac

添加执行权限并执行该脚本

[root@web1 ~]# chmod a+x VIP.sh 

[root@web1 ~]# bash /root/VIP.sh start

在web2上也执行该操作


在LVS服务器安装 LVS 和 Keepalived

1、安装软件包

[root@LVS1 ~]# yum install -y ipvsadm keepalived

[root@LVS2 ~]# yum install -y ipvsadm keepalived


2、编辑 LVS1的 keepalived 配置文

[root@LVS1 ~]# vim /etc/keepalived/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 LVS
   vrrp_mcast_group4 224.0.100.19
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 51
    priority 100
	nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass xhk
    }
    virtual_ipaddress {
        192.168.163.200 dev ens32
    }
}
 
virtual_server 192.168.163.200 80 {       # 定义转移ip端口80的集群服务
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP
	sorry_server 127.0.0.1 80 
    real_server 192.168.163.159 80 {      # 定义集群服务包含的RS 1
         weight 1                     # 权重为1
     HTTP_GET {                   # 定义RS1的健康状态检测
         url {
           path /
           status_code 200
         }
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 1
      }
    }
    real_server 192.168.163.160 80 {      # 定义集群服务包含的RS 2
         weight 1                      # 权重为1
     HTTP_GET {                    # 定义RS2的健康状态检测
         url {
           path /
           status_code 200
         }
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 1
      }
    }
}


3、编辑 LVS2的 keepalived 配置文件

[root@LVS2 ~]# vim /etc/keepalived/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 LVS
   vrrp_mcast_group4 224.0.100.19
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 51
    priority 99
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass xhk
    }
    virtual_ipaddress {
        192.168.163.200 dev ens32
    }
}
 
virtual_server 192.168.163.200 80 {       # 定义转移ip端口80的集群服务
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP
    sorry_server 127.0.0.1 80 
    real_server 192.168.163.159 80 {      # 定义集群服务包含的RS 1
         weight 1                     # 权重为1
     HTTP_GET {                   # 定义RS1的健康状态检测
         url {
           path /
           status_code 200
         }
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 1
      }
    }
    real_server 192.168.163.160 80 {      # 定义集群服务包含的RS 2
         weight 1                      # 权重为1
     HTTP_GET {                    # 定义RS2的健康状态检测
         url {
           path /
           status_code 200
         }
         connect_timeout 1
         nb_get_retry 3
         delay_before_retry 1
      }
    }
}

2个配置文件不同的只是优先级的不同,分出MASTER,并且设置了不抢占模式,为的是防止业务切换国语频繁!


4、开启路由转发

[root@LVS1 ~]# echo "1" /proc/sys/net/ipv4/ip_forward

[root@LVS2 ~]# echo "1" /proc/sys/net/ipv4/ip_forward


5、设置虚拟IP

[root@LVS1 ~]# ifconfig lo:0 192.168.163.200 broadcast 192.168.163.200 netmask 255.255.255.255 up

[root@LVS2 ~]# ifconfig lo:0 192.168.163.200 broadcast 192.168.163.200 netmask 255.255.255.255 up


6、启动keepalived服务

[root@LVS1 ~]# systemctl restart keepalived

[root@LVS2 ~]# systemctl restart keepalived


三、测试环节


1、查看keepalived状态

[root@LVS1 ~]# systemctl status keepalived

● keepalived.service - LVS and VRRP High Availability Monitor

   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)

   Active: active (running) since Sun 2017-10-22 01:26:32 EDT; 3min 48s ago

  Process: 2656 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)

 Main PID: 2657 (keepalived)

   CGroup: /system.slice/keepalived.service

           ├─2657 /usr/sbin/keepalived -D

           ├─2658 /usr/sbin/keepalived -D

           └─2659 /usr/sbin/keepalived -D

Oct 22 01:26:37 LVS1 Keepalived_vrrp[2659]: Sending gratuitous ARP on ens32 for 192.168.163.200


查看虚拟IP

[root@LVS1 ~]# ip addr sh

1: lo: 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

    inet 192.168.163.200/32 brd 192.168.163.200 scope global lo:0

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: ens32: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:5f:46:a6 brd ff:ff:ff:ff:ff:ff

    inet 192.168.163.158/24 brd 192.168.163.255 scope global dynamic ens32

       valid_lft 1654sec preferred_lft 1654sec

    inet 192.168.163.200/32 scope global ens32

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:fe5f:46a6/64 scope link 

       valid_lft forever preferred_lft forever


访问网页

[root@client ~]# curl 192.168.163.200

web1:192.168.163.159

[root@client ~]# curl 192.168.163.200

web2:192.168.163.160


将LVS1的Keepalived停掉


[root@LVS1 ~]# systemctl stop keepalived


可以看到LVS拿到了VIP

[root@LVS2 ~]# systemctl status keepalived

● keepalived.service - LVS and VRRP High Availability Monitor

   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)

   Active: active (running) since Sun 2017-10-22 01:26:38 EDT; 6min ago

  Process: 2553 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)

 Main PID: 2555 (keepalived)

   CGroup: /system.slice/keepalived.service

           ├─2555 /usr/sbin/keepalived -D

           ├─2556 /usr/sbin/keepalived -D

           └─2557 /usr/sbin/keepalived -D


Oct 22 01:32:50 LVS2 Keepalived_vrrp[2557]: Sending gratuitous ARP on ens32 for 192.168.163.200


继续访问网页

[root@client ~]# curl 192.168.163.200

web1:192.168.163.159

[root@client ~]# curl 192.168.163.200

web2:192.168.163.160



测试成功!!!!!!!!!!!!!!!!!!!