zoukankan      html  css  js  c++  java
  • 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 的方式,而且是双主模式的实现。

  • 相关阅读:
    CODING x 百果园 _ 水果零售龙头迈出 DevOps 体系建设第一步
    Nocalhost 亮相 CD Foundation 国内首届 Meetup,Keith Chan 将出席致辞
    做云原生时代标准化工具,实现高效云上研发工作流
    打造数字化软件工厂 —— 一站式 DevOps 平台全景解读
    WePack —— 助力企业渐进式 DevOps 转型
    CODING Compass —— 打造行云流水般的软件工厂
    Nocalhost —— 让云原生开发回归原始而又简单
    CODING 代码资产安全系列之 —— 构建全链路安全能力,守护代码资产安全
    Nocalhost:云原生开发新体验
    使用 Nocalhost 开发 Kubernetes 中的 APISIX Ingress Controller
  • 原文地址:https://www.cnblogs.com/ckh2014/p/15777789.html
Copyright © 2011-2022 走看看