码迷,mamicode.com
首页 > 系统相关 > 详细

2018-05-23 Linux学习

时间:2018-05-22 23:59:54      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:Linux学习

18.11 LVS DR模式搭建 (推荐)

DR模式搭建 – 准备工作

三台机器
分发器,也叫调度器(简写为dir)
133.130
rs1
133.132
rs2
133.133
vip
133.200

DR模式搭建

dir上编写脚本 vim /usr/local/sbin/lvs_dr.sh //内容如下
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.133.200
rs1=192.168.133.132
rs2=192.168.133.133
#注意这里的网卡名字
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

两台rs上也编写脚本 vim /usr/local/sbin/lvs_rs.sh//内容如下
#/bin/bash
vip=192.168.133.200
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
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
分别在dir上和两个rs上执行这些脚本
测试

操作过程

rs网关改回可正常连接外网

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.106.165
NETMASK=255.255.255.0
GATEWAY=192.168.106.2
DNS1=119.29.29.29
[[email protected] ~]# systemctl restart network

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.106.166
NETMASK=255.255.255.0
GATEWAY=192.168.106.2
DNS1=119.29.29.29
[[email protected] ~]# systemctl restart network

[[email protected] ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.106.200
rs1=192.168.106.165
rs2=192.168.106.166
#注意这里的网卡名字
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

[[email protected] ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 ‘ens33‘。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3

rs两台配置同样的脚本

[[email protected] ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.106.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

[[email protected] ~]# sh /usr/local/sbin/lvs_rs.sh

[[email protected] ~]# vim /usr/local/sbin/lvs_rs.sh
[[email protected] ~]# sh /usr/local/sbin/lvs_rs.sh

所有机器上都可以看到绑定的vip 192.168.106.200

[[email protected] ~]# ip add

测试

Mikes-MacBook-Pro:~ mike$ curl http://192.168.106.200/
aminglinux03
Mikes-MacBook-Pro:~ mike$ curl http://192.168.106.200/
aminglinux02
Mikes-MacBook-Pro:~ mike$ curl http://192.168.106.200/
aminglinux03
Mikes-MacBook-Pro:~ mike$ curl http://192.168.106.200/
aminglinux02

[[email protected] ~]# 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.106.200:80 wrr
  -> 192.168.106.165:80           Route   1      0          2         
  -> 192.168.106.166:80           Route   1      0          2 

18.12 keepalived lvs

完整架构需要两台服务器(角色为dir)分别安装keepalived软件,目的是实现高可用,但keepalived本身也有负载均衡的功能,所以本次实验可以只安装一台keepalived
keepalived内置了ipvsadm的功能,所以不需要再安装ipvsadm包,也不用编写和执行那个lvs_dir的脚本
三台机器分别为:
dir(安装keepalived)133.130
rs1 133.132
rs2 133.133
vip 133.200

dir上编写脚本 vim /usr/local/sbin/lvs_dr.sh //内容如下
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
以下四行不确定要不要 *****
ipv=/usr/sbin/ipvsadm
vip=192.168.133.200
rs1=192.168.133.132
rs2=192.168.133.133

两台rs上也编写脚本 vim /usr/local/sbin/lvs_rs.sh//内容如下
#/bin/bash
vip=192.168.133.200
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
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
分别在dir上和两个rs上执行这些脚本

编辑keepalived配置文件 vim /etc/keepalived/keepalived.conf//内容请到https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/lvs_keepalived.conf 获取
需要更改里面的ip信息
执行ipvsadm -C  把之前的ipvsadm规则清空掉
systemctl restart network 可以把之前的vip清空掉
两台rs上,依然要执行/usr/local/sbin/lvs_rs.sh脚本
keepalived有一个比较好的功能,可以在一台rs宕机时,不再把请求转发过去
测试

操作过程

[[email protected] ~]# 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 aminglinux
}
virtual_ipaddress {
192.168.106.200
}
}
virtual_server 192.168.106.200 80 {
#(每隔10秒查询realserver状态)
delay_loop 10
#(lvs 算法)
lb_algo wlc
#(DR模式)
lb_kind DR
#(同一IP的连接60秒内被分配到同一台realserver)
persistence_timeout 60
#(用TCP协议检查realserver状态)
protocol TCP
real_server 192.168.106.165 80 {
#(权重)
weight 100
TCP_CHECK {
#(10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.106.166 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

[[email protected] ~]# systemctl stop keepalived.service 
[[email protected] ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# 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.106.200:80 wlc persistent 60
  -> 192.168.106.165:80           Route   100    0          0         
  -> 192.168.106.166:80           Route   100    0          0     

测试

[[email protected] ~]# systemctl stop nginx.service

[[email protected] ~]# 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.106.200:80 wlc persistent 60
  -> 192.168.106.165:80           Route   100    0          0 

[[email protected] ~]# systemctl start nginx.service

[[email protected] ~]# 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.106.200:80 wlc persistent 60
  -> 192.168.106.165:80           Route   100    0          0         
  -> 192.168.106.166:80           Route   100    0          0

访问测试

Mikes-MacBook-Pro:~ mike$ curl http://192.168.106.200/
aminglinux03

[[email protected] ~]# 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.106.200:80 wlc persistent 60
  -> 192.168.106.165:80           Route   100    0          0         
  -> 192.168.106.166:80           Route   100    0          9

2018-05-23 Linux学习

标签:Linux学习

原文地址:http://blog.51cto.com/9298822/2119281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!