当前位置: > Linux集群 > 负载均衡SLB >

Keepalived+LVS-DR模式配置高可用负载均衡集群

时间:2015-03-14 21:39来源:linux.it.net.cn 作者:IT
环境: 虚拟机:VMware Workstation
          操作系统:CentOS 5.5

 

一、keepalived+lvs-DR模式配置高可用负载均衡

拓扑如下:

 

二、服务器ip配置信息

1. ip_lb 主机(master) :

 

2.ip_lb_2 主机 (backup)

3.web1 主机

4.web2 主机

 

三、在master和backup分别查看是否有ipvsadm软件

1. 检查ipvsadm是否安装成功.可以直接输入

[root@localhost ~]# ipvsadm 

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
 
2. 检查模版是否加入内核
[root@localhost ~]# lsmod |grep ip_vs
 ip_vs 78081 0 

四、分别在web1和web2上面安装apache服务.
#  安装apache服务不说明了.  开放的端口是:8099

五、在master主机(192.168.96.2)和backup主机(192.168.96.22)上安装keepalived
1. keepalived-1.2.1.tar.gz     # 解压这个文件并且安装 (tar -zxvf keepalived-1.2.1.tar.gz)
2. ./configure --prefix=/home/keepalived/ --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
3.make
4.make install
5.配置文件的路径
cp /home/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /home/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /home/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /home/keepalived/sbin/keepalived /sbin/

6.配置keepalived.conf文件.( ip_lb 主机<master> )
vim /etc/keepalived/keepalived.conf


 
  1. #global define  
  2. global_defs {  
  3. router_id LVS_T1  
  4. }  
  5.   
  6.   
  7. vrrp_sync_group bl_group {  
  8. group {  
  9.   bl_one  
  10. }  
  11. }  
  12.   
  13. vrrp_instance bl_one {  
  14.     state MASTER  
  15.     interface eth0  
  16.     lvs_sync_daemon_interface eth0  
  17.     virtual_router_id 38  
  18.     priority 150  
  19.     advert_int 3  
  20.     authentication {  
  21.         auth_type PASS  
  22.         auth_pass 1111  
  23.     }  
  24.     virtual_ipaddress {  
  25.       192.168.96.100  
  26.     }  
  27. }  
  28.   
  29.   
  30. virtual_server 192.168.96.100 8099 {  
  31.     delay_loop 3  
  32.     lb_algo rr  
  33.     lb_kind DR  
  34.     persistence_timeout 1  
  35.     protocol TCP  
  36.   
  37.   
  38.     real_server 192.168.96.33 8099 {  
  39.         weight 1                                                                                                                                        
  40.         TCP_CHECK {                                                                                                                                     
  41.             connect_timeout 10                                                                                                                          
  42.             nb_get_retry 3                                                                                                                              
  43.             delay_before_retry 3                                                                                                                        
  44.             connect_port 8099                                                                                                                           
  45.         }                                                                                                                                               
  46.                                                                                                                                                         
  47.     }                                                                                                                                                   
  48.                                                                                                                                                         
  49.     real_server 192.168.96.4 8099 {                                                                                                                     
  50.         weight 1                                                                                                                                        
  51.         TCP_CHECK {                                                                                                                                     
  52.             connect_timeout 10                                                                                                                          
  53.             nb_get_retry 3                                                                                                                              
  54.             delay_before_retry 3                                                                                                                        
  55.             connect_port 8099                                                                                                                           
  56.         }                                                                                                                                               
  57.     }                                                                                                                                                   
  58. }                   

7、配置keepalived.conf文件.(ip_lb_2 主机 <backup> )
vim /etc/keepalived/keepalived.conf

[plain] view plaincopy
 
  1. #global define  
  2. global_defs {  
  3. router_id LVS_T2  
  4. }  
  5.   
  6.   
  7. vrrp_sync_group bl_group {  
  8. group {  
  9.   bl_one  
  10. }  
  11. }  
  12.   
  13. vrrp_instance bl_one {  
  14.     state BACKUP  
  15.     interface eth0  
  16.     lvs_sync_daemon_interface eth0  
  17.     virtual_router_id 38  
  18.     priority 120  
  19.     advert_int 3  
  20.     authentication {  
  21.         auth_type PASS  
  22.         auth_pass 1111  
  23.     }  
  24.     virtual_ipaddress {  
  25.       192.168.96.100  
  26.     }  
  27. }  
  28.   
  29.   
  30. virtual_server 192.168.96.100 8099 {  
  31.     delay_loop 3  
  32.     lb_algo rr  
  33.     lb_kind DR  
  34.     persistence_timeout 1  
  35.     protocol TCP  
  36.   
  37.   
  38.     real_server 192.168.96.33 8099 {                                                                                                                    
  39.         weight 1                                                                                                                                        
  40.         TCP_CHECK {                                                                                                                                     
  41.             connect_timeout 10                                                                                                                          
  42.             nb_get_retry 3                                                                                                                              
  43.             delay_before_retry 3                                                                                                                        
  44.             connect_port 8099                                                                                                                           
  45.         }                                                                                                                                               
  46.                                                                                                                                                         
  47.     }                                                                                                                                                   
  48.                                                                                                                                                         
  49.     real_server 192.168.96.4 8099 {                                                                                                                     
  50.         weight 1                                                                                                                                        
  51.         TCP_CHECK {                                                                                                                                     
  52.             connect_timeout 10                                                                                                                          
  53.             nb_get_retry 3                                                                                                                              
  54.             delay_before_retry 3                                                                                                                        
  55.             connect_port 8099                                                                                                                           
  56.         }                                                                                                                                               
  57.     }                                                                                                                                                   
  58. }        

8. 分别在主备上启动keepalived
[root@ip_lb home]# /etc/init.d/keepalived start
Starting keepalived: [  OK  ]

[root@ip_lb home]# tail -f /var/log/message


# PS:这里说的LVS.并不是用ipvsadm软件配置.而是直接用keepalived的virtual_server配置项控制的.
#  安装ipvsadm只是可以看到负载状况.其实只需要keepalived也可以实现负载均衡集群.

六、在web1和web2主机上配置LVS-DR的相关配置.
1. vim /etc/init.d/lvsDR_node   (web1主机)

 
  1. #! /bin/sh   
  2. # descript : start real server DR  
  3. VIP=192.168.96.100  
  4. . /etc/rc.d/init.d/functions  
  5. case "$1" in  
  6.        start)  
  7.              echo "start LVS of RealServer DR"  
  8.              /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up  
  9.              echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore  
  10.              echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce  
  11.                ;;  
  12.        stop)  
  13.              /sbin/ifconfig lo:0 down  
  14.              echo "close LVS of RealServer DR"  
  15.              echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore  
  16.              echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce  
  17.               ;;  
  18.          *)  
  19.               echo "Usage : $0 {start|stop}"  
  20.               exit 1  
  21. esac  

2. 把这个文件复制到web2主机上.
3. 在web1和web2上分别启动这个脚本:
    /etc/init.d/lvsDR_node start

七、web1和web2主机上开启httpd服务.
/etc/init.d/httpd start

# ================= TEST =======================================

八、测试是否可以负载均衡.
编写测试脚本:

 
  1. #! /bin/sh  
  2. for((i=1;i<=100;i++));do   
  3. curl http://192.168.96.100:8099 >> /tmp/q;  
  4. done  

结果:


九、测试断开一个web服务或加入一个web服务
现在关闭 web2主机(192.168.96.4) 的web 服务..
查看日志可以看到:


现在再把 web2主机(192.168.96.4)的web 服务加入进来:
查看日志可以看到:


十、 测试HA
现在模拟主(master)的keepalived主机死掉. 
备机的日志文件:


可以看到备机的keepalived已经开始工作....

整个部署就完成了.

官方参考中文文档:http://www.keepalived.org/pdf/sery-lvs-cluster.pdf



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容