KeepAlive+LVS 實現高可用負載均衡

環境介紹:

http 服務: node1(10.11.8.222), node2(10.11.8.158)
KeepAlive 服務: keep1(10.11.8.219), keep2(10.11.8.216)html

keep1 & keep2 # yum -y install ipvsadm keepalivednode

配置 real_server(node1, node2) :

編輯/etc/sysctl.conf 添加如下記錄web

net.ipv4.ip_forward = 1
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

重讀配置並添加 lo:0 別名:算法

root@node1:~# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
root@node1:~# ifconfig lo:0 10.11.8.100
root@node2:~# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
root@node2:~# ifconfig lo:0 10.11.8.100

給http服務添加測試頁面並啓動服務:apache

root@node1:~# echo "real1" > /var/www/html/index.html
root@node1:~# service apache2 start
root@node2:~# echo "real2" > /var/www/html/index.html
root@node2:~# service apache2 start

配置 KeepAlive :

MASTER:服務器

! Configuration File for keepalived

global_defs {
   notification_email {
    root@localhost #通知email的地址
   }
   notification_email_from keepalived@localhost #發件人email地址
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER #MASTER,BACKUP節點的不一樣配置用紅色標記
    interface eth0
    virtual_router_id 51
    priority 101 #優先級值, MASTER節點的值要高於BACKUP
    advert_int 1
    authentication { #認證配置
        auth_type PASS
        auth_pass shiina
    }
    virtual_ipaddress {
    10.11.8.100/16 dev eth0 label eth0:0 #虛擬IP地址
    }
}

virtual_server 10.11.8.100 80 {
    delay_loop 6
    lb_algo rr #lb使用的調度算法
    lb_kind DR #lb所使用的模型
    nat_mask 255.255.0.0
    #persistence_timeout 50
    protocol TCP
    sorry_server 127.0.0.1 80 #當全部real_server宕機後的錯誤提示服務器

    real_server 10.11.8.222 80 { #node1節點
        weight 1
        HTTP_GET { #驗證real_server方式
            url {
              path /
              status_code 200
            }
            connect_timeout 3 #超時時間
            nb_get_retry 3 #重試次數
            delay_before_retry 3 #重試前的等待時間
        }
    }
    real_server 10.11.8.158 80 { #node2節點
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

BACKUP:curl

! Configuration File for keepalived

global_defs {
   notification_email {
    root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass shiina
    }
    virtual_ipaddress {
    10.11.8.100/16 dev eth0 label eth0:0
    }
}

virtual_server 10.11.8.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    #persistence_timeout 50
    protocol TCP
    sorry_server 127.0.0.1 80

    real_server 10.11.8.222 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 10.11.8.158 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

啓動服務, 查看狀態:

[root@keep1 ~]# service keepalived start 
Starting keepalived:                                       [  OK  ]
[root@keep2 ~]# service keepalived start 
Starting keepalived:                                       [  OK  ]
[root@keep1 ~]# tail /var/log/messages #查看日誌
Jun  1 06:22:37 keep1 kernel: IPVS: [rr] scheduler registered.
Jun  1 06:22:37 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun  1 06:22:37 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jun  1 06:22:37 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jun  1 06:22:37 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jun  1 06:22:38 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Entering MASTER STATE
Jun  1 06:22:38 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) setting protocol VIPs.
Jun  1 06:22:38 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.11.8.100
Jun  1 06:22:38 keep1 Keepalived_healthcheckers[1599]: Netlink reflector reports IP 10.11.8.100 added
Jun  1 06:22:43 keep1 Keepalived_vrrp[1600]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.11.8.100

查看運行狀態:oop

[root@keep1 keepalived]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:4F:81:C9  
          inet addr:10.11.8.219  Bcast:10.11.9.255  Mask:255.255.254.0
          inet6 addr: fe80::a00:27ff:fe4f:81c9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5025 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2800 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:389646 (380.5 KiB)  TX bytes:239786 (234.1 KiB)

eth0:0    Link encap:Ethernet  HWaddr 08:00:27:4F:81:C9  
          inet addr:10.11.8.100  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:261 errors:0 dropped:0 overruns:0 frame:0
          TX packets:261 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:21926 (21.4 KiB)  TX bytes:21926 (21.4 KiB)

[root@keep1 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.11.8.100:80 rr
  -> 10.11.8.158:80               Route   1      1          0         
  -> 10.11.8.222:80               Route   1      0          0

測試服務:

[root@keep1 keepalived]# ipvsadm -Lnc
    IPVS connection entries
    pro expire state       source             virtual            destination
    TCP 14:52  ESTABLISHED 10.11.8.138:58173  10.11.8.100:80     10.11.8.158:80
    root@node2:~# service apache2 stop
     * Stopping web server apache2                                                                      * 
    [root@keep1 keepalived]# ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.11.8.100:80 rr
      -> 10.11.8.222:80               Route   1      0          0
    
    
    root@node1:~# service apache2 stop
     * Stopping web server apache2                                                                      * 
    [root@keep1 keepalived]# ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.11.8.100:80 rr
      -> 127.0.0.1:80                 Local   1      0          0  
    [root@keep1 keepalived]# curl 10.11.8.100
    error this is keep1

此時全部real_server 所有中止, 啓動了sorry_server測試

自寫監測腳本,完成維護模式切換

編輯keepalived.conf 添加如下內容:ui

vrrp_script chk_schedown {
    script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" #執行的腳本
    interval 1 #check間隔
    weight -10 #失敗時優先級的變化
    fall 2 #失敗幾回後真正確認爲失敗
    rise 1 #成功幾回後確認爲成功
}

在實例中進行調用, 添加在 vrrp_instance 中, 注意vrrp_script必須定義在實例的前面

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass shiina
    }
    virtual_ipaddress {
        10.11.8.100/16 dev eth0 label eth0:0
    }
    track_script {
        chk_schedown
    }
}

當 touch /etc/keepalived down 後, MASTER 的優先級會減小10, 服務會遷移到 BACKUP 上

相關文章
相關標籤/搜索