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

  • 相关阅读:
    samtools使用过程中出现的问题
    转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系
    韦恩图的画法
    python的计算保留小数
    awk的输出格式控制:print 和printf
    awk遇到windows 的^M
    从引物序列出发查找pcr产物的内容和在基因组上的位置
    八.Windows内核保护机制--页保护3--PDE PTE属性
    九.Windows内核保护机制--TSS
    七.Windows内核保护机制--陷阱门
  • 原文地址:https://www.cnblogs.com/ckh2014/p/15777789.html
Copyright © 2011-2022 走看看