歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux配置

centos6.2,64bit搭建LVS+Keepalived(DR模式)

     最近公司網站訪問又見瓶頸,為了緩解壓力,決定用LVS+Keepalived來實現負載均衡.在前面的文章中已經寫過了用NAT模式來實現負載均衡 http://duyunlong.blog.51cto.com/1054716/716138,而今天用的是DR模式,其實NAT跟DR模式,各有優缺點,根據實際情況來選擇適合自己的。下面看下具體步驟以及注意事項。 拓撲圖如下:


首先看下我用的系統,系統為centos6.2,64位系統,用getconf LONG_BIT,和uname -a都可以查看
[root@localhost src]# getconf LONG_BIT
64
[root@localhost src]# uname -a
Linux localhost 2.6.32-279.19.1.el6.x86_64 #1 SMP Wed Dec 19 07:05:20 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

然後,分析下搭建這個LVS+Keepalived(DR)需要的關鍵步驟及注意事項,第一、DR模式主要是靠虛擬IP來實現,那麼每台機器都需要建立 一個虛擬IP,這個是必需的,第二、要注意的就是popt-static-1.13-7.el6.x86_64.rpm這個包,不裝這個包,安裝ipvs 的時候會報錯,第三、就是一些零碎的配置。
[root@localhost src]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[root@localhost src]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[root@localhost src]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm

然後,在LVS主跟備份上安裝
[root@localhost src]# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
[root@localhost src]# yum -y install kernel-devel make gcc openssl-devel libnl* popt*
[root@localhost src]# ln -s /usr/src/kernels/2.6.32-220.13.1.el6.x86_64/ /usr/src/linux
[root@localhost src]# tar -zxvf ipvsadm-1.26.tar.gz
[root@localhost src]# cd ipvsadm-1.26/
[root@localhost ipvsadm-1.26]# make
[root@localhost ipvsadm-1.26]# make install
[root@localhost ipvsadm-1.26]# cd ..
[root@localhost src]# tar -zxvf keepalived-1.2.2.tar.gz -C /usr/src/
[root@localhost src]# cd keepalived-1.2.2/
[root@localhost keepalived-1.2.2]# ./configure
[root@localhost keepalived-1.2.2]# make
[root@localhost keepalived-1.2.2]# make install
[root@localhost keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@localhost keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.2.2]# mkdir /etc/keepalived
[root@localhost keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/[root@localhost keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/

在兩台LVS上分別配置keepalived, vim /etc/keepalived/keepalived.conf
[root@localhost src]#  vim /etc/keepalived/keepalived.conf     
! Configuration File for keepalived    
global_defs {    
     router_id LVS_MASTER            #網上資料說這個值也需要修改,具體不詳,之前我們線上的主備就一直是一樣的 ^ ^還是修改一下吧!    
}    
vrrp_instance VI_1 {    
        state MASTER                         #LVS_Backup上修改成BACKUP    
        interface eth0    
        virtual_router_id 60 #默認51 主從都修改為60    
        priority 100                         #LVS_Backup上修改成80
        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 rr    
        lb_kind DR    
        nat_mask 255.255.255.0    
        persistence_timeout 50    
        protocol TCP    

        real_server 192.168.10.3 80 {    
                weight 1    
                TCP_CHECK {    
                connect_timeout 10    
                nb_get_retry 3    
                connect_port 80    
                }    
        }    
        real_server 192.168.10.4 80 {    
                weight 1    
                TCP_CHECK {    
                connect_timeout 10    
                nb_get_retry 3    
                connect_port 80    
                }    
        }    
}

在LVS_Backup上也進行配置,只要把標紅的地方改下就OK,然後我們在真實機上配置虛擬IP啟動腳本
[root@localhost src]#  vim /etc/init.d/realserver.sh            
#!/bin/bash    
SNS_VIP=192.168.10.100                    
. /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

在兩台真是機器上面配置apache並啟動realserver.sh
[root@localhost src]# yum -y install httpd
[root@localhost src]# cd /var/www/html/
[root@localhost html]# cat index.html < h1>RealServer 192.168.10.3</h1>
[root@localhost html]# /etc/init.d/httpd start
[root@localhost src]# /etc/init.d/realserver.sh start

配置完後我們來測試下:分別啟動Keepalived
[root@localhost src]# chkconfig keepalived on
[root@localhost src]# /etc/init.d/keepalived restart
[root@localhost src]# ipvsadm
IP Virtual Server version 1.2.1
(size=4096)Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.100:http rr persistent 50
-> 192.168.10.3:http Route 1 0 0
-> 192.168.10.4:http Route 1 0 0

關閉LVS_MASTER,然後查看LVS_backup日志
[root@localhost src]# tail -f /var/log/messages
Mar 21 07:22:28 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.
Mar 21 07:22:29 host2 Keepalived_healthcheckers: Netlink
reflector reports IP 192.168.10.100 added
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 192.168.10.100

測試訪問
[root@localhost html]# curl http://192.168.10.100
< h1>RealServer 192.168.10.4</h1>

然後,啟動LVS_MASTER查看LVS_BACKUP日志
[root@localhost src]# tail -f /var/log/messages
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
Received higher prio advert
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.
Mar 21 07:26:20 host2 Keepalived_healthcheckers: Netlink
reflector reports IP 192.168.10.100 removed

Copyright © Linux教程網 All Rights Reserved