ipvs负载均衡(lvs)html
IPVS在内核中的负载均衡调度是以链接为粒度的。在HTTP协议(非持久)中,每一个对象从WEB服务器上获取都须要创建一个TCP链接,同一用户 的不一样请求会被调度到不一样的服务器上,因此这种细粒度的调度在必定程度上能够避免单个用户访问的突发性引发服务器间的负载不平衡。node
在内核中的链接调度算法上,IPVS已实现了如下十种调度算 (http://zh.linuxvirtualserver.org/node/35)linux
轮叫调度(Round-Robin Scheduling) web
加权轮叫调度(Weighted Round-Robin Scheduling) 算法
最小链接调度(Least-Connection Scheduling) 后端
加权最小链接调度(Weighted Least-Connection Scheduling) 浏览器
基于局部性的最少连接(Locality-Based Least Connections Scheduling) 服务器
带复制的基于局部性最少连接(Locality-Based Least Connections with Replication Scheduling) 负载均衡
目标地址散列调度(Destination Hashing Scheduling) tcp
源地址散列调度(Source Hashing Scheduling)
最短预期延时调度(Shortest Expected Delay Scheduling)
不排队调度(Never Queue Scheduling)
具体的调度算法会在之后的博客中概述!
################################################################################
注意:实验以前关闭防火墙与selinux
实验环境:rhel6.5
lvs调度器主机:172.25.28.8 vm8.example.com
后台web应用服务器:172.25.28.1 vm1.example.com
172.25.28.4 vm4.example.com
两台web服务主机安装httpd:yum install httpd -y
两台web主机执行如下操做:
[root@vm1 ~]# cd /var/www/html/ 分别 在各自默认发布目录下创建html文件
[root@vm1 html]# cat index.html
vm1.example.com
[root@vm4 html]# cat index.html
vm4.example.com
[root@vm8 ~]# yum install -y ipvsadm 在调度主机上面安装软件
[root@vm8 ~]# ifconfig eth0:0 172.25.28.100 netmask 255.255.255.0 up 设置虚拟ip
[root@vm1 html]# ifconfig eth0:0 172.25.28.100 netmask 255.255.255.255 设置后端应用服务器的虚拟ip
vm1的操做与vm4进行同样的操做
[root@vm1 html]# yum install -y arptables_jf
[root@vm1 html]# /etc/init.d/arptables_jf start
[root@vm1 html]# arptables -A IN -d 172.25.28.100 -j DROP
[root@vm1 html]# arptables -A OUT -s 172.25.28.100 -j mangle --mangle-ip-s 172.25.28.1
[root@vm1 html]# /etc/init.d/arptables_jf save
Saving current rules to /etc/sysconfig/arptables: [ OK ]
[root@vm4 html]# yum install -y arptables_jf
[root@vm4 html]# /etc/init.d/arptables_jf start
[root@vm4 html]# arptables -A IN -d 172.25.28.100 -j DROP
[root@vm4 html]# arptables -A OUT -s 172.25.28.100 -j mangle --mangle-ip-s 172.25.28.4
[root@vm4 html]# /etc/init.d/arptables_jf save
Saving current rules to /etc/sysconfig/arptables: [ OK ]
[root@vm8 ~]# ipvsadm -A -t 172.25.28.100:80 -s rr
[root@vm8 ~]# ipvsadm -a -t 172.25.28.100:80 -r 172.25.28.1:80 -g
[root@vm8 ~]# ipvsadm -a -t 172.25.28.100:80 -r 172.25.28.4:80 -g
测试:
在浏览器中输入:http://172.25.28.100 会负载均衡显示应用服务器的首页内容
可使用命令在调度主机上查看:
[root@vm8 ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.28.100:http rr
-> vm1.example.com:http Route 1 0 2
-> vm4.example.com:http Route 1 0 2
-A --add-service 在内核的虚拟服务器表中添加一条新的虚拟服务器记录。也
就是增长一台新的虚拟服务器。
-t --tcp-service service-address 说明虚拟服务器提供的是tcp 的服务
[vip:port] or [real-server-ip:port]
-s --scheduler scheduler 使用的调度算法,有这样几个选项
rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq, 默认的调度算法是: wlc.
-a --add-server 在内核虚拟服务器表的一条记录里添加一条新的真实服务器
记录。也就是在一个虚拟服务器中增长一台新的真实服务器
-r --real-server server-address 真实的服务器[Real-Server:port]
-g --gatewaying 指定LVS 的工做模式为直接路由模式(也是LVS 默认的模式)
-L|-l --list 显示内核虚拟服务器表
-C --clear 清除内核虚拟服务器表中的全部记录。