LVS搭建负载均衡
LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器。它是我们国家的章文嵩博士的一个开源项目。在linux内核2.6中,它已经成为内核的一部分,在此之前的内核版本则需要重新编译内核。
1、抗负载能力强,因为lvs工作方式的逻辑是非常之简单,而且工作在网络4层仅做请求分发之用,没有流量,所以在效率上基本不需要太过考虑。在我手里的lvs,仅仅出过一次问题:在并发最高的一小段时间内均衡器出现丢包现象,据分析为网络问题,即网卡或linux2.4内核的承载能力已到上限,内存和cpu方面基本无消耗。
2、配置性低,这通常是一大劣势,但同时也是一大优势,因为没有太多可配置的选项,所以除了增减服务器,并不需要经常去触碰它,大大减少了人为出错的几率。
3、工作稳定,因为其本身抗负载能力很强,所以稳定性高也是顺理成章,另外各种lvs都有完整的双机热备方案,所以一点不用担心均衡器本身会出什么问题,节点出现故障的话,lvs会自动判别,所以系统整体是非常稳定的。
4、无流量,上面已经有所提及了。lvs仅仅分发请求,而流量并不从它本身出去,所以可以利用它这点来做一些线路分流之用。没有流量同时也保住了均衡器的IO性能不会受到大流量的影响。
5、基本上能支持所有应用,因为lvs工作在4层,所以它可以对几乎所有应用做负载均衡,包括http、数据库、聊天室等等。
1:负载度 LVS KO Nginx
2:功能多少 Nginx KO LVS
3:稳定度 LVS KO Nginx
4:服务器性能要求 LVS KO Nginx
调度器的实现技术中,IP负载均衡技术是效率最高的,IP虚拟服务器软件(IPVS)是在linux内核中实现的。
1:VS/NAT
2: VS/TUN
3: VS/DR
1:轮叫调度(Round-RobinScheduling)
2: 加权轮叫调度(Weighted Round-Robin Scheduling)
3:最小连接调度(Least-Connection Scheduling)
4:加权最小连接调度(Weighted Least-Connection Scheduling)
5:基于局部性的最少链接(Locality-Based Least Connections Scheduling)
6:带复制的基于局部性最少链接(Locality-Based Least Connectionswith Replication Scheduling)
7:目标地址散列调度(Destination Hashing Scheduling)
8:源地址散列调度(Source Hashing Scheduling)
9:最短预期延时调度(Shortest Expected Delay Scheduling)
10:不排队调度(Never Queue Scheduling)
对应:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
1、VIP(virtual ip):用来提供virtualserver服务的ip地址。分别绑定在Director一个物理网卡上(对外接收请求包)和RS的回环设备上(回环设备需要绑定两个ip,一个是127.0.0.1,另一个就是vip)。
2、DIP(director ip):与vip绑定在一个物理网卡上,用来转发请求包到RS的RIP对应的mac上,此设备可以通过arp请求获取RIP对应的mac地址。
3、RIP(real serverip):绑定在RS上的一个物理网卡上,用来接收从Directory转发过来的请求包。
l 系统:Centos6 (四台)
l 负载均衡:LVS + keepalived
l 负载均衡备机:LVS + keepalived
l 服务器1:Http
l 服务器2:Http
步骤
1. 启动服务器1的httpd服务
[root@slave01 ~]# service httpd status httpd is stopped [root@slave01 ~]# service httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.10.3 for ServerName [ OK ] |
2. 关闭防火墙
service iptables stop chkconfig iptables off |
3. 测试服务器1,新建index.html文件
[root@slave01 html]# cd /var/www/html [root@slave01 html]# pwd /var/www/html [root@slave01 html]# vi index.html |
4. 配置VIP
确定VIP:192.168.10.10
新建/etc/init.d/realserver文件,增加如下内容
SNS_VIP=192.168.8.150 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 |
参数说明:
SNS_VIP=192.168.1.98 #定义VIP变量 . /etc/rc.d/init.d/functions #导脚本库 case "$1" in #case语句 $1传递给该shell脚本的第一个参数 start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP #设置Lo:0 VIP netmask 及广播 /sbin/route add -host $SNS_VIP dev lo:0 ##route del 增加本地路由 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 # -p echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 #route del 删除本地路由 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" #$0 是脚本本身的名字 exit 1 #表示进程正常退出 esac #case结束 exit 0 #表示进程非正常退出 |
启动realserver
[root@master init.d]# chmod 755 realserver [root@master init.d]# service realserver start RealServer Start OK [root@master init.d]# |
检查ifconfig,增加如下内容
lo:0 Link encap:Local Loopback inet addr:192.168.10.100 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:16436 Metric:1 |
5. 在服务器2执行步骤1、2、3、4.
6. 在负载均衡服务器上安装keepalived
将keepalived-1.2.7-3.el6.i686.rpm上传到负载均衡服务器上
安装并启动
-rw-r--r--. 1 root root 174004 Feb 23 2013 keepalived-1.2.7-3.el6.i686.rpm [root@master Downloads]# rpm -ivh keepalived-1.2.7-3.el6.i686.rpm warning: keepalived-1.2.7-3.el6.i686.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Preparing... ########################################### [100%] 1:keepalived ########################################### [100%] [root@master Downloads]# service keepalived status keepalived is stopped [root@master Downloads]# service keepalived start Starting keepalived: [ OK ] [root@master Downloads]# |
7. 配置keepalived
#查看keepalived安装目录,配置文件目录 rpm -ql keepalived
|
修改配置文件/etc/keepalived/keepalived.conf
清空keepalived.conf文件
> /etc/keepalived/keepalived.conf |
修改后如下:
global_defs { # notification_email { # } # notification_email_from [email protected] # smtp_server smtp.exmail.qq.com # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.100 } } virtual_server 192.168.10.100 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 192.168.10.3 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.10.4 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } |
参数说明:
global_defs { ##全局配置部分 # notification_email { ##下面几行均为全局通知配置,可以实现出现问题后报警,但功能有限,因此注释掉,并采用Nagios监视lvs运行情况 # } # notification_email_from [email protected] # smtp_server smtp.exmail.qq.com # smtp_connect_timeout 30 router_id LVS_DEVEL ##设置lvs的id,在一个网络内应该是唯一的 } vrrp_instance VI_1 { ##设置vrrp组,唯一且同一LVS服务器组要相同 state MASTER ##备份LVS服务器设置为BACKUP interface eth0 # #设置对外服务的接口 virtual_router_id 51 ##设置虚拟路由标识 priority 100 #设置优先级,数值越大,优先级越高,backup设置为99,这样就能实现当master宕机后自动将backup变为master,而当原master恢复正常时,则现在的master再次变为backup。 advert_int 1 ##设置同步时间间隔 authentication { ##设置验证类型和密码,master和buckup一定要设置一样 auth_type PASS auth_pass 1111 } virtual_ipaddress { ##设置VIP,可以多个,每个占一行 192.168.18.60 } } virtual_server 192.168.18.60 80 { delay_loop 6 ##健康检查时间间隔,单位s lb_algo wrr ##负载均衡调度算法设置为加权轮叫 lb_kind DR ##负载均衡转发规则 nat_mask 255.255.255.0 ##网络掩码,DR模式要保障真实服务器和lvs在同一网段 persistence_timeout 50 ##会话保持时间,单位s protocol TCP ##协议 real_server 192.168.18.61 80 { ##真实服务器配置,80表示端口 weight 3 ##权重 TCP_CHECK { ##服务器检测方式设置 keepalived的健康检查方式 有:HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK connect_timeout 0 ##连接超时时间 nb_get_retry 3 ##失败重试次数 delay_before_retry 3 ##失败重试的间隔时间 connect_port 80 ##连接的后端端口 } }
real_server 192.168.18.62 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
|
启动keepalived,并查看配置文件,是否正确启动
注:由于keepalived配置文件有语法错误也能启动,因此看到启动了lvs服务,不代表配置文件没有错误,如果遇到lvs不能正常转发,及时跟踪日志进行处理。
[root@master keepalived]# service keepalived start Starting keepalived: [ OK ] [root@master keepalived]# tail -f /var/log/messages Jan 31 00:54:27 master Keepalived_healthcheckers[3154]: Using LinkWatch kernel netlink reflector... Jan 31 00:54:27 master Keepalived_healthcheckers[3154]: Activating healthchecker for service [192.168.10.3]:80 Jan 31 00:54:27 master Keepalived_healthcheckers[3154]: Activating healthchecker for service [192.168.10.4]:80 Jan 31 00:54:27 master kernel: IPVS: [wrr] scheduler registered. Jan 31 00:54:27 master Keepalived_vrrp[3155]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 31 00:54:28 master Keepalived_vrrp[3155]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 31 00:54:28 master Keepalived_vrrp[3155]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 31 00:54:28 master Keepalived_vrrp[3155]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.100 Jan 31 00:54:28 master Keepalived_healthcheckers[3154]: Netlink reflector reports IP 192.168.10.100 added Jan 31 00:54:33 master Keepalived_vrrp[3155]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.100 |
已相同的方式搭建负载均衡备机,备机配置文件如下
global_defs { # notification_email { # } # notification_email_from [email protected] # smtp_server smtp.exmail.qq.com # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BAKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.100 } } virtual_server 192.168.10.100 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 192.168.10.3 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.10.4 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } |