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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS(DR) + Keepalived 实现负载均衡相关的知识,希望对你有一定的参考价值。

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

[[email protected] ~]# yum install -y httpd

[[email protected] ~]# yum install -y httpd


2、创建各自的网页

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

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


3、启动服务

[[email protected] ~]# systemctl start httpd

[[email protected] ~]# systemctl start httpd


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

[[email protected] ~]# 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

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

[[email protected] ~]# chmod a+x VIP.sh 

[[email protected] ~]# bash /root/VIP.sh start

在web2上也执行该操作


在LVS服务器安装 LVS 和 Keepalived

1、安装软件包

[[email protected] ~]# yum install -y ipvsadm keepalived

[[email protected] ~]# yum install -y ipvsadm keepalived


2、编辑 LVS1的 keepalived 配置文

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived
 
global_defs {
   notification_email {
       [email protected]
   }
   notification_email_from [email protected]
   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 配置文件

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived
 
global_defs {
   notification_email {
       [email protected]
   }
   notification_email_from [email protected]
   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、开启路由转发

[[email protected] ~]# echo "1" /proc/sys/net/ipv4/ip_forward

[[email protected] ~]# echo "1" /proc/sys/net/ipv4/ip_forward


5、设置虚拟IP

[[email protected] ~]# ifconfig lo:0 192.168.113.200 broadcast 192.168.113.200 netmask 255.255.255.255 up

[[email protected] ~]# ifconfig lo:0 192.168.113.200 broadcast 192.168.113.200 netmask 255.255.255.255 up


6、启动keepalived服务

[[email protected] ~]# systemctl restart keepalived

[[email protected] ~]# systemctl restart keepalived


三、测试环节


1、查看keepalived状态

[[email protected] ~]# 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

[[email protected] ~]# ip addr sh

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

    inet 192.168.113.200/32 brd 192.168.113.200 scope global lo:0

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> 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


访问网页

[[email protected] ~]# curl 192.168.163.200

web1:192.168.163.159

[[email protected] ~]# curl 192.168.163.200

web2:192.168.163.160


将LVS1的Keepalived停掉

[[email protected] ~]# systemctl stop keepalived


可以看到LVS拿到了VIP

[[email protected] ~]# 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


继续访问网页

[[email protected] ~]# curl 192.168.163.200

web1:192.168.163.159

[[email protected] ~]# curl 192.168.163.200

web2:192.168.163.160



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

本文出自 “xhk777” 博客,请务必保留此出处http://xhk777.blog.51cto.com/13405744/1975026

以上是关于LVS(DR) + Keepalived 实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

LVS/DR + keepalived负载均衡高可用实现

LVS+Keepalived实现负载均衡+高可用性

LVS负载均衡群集DR模式+Keepalived部署!

LVS负载均衡群集DR模式+Keepalived部署!

LVS + keepalived DR 模式

负载均衡之----DR模式下LVS负载均衡 / keepalived后端故障检测 / Lvs+keepalived 实现高可用