分发器上操作
>#分发器上的网卡配置要去掉网关
[root@wsl3 network-scripts]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=172.16.230.200
rs1=172.16.230.131
rs2=172.16.230.132
#注意这里的网卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
[root@wsl3 network-scripts]# sh /usr/local/sbin/lvs_dr.sh
两台rs上操作
[root@wsl1 network-scripts]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=172.16.230.200
#把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@wsl1 network-scripts]# sh /usr/local/sbin/lvs_rs.sh
测试
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 84923 0 --:--:-- --:--:-- --:--:-- 87714
Welcome to MASTER!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 340k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to BACKUP!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 335k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to MASTER!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 344k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to BACKUP!
[root@wsl3 network-scripts]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.230.200:80 rr
-> 172.16.230.131:80 Route 1 3 1
-> 172.16.230.132:80 Route 1 2 1
keepalived lvs
[root@wsl3 ~]# > /etc/keepalived/keepalived.conf
[root@wsl3 ~]# 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 {
172.16.230.200
}
}
virtual_server 172.16.230.200 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 172.16.230.131 80 {
#(权重)
weight 100
TCP_CHECK {
#(10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 172.16.230.132 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@wsl3 ~]# systemctl start keepalived
[root@wsl3 ~]# ps aux|grep keep
root 5947 0.0 0.1 118676 1396 ? Ss 22:26 0:00 /usr/sbin/keepalived -D
root 5948 0.0 0.3 129608 3332 ? S 22:26 0:00 /usr/sbin/keepalived -D
root 5949 0.0 0.2 129476 2612 ? S 22:26 0:00 /usr/sbin/keepalived -D
root 5951 0.0 0.0 112720 984 pts/0 S+ 22:26 0:00 grep --color=auto keep
[root@wsl3 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.230.200:80 wlc persistent 60
-> 172.16.230.131:80 Route 100 0 0
-> 172.16.230.132:80 Route 100 0 0
测试
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 357k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to BACKUP!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 339k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to MASTER!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 379k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to BACKUP!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 381k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to MASTER!
WSL:~ Martin.Wang$ curl 172.16.230.200 |head -4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614 100 614 0 0 333k 0 --:--:-- --:--:-- --:--:-- 599k
Welcome to BACKUP!
[root@wsl3 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.230.200:80 rr
-> 172.16.230.131:80 Route 100 0 4
-> 172.16.230.132:80 Route 100 0 5
#关闭131的nginx
[root@wsl3 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.230.200:80 rr
-> 172.16.230.132:80 Route 100 0 4
扩展
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