> Linux集群 > 服务器集群 >

《Linux keepalived与lvs的深入分析》二之虚拟服务器配置分析


六)虚拟服务器配置分析
 
virtual_server vip 端口{}是虚拟服务器配置定义部份.
 
下面的示例是lvs1中的配置:
virtual_server 10.1.1.166 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 10.1.1.163 80 {
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.1.1.164 80 {
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
 
delay_loop 6表示健康检查时间间隔,单位是秒,这里表示6秒检查一下real server.
示例:
10.1.1.160:~# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr persistent 60
  -> 10.1.1.163:www               Route   1      0          0         
  -> 10.1.1.164:www               Route   1      0          0       
 
停止10.1.1.163上面的apache服务,如下:
/etc/init.d/apache2 stop
 
再次检查lvs1上面的real server,如下:  
10.1.1.160:~# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr persistent 60
  -> 10.1.1.164:www               Route   1      0          0      
 
我们看到10.1.1.163被踢出了lvs.
 
 
lb_algo wrr表示负载均衡调度算法,互联网应用常使用wlc或rr.这里我们使用wrr,关于调度算法后面再进行分析.
 
lb_kind负载均衡转发规则,一般包括DR,NAT,TUN这3种,这部份内容我们后面进行分析.
 
persistence_timeout,会话保持时间,单位是秒
这个选项对动态网站很有用处:当用户从远程用帐号进行登陆网站时,有了这个会话保持功能,就能把用户的请求转发给同一个应用服务器.
在这里,我们来做一个假设,假定现在有一个lvs 环境,使用DR转发模式,真实服务器有2个,如果负载均衡器不启用会话保持功能.当用户第一次访问的时候,他的访问请求被负载均衡器转给某个真实服务器,这样他看到一个登陆页面,第一次访问完毕;
接着他在登陆框填写用户名和密码,然后提交,这时候,问题就可能出现了—登陆不能成功.因为没有会话保持,负载均衡器可能会把第2次的请求转发到其他的服务器.
 
 
virtual_server 10.1.1.166 80 {
        delay_loop 6
        lb_algo wrr
        lb_kind TUN
        persistence_timeout 60
        protocol TCP
        real_server 10.1.1.163 80 {
                weight 5
                TCP_CHECK {
                        connect_timeout 10
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
        }
        real_server 10.1.1.164 80 {
                weight 5
                TCP_CHECK {
                        connect_timeout 10
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
        }
}
 
 
ab -c 100 -n 10000 http://10.1.1.166/index.html
 
 
查看lvs调度情况,如下:
ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr persistent 60
  -> 10.1.1.164:www               Tunnel  5      44         2131      
  -> 10.1.1.163:www               Tunnel  5      0          0         
10.1.1.160:~# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr persistent 60
  -> 10.1.1.164:www               Tunnel  5      71         2667      
  -> 10.1.1.163:www               Tunnel  5      0          0         
10.1.1.160:~# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr persistent 60
  -> 10.1.1.164:www               Tunnel  5      54         3563      
  -> 10.1.1.163:www               Tunnel  5      0          0         
10.1.1.160:~# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr persistent 60
  -> 10.1.1.164:www               Tunnel  5      40         4406      
  -> 10.1.1.163:www               Tunnel  5      0          0  
 
注:我们看到请求都转发到了10.1.1.164,证明了保持会话功能的作用.
 
protocol是转发协议,分为有tcp和udp两种,这里不做详细分析.
 
real_server 10.1.1.163 80 {
                weight 5
                TCP_CHECK {
                        connect_timeout 10
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
        }
这里是定义real server服务器池,weight表示权重,权重值在有权重的调试算法的策略中才有意义,比如wrr,wlc等.
 
inhibit_on_failure来设置如果服务器健康检查失败,将其weight设置为0,而不是直接从ipvs里面删除.
 
下面我们来做一下这个测试
修改lvs配置如下:
virtual_server 10.1.1.166 80 {
        delay_loop 6
        lb_algo wrr
        lb_kind TUN
        #persistence_timeout 60
        protocol TCP
        real_server 10.1.1.163 80 {
                weight 5
                inhibit_on_failure
                TCP_CHECK {
                        connect_timeout 10
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
        }
        real_server 10.1.1.164 80 {
                weight 5
                inhibit_on_failure
                TCP_CHECK {
                        connect_timeout 10
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
        }
}
 
查看lvs权重,如下:
ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr
  -> 10.1.1.164:www               Tunnel  0      0          0         
  -> 10.1.1.163:www               Tunnel  5      0          0         
 
  
客户端测试:
ab -c 100 -n 10000 http://10.1.1.166/index.html
 
查看负载情况,我们看到权重为0的real server不会接收任何访问,如下:
ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.1.166:www wrr
  -> 10.1.1.164:www               Tunnel  0      0          0         
  -> 10.1.1.163:www               Tunnel  5      66         2842      
 
 
TCP_CHECK {}是TCP方式的健康检查.
connect_timeout 10表示连接超时时间,这里连接real server的端口超过10秒没有反映,则将real server踢出lvs,或将权值清0.
nb_get_retry 3表示连接的重试次数,这里如果3次都连接失败,则将real server踢出lvs,或将权值清0.
delay_before_retry 3表示每次连接重试的间隔,这里的间隔是3秒.
connect_port 80表示连接测试的端口.


(责任编辑:IT)