直上幹活web
dr1上keepalived的配置:算法
/etc/keepalived/keepalived.confbash
global_defs { router_id LVS1 # 設置lvs的id,在一個網絡內應該是惟一的 } vrrp_sync_group test { #設置vrrp組 group { loadbalance } } vrrp_instance loadbalance { state MASTER #設置lvs的狀態,報錯MASTER和BACKUP兩種,必須大寫 interface eth0 #設置對外服務的接口 lvs_sync_daemon_inteface eth0 #設置lvs監聽的接口 virtual_router_id 51 #設置虛擬路由表示 priority 180 #設置優先級,數值越大,優先級越高 advert_int 1 #設置同步時間間隔 authentication { #設置驗證類型和密碼 auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.200 } } virtual_server 192.168.56.200 80 { delay_loop 6 #健康檢查時間間隔 lb_algo rr #負載均衡調度算法 lb_kind DR #負載均衡轉發規則 #persistence_timeout 20 #設置會話保持時間,對bbs等頗有用 protocol TCP #協議 real_server 192.168.56.105 80 { weight 3 #設置權重 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.56.106 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
dr2上keepalived的配置
網絡
/etc/keepalived/keepalived.conf負載均衡
global_defs { router_id LVS2 } vrrp_sync_group test { group { loadbalance } } vrrp_instance loadbalance { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.200 } } virtual_server 192.168.56.200 80 { delay_loop 6 lb_algo rr lb_kind DR #persistence_timeout 20 protocol TCP real_server 192.168.56.105 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.56.106 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }}
啓動dr1上的keepalivedide
keepalived -f /etc/keepalived/keepalived.confoop
查看信息3d
而後啓動dr2上keepalivedrouter
keepalived -f /etc/keepalived/keepalived.confserver
查看信息
在realserver1與realserver2上分別執行下面的腳本
/home/lhb/sh/rs.sh
#!/bin/bash vip=192.168.56.200 ifconfig lo:0 $vip netmask 255.255.255.255 route add -host $vip dev lo:0 route -n 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 #查看sysctl的配置變化,可不執行
而後咱們開一個客戶端方位vip:
在dr1上執行ipvsadm -ln
而後咱們在dr1上關閉keepalived
這是咱們繼續訪問vip,訪問正常如圖所示:
訪問正常,說明web業務沒有中止:
由此推斷咱們的dr2已經開始接管業務了.而後到dr2上看一下信息:
說明故障發生時,業務服務已經自動從dr1轉移到dr2上了。
而後當咱們的dr1修復好後,咱們執行keepalived -f /etc/keepalived/keepalived.conf
此時訪問vip,業務仍然正常訪問.
回到dr2上看信息
到此爲止,keepalived實現lvs的故障在主備機自動切換已經展現完畢。