LVS负载均衡群集——DR模式

   群集(或集群)的称呼来自于英文单词“Cluster”,表示一群、一串的意思,用在服务器领域则表示大量服务器的集合体,以区分单个服务器。本篇博客将介绍企业中常用的一种群集技术——LVS(Linux Virtual Server,Linux虚拟服务器)


    上一篇博客已经对集群的结构、工作模式等进行了介绍,还配置了LVS——NAT模式,这里就直接配置LVS——DR了   


案例:构建直接路由模式负载均衡集群(LVS——DR)

   为了进一步提高公司的负载能力,公司决定扩展现有的网站平台,经过多方面考虑,管理员准备采用LVS群集的DR模式,如下图所示:

LVS负载均衡群集——DR模式


需求描述:

1. 使用一台LVS调度器,四台节点服务器,均配置双网卡

(1)第一块网卡eth0连接至Internet,位于172.16.16.0/24网段

(2)第二块网卡eth1连接至存储内网,位于192.168.7.0/24网段

2. 共享存储器有一台Linux服务器承担,将目录/var/www/html发布为NFS可读写共享

3. 群集地址(VIP)为172.16.16.172,通过此地址访问公司的Web站点


实验步骤如下:

1. 配置LVS负载调度器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
[[email protected] network-scripts]# vim ifcfg-eth0:0       //配置虚拟IP地址(VIP)
DEVICE=eth0:0
ONBOOT=yes
IPADDR=172.16.16.172
NETMASK=255.255.255.0
 
[[email protected] network-scripts]# ifup eth0:0            //启动网卡
[[email protected] network-scripts]# ifconfig               //查看IP地址
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1C:B4:FB  
          inet addr:172.16.16.173  Bcast:172.16.16.255  Mask:255.255.255.0
 
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:1C:B4:FB  
          inet addr:172.16.16.172  Bcast:172.16.16.255  Mask:255.255.255.0          
 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:1C:B4:05  
          inet addr:192.168.7.173  Bcast:192.168.7.255  Mask:255.255.255.0
 
[[email protected] network-scripts]# cd ~
 
# 由于LVS负载调度器和各节点需要共用VIP地址,应该关闭Linux内核的重定向参数响应
[[email protected] ~]# vim /etc/sysctl.conf          //调整/proc响应参数
net.ipv4.conf.all.send_redirects = 0              //关闭Linux内核的重定向参数响应 
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
 
[[email protected] ~]# sysctl -p             //立即生效
[[email protected] ~]# iptables -I FORWARD -p tcp  -j ACCEPT     //设置防火墙策略
 
[[email protected] ~]# modprobe ip_vs
[[email protected] ~]# yum -y install ipvsadm
[[email protected] ~]# service ipvsadm stop      //清除原有策略
[[email protected] ~]# ipvsadm -A -t 172.16.16.172:80 -s rr
[[email protected] ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.177 -g -w 1
[[email protected] ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.178 -g -w 1
[[email protected] ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.179 -g -w 1
[[email protected] ~]# ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.180 -g -w 1
[[email protected] ~]# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [确定]
[[email protected] ~]# chkconfig ipvsadm on
[[email protected] ~]# cat /etc/sysconfig/ipvsadm
-A -t 172.16.16.172:80 -s rr
-a -t 172.16.16.172:80 -r 172.16.16.177:80 -g -w 1
-a -t 172.16.16.172:80 -r 172.16.16.178:80 -g -w 1
-a -t 172.16.16.172:80 -r 172.16.16.179:80 -g -w 1
-a -t 172.16.16.172:80 -r 172.16.16.180:80 -g -w 1


2. 配置NFS共享存储服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[[email protected] ~]# ifconfig                   //配置IP地址
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0D:16:6A  
          inet addr:192.168.7.250  Bcast:192.168.7.255  Mask:255.255.255.0
  
[[email protected] ~]# iptables -I INPUT -p tcp -j ACCEPT        //设置防火墙策略
[[email protected] ~]# iptables -I INPUT -p udp -j ACCEPT                    
            
[[email protected] ~]# yum -y install nfs-utils rpcbind                    
[[email protected] ~]# chkconfig nfs on        
[[email protected] ~]# chkconfig rpcbind on          
[[email protected] ~]# mkdir /opt/wwwroot      //设置共享目录
[[email protected] ~]# echo welcome to beijing!!! > /opt/wwwroot/index.html
[[email protected] ~]# cat /opt/wwwroot/index.html     //创建测试网页
welcome to beijing!!!
[[email protected] ~]# vim /etc/exports        //将目录共享给192.168.7.0/24使用   
/opt/wwwroot 192.168.7.0/24(rw,sync,no_root_squash)          
# rw表示允许读写(ro为只读)          
# sync表示同步写入          
# no_root_squash当客户机以root身份访问时赋予本地root权限(默认是root_squash,将作为nfsnobody用户降权对待)
  
[[email protected] ~]# service rpcbind start
[[email protected] ~]# service nfs start
[[email protected] ~]# showmount -e 127.0.0.1       //查看本机发布的NFS共享目录
Export list for 127.0.0.1:
/opt/wwwroot 192.168.7.0/24


3. 配置Web节点服务器

   使用DR模式时,节点服务器也需要配置VIP地址,并调整内核的ARP响应参数以阻止更新VIP的MAC地址,避免发生冲突。其他的配置和NAT模式差不多

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-lo ifcfg-lo:0
[[email protected] network-scripts]# vim ifcfg-lo:0        //配置虚拟IP地址(VIP)
DEVICE=lo:0
IPADDR=172.16.16.172
NETMASK=255.255.255.255               //子网掩码必须全为1
ONBOOT=yes
 
[[email protected] network-scripts]# ifconfig           //查看IP地址
eth0      Link encap:Ethernet  HWaddr 00:0C:29:8A:0D:26  
          inet addr:172.16.16.177  Bcast:172.16.16.255  Mask:255.255.255.0
 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:8A:0D:30  
          inet addr:192.168.7.177  Bcast:192.168.7.255  Mask:255.255.255.0
 
lo:0      Link encap:Local Loopback  
          inet addr:172.16.16.172  Mask:255.255.255.255
 
[[email protected] network-scripts]# cd ~
[[email protected] ~]# vim /etc/rc.local          //添加VIP为本地访问路由
/sbin/route add -host 172.16.16.172 dev lo:0
[[email protected] ~]# route add -host 172.16.16.172 dev lo:0    
 
[[email protected] ~]# vim /etc/sysctl.conf      //调整内核的ARP响应参数
net.ipv4.conf.all.arp_ignore = 1          //阻止更新VIP的MAC地址,避免发生冲突
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
 
[[email protected] ~]# sysctl -p       //立即生效
[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT   //配置防火墙策略     
   
[[email protected] ~]# yum -y install httpd nfs-utils rpcbind
  
[[email protected] ~]# showmount -e 192.168.7.250     //查看NFS服务器共享了哪些目录
Export list for 192.168.7.250:
/opt/wwwroot 192.168.7.0/24
[[email protected] ~]# mount 192.168.7.250:/opt/wwwroot /var/www/html    //将共享目录挂载到本地目录
[[email protected] ~]# tail -1 /etc/mtab 
192.168.7.250:/opt/wwwroot /var/www/html nfs rw,vers=4,addr=192.168.7.250,clientaddr=192.168.7.177 0 0
  
[[email protected] ~]# cat /var/www/html/index.html     //验证测试网页是否共享
welcome to beijing!!!
[[email protected] ~]# vim /etc/fstab          //设置开机自动挂载
192.168.7.250:/opt/wwwroot   /var/www/html     nfs     defaults,_netdev   0 0
  
[[email protected] ~]# chkconfig nfs on        
[[email protected] ~]# chkconfig rpcbind on  
[[email protected] ~]# chkconfig httpd on
[[email protected] ~]# service rpcbind start
[[email protected] ~]# service nfs start
[[email protected] ~]# service httpd start


4. 测试LVS群集

   安排多台测试机,从Internet中直接访问VIP地址http://172.16.16.172/,将能看到有真实Web服务器提供的网页内容。

通过LVS负载均衡调度器,可查看当前的负载分配情况,由于使用轮询算法,各节点所获得的连接负载大致相当。

LVS负载均衡群集——DR模式


在LVS负载均衡调度器中执行以下命令:

1
2
3
4
5
6
7
8
9
[[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  172.16.16.172:80 rr
  -> 172.16.16.177:80             Route   1      2          9         
  -> 172.16.16.178:80             Route   1      3          8         
  -> 172.16.16.179:80             Route   1      2          9         
  -> 172.16.16.180:80             Route   1      4          7

   可看到各节点服务器的连接负载大致相当,从而实现了负载均衡











本文转自 杨书凡 51CTO博客,原文链接:http://blog.51cto.com/yangshufan/2065686,如需转载请自行联系原作者