为什么80%的码农都做不了架构师?>>>
LVS DR模式搭建
准备工作:三台机器
分发器,也叫调度器(简写为dir):192.168.248.128
rs1 :192.168.248.129
rs2 : 192.168.248.130
vip : 192.168.248.200
1.dir上编辑脚本文件**/usr/local/sbin/lvs_dr.sh**,文件内容如下:
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward #打开端口转发
ipv=/usr/sbin/ipvsadm
vip=192.168.248.200
rs1=192.168.248.132
rs2=192.168.248.133
#注意这里的网卡名字
#绑定vip
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
2.执行脚本
[root@yolks-001 ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5)
3.rs机器也需要编辑配置文件,添加脚本文件**/usr/local/sbin/lvs_rs.sh**,内容如下:
#/bin/bash
vip=192.168.248.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
4.测试查看活动状态
keepalived + LVS
完整架构需要两台服务器(角色为dir)分别安装keepalived软件,目的是实现高可用,但keepalived本身也有负载均衡的功能,所以本次实验可以只安装一台keepalived
keepalived内置了ipvsadm的功能,所以不需要再安装ipvsadm包,也不用编写和执行那个lvs_dir的脚本
三台机器分别为:
dir(安装keepalived)192.168.248.128
rs1 192.168.248.129
rs2 192.168.248.130
vip 192.168.248.200
1.编辑keepalived配置文件/etc/keepalived/keepalived.conf,内容如下:
vrrp_instance VI_1 {#备用服务器上为 BACKUPstate MASTER#绑定vip的网卡为ens33,你的网卡和阿铭的可能不一样,这里需要你改一下interface ens33virtual_router_id 51#备用服务器上为90priority 100advert_int 1authentication {auth_type PASSauth_pass aminglinux}virtual_ipaddress {192.168.248.200}
}
virtual_server 192.168.248.200 80 {#(每隔10秒查询realserver状态)delay_loop 10#(lvs 算法)lb_algo wlc#(DR模式)lb_kind DR#(同一IP的连接60秒内被分配到同一台realserver)persistence_timeout 60#(用TCP协议检查realserver状态)protocol TCPreal_server 192.168.248.129 80 {#(权重)weight 100TCP_CHECK {#(10秒无响应超时)connect_timeout 10nb_get_retry 3delay_before_retry 3connect_port 80}}real_server 192.168.248.130 80 {weight 100TCP_CHECK {connect_timeout 10nb_get_retry 3delay_before_retry 3connect_port 80}}
}
2.dir上重启keepalived
systemctl stop keepalived
systemctl start keepalived
3.查看keepalived规则
[root@yolks-001 ~]# 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.248.200:80 wlc persistent 60-> 192.168.248.129:80 Route 100 0 0 -> 192.168.248.130:80 Route 100 0 0
4.停止掉rs3机器的nginx
systemctl stop nginx
5.dir机器再次查看keepalived规则:已经少了停掉的rs2机器的规则
[root@yolks-001 ~]# 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.248.200:80 wlc persistent 60-> 192.168.248.129:80 Route 100 0 0
拓展
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