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 Alexandre.Cassen@firewall.loc
   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 Alexandre.Cassen@firewall.loc
   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 的方式,而且是双主模式的实现。

原文地址:https://www.cnblogs.com/ckh2014/p/15777789.html