构建LVS负载均衡群集

构建LVS负载均衡群集

实验环境

201:客户端
101:调度器 (需要用到2个网卡)
102:web
103:web
104:NFS存储
在这里插入图片描述

101:(添加一个网卡)
2 [root@localhost ~]# cd /etc/sysconfig/network-scripts/
3
4 //查看另一个网卡的名字ens36
5 [root@localhost network-scripts]# ifconfig
6 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
7 			inet 192.168.10.101 netmask 255.255.255.0 broadcast 192.168.10.255
8 			inet6 fe80::f6d:8bdc:c21a:5b47 prefixlen 64 scopeid 0x20<link>
9 			ether 00:0c:29:8b:11:fe txqueuelen 1000 (Ethernet)
10			RX packets 240 bytes 25054 (24.4 KiB)
11 			RX errors 0 dropped 0 overruns 0 frame 0
12 			TX packets 200 bytes 28380 (27.7 KiB)
13 			TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
14
15 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
16 			inet6 fe80::d9a8:ee94:b8fb:e3e8 prefixlen 64 scopeid 0x20<link>
17 			ether 00:0c:29:8b:11:08 txqueuelen 1000 (Ethernet)
18 			RX packets 0 bytes 0 (0.0 B)
19 			RX errors 0 dropped 0 overruns 0 frame 0
20 			TX packets 23 bytes 3210 (3.1 KiB)
21 			TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
22
23 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
24 			inet 127.0.0.1 netmask 255.0.0.0
25 			inet6 ::1 prefixlen 128 scopeid 0x10<host>
26 			loop txqueuelen 1000 (Local Loopback)
27 			RX packets 0 bytes 0 (0.0 B)
28 			RX errors 0 dropped 0 overruns 0 frame 0
29 			TX packets 0 bytes 0 (0.0 B)
30 			TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
31
32 //拷贝一下配置文件给ens36,并进行修改
33 [root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
34 [root@localhost network-scripts]# vim ifcfg-ens36
35 TYPE=Ethernet
36 PROXY_METHOD=none
37 BROWSER_ONLY=no
38 BOOTPROTO=static
39 IPADDR=172.16.16.172 		//修改ip
NETMASK=255.255.255.0
41 #GATEWAY=192.168.10.254 //注释掉网关
42 DNS1=114.114.114.114
43 DNS2=8.8.8.8
44 DEFROUTE=yes
45 IPV4_FAILURE_FATAL=no
46 IPV6INIT=yes
47 IPV6_AUTOCONF=yes
48 IPV6_DEFROUTE=yes
49 IPV6_FAILURE_FATAL=no
50 IPV6_ADDR_GEN_MODE=stable-privacy
51 NAME=ens36 //修改值
52 DEVICE=ens36 //修改值
53 ONBOOT=YES
54
55 //重新启动一下网络
56 [root@localhost network-scripts]# systemctl restart network
57
58 //查看ip是否添加成功
59 [root@localhost network-scripts]# ifconfig
60 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
61 			inet 192.168.10.101 netmask 255.255.255.0 broadcast 192.168.10.255
62 			inet6 fe80::f6d:8bdc:c21a:5b47 prefixlen 64 scopeid 0x20<link>
63 			ether 00:0c:29:8b:11:fe txqueuelen 1000 (Ethernet)
64 			RX packets 714 bytes 66771 (65.2 KiB)
65 			RX errors 0 dropped 0 overruns 0 frame 0
66 			TX packets 543 bytes 65369 (63.8 KiB)
67 			TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
68
69 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
70 			inet 172.16.16.172 netmask 255.255.255.0 broadcast 172.16.16.255
71 			inet6 fe80::7d4:8d2b:6db7:777a prefixlen 64 scopeid 0x20<link>
72 			ether 00:0c:29:8b:11:08 txqueuelen 1000 (Ethernet)
73 			RX packets 10 bytes 600 (600.0 B)
74 			RX errors 0 dropped 0 overruns 0 frame 0
75 			TX packets 78 bytes 11460 (11.1 KiB)
76 			TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
77
78 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
79 			inet 127.0.0.1 netmask 255.0.0.0
80 			inet6 ::1 prefixlen 128 scopeid 0x10<host>
81 			loop txqueuelen 1000 (Local Loopback)
82			RX packets 4 bytes 348 (348.0 B)
83 			RX errors 0 dropped 0 overruns 0 frame 0
84 			TX packets 4 bytes 348 (348.0 B)
85 			TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
86
87 //前往虚拟机进行修改ip和注释掉网关
88 20189 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
90 IPADDR=172.16.16.200
91 #GATEWAY=192.168.10.254
92
93 //重新启动网络
94 [root@localhost ~]# systemctl restart network
95
96 //查看ip是否修改成功
97 [root@localhost ~]# ifconfig 
98 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
99 			inet 172.16.16.200 netmask 255.255.255.0 broadcast 172.16.16.255
100 		inet6 fe80::e7f5:13b8:da41:dcb0 prefixlen 64 scopeid 0x20<link>
101 		ether 00:0c:29:97:3a:b7 txqueuelen 1000 (Ethernet)
102 		RX packets 1608 bytes 1981591 (1.8 MiB)
103 		RX errors 0 dropped 0 overruns 0 frame 0
104 		TX packets 494 bytes 50411 (49.2 KiB)
105 		TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
106
107 //ping,看能不能ping通
108 [root@localhost ~]# ping 172.16.16.200
109
110 102103111 //安装软件包
112 [root@localhost ~]# yum -y install httpd
113
114 //关闭防火墙
115 [root@localhost ~]# systemctl stop firewalld
116 [root@localhost ~]# setenforce 0
117
118 102:
119 [root@localhost ~]# vim /var/www/html/index.html
120 test web01
121
122 103123 [root@localhost ~]# vim /var/www/html/index.html
124 test web02
125
126 102,103127 //启动服务
128 [root@localhost ~]# systemctl start httpd
129
130 104131 //进行测试
132 [root@localhost ~]# curl 192.168.10.102
133 test web01
134 [root@localhost ~]# curl 192.168.10.103
135 test web02
136
137 102103138 //修改网关地址为101
139 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
140 GATEWAY=192.168.10.101
141
142 //重新启动网络
143 [root@localhost ~]# systemctl restart network
144
145 101146 //关闭防火墙
147 [root@localhost ~]# systemctl stop firewalld
148 [root@localhost ~]# setenforce 0
149
150 //安装软件包
151 [root@localhost ~]# yum -y install ipvsadm
152
153 //查询ipvsadm的版本
154 [root@localhost ~]# ipvsadm -v
155 ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
156
157 //修改文件
158 [root@localhost ~]# vim /etc/sysctl.conf
159 net.ipv4.ip_forward=1
160
161 //使他能够立即生效
162 [root@localhost ~]# sysctl -p
163 net.ipv4.ip_forward = 1
164
165 //查看当前的配置信息(是空的)
166 [root@localhost ~]# ipvsadm-save
167
168 //清除所有策略(因为本来都没有什么策略,所有清不清都无所谓)
169 [root@localhost ~]# ipvsadm -C
170
171 //配置新的策略(-A:添加集群、-t:TCP:定义端口和ip、-s:指定调度算法 rr(轮询(服务器的性能相当时可以使用轮询))、wrr(加权轮询(服务器的性能不一样,可以使用加权轮询,权重越高,轮询的越多))、)
172 [root@localhost ~]# ipvsadm -A -t 172.16.16.172:80 -s wrr
173
174 //配置新的策略(-a:添加集群中的服务器、-r:真实的服务器、-m:指定集群模式为net模式、-i:指定集群模式为IP隧道模式、-g:指定集群模式为DR模式、-w:设置权重值)
175 [root@localhost ~]# ipvsadm -a -t 172.16.16.172:80 -r 192.168.10.102:80 -m -w 1
176 [root@localhost ~]# ipvsadm -a -t 172.16.16.172:80 -r 192.168.10.103:80 -m -w 2
177
178 //再次查看他的配置信息(时间有一点长)
179 [root@localhost ~]# ipvsadm-save
180 -A -t localhost.localdomain:http -s wrr
181 -a -t localhost.localdomain:http -r 192.168.10.102:http -m -w 1
182 -a -t localhost.localdomain:http -r 192.168.10.103:http -m -w 2
183
184 //进行验证(由下可看出,它的分配是1:2,也就是102给的多,101给的少)(当某一个网站节点出现故障,lvm没办法进行检测)
185 201186 [root@localhost ~]# curl 172.16.16.172
187 test web02
188 [root@localhost ~]# curl 172.16.16.172
189 test web01
190 [root@localhost ~]# curl 172.16.16.172
191 test web02
192 [root@localhost ~]# curl 172.16.16.172
193 test web02
194 [root@localhost ~]# curl 172.16.16.172
195 test web01
196 [root@localhost ~]# curl 172.16.16.172
197 test web02
198 [root@localhost ~]# curl 172.16.16.172
199 test web02
200 [root@localhost ~]# curl 172.16.16.172
201 test web01
202
203 //也可以使用for循环,可以更加清晰的看出循环结果
204 [root@localhost ~]# for i in $( seq 10 );do curl 172.16.16.172;done
205 test web02
206 test web02
207 test web01
208 test web02
209 test web02
210 test web01
211 test web02
212 test web02
213 test web01
214 test web02
215
216 101217 //查看当前lvs的运行状态
218 [root@localhost ~]# ipvsadm -ln
219 IP Virtual Server version 1.2.1 (size=4096)
220 Prot LocalAddress:Port Scheduler Flags
221 	-> RemoteAddress:Port Forward Weight ActiveConn InActConn
222 TCP 172.16.16.172:80 wrr
223 	-> 192.168.10.102:80 Masq 1 0 3 
224 	-> 192.168.10.103:80 Masq 2 0 7
225
226 104227 //关闭防火墙
228 [root@localhost ~]# systemctl stop firewalld
229 [root@localhost ~]# setenforce 0
230
231 //安装软件包
232 [root@localhost ~]# yum -y install nfs-utils rpcbind
233
234 //往配置文件中添加东西(文件里面本来就是空的)
235 [root@localhost ~]# vim /etc/exports
236 /opt/wwwroot 192.168.10.0/24(rw,sync,no_root_squash)
237
238 //启动服务
239 [root@localhost ~]# systemctl start rpcbind
240 [root@localhost ~]# systemctl start nfs
241
242 //查进程,查的方式不同
243 [root@localhost ~]# netstat -anpt | grep rpc
244 tcp 	0 	0 0.0.0.0:35109 	0.0.0.0:* 	LISTEN 11836/rpc.statd 
245 tcp 	0 	0 0.0.0.0:111 		0.0.0.0:* 	LISTEN 11814/rpcbind 
246 tcp 	0 	0 0.0.0.0:20048 	0.0.0.0:* 	LISTEN 11846/rpc.mountd 
247 tcp6 	0 	0 :::111 			:::* 		LISTEN 11814/rpcbind 
248 tcp6 	0 	0 :::20048 			:::* 		LISTEN 11846/rpc.mountd 
249 tcp6 	0 	0 :::48217 			:::* 		LISTEN 11836/rpc.statd 
250 [root@localhost ~]# ps -aux | grep nfs
251 root 	11854 0.0 0.0 		0  	 0 ? 	 I 	11:00 	0:00 [nfsd]
252 root	11855 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
253 root 	11856 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
254 root 	11857 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
255 root 	11858 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
256 root 	11859 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
257 root 	11860 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
258 root 	11861 0.0 0.0 		0 	 0 ? 	 I 	11:00 	0:00 [nfsd]
259 root 	11893 0.0 0.0  112840 2208 pts/0 S+ 11:01   0:00 grep --color=auto
nfs
260
261 //创建共享目录
262 [root@localhost ~]# mkdir /opt/wwwroot
263 [root@localhost ~]# cd /opt/wwwroot/
264 [root@localhost wwwroot]# vim index.html
265 test web
266
267 //前往102、103挂载共享目录
268 102103269 //安装软件包
[root@localhost ~]# yum -y install nfs-utils
271
272 //挂载共享目录
273 [root@localhost ~]# mount 192.168.10.104:/opt/wwwroot /var/www/html
274
275 201276 //测试
277 [root@localhost ~]# for i in $( seq 10 );do curl 172.16.16.172;done
278 test web
279 test web
280 test web
281 test web
282 test web
283 test web
284 test web
285 test web
286 test web
287 test web
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值