群集(或集群)的称呼来自于英文单词“Cluster”,表示一群、一串的意思,用在服务器领域则表示大量服务器的集合体,以区分单个服务器。本篇博客将介绍企业中常用的一种群集技术——LVS(Linux Virtual Server,Linux虚拟服务器)
上一篇博客已经对集群的结构、工作模式等进行了介绍,还配置了LVS——NAT模式,这里就直接配置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
|
[root@localhost ~] # cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts] # cp ifcfg-eth0 ifcfg-eth0:0
[root@localhost network-scripts] # vim ifcfg-eth0:0 //配置虚拟IP地址(VIP)
DEVICE=eth0:0 ONBOOT= yes
IPADDR=172.16.16.172 NETMASK=255.255.255.0 [root@localhost network-scripts] # ifup eth0:0 //启动网卡
[root@localhost 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
[root@localhost network-scripts] # cd ~
# 由于LVS负载调度器和各节点需要共用VIP地址,应该关闭Linux内核的重定向参数响应 [root@localhost ~] # 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 [root@localhost ~] # sysctl -p //立即生效
[root@localhost ~] # iptables -I FORWARD -p tcp -j ACCEPT //设置防火墙策略
[root@localhost ~] # modprobe ip_vs
[root@localhost ~] # yum -y install ipvsadm
[root@localhost ~] # service ipvsadm stop //清除原有策略
[root@localhost ~] # ipvsadm -A -t 172.16.16.172:80 -s rr
[root@localhost ~] # ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.177 -g -w 1
[root@localhost ~] # ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.178 -g -w 1
[root@localhost ~] # ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.179 -g -w 1
[root@localhost ~] # ipvsadm -a -t 172.16.16.172:80 -r 172.16.16.180 -g -w 1
[root@localhost ~] # service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm : [确定]
[root@localhost ~] # chkconfig ipvsadm on
[root@localhost ~] # 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
|
[root@localhost ~] # 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
[root@localhost ~] # iptables -I INPUT -p tcp -j ACCEPT //设置防火墙策略
[root@localhost ~] # iptables -I INPUT -p udp -j ACCEPT
[root@localhost ~] # yum -y install nfs-utils rpcbind
[root@localhost ~] # chkconfig nfs on
[root@localhost ~] # chkconfig rpcbind on
[root@localhost ~] # mkdir /opt/wwwroot //设置共享目录
[root@localhost ~] # echo welcome to beijing!!! > /opt/wwwroot/index.html
[root@localhost ~] # cat /opt/wwwroot/index.html //创建测试网页
welcome to beijing!!! [root@localhost ~] # 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用户降权对待) [root@localhost ~] # service rpcbind start
[root@localhost ~] # service nfs start
[root@localhost ~] # 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
|
[root@localhost ~] # cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts] # cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts] # vim ifcfg-lo:0 //配置虚拟IP地址(VIP)
DEVICE=lo:0 IPADDR=172.16.16.172 NETMASK=255.255.255.255 // 子网掩码必须全为1
ONBOOT= yes
[root@localhost 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
[root@localhost network-scripts] # cd ~
[root@localhost ~] # vim /etc/rc.local //添加VIP为本地访问路由
/sbin/route add -host 172.16.16.172 dev lo:0
[root@localhost ~] # route add -host 172.16.16.172 dev lo:0
[root@localhost ~] # 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 [root@localhost ~] # sysctl -p //立即生效
[root@localhost ~] # iptables -I INPUT -p tcp --dport 80 -j ACCEPT //配置防火墙策略
[root@localhost ~] # yum -y install httpd nfs-utils rpcbind
[root@localhost ~] # showmount -e 192.168.7.250 //查看NFS服务器共享了哪些目录
Export list for 192.168.7.250:
/opt/wwwroot 192.168.7.0 /24
[root@localhost ~] # mount 192.168.7.250:/opt/wwwroot /var/www/html //将共享目录挂载到本地目录
[root@localhost ~] # 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
[root@localhost ~] # cat /var/www/html/index.html //验证测试网页是否共享
welcome to beijing!!! [root@localhost ~] # vim /etc/fstab //设置开机自动挂载
192.168.7.250: /opt/wwwroot /var/www/html nfs defaults,_netdev 0 0
[root@localhost ~] # chkconfig nfs on
[root@localhost ~] # chkconfig rpcbind on
[root@localhost ~] # chkconfig httpd on
[root@localhost ~] # service rpcbind start
[root@localhost ~] # service nfs start
[root@localhost ~] # service httpd start
|
4. 测试LVS群集
安排多台测试机,从Internet中直接访问VIP地址http://172.16.16.172/,将能看到有真实Web服务器提供的网页内容。
通过LVS负载均衡调度器,可查看当前的负载分配情况,由于使用轮询算法,各节点所获得的连接负载大致相当。
在LVS负载均衡调度器中执行以下命令:
1
2
3
4
5
6
7
8
9
|
[root@localhost ~] # 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
|
可看到各节点服务器的连接负载大致相当,从而实现了负载均衡