LVS DR模式搭建、 keepalived + LVS

一、LVS DR模式搭建

三台机器, 三台机器均有公网IP。

调度器(director)
IP:192.168.8.133

real server1(real1)
IP:192.168.8.134

real server2(real2)
IP:192.168.8.135

VIP:192.168.8.100

1、开始搭建
配置director

[root@director ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
#开启端口转发
ipv=/usr/sbin/ipvsadm
vip=192.168.8.100
rs1=192.168.8.134
rs2=192.168.8.135
#注意这里的网卡名字
ifdown ens33
ifup ens33
#在此重启网卡的目的是避免重复设置命令行提供的IP
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
#绑定VIP到dir的虚拟网卡ens33:2
route add -host $vip dev ens33:2
#添加网关
$ipv -C
$ipv -A -t $vip:80 -s wrr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
#设置ipvsadm规则,-g=gateway:使用默认网关(DR模式)
 
[root@director ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 'ens33'。
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
 
[root@director ~]# ip add
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:be:0e:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.133/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.8.100/32 brd 192.168.8.100 scope global ens33:2
       valid_lft forever preferred_lft forever
    inet6 fe80::592f:39cc:1b50:1d07/64 scope link 
       valid_lft forever preferred_lft forever

注: VIP绑定到了ens33网卡上

2、配置real server

real1 real2配置


[root@real1 ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.8.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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
 
[root@real1 ~]# sh /usr/local/sbin/lvs_rs.sh
 
[root@real1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.2     0.0.0.0         UG    100    0        0 ens33
192.168.8.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.8.100   0.0.0.0         255.255.255.255 UH    0      0        0 lo
[root@real1 ~]# ip add
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.8.100/32 brd 192.168.8.100 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever

VIP绑定到了lo上

3、测试

在浏览器访问VIP:192.168.8.100,刷新网页,访问结果由real1、real2交替回复

二、Keepalived LVS

当单独使用LVS负载的时候,如果其中一台 rs 宕机了,就会出问题

所以就需要使用到keepalived+LVS的架构了

keepalived有一个比较好的功能,可以在 rs 宕机时,不再把请求转发过去

三台机器

调度器director:
IP:192.168.8.133;安装Keepalived

real server(real1):
IP:192.168.8.134

real server(real2):
IP:192.168.8.135

VIP:192.168.8.100

1、配置director

[root@director sbin]# yum install -y keepalived
 
自定义Keepalived配置文件:
[root@director ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    #备用服务器上为 BACKUP
    state MASTER
    #绑定vip的网卡为ens33,你的网卡和阿铭的可能不一样,这里需要你改一下
    interface ens33
    virtual_router_id 51
    #备用服务器上为90
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.8.100
    }
}
virtual_server 192.168.8.100 80 {
    #(每隔10秒查询realserver状态)
    delay_loop 10
    #(lvs 算法) 
    lb_algo wlc 
    #算法(DR模式)
    lb_kind DR
    #(同一IP的连接60秒内被分配到同一台realserver)
    persistence_timeout 0 
    #(用TCP协议检查realserver状态)
    protocol TCP 
    real_server 192.168.8.134 80 {
        #(权重) 
        weight 100
        TCP_CHECK {
        #(10秒无响应超时)
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }   
    real_server 192.168.8.135 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }  
}    
 
启动Keepalived服务:
[root@director ~]# systemctl start keepalived
 
查看网卡信息:
[root@director ~]# ip add
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:be:0e:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.133/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.8.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::592f:39cc:1b50:1d07/64 scope link 
       valid_lft forever preferred_lft forever
#虚拟IP(VIP)在ens33网卡上
 
查看ipvsadm规则:
[root@director ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.8.100:80 wlc
  -> 192.168.8.134:80             Route   100    0          0         
  -> 192.168.8.135:80             Route   100    0          0        

2、配置real server

配置路由转发脚本:
[root@real2 ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.8.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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
 
[root@real2 ~]# sh /usr/local/sbin/lvs_rs.sh

配置完成

3、测试

在浏览器访问VIP:192.168.8.100,刷新网页,访问结果由real1、real2交替回复。



扩展

heartbeat和keepalived比较http://blog.csdn.net/yunhua_lee/article/details/9788433
DRBD工作原理和配置 http://502245466.blog.51cto.com/7559397/1298945
mysql+keepalived http://lizhenliang.blog.51cto.com/7876557/1362313
lvs 三种模式详解 http://www.it165.net/admin/html/201401/2248.html
lvs几种算法 http://www.aminglinux.com/bbs/thread-7407-1-1.html
关于arp_ignore和 arp_announce http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
lvs原理相关的 http://blog.csdn.net/pi9nc/article/details/23380589
haproxy+keepalived http://blog.csdn.net/xrt95050/article/details/40926255
nginx、lvs、haproxy比较 http://www.csdn.net/article/2014-07-24/2820837
keepalived中自定义脚本 vrrp_script http://my.oschina.net/hncscwc/blog/158746
lvs dr模式只使用一个公网ip的实现方法 http://storysky.blog.51cto.com/628458/338726

你可能感兴趣的:(LVS DR模式搭建、 keepalived + LVS)