LVS-NAT
配置环境:
调度器:
VIP:172.23.104.27
DIP:192.168.85.128
realserver:
RIP1:192.168.85.140 GW:192.168.85.128
RIP2:192.168.85.143 GW:192.168.85.128
确定调度器能ping通RS1和RS2;
RS1和RS2安装web服务,且调度器能访问RS1和RS2的web页面
[root@www ~]# curl
RS1:192.168.85.140
[root@www ~]# curl
RS2:192.168.85.143
1.检查内核是否支持LVS
LVS是在linux操作系统基础上建立虚拟服务器,实现服务节点之间的负载均衡。它是基于linux内核实现的,2.6.X内核默认集成了lvs模块,LVS常用负载均衡的实现是基于ip协议的,所以一般称为IPVS。
检查操作系统是否已经集成了LVS:
[root@www ~]# modprobe -l | grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
2.调度器所在主机安装配置ipvsadm,其他主机不安装
[root@www ~]# yum list all | grep ipvsadm
ipvsadm.i686 1.26-4.el6 base
[root@www ~]# yum install -y ipvsadm.i686
3.ipvsadm用法
http://blog.chinaunix.net/uid-30212356-id-5213284.html
4.配置
调度器端:
4.1添加一个虚拟IP记录(集群),调度方法为RR
[root@www ~]# ipvsadm -A -t 172.23.104.27:80 -s rr
4.2该集群内添加realserver:
[root@www ~]# ipvsadm -a -t 172.23.104.27:80 -r 192.168.85.140 -m
[root@www ~]# ipvsadm -a -t 172.23.104.27:80 -r 192.168.85.143 -m
4.3打开地址转发功能:
[root@www ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@www ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
4.4测试:
连接前:
[root@www ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.23.104.27:80 rr
-> 192.168.85.140:80 Masq 1 0 0
-> 192.168.85.143:80 Masq 1 0 0
用浏览器连续打开两个链接后:
[root@www ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.23.104.27:80 rr
-> 192.168.85.140:80 Masq 1 0 1
-> 192.168.85.143:80 Masq 1 0 1
或者:
[root@www ~]# ab -c 10 -n 10
[root@www ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.23.104.27:80 rr
-> 192.168.85.140:80 Masq 1 0 5
-> 192.168.85.143:80 Masq 1 0 5
5.调整调度方式为WRR测试
5.1[root@www ~]# ipvsadm -E -t 172.23.104.27:80 -s wrr
[root@www ~]# ipvsadm -e -t 172.23.104.27:80 -r 192.168.85.140:80 -w 2 -m
[root@www ~]# ipvsadm -e -t 172.23.104.27:80 -r 192.168.85.143:80 -w 2 -m
5.2通过172.23.104.27本地测试(本机要安装web服务):
[root@www ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.23.104.27:80 wrr
-> 192.168.85.140:80 Masq 2 0 0
-> 192.168.85.143:80 Masq 2 0 0
5.3输入命令ab -c 1000 -n 1000 index.html即可