centos6.4下LVS+keepalived的高可用(LVS/DR模式)
LVS負載均衡機制:
LVS工作在網絡層。相對于其它負載均衡的解決辦法,比如DNS域名輪流解析、應用層負載的調度、客戶端的調度等,它的效率是非常高的。LVS的通過控制IP來實現負載均衡。IPVS是其具體的實現模塊。IPVS的主要作用:安裝在Director Server上面,在Director Server虛擬一個對外訪問的IP(VIP)。用戶訪問VIP,到達Director Server,Director Server根據一定的規則選擇一個Real Server,處理完成后然后返回給客戶端數據。這些步驟產生了一些具體的問題,比如如何選擇具體的Real Server,Real Server如果返回給客戶端數據等等。IPVS為此有三種機制:
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肯定會不堪重負。
2.VS/TUN(Virtual Server via IP Tunneling),即IP隧道技術實現虛擬服務器。它跟VS/NAT基本一樣,但是Real server是直接返回數據給客戶端,不需要經過Diretor server,這大大降低了Diretor server的壓力。
3.VS/DR(Virtual Server via Direct Routing),即用直接路由技術實現虛擬服務器。跟前面兩種方式,它的報文轉發方法有所不同,VS/DR通過改寫請求報文的MAC地址,將請求發送到Real Server,而Real Server將響應直接返回給客戶,免去了VS/TUN中的IP隧道開銷。這種方式是三種負載調度機制中性能最高最好的,但是必須要求Director Server與Real Server都有一塊網卡連在同一物理網段上
//環境介紹
#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/ifconfiglo: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/ifconfiglo: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}" exit1 esac ----------------------------------------------------------------- 2.shell> /etc/init.d/realserverstart//【Direct server上操作】
1.[master]修改配置文件 shell> vim /etc/keepalived/keepalived.conf --------------------------------------------- global_defs { notification_email { lij@***.com } notification_email_from lij@***.com smtp_server lij@***.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/24dev 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】修改配置文件
//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故障轉移成功
轉載于:https://blog.51cto.com/3974020/1393370
總結
以上是生活随笔為你收集整理的centos6.4下LVS+keepalived的高可用(LVS/DR模式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装配置nova
- 下一篇: 那些开源程序中让人叹为观止的代码 - 3