阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

构建高可用集群Keepalived+Haproxy负载均衡

425次阅读
没有评论

共计 7144 个字符,预计需要花费 18 分钟才能阅读完成。

重点概念
vrrp_script 中节点权重改变算法
vrrp_script 里的 script 返回值为 0 时认为检测成功,其它值都会当成检测失败;
weight 为正时,脚本检测成功时此 weight 会加到 priority 上,检测失败时不加;
主失败:
主 priority < 从 priority + weight 时会切换。
主成功:
主 priority + weight > 从 priority + weight 时,主依然为主
weight 为负时,脚本检测成功时此 weight 不影响 priority,检测失败时 priority – abs(weight)
主失败:
主 priority – abs(weight) < 从 priority 时会切换主从
主成功:
主 priority > 从 priority 主依然为主

主要贴配置:VIP:10.16.37.198,10.16.37.199

web 服务器 IP:10.16.37.94,10.16.37.101

二台 keepalived 的 IP:10.16.37.107,10.16.37.110

一台:

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

  notification_email {

        linux@linuxidc

  }

  notification_email_from admin@lnmp.com

  smtp_connect_timeout 3

  smtp_server 127.0.0.1

  router_id Iptables

}

vrrp_script chk_maintaince_down {

  script “[[-f /etc/keepalived/down]] && exit 1 || exit 0”

  interval 1

  weight 2

}

vrrp_script chk_haproxy {

  script “killall -0 haproxy”

  interval 1

  weight 2

}

vrrp_instance VI_1 {

  interface eth0

  state MASTER

  priority 100

  virtual_router_id 125

  garp_master_delay 1

  authentication {

      auth_type PASS

      auth_pass 1e3459f77aba4ded

  }

  track_interface {

      eth0

  }

  virtual_ipaddress {

      10.16.37.198/22 dev eth0 label eth0:0

  }

  track_script {

      chk_haproxy

  }

  notify_master “/etc/keepalived/notify.sh master 10.16.37.198”

  notify_fault “/etc/keepalived/notify.sh fault 10.16.37.198”

}

vrrp_instance VI_2 {

  interface eth0

  state BACKUP

  priority 99

  virtual_router_id 126

  grap_master_delay 1

  authentication {

      auth_type pass

      auth_pass 7615c4b7f518cede

  }

  track_interface {

      eth0

  }

  virtual_ipaddress {

      10.16.37.199/22 dev eth0 label eth0:1

  }

  track_script {

      chk_haproxy

      chK_maintaince_down

  }

  notify_master “/etc/keepalived/notify.sh master 10.16.37.199”

  notify_backup “/etc/keepalived/notify.sh backup 10.16.37.199”

  notify_fault “/etc/keepalived/notify.sh fault 10.16.37.199”

}

另一台:vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

  notification_email {

        linux@linuxidc

  }

  notification_email_from admin@lnmp.com

  smtp_connect_timeout 3

  smtp_server 127.0.0.1

  router_id Iptables

}

vrrp_script chk_maintaince_down {

  script “[[-f /etc/keepalived/down]] && exit 1 || exit 0”

  interval 1

  weight 2

}

vrrp_script chk_haproxy {

  script “killall -0 haproxy”

  interval 1

  weight 2

}

vrrp_instance VI_1 {

  interface eth0

  state BACKUP

  priority 99

  virtual_router_id 125

  garp_master_delay 1

  authentication {

      auth_type PASS

      auth_pass 1e3459f77aba4ded

  }

  track_interface {

      eth0

  }

  virtual_ipaddress {

      10.16.37.198/22 dev eth0 label eth0:1

  }

  track_script {

      chk_haproxy

  }

  notify_master “/etc/keepalived/notify.sh master 10.16.37.198”

  notify_fault “/etc/keepalived/notify.sh fault 10.16.37.198”

}

vrrp_instance VI_2 {

  interface eth0

  state MASTER

  priority 100

  virtual_router_id 126

  grap_master_delay 1

  authentication {

      auth_type pass

      auth_pass 7615c4b7f518cede

  }

  track_interface {

      eth0

  }

  virtual_ipaddress {

      10.16.37.199/22 dev eth0 label eth0:0

  }

  track_script {

      chk_haproxy

      chK_maintaince_down

  }

  notify_master “/etc/keepalived/notify.sh master 10.16.37.199”

  notify_backup “/etc/keepalived/notify.sh backup 10.16.37.199”

  notify_fault “/etc/keepalived/notify.sh fault 10.16.37.199”

}

脚本配置:

vi /etc/keepalived/notify.sh

#!/bin/bash

contact=’root@localhost’

notify() {

    mailsubject=”‘hostname’ to be $1: $2 floating”

    mailbody=”‘date ‘+%F %H:%M:%S’`: vrrp transition, `hostname` changed to be $1″

    echo $mailbody | mail -s “$mailsubject” $contact

}

case “$1” in

    master)

        notify master $2

        /etc/rc.d/init.d/haproxy restart

        exit 0

    ;;

    backup)

        notify backup $2

        exit 0

    ;;

    fault)

        notify fault $2

        exit 0

    ;;

    *)

        echo ‘Usage: ‘basename $0′ {master|backup|fault}’

        exit 1

    ;;

Esac

Haproxy 配置:

vi /etc/haproxy/haproxy.cfg

#———————————————————————

# Example configuration for a possible web application.  See the

# full configuration options online.

#

#  http://haproxy.1wt.eu/download/1.4/doc/configuration.txt

#

#———————————————————————

#———————————————————————

# Global settings

#———————————————————————

global

    # to have these messages end up in /var/log/haproxy.log you will

    # need to:

    #

    # 1) configure syslog to accept network log events.  This is done

    #    by adding the ‘-r’ option to the SYSLOGD_OPTIONS in

    #    /etc/sysconfig/syslog

    #

    # 2) configure local2 events to go to the /var/log/haproxy.log

    #  file. A line like the following can be added to

    #  /etc/sysconfig/syslog

    #

    #    local2.*                      /var/log/haproxy.log

    #

    log        127.0.0.1 local2

    chroot      /var/lib/haproxy

    pidfile    /var/run/haproxy.pid

    maxconn    4000

    user        haproxy

    group      haproxy

    daemon

    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats

#———————————————————————

# common defaults that all the ‘listen’ and ‘backend’ sections will

# use if not designated in their block

#———————————————————————

defaults

    mode                    http

    log                    global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor      except 127.0.0.0/8

    option                  redispatch

    retries                3

    timeout http-request    10s

    timeout queue          1m

    timeout connect        10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check          10s

    maxconn                3000

#———————————————————————

# main frontend which proxys to the backends

#———————————————————————

#frontend  main *:5000

#    acl url_static      path_beg      -i /static /images /javascript /stylesheets

#    acl url_static      path_end      -i .jpg .gif .png .css .js

#

#    use_backend static          if url_static

#    default_backend            app

#———————————————————————

# static backend for serving up images, stylesheets and such

#———————————————————————

#backend static

#    balance    roundrobin

#    server      static 127.0.0.1:4331 check

#———————————————————————

# round robin balancing between the various backends

#———————————————————————

#backend app

#    balance    roundrobin

#    server  app1 127.0.0.1:5001 check

#    server  app2 127.0.0.1:5002 check

#    server  app3 127.0.0.1:5003 check

#    server  app4 127.0.0.1:5004 check

listen stats

    mode http

    bind 0.0.0.0:1080

    stats enable

    stats refresh 30s

    maxconn 200

    stats hide-version

    stats uri    /haproxy-stats

    stats realm  Haproxy\ Statistics

    stats auth    admin:admin

    stats admin if TRUE

frontend http-in

    bind *:80

    mode http

    log global

    option httpclose

    option logasap

    option dontlognull

    capture request header Host len 20

    capture request header Referer len 60

    acl url_static path_beg -i /static /images /javascript /stylesheets

    acl url_static path_end        -i .jpg .jpeg .gif .png .css .js .html

    use_backend static_servers if url_static

    default_backend dynamic_servers

backend static_servers

    balance roundrobin

    server imgsrv1 10.16.37.101:80 check maxconn 6000

    server imgsrv2 10.16.37.94:80 check maxconn 6000

backend dynamic_servers

    balance source

    server websrv1 10.16.37.94:80 check maxconn 1000

server websrv2 10.16.37.101:80 check maxconn 1000

因为端口使用的是 1080 需要 Iptables 开启:

/sbin/iptables –I INPUT –p tcp –dport 1080 –j ACCEPT

/etc/rc.d/init.d/iptables save

Service iptables restart

Vi /etc/selinux/config

关闭 selinux 然后呢重启!!

二个 server web 采用 nginx+ 双主 mysql 数据库,保证了 web 服务器的高可用性能,一台服务器宕机,另外一台立马连接!!

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138917.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计7144字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7971578
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击 PHP-FPM(FastCGl Process M...
终于收到了以女儿为原型打印的3D玩偶了

终于收到了以女儿为原型打印的3D玩偶了

终于收到了以女儿为原型打印的 3D 玩偶了 前些日子参加某网站活动,获得一次实物 3D 打印的机会,于是从众多...
浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍 前言 在 AI 自动化快速发展的当下,浏览器早已不再只是...
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
安装并使用谷歌AI编程工具Antigravity(亲测有效)

安装并使用谷歌AI编程工具Antigravity(亲测有效)

  安装并使用谷歌 AI 编程工具 Antigravity(亲测有效) 引言 Antigravity...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛 NAS-12:开源笔记的进化之路,效率玩家的新选择 前言 如何高效管理知识与笔记,已经成为技术...
免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

  免费无广告!这款跨平台 AI RSS 阅读器,拯救你的信息焦虑 在算法推荐主导信息流的时代,我们...
星哥带你玩飞牛NAS-14:解锁公网自由!Lucky功能工具安装使用保姆级教程

星哥带你玩飞牛NAS-14:解锁公网自由!Lucky功能工具安装使用保姆级教程

星哥带你玩飞牛 NAS-14:解锁公网自由!Lucky 功能工具安装使用保姆级教程 作为 NAS 玩家,咱们最...