1. 程式人生 > 其它 >keepalived 高可用 haproxy

keepalived 高可用 haproxy

首先實現簡單的 harproxy 負載均衡

node1 and node4:安裝 httpd 服務,提供測試頁

node 2 and node3:安裝 haproxy,安裝 keepalived

haproxy 的簡單配置:

frontend webserver
    bind *:80
    use_backend websrvs

backend websrvs
    balance roundrobin
    server web1 192.168.2.11:80 check
    server web2 192.168.2.14:80 check 

下面我們來實現高可用 haproxy:

node3配置:

! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node3.ckh.com
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_haproxy {    # harproxy 健康狀態檢測
    script "killall -0 haproxy"
    interval 3
    weight -2
}

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 {
	192.168.2.18/32 dev eth0 label eth0:1
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

node2 配置:

! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node2.ckh.com
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 3
    weight -2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
	192.168.2.18/32 dev eth0 label eth0:1
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

notify.sh 指令碼:

#! /bin/bash
# Author: ckh
# Description: An example of notiry script
#

vip="192.168.2.18"
contack="root@localhost"

notify() {
  mailsubject="$(hostname) to be $1:$vip floating"
  mailbody="$(date +'%F %H:%M:%S'): vrrp transition,$(hostname) changed to be $1"
  echo $mailbody | mail -s "$mailsubject" $contack
}

case $1 in
master)
  notify master
  systemctl restart haproxy  # active-active 方式執行的話,我們可以讓 haproxy 成為 主備時都重啟haproxy
  exit 0
  ;;
backup)
  notify backup
systemctl restart haproxy # active-active 方式執行的話,我們可以讓 haproxy 成為 主備時都重啟haproxy exit 0 ;; fault) notify fault exit 0 ;; *) echo "Usage:$(basename $0) {master|backup|fault}" exit 1 ;; esac

以上是單主高可用的實現,下面我們實現雙主高可用的實現:

增加一個 vip,新增一個 例項 即可,和 高可用 nginx 類似。

node2 新增一個例項:

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1122
    }
    virtual_ipaddress {
	192.168.2.28/32 dev eth0 label eth0:2
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

node3 新增一個例項:

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1122
    }
    virtual_ipaddress {
	192.168.2.28/32 dev eth0 label eth0:2
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

以上就是 keepalived 高可用 haproxy 的方式,而且是雙主模式的實現。