• VM虚拟机上 实现CentOS 6.X下部署LVS(DR)+keepalived实现高性能高可用负载均衡


    一、简介

    LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。

    目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);十种调度算法(rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。

    Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。

    LVS+Keepalived能实现的功能:利用LVS控制器主备模式避免单点故障以及自动删除故障WEB服务器结点并当它恢复后再自动添加到群集中。

    拓扑图:

    1 准备工作

    a) 虚拟机上安装CentOS6.x操作系统,参考一下博客内容

    http://www.cnblogs.com/seesea125/archive/2012/02/25/2368255.html

    b)将上一步安装上的虚拟机clone 3分,如图所示 

       

    2个LVS服务器,一个作为Master,一个作为Backup 2个真实web服务器

    c)设置各个系统的网络

    c-1) win7操作系统,作为外部访问LVS服务器的设备,设置VMnet1的IP地址,如图设置

     

    c-2)在VM软件中分别设置各个CentOS6系统的网卡

    c-2-1)LVS MASTER 服务器的网卡设置,需要1块网卡连接,如图所示

       

    c-2-2)LVS Backup 服务器的网卡设置,需要1块网卡连接,如图所示

        

    c-2-3)设置2台真实web服务器的网卡连接,需要一块网卡连接,如图所示

     

    c-3)通过ifconfig命令,设置各个CentOS系统的网路设置

    c-3-1)LVS MASTER 服务器的网路设置,分别设置eth0为10.0.0.136 255.255.255.0,eth0:1为10.0.0.148 255.255.255.255  

    c-3-2)LVS BACKUP 服务器的网路设置,分别设置eth0为10.0.0.157 255.255.255.0,eth0:1为10.0.0.148 255.255.255.255

    c-3-3)2台真实web 服务器的网路设置,分别设置eth0为10.0.0.137 255.255.255.0和10.0.0.139 255.255.255.0

    通过以上步骤的话,通过ping命令检验是否所有的系统之间都可以联通了。

    2  安装必要的软件

    a) LVS MASTER和Backup 服务器

    a-1)ipvsadm 和keepalived的安装

    安装之前最好查看一下系统中是否已经安装了这2个软件

    命令录下 yum -y install ipvsadm 和 yum -y install keepalived

    a-2) 开启路由转发

    [root@CentOS-LVS_MASTER ~]# vim /etc/sysctl.conf 
    net.ipv4.ip_forward = 1
    [root@CentOS-LVS_MASTER ~]# sysctl -p
     

    a-3) 配置keepalived,如图所示

    Master端的配置:cat /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         root@linux.tang.chao               #设置报警邮件地址,可以设置多个,每行一个。
         mchina_tang@qq.com                 #需开启本机的sendmail服务
       }
       notification_email_from keepalived@localhost         #设置邮件的发送地址
       smtp_server 127.0.0.1                                #设置smtp server地址
       smtp_connect_timeout 30                              #设置连接smtp server的超时时间
       router_id LVS_DEVEL                                  #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息
    }
    
    vrrp_instance VI_1 {
        state MASTER              #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器
        interface eth0            #指定HA监测网络的接口
        virtual_router_id 51      #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的
        priority 100              #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级
        advert_int 1              #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
        authentication {          #设置验证类型和密码
            auth_type PASS        #设置验证类型,主要有PASS和AH两种
            auth_pass 1111        #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
        }
        virtual_ipaddress {       #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
            10.0.0.148
        }
    }
    
    virtual_server 10.0.0.148 80 {      #设置虚拟服务器,需要指定虚拟IP地址和服务端口,IP与端口之间用空格隔开
        delay_loop 6                    #设置运行情况检查时间,单位是秒
        lb_algo rr                      #设置负载调度算法,这里设置为rr,即轮询算法
        lb_kind DR                      #设置LVS实现负载均衡的机制,有NAT、TUN、DR三个模式可选
        persistence_timeout 50          #会话保持时间,单位是秒。这个选项对动态网页是非常有用的,为集群系统中的session共享提供了一个很好的解决方案。
                                        #有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话的保持时间。
                                        #需要注意的是,这个会话保持时间是最大无响应超时时间,也就是说,用户在操作动态页面时,如果50秒内没有执行任何操作,
                                        #那么接下来的操作会被分发到另外的节点,但是如果用户一直在操作动态页面,则不受50秒的时间限制
        protocol TCP                    #指定转发协议类型,有TCP和UDP两种
    
        real_server 10.0.0.137 80 {     #配置服务节点1,需要指定real server的真实IP地址和端口,IP与端口之间用空格隔开
            weight 3                    #配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值大小可以为不同性能的服务器
                                        #分配不同的负载,可以为性能高的服务器设置较高的权值,而为性能较低的服务器设置相对较低的权值,这样才能合理地利用和分配系统资源
            TCP_CHECK {                 #realserver的状态检测设置部分,单位是秒
                connect_timeout 10      #表示3秒无响应超时
                nb_get_retry 3          #表示重试次数
                delay_before_retry 3    #表示重试间隔
                connect_port 80
            }
        }
        real_server 10.0.0.139 80 {
            weight 3
            TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
            }
        }
    }

      

      Backup端的配置:cat /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    global_defs {
       notification_email {
         root@linux.tang.chao
         mchina_tang@qq.com
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 51
        priority 99
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.148
        }
    }
    
    virtual_server 10.0.0.148 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        persistence_timeout 50
        protocol TCP
    
        real_server 10.0.0.137 80 {
            weight 3
            TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
            }
        }
        real_server 10.0.0.139 80 {
            weight 3
            TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
            }
        }
    }

    a-4) 将keepalived设置为服务

    chkconfig keepalived on 

    b) 在2台真实的web服务器分别配置HTTP 

    [root@WEB1 ~]# yum -y install httpd
    [root@WEB1 ~]# cd /var/www/html/
    [root@WEB1 html]# cat index.html 
    <h1>WEB1/10.0.0.137</h1>
    [root@WEB1 html]# /etc/init.d/httpd start
    另一台机器配置一样,过程略。

    3、在两台Web Server上执行realserver.sh脚本,为lo:0绑定VIP地址10.0.0.148、抑制ARP广播。

    [root@web1 ~]# cat realserver.sh

    #!/bin/bash
    #description: Config realserver
    
    VIP=10.0.0.148
     
    /etc/rc.d/init.d/functions
     
    case "$1" in
    start)
           /sbin/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)
           /sbin/ifconfig lo:0 down
           /sbin/route del $VIP >/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
     
    exit 0

    [root@web1 ~]# sh realserver.sh start

    4、分别在LVS-MASTER、LVS-BACKUP上执行service keepalived start启动keepalived就可实现负载均衡及高可用集群了;

    [root@LVS-MASTER keepalived]# service keepalived start

    [root@LVS-MASTER keepalived]# ipvsadm -L
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.0.0.148:http rr persistent 50
      -> 10.0.0.139:http              Route   3      0          0         
      -> 10.0.0.137:http              Route   3      0          0         
    [root@LVS-MASTER keepalived]#

    查看日志/var/log/messages

    [root@LVS-MASTER keepalived]# tail -n 30/var/log/messages
    Apr 19 03:59:58 LVS-MASTER avahi-daemon[2845]: Withdrawing address record for10.0.0.148 on eth0.
    Apr 19 04:00:19 LVS-MASTER Keepalived: Starting Keepalived v1.2.1 (04/17,2012)
    Apr 19 04:00:19 LVS-MASTER Keepalived: Starting Healthcheck child process,pid=5960
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.136 added
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.159 added
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Registering Kernelnetlink reflector
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Registering Kernelnetlink command channel
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Configuration is using :12257 Bytes
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Using LinkWatch kernelnetlink reflector...
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Activating healtcheckerfor service [10.0.0.137:80]
    Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Activating healtcheckerfor service [10.0.0.139:80]
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Netlink reflector reports IP10.0.0.136 added
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Netlink reflector reports IP10.0.0.159 added
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Registering Kernel netlinkreflector
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Registering Kernel netlink commandchannel
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Registering gratutious ARP sharedchannel
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Opening file'/etc/keepalived/keepalived.conf'.
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Configuration is using : 36698Bytes
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Using LinkWatch kernel netlinkreflector...
    Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: VRRP sockpool: [ifindex(2),proto(112), fd(11,12)]
    Apr 19 04:00:19 LVS-MASTER Keepalived: Starting VRRP child process, pid=5961
    Apr 19 04:00:20 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Transition toMASTER STATE
    Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTERSTATE
    Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) settingprotocol VIPs.
    Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148
    Apr 19 04:00:21 LVS-MASTER Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.136 added
    Apr 19 04:00:21 LVS-MASTER avahi-daemon[2845]: Registering new address recordfor 10.0.0.148 on eth0.
    Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: Netlink reflector reports IP10.0.0.136 added
    Apr 19 04:00:26 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148
    [root@LVS-MASTER keepalived]#

    四、测试

    接下来做高可用性测试&故障切换测试......

    ####高可用性测试####

    模拟故障,将LVS-MASTER上的keepalived服务停掉,然后观察LVS-BACKUP上的日志,信息如下

    [root@LVS-BACKUP keepalived]# tail -f /var/log/messages
    Apr 19 03:33:04 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1)Transition to MASTER STATE
    Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTERSTATE
    Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) settingprotocol VIPs.
    Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148
    Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: Netlink reflector reports IP10.0.0.157 added
    Apr 19 03:33:05 LVS-BACKUP Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.157 added
    Apr 19 03:33:05 LVS-BACKUP avahi-daemon[2825]: Registering new address recordfor 10.0.0.148 on eth0.
    Apr 19 03:33:10 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148

    从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源,最后将虚拟IP绑定在etho设备上。

    将LVS-MASTER 上的keepalived服务开启后,LVS-BACKUP的日志状态。

    Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
    Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1)Entering BACKUP STATE
    Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
    Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: Netlink reflector reports IP 10.0.0.157 removed
    Apr 19 03:34:02 LVS-BACKUP Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.157 removed
    Apr 19 03:34:02 LVS-BACKUP avahi-daemon[2825]: Withdrawing address record for 10.0.0.148 on eth0.

    从日志可知,备机在检测到主机重新恢复正常后,释放了虚拟IP资源重新成为BACKUP角色

    ####故障切换测试####

    故障切换是测试当某个节点出现故障后,Keepalived监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行。

    将web2节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下

    Apr 19 03:35:04 LVS-MASTER Keepalived_healthcheckers:TCP connection to [10.0.0.139:80] failed!!!
    Apr 19 03:35:04 LVS-MASTER Keepalived_healthcheckers:Removing service [10.0.0.139:80] from VS [10.0.0.148:80]
    Apr 19 03:35:04 LVS-MASTER Keepalived_healthcheckers: Remote SMTP server [127.0.0.1:25] connected.

    [root@LVS-MASTER keepalived]# ipvsadm -L
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.0.0.148:http rr persistent 50
      -> 10.0.0.137:http              Route   3      0          0         
    [root@LVS-MASTER keepalived]#

    从以上可以看出,Keepalived监控模块检测到10.0.0.139这台主机出现故障后,将些web2从集群系统中剔除掉了。 此时访问http://10.0.0.148只能看到web1了)

    keepalived

    重新启动web2节点的服务,日志信息如下:

    Apr 19 03:38:22 LVS-MASTER Keepalived_healthcheckers:TCP connection to [10.0.0.139:80] success.
    Apr 19 03:38:22 LVS-MASTER Keepalived_healthcheckers:Adding service [10.0.0.139:80] to VS [10.0.0.148:80]
    Apr 19 03:38:22 LVS-MASTER Keepalived_healthcheckers: Remote SMTP server [127.0.0.1:25] connected.

    [root@LVS-MASTERkeepalived]# ipvsadm -L
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.0.0.148:http rr persistent 50
      -> 10.0.0.139:http              Route   3      0          0         
      -> 10.0.0.137:http              Route   3      0          0         
    [root@LVS-MASTER keepalived]#

    Keepalived监控模块检测到10.0.0.139这台主机恢复正常后,又将此节点加入集群系统中,再次访问就可以访问到web2页面了)

    lvs

     参考自:http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html

  • 相关阅读:
    NOIP2019 Emiya 家今天的饭 [提高组]
    Codeforces Round #663 (Div. 2) 题解
    树上差分入门
    Codeforces Round #664 (Div. 2) 题解
    [USACO19OPEN]Snakes
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/guoyongrong/p/3311933.html
Copyright © 2020-2023  润新知