Loading

LVS负载均衡DR模式实现

LVS负载均衡之DR模式配置

DR 模式架构图:

操作步骤

实验环境准备:(centos7平台)

所有服务器上配置

# systemctl stop firewalld         //关闭防火墙
# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux        //关闭selinux,重启生效
# setenforce 0                //关闭selinux,临时生效
# ntpdate 0.centos.pool.ntp.org    //时间同步
注意:realserver的网关需要指向DIP

步骤一:配置 router

1)打开 ip_forward

[root@router ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@router ~]# sysctl -p

2)添加防火墙规则,指定客户端进来的规则,(此处使用 iptables 做的,也可以换成 firewalld来做)

[root@router ~]# iptables -F
[root@router ~]# yum install iptables-services iptables
[root@router ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -i ens33 -j DNAT --to-destination 10.10.10.110        //这条表示从 ens33(也就是192.168.1.31的)网卡进来访问80的包,DNAT到 10.10.10.110(也就是 LVS 调度器的 IP)
[root@router ~]# 
[root@router ~]# iptables -t nat -A POSTROUTING -p tcp --dport 80 -o ens37 -j SNAT --to-source 10.10.10.120        //这条表示(为了客户端 192.168.1.35 访问 192.168.1.31 变成 10.10.10.120 访问10.10.10.110),这样可以实现 LVS 调度器能回客户端
如果不加这条的话,也可以在LVS 调度器上添加路由(route add default gw 10.10.10.120 指一个网关回去,因为 DNAT 的目标机器需要一个网关才能回给 client)

[root@router ~]# iptables-save > /etc/sysconfig/iptables
[root@router ~]# systemctl start iptables.service
[root@router ~]# systemctl enable iptables.service

步骤二:配置 LVS 调度器

1)安装ipvsadm

[root@lvs-director ~]# yum install ipvsadm -y

2)配置调度规则

[root@lvs-director ~]# ipvsadm -A -t 10.10.10.110:80 -s rr
[root@lvs-director ~]# ipvsadm -a -t 10.10.10.110:80 -r 10.10.10.11:80 -g        //这里的 -g 就是表示使用直接路由模式,LVS 调度器就会把数据包调给 10.10.10.11 或 10.10.10.12 时,就只修改 MAC 地址,不修改目标 IP 直接路由过去
[root@lvs-director ~]# ipvsadm -a -t 10.10.10.110:80 -r 10.10.10.12:80 -g
[root@lvs-director ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.10.10.110:80 rr
  -> 10.10.10.11:80               Route   1      0          0         
  -> 10.10.10.12:80               Route   1      0          0

3)保存在文件中,设置为开机启动

[root@lvs-director ~]# 
[root@lvs-director ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@lvs-director ~]# systemctl start ipvsadm
[root@lvs-director ~]# systemctl enable ipvsadm 

4) 由于下面会在 web服务器上面添加一个子接口 lo:0 10.10.10.110网卡,这样就会到导致 lvs 调度器过去的包可以成果过去,但是不会回来,因为回来时它会直接查找自己的 lo:0的10.10.10.110。所以需要加一个子接口 掩码给到 255.255.255.128。

[root@lvs-director ~]# ifconfig ens33:0 10.10.10.111 netmask 255.255.255.128

  注意:如果用掩码 255.255.255.0 还是会出现ping 不通的情况,因为ping的时候 10.10.10.110和10.10.10.111掩码相同,优先级一样。而用225.225.225.128路由选择会优先使用10.10.10.111去ping

步骤三:配置realserver

 在 realserver(web01和web02)上安装 nginx,并在不同的 web 服务器上建立不同的主页内容(方便测试),并启动。

1) 在 web01 服务器配置

[root@web01 ~]# yum install nginx -y
[root@web01 ~]# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html 
[root@web01 ~]# systemctl start nginx 
[root@web01 ~]# systemctl enable nginx 

2) 在 web02 服务器配置

[root@web02 ~]# yum install nginx -y
[root@web02 ~]# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html
[root@web02 ~]# systemctl start nginx 
[root@web02 ~]# systemctl enable nginx

3) 添加vip (不论后端有几个web服务器,都需要做)

# ifconfig lo:0 10.10.10.110 netmask 255.255.255.255  //注意掩码为4个255,想永久生效,可以写一个 ifcfg-lo:0 的网卡配置文件即可。

  最好不要写成 ifconfig lo:0 10.10.10.110/32 的形式,用ifconfig 查掩码会出现四个0。
  这一步是非常重要的,因为路由方式扔过来的包,目标 IP 不变,也就是说还是 10.10.10.120,只是通过找 10.10.10.11 或 10.10.10.12 的 MAC 地址扔过来的。
  所以 web 服务器上也需要有一个 10.10.10.120 这个 IP 来解析;用 lo 网卡来虚拟就是为了尽量不要与 lvs 网卡造成 ARP 广播问题。
  这里 netmask 为什么是4个 255,而不是 255.255.255.0?
  如果为 255.255.255.0,那么 10.10.10.0/24 整个网络都无法和web服务器通讯。

4) 真实服务器把默认路由指向 router 同物理网段的 IP,可以临时加也可以直接写在配置文件里面,这里上面的环境准备已经写在了配置文件。 (web1 和 web2 都需要做) 临时加示例:

# route add default gw 10.10.10.120

5) 抑制 web 服务器上 IP 冲突问题 (web1 和 web2 都需要做)

# vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
# sysctl -p

步骤四:在客户机上测试

[root@client ~]# curl 192.168.1.31
web01 10.10.10.11  
[root@client ~]# curl 192.168.1.31
web02 10.10.10.12  
[root@client ~]# curl 192.168.1.31
web01 10.10.10.11  
[root@client ~]# curl 192.168.1.31
web02 10.10.10.12  

从测试结果可以看出,轮循调度给后端web服务器了。至此dr模式就完成了。

 

LVS 概念篇参考 ->点我

NAT 模式实现参考 ->点我

 

posted @ 2019-04-17 17:09  别来无恙-  阅读(2854)  评论(0编辑  收藏  举报