linux使用ipvsadm实现web负载均衡
一,操作系统,坏境
从外网访问WEB服务器时先访问到192.168.0.44这台机器,通过192.168.0.44的机器搭建ipvsadm服务,当访问192.168.0.44这台机器的80端口时,将循环将访问者转发到内网的其他俩台机器上。其中0.44的服务器被当做路由器使,安装有俩块网卡。
Red Hat AS5 linux
二,开始搭建
1,下载软件并安装ipvsadm-1.24-12.el5.i386.rpm
2, 查看版本信息
[root@station44 Desktop]# rpm -qa |grep ipvsadm
ipvsadm-1.24-12.el5
[root@station44 ~]# ipvsadm --version
ipvsadm v1.24 2003/06/07 (compiled with popt and IPVS v1.2.0)
[root@station44 ~]#
ipvsadm-1.24-12.el5
[root@station44 ~]# ipvsadm --version
ipvsadm v1.24 2003/06/07 (compiled with popt and IPVS v1.2.0)
[root@station44 ~]#
3,开启路由转发
[root@station44 ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.ip_forward = 1
4,设置NAT软发
[root@station44 ~]#
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0
-j MASQUERADE
[root@station44 ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
[root@station44 ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 192.168.1.0/24 0.0.0.0/0
target prot opt source destination
MASQUERADE all -- 192.168.1.0/24 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
5,添加服务器
[root@station44 ~]# ipvsadm -A -t 192.168.0.44:80 -s rr
[root@station44 ~]# ipvsadm -a -t 192.168.0.44:80 -r 192.168.1.206:80 -m -w 1
[root@station44 ~]# ipvsadm -a -t 192.168.0.44:80 -r 192.168.1.216:80 -m -w 1
[root@station44 ~]# ipvsadm -a -t 192.168.0.44:80 -r 192.168.1.217:80 -m -w 1
[root@station44 ~]# 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.0.44:80 rr
-> 192.168.1.206:80 Local 1 0 0
-> 192.168.1.217:80 Masq 1 0 0
-> 192.168.1.216:80 Masq 1 0 0
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.44:80 rr
-> 192.168.1.206:80 Local 1 0 0
-> 192.168.1.217:80 Masq 1 0 0
-> 192.168.1.216:80 Masq 1 0 0
6,其他从服务器端设置
将网关指向192.168.1.206
[root@station216 ~]# route add default gw 192.168.1.206
[root@station217~]# route add default gw 192.168.1.206
7,启动http服务,然后做测试
[root@station216 ~]# cd /var/www/html/
[root@station216 html]# vi index.html
[root@station216 html]# vi index.html
添加 192.168.1.216
[root@station217 ~]# cd /var/www/html/
[root@station217 html]# vi index.html
[root@station217 html]# vi index.html
添加 192.168.1.217
[root@station44 ~]# cd /var/www/html/
[root@station44 html]# vi index.html
[root@station44 html]# vi index.html
添加 192.168.1.206
8,在客户端访问
http://192.168.0.44
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
这样每刷新一次就看到不同的页面。巡环改变
9,设置访问相同的网页文件
[root@station44 ~]# mkdir /www/
[root@station44 ~]# mkdir /www/
[root@station44 ~]# cd /www/
[root@station44 www]# more index.html
This is Cluster
[root@station44 www]#
[root@station44 ~]# cd /www/
[root@station44 www]# more index.html
This is Cluster
[root@station44 www]#
10, 将/www目录共享出去
[root@station44 ~]# cat /etc/exports
/www 192.168.1.0/24(rw)
[root@station44 ~]#
/www 192.168.1.0/24(rw)
[root@station44 ~]#
11,让其他机器使用/www下的网页文件
[root@station44 ~]# mount 192.168.1.206:/www /var/www/html/
[root@station216 ~]# mount 192.168.1.216:/www /var/www/html/
[root@station217~]# mount 192.168.1.217:/www /var/www/html/
12、然后从客户端访问
http://192.168.0.44接可以看到同样的网页内容了
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
13,可以使用命令查看分发状态
[root@station44 ~]# 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.0.44:80 rr
-> 192.168.1.206:80 Local 1 0 1
-> 192.168.1.217:80 Masq 1 0 2
-> 192.168.1.216:80 Masq 1 0 1
[root@station44 ~]#
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.44:80 rr
-> 192.168.1.206:80 Local 1 0 1
-> 192.168.1.217:80 Masq 1 0 2
-> 192.168.1.216:80 Masq 1 0 1
[root@station44 ~]#
每次客户端访问时,后面那3个数在变化,加1
14,保存配置
[root@station44 ~]# ipvsadm -save
[root@station44 ~]#service iptables save
本文转自zhaoyun00 51CTO博客,原文链接:http://blog.51cto.com/zhaoyun/561584