centos6.4下keepalived+nginx的高可用(LVS/DR模式)

LVS負載均衡機制:nginx

LVS工做在網絡層。相對於其它負載均衡的解決辦法,好比DNS域名輪流解析、應用層負載的調度、客戶端的調度等,它的效率是很是高的。LVS的經過控制IP來實現負載均衡。IPVS是其具體的實現模塊。IPVS的主要做用:安裝在Director Server上面,在Director Server虛擬一個對外訪問的IP(VIP)。用戶訪問VIP,到達Director Server,Director Server根據必定的規則選擇一個Real Server,處理完成後而後返回給客戶端數據。這些步驟產生了一些具體的問題,好比如何選擇具體的Real Server,Real Server若是返回給客戶端數據等等。IPVS爲此有三種機制:c++

1.VS/NAT(Virtual Server via Network Address Translation),即網絡地址翻轉技術實現虛擬服務器。當請求來到時,Diretor server上處理的程序將數據報文中的目標地址(即虛擬IP地址)改爲具體的某臺Real Server,端口也改爲Real Server的端口,而後把報文發給Real Server。Real Server處理完數據後,須要返回給Diretor Server,而後Diretor server將數據包中的源地址和源端口改爲VIP的地址和端口,最後把數據發送出去。由此能夠看出,用戶的請求和返回都要通過Diretor Server,若是數據過多,Diretor Server確定會不堪重負。shell

2.VS/TUN(Virtual Server via IP Tunneling),即IP隧道技術實現虛擬服務器。它跟VS/NAT基本同樣,可是Real server是直接返回數據給客戶端,不須要通過Diretor server,這大大下降了Diretor server的壓力。vim

3.VS/DR(Virtual Server via Direct Routing),即用直接路由技術實現虛擬服務器。跟前面兩種方式,它的報文轉發方法有所不一樣,VS/DR經過改寫請求報文的MAC地址,將請求發送到Real Server,而Real Server將響應直接返回給客戶,免去了VS/TUN中的IP隧道開銷。這種方式是三種負載調度機制中性能最高最好的,可是必需要求Director Server與Real Server都有一塊網卡連在同一物理網段上bash

//環境介紹服務器

#VS/DR模式下,Direct server在VIP:80端口監聽用戶請求,改寫請求報文的MAC地址,將請求負載到real server上,real server將響應直接返回給用戶,所以全部的主機必須在同一個網段,且real server能夠直接與用戶通訊

1.主機配置
DIRECT SERVER:10.10.54.155  
vip:10.10.54.151
DIRECT BACKUP:10.10.54.156
vip:10.10.54.151

real server:10.10.54.222(80) --nginx
real server:10.10.54.226(80) --nginx

//軟件安裝網絡

1.所需軟件
ipvsadm-1.26.tar.gz  
keepalived-1.2.9.tar.gz
2.安裝ipvsadm
shell> yum -y install wget libnl* popt* gcc.x86_64 gcc-c++.x86_64 gcc-objc++.x86_64 kernel-devel.x86_64 make popt-static.x86_64
shell> tar xvf ipvsadm-1.26.tar.gz
shell> cd ipvsadm-1.26
shell> ./configure && make && make install
3.安裝keepalived
shell> yum install -y net-snmp.x86_64 net-snmp-devel.x86_64
shell> tar xvf keepalived-1.2.9.tar.gz 
shell> cd keepalived-1.2.9
shell> ./configure && make && make install
shell> ./configure --prefix=/usr/local/keepalived --enable-snmp --sysconfdir=/etc
shell> cp /usr/local/keepalived/sbin/keepalived  /sbin/
shell> cp /usr/local/keepalived/bin/genhash /bin/

//【real server上操做】負載均衡

1.編輯realserver腳本
shell> vim /etc/init.d/realserver
-----------------------------------------------------
#!/bin/bash
#description:start realserver
#script_name:realserver_config
VIP=10.10.54.151#虛擬IP
source /etc/init.d/functions
case "$1" in
start)
echo "start LVS of realserver."
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
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
;;
stop)
/sbin/ifconfig lo:0 down
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 "Usage: $0 {start|stop}"
exit 1
esac
-----------------------------------------------------------------
2.shell> /etc/init.d/realserver start

//【Direct server上操做】oop

1.[master]修改配置文件
shell> vim /etc/keepalived/keepalived.conf 
---------------------------------------------
global_defs {
   notification_email {
     lij@ssr.com
   }
   notification_email_from lij@ssr.com
   smtp_server lij@ssr.com
   smtp_connect_timeout 30
   router_id LVS_MASTER2
}
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 {
        10.10.54.151/24 dev eth0 label eth0:1#虛擬IP,用戶可見IP
    }
    }
virtual_server 10.10.54.151 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
#   nat_mask 255.255.255.0
#   persistence_timeout 50
    protocol TCP
    real_server 10.10.54.157 80 {#real server上80端口
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
        }
    real_server 10.10.54.159 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
        }
}

#查看IPVS表
shell> ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.10.54.151:80 rr
  -> 10.10.54.157:80              Route   1      0          0         
  -> 10.10.54.159:80              Route   1      0          0      
--------------------------------------------------                                                                      

2.【backup server】修改配置文件
---------------------------------------------------
global_defs {
   notification_email {
     lij@ssr.com
   }
   notification_email_from lij@ssr.com
   smtp_server lij@ssr.com
   smtp_connect_timeout 30
   router_id LVS_BACKUP        #改1
}
vrrp_instance VI_1 {
    state MASTER        #改2
    interface eth0
    virtual_router_id 51
    priority 80        #改3
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
...
}

#backup上查看IPVS表
shell> ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.10.54.151:80 rr
  -> 10.10.54.157:80              Route   1      0          0         
  -> 10.10.54.159:80              Route   1      0          0      
-----------------------------------------------

//telnet 測試負載均衡和故障轉移性能

#####################
測試負載均衡
1.master 和 backup上啓動keepalived
/etc/init.d/keepalived start

2.150主機上telnet VIP
shell> telnet 10.10.54.151 80
Trying 10.10.54.151...
Connected to 10.10.54.151.
Escape character is '^]'.

3.master主機上查看IPVS信息
shell> ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.10.54.151:80 rr
  -> 10.10.54.157:80              Route   1      1          0         
  -> 10.10.54.159:80              Route   1      0          1        
##由上面查看157機子上"ActiveConn"變爲1
##從新執行 telnet 10.10.54.151 80 操做後,發現159機子"ActiveConn"變爲1
##上面測試結果顯示,LVS負載均衡已經成功



###################
測試故障轉移是否成功
1.當掉master主機155
shell> /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

2.backup主機上查看ip信息
shell> ip add list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:1f:da:47 brd ff:ff:ff:ff:ff:ff
    inet 10.10.54.156/24 brd 10.10.54.255 scope global eth0
    inet 10.10.54.151/24 scope global secondary eth0:1
    inet6 fe80::a00:27ff:fe1f:da47/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:ac:b4:36 brd ff:ff:ff:ff:ff:ff

3.測試backup是否能夠負載
shell> telnet 10.10.54.151 80
shell> ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.10.54.151:80 rr
  -> 10.10.54.157:80              Route   1      1          0         
  -> 10.10.54.159:80              Route   1      0          0     
##由上面可知,LVS故障轉移成功
相關文章
相關標籤/搜索