LVS DR模式搭建
三台机器
调度器(director)
IP:192.168.3.74
real server1(real1)
IP:192.168.3.76
real server2(real2)
IP:192.168.3.83
VIP:192.168.3.100
开始搭建
配置director
[root@centos7 ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
#开启端口转发
ipv=/usr/sbin/ipvsadm
vip=192.168.3.100
rs1=192.168.3.76
rs2=192.168.3.83
#注意这里的网卡名字
ifdown eno16777736
ifup eno16777736
#在此重启网卡的目的是避免重复设置命令行提供的IP
ifconfig eno16777736:2 $vip broadcast $vip netmask 255.255.255.255 up
#绑定VIP到dir的虚拟网卡eno16777736:2
route add -host $vip dev eno16777736: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@centos7 ~]# sh /usr/local/sbin/lvs_dr.sh
Device 'eno16777736' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
分别在real1、real2配置下面的脚本:
[root@test ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.3.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地址给客户端
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@test ~]# sh /usr/local/sbin/lvs_rs.sh
没任何输出表示执行成功
验证:
访问vip:192.168.3.100,交替变换rs1、rs2内容
keepalived + LVS
当以上的lvs挂了,那服务就停止了,现用到keepalived高可用,将lvs搭建一个集群,保障lvs宕机后会另外一个代替。
keepalived内置了ipvsadm的功能,所以不需要再安装ipvsadm包;当一台主机宕机后,自动移除,正好后自动添加
三台机器分别为:
dir(安装keepalived)192.168.3.74
rs1 192.168.3.76
rs2 192.168.3.83
vip 192.168.3.120
1、[root@centos7 ~]# yum install -y keepalived
2、[root@centos7 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
#备用服务器上为 BACKUP
state MASTER
#绑定vip的网卡
interface eno16777736
virtual_router_id 51
#备用服务器上为90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.3.101
}
}
virtual_server 192.168.3.101 80 {
#(每隔10秒查询realserver状态)
delay_loop 10
#(lvs 算法)
lb_algo rr
#算法(DR模式)
lb_kind DR
#(同一IP的连接60秒内被分配到同一台realserver)
persistence_timeout 0
#(用TCP协议检查realserver状态)
protocol TCP
real_server 192.168.3.76 80 {
#(权重)
weight 1
TCP_CHECK {
#(10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.3.83 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动:
[root@centos7 ~]# systemctl start keepalived
[root@centos7 ~]# 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.3.120:80 wlc
-> 192.168.3.76:80 Route 100 0 0
-> 192.168.3.83:80 Route 100 0 0
配置rs服务:
[root@test ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.3.120
#把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地址给客户端
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
验证:
在浏览器访问VIP:192.168.3.120,刷新网页,访问结果由real1、real2交替回复。