LVS-NAT 模型实现WEB负载均衡

Posted by Mathew on 2017-02-11

LVS-NAT 模型实现WEB负载均衡

主机规划:
| 主机名 | 主机地址 | 角色 | OS |
| :-------- | --------| -------- | :–: |
| Director | 192.168.1.9/192.168.20.2 | Director | CentOS 6.8|
| RS1 | 192.168.20.7 | Real Server1| CentOS 7.2|
| RS2 | 192.168.20.8 | Real Server2| CentOS 7.2|

Notice:
 注意:我这里node1node2的网关均是192.168.20.2,所有主机selinuxiptables均关闭。

RS1

配置web服务

1
2
3
4
5
6
7
[root@rs1 ~]#  yum install httpd -y
[root@rs1 ~]# echo "<h1>Test Page On node1.maricle.com.cn</h1>" >> /var/www/html/index.html
[root@rs1 ~]# systemctl start httpd
[root@rs1 ~]# ss -tnlp | grep 80
LISTEN 0 128 :::80 :::* users:(("httpd",pid=6562,fd=4),("httpd",pid=6561,fd=4),("httpd",pid=6560,fd=4),("httpd",pid=6559,fd=4),("httpd",pid=6558,fd=4),("httpd",pid=6557,fd=4))
[root@rs1 ~]# curl 192.168.20.7
<h1>Test Page On node1.maricle.com.cn</h1>

RS2

配置web服务

1
2
3
4
5
[root@rs2 ~]# yum install httpd -y
[root@rs2 ~]# echo "<h1>Test Page On node2.maricle.com.cn</h1>" >> /var/www/html/index.html
[root@rs2 ~]# systemctl start httpd
[root@rs2 ~]# ss -tnlp | grep 80
LISTEN 0 128 :::80 :::* users:(("httpd",pid=6562,fd=4),("httpd",pid=6561,fd=4),("httpd",pid=6560,fd=4),("httpd",pid=6559,fd=4),("httpd",pid=6558,fd=4),("httpd",pid=6557,fd=4))

Director Server中安装ipvsadm

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[root@Director ~]# ip a | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.9/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe33:9a8e/64 scope link tentative dadfailed
inet 192.168.20.2/24 brd 192.168.20.255 scope global eth1
inet6 fe80::20c:29ff:fe33:9a98/64 scope link
[root@Director ~]# yum install ipvsadm -y

[root@Director ~]# curl 192.168.20.7 # 可以访问RS1
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.20.8 # 可以访问RS2
<h1>Test Page On RS2.maricle.com.cn</h1>
[root@Director ~]# vim /etc/sysctl.conf # 开启核心转发
[root@Director ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@Director ~]#

## 添加ipvsadm规则
## rr算法
[root@Director ~]# ipvsadm -A -t 192.168.1.9:80 -s rr
[root@Director ~]# ipvsadm -L -n ^C
[root@Director ~]#
[root@Director ~]#
[root@Director ~]# ipvsadm -a -t 192.168.1.9:80 -r 192.168.20.7 -m
[root@Director ~]# ipvsadm -a -t 192.168.1.9:80 -r 192.168.20.8 -m
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.9:80 rr
-> 192.168.20.7:80 Masq 1 0 0
-> 192.168.20.8:80 Masq 1 0 0

[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS2.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS2.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS2.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.9:80 rr
-> 192.168.20.7:80 Masq 1 0 12
-> 192.168.20.8:80 Masq 1 0 13
[root@Director ~]#

## sh算法
[root@Director ~]# ipvsadm -E -t 192.168.1.9:80 -s sh
[root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.9:80 sh
-> 192.168.20.7:80 Masq 1 0 0
-> 192.168.20.8:80 Masq 1 0 0
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
[root@Director ~]# curl 192.168.1.9
<h1>Test Page On RS1.maricle.com.cn</h1>
root@Director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.9:80 sh
-> 192.168.20.7:80 Masq 1 0 11
-> 192.168.20.8:80 Masq 1 0 0

关于rr算法和sh算法请看我上一篇博客LVS工作模型和调度算法详解

本文偏操作,下面一篇将介绍DR模型的实现