本文共 3934 字,大约阅读时间需要 13 分钟。
一、LVS DR模式搭建
修改两台rs1和rs2机器的网关为之前的网关:192.168.238.2#vi /etc/sysconfig/network-scripts/ifcfg-ens33#systemctl restart networkdir机器上也需要编写一个脚本,加入如下内容:[root@linux-01 ~]# vim /usr/local/sbin/lvs_dr.sh#! /bin/bashecho 1 > /proc/sys/net/ipv4/ip_forwardipv=/usr/sbin/ipvsadmvip=192.168.238.200rs1=192.168.238.130rs2=192.168.238.131#注意这里的网卡名字ifdown ens33ifup ens33ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 uproute 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//脚本解释:echo 1 > /proc/sys/net/ipv4/ip_forward 打开端口转发;ipv=/usr/sbin/ipvsadm 变量;ifdown ens33ifup ens33 重启下网卡,再次执行这个脚本时不需要重复设置这个IPifconfig ens33:2 设置虚拟网卡名称为ens33:2;route add -host $vip dev ens33:2 添加网关;$ipv -C 是清空规则;$ipv -a -t $vip:80 -r $rs1:80 -g -w 1 其中-g是getway,就是DR模式[root@linux-01 ~]# sh /usr/local/sbin/lvs_dr.sh //执行脚本两台rs机器上分别编写脚本,内容如下:[root@linux-02 ~]# vim /usr/local/sbin/lvs_rs.sh #/bin/bashvip=192.168.238.200#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端ifdown loifup loifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip lo:0#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.htmlecho "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce[root@linux-02 ~]# sh /usr/local/sbin/lvs_rs.sh //执行脚本在rs2机器上同样编写/usr/local/sbin/lvs_rs.sh脚本,写入如上脚本内容
[root@linux-03 ~]# vim /usr/local/sbin/lvs_rs.sh #/bin/bashvip=192.168.238.200#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端ifdown loifup loifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 uproute add -host $vip lo:0#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.htmlecho "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce[root@linux-03 ~]# sh /usr/local/sbin/lvs_rs.sh //执行脚本[root@linux-03 ~]# route -n[root@linux-03 ~]# ip add[root@linux-02 ~]# ip add[root@linux-01 ~]# ip add //分别查看三台机器的网卡信息是否设置了vip=192.168.238.200打开浏览器访问:192.168.238.200[root@linux-01 ~]# iptables -t nat -F //清空防火墙规则[root@linux-01 ~]# ipvsadm -ln二、keepalived + LVS
[root@linux-01 ~]# ipvsadm -ln[root@linux-01 ~]# vim /etc/keepalived/keepalived.conf//配置文件内容请到https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/lvs_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.238.200}}virtual_server 192.168.238.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.238.130 80 { #(权重) weight 100 TCP_CHECK { #(10秒无响应超时) connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 }}real_server 192.168.238.131 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }
}
配置完keepalived.conf配置文件,启动keepalived服务[root@linux-01 ~]# systemctl start keepalived[root@linux-01 ~]# ps aux |grep keep //查看keepalived进程[root@linux-01 ~]# ipvsadm -ln[root@linux-01 ~]# systemctl stop keepalived [root@linux-03 ~]# systemctl start nginx //启动rs2的nginx[root@linux-01 ~]# ip add //关掉原来的虚拟IP[root@linux-01 ~]# ipvsadm -ln //再次查看已经没有规则了[root@linux-01 ~]# systemctl start keepalived //再次启动keepalived[root@linux-01 ~]# ipvsadm -ln //查看有两个规则了说明keepalived有自动把宕机的rs剔除的功能需要注意在两台rs机器上依然要执行/usr/local/sbin/lvs_rs.sh脚本转载于:https://blog.51cto.com/13669226/2138956