扩展 :
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
18.11 LVS DR模式搭建
1. 改回hao2 hao3机器ens33网卡中网关(.2)
[root@hao-02 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
[root@hao-03 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
2. 重启网卡:
[root@hao-02 ~]# systemctl restart network
[root@hao-03 ~]# systemctl restart network
hao1 机器(dir)上操作:
1. hao1机器(dir)上,创建lvs_dr.sh脚本 :
[root@hao-01 ~]# vim /usr/local/sbin/lvs_dr.sh
添加内容:
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.223.200
rs1=192.168.223.143
rs2=192.168.223.144
#注意这里的网卡名字(ens33 上面设置了vip虚拟网卡ip)
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 wrr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
2. 执行lvs_dr.sh脚本 :
[root@hao-01 ~]# sh /usr/local/sbin/lvs_dr.sh
hao2 机器(rr)上操作:
1. hao2机器(rr)上,创建lvs_rs.sh脚本 :
[root@hao-02 ~]# vim /usr/local/sbin/lvs_rs.sh
添加内容 :
#/bin/bash
vip=192.168.223.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
2. 执行lvs_rs.sh脚本 :
[root@hao-02 ~]# sh /usr/local/sbin/lvs_rs.sh
hao3 机器(rr)上操作:
1. hao3机器(rr)上,创建lvs_rs.sh脚本 :
[root@hao-03 ~]# vim /usr/local/sbin/lvs_rs.sh
添加内容:
#/bin/bash
vip=192.168.223.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
2. 执行lvs_rs.sh脚本 :
[root@hao-03 ~]# sh /usr/local/sbin/lvs_rs.sh
测试:
游览器访问vip:192.168.223.200
18.12 keepalived lvs
hao1 机器(dir)上操作:
yum install -y keepalived
1. 编辑keepalived.conf配置文件 :
[root@hao-01 ~]# 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 haomima
}
virtual_ipaddress {
192.168.223.200
}
}
virtual_server 192.168.223.200 80 {
#(每隔10秒查询realserver状态)
delay_loop 10
#(lvs 算法)
lb_algo wlc
#(DR模式)
lb_kind DR
#(同一IP的连接0秒内被分配到同一台realserver)
persistence_timeout 0
#(用TCP协议检查realserver状态)
protocol TCP
real_server 192.168.223.143 80 {
#(权重)
weight 100
TCP_CHECK {
#(10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.223.144 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
2. 启动keepalived :
[root@hao-01 ~]# systemctl start keepalived
3. hao1机器(dir)上,先关闭keepalived :
[root@hao-01 ~]# systemctl stop keepalived
4. hao2机器(rr1)上,先关闭nginx,再启动nginx :
[root@hao-02 ~]# systemctl stop nginx
[root@hao-02 ~]# systemctl start nginx
5. hao3机器(rr2)上,先关闭nginx,再启动nginx :
先关闭nginx,再启动nginx:
[root@hao-03 ~]# systemctl stop nginx
[root@hao-03 ~]# systemctl start nginx
6. 再启动keepalived:
[root@hao-01 ~]# systemctl start keepalived
7. (哪个机器nginx挂了,对应的ip就不会在这里显示了)
[root@hao-01 ~]# ipvsadm -ln
8. hao2 hao3机器nginx关闭了挂了,这hao2和hao3对应ip都不再显示了 :
[root@hao-02 ~]# systemctl stop nginx
[root@hao-03 ~]# systemctl stop nginx
[root@hao-01 ~]# ipvsadm -ln