ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

keepalived 高可用 haproxy

2022-01-08 11:33:31  阅读:156  来源: 互联网

标签:haproxy 可用 keepalived vrrp sh notify eth0


首先实现简单的 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 的方式,而且是双主模式的实现。

 

标签:haproxy,可用,keepalived,vrrp,sh,notify,eth0
来源: https://www.cnblogs.com/ckh2014/p/15777789.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有