设为首页 收藏本站
查看: 2410|回复: 0

[经验分享] 如何快速构建高可用集群(Keepalived+Haproxy+Nginx)

[复制链接]

尚未签到

发表于 2015-11-20 08:46:31 | 显示全部楼层 |阅读模式
  转自次元立方网:http://www.it165.net/admin/html/201405/2957.html
  另一篇:HA-Proxy+Nginx实现web负载均衡(haproxy篇)http://www.it165.net/admin/html/201405/3171.html
  
组件及实现的功能
Keepalived:实现对Haproxy服务的高可用,并采用双主模型配置;
Haproxy:实现对Nginx的负载均衡和读写分离;
Nginx:实现对HTTP请求的高速处理;

架构设计图
DSC0000.jpg

重点概念
vrrp_script中节点权重改变算法
vrrp_script 里的script返回值为0时认为检测成功,其它值都会当成检测失败;
weight 为正时,脚本检测成功时此weight会加到priority上,检测失败时不加;
主失败:
主 priority < 从 priority &#43; weight 时会切换。
主成功:
主 priority &#43; weight > 从 priority &#43; weight 时,主依然为主
weight 为负时,脚本检测成功时此weight不影响priority,检测失败时priority – abs(weight)
主失败:
主 priority – abs(weight) < 从priority 时会切换主从
主成功:
主 priority > 从priority 主依然为主
具体解释详见博文“Keepalived双主模型中vrrp_script中权重改变故障排查”

部署配置
Keepalived部署
配置
001.yum -yinstallkeepalived#两节点都需部署002.# 172.16.25.109003.# vi /etc/keepalived/keepalived.conf004.! Configuration Fileforkeepalived005.global_defs {006.   notification_email {007.         root@localhost008.   }009.   notification_email_from admin@lnmmp.com010.   smtp_connect_timeout 3011.   smtp_server 127.0.0.1012.   router_id LVS_DEVEL013.}014.vrrp_script chk_maintaince_down {015.   script&quot;[[ -f /etc/keepalived/down]] && exit 1 || exit 0&quot;016.   interval 1017.   weight 2018.}019.vrrp_script chk_haproxy {020.    script&quot;killall -0 haproxy&quot;021.    interval 1022.    weight 2023.}024.vrrp_instance VI_1 {025.    interface eth0026.    state MASTER027.    priority 100028.    virtual_router_id 125029.    garp_master_delay 1030.    authentication {031.        auth_type PASS032.        auth_pass 1e3459f77aba4ded033.    }034.    track_interface {035.       eth0036.    }037.    virtual_ipaddress {038.        172.16.25.10/16 dev eth0 label eth0:0039.    }040.    track_script {041.        chk_haproxy042.    }043.    notify_master&quot;/etc/keepalived/notify.shmaster 172.16.25.10&quot;044.    notify_fault&quot;/etc/keepalived/notify.shfault 172.16.25.10&quot;045.}046.vrrp_instance VI_2 {047.    interface eth0048.    state BACKUP049.    priority 99050.    virtual_router_id 126051.    garp_master_delay 1052.    authentication {053.        auth_type PASS054.        auth_pass 7615c4b7f518cede055.    }056.    track_interface {057.       eth0058.    }059.    virtual_ipaddress {060.        172.16.25.11/16 dev eth0 label eth0:1061.    }062.    track_script {063.        chk_haproxy064.chk_maintaince_down065.    }066.    notify_master&quot;/etc/keepalived/notify.shmaster 172.16.25.11&quot;067.    notify_backup&quot;/etc/keepalived/notify.shbackup 172.16.25.11&quot;068.    notify_fault&quot;/etc/keepalived/notify.shfault 172.16.25.11&quot;069.}070.# 172.16.25.110071.# vi /etc/keepalived/keepalived.conf072.! Configuration Fileforkeepalived073.global_defs {074.   notification_email {075.         root@localhost076.   }077.   notification_email_from admin@lnmmp.com078.   smtp_connect_timeout 3079.   smtp_server 127.0.0.1080.   router_id LVS_DEVEL081.}082.vrrp_script chk_maintaince_down {083.   script&quot;[[ -f /etc/keepalived/down]] && exit 1 || exit 0&quot;084.   interval 1085.   weight 2086.}087.vrrp_script chk_haproxy {088.    script&quot;killall -0 haproxy&quot;089.    interval 1090.    weight 2091.}092.vrrp_instance VI_1 {093.    interface eth0094.    state BACKUP095.    priority 99096.    virtual_router_id 125097.    garp_master_delay 1098.    authentication {099.        auth_type PASS100.        auth_pass 1e3459f77aba4ded101.    }102.    track_interface {103.       eth0104.    }105.    virtual_ipaddress {106.        172.16.25.10/16 dev eth0 label eth0:0107.    }108.    track_script {109.        chk_haproxy110.chk_maintaince_down111.    }112.    notify_master&quot;/etc/keepalived/notify.shmaster 172.16.25.10&quot;113.    notify_backup&quot;/etc/keepalived/notify.shbackup 172.16.25.10&quot;114.    notify_fault&quot;/etc/keepalived/notify.shfault 172.16.25.10&quot;115.}116.vrrp_instance VI_2 {117.    interface eth0118.    state MASTER119.    priority 100120.    virtual_router_id 126121.    garp_master_delay 1122.    authentication {123.        auth_type PASS124.        auth_pass 7615c4b7f518cede125.    }126.    track_interface {127.       eth0128.    }129.    virtual_ipaddress {130.        172.16.25.11/16 dev eth0 label eth0:1131.    }132.    track_script {133.        chk_haproxy134.    }135.    notify_master&quot;/etc/keepalived/notify.shmaster 172.16.25.11&quot;136.    notify_backup&quot;/etc/keepalived/notify.shbackup 172.16.25.11&quot;137.    notify_fault&quot;/etc/keepalived/notify.shfault 172.16.25.11&quot;138.}139.# vi /etc/keepalived/notify.sh140.#!/bin/bash141.# Author: Jason.Yu <admin@lnmmp.com>142.# description: An example of notify script143.#144.contact='root@localhost'145.notify() {146.    mailsubject=&quot;`hostname` tobe $1: $2 floating&quot;147.    mailbody=&quot;`date '&#43;%F %H:%M:%S'`:vrrp transition, `hostname` changed to be $1&quot;148.    echo$mailbody | mail-s &quot;$mailsubject&quot;$contact149.}150.case&quot;$1&quot;in151.    master)152.        notify master $2153.        /etc/rc.d/init.d/haproxy restart154.        exit0155.    ;;156.    backup)157.        notify backup $2# 在节点切换成backup状态时,无需刻意停止haproxy服务,防止chk_maintaince和chk_haproxy多次对haproxy服务操作;158.        exit0159.    ;;160.    fault)161.        notify fault $2# 同上162.        exit0163.    ;;164.    *)165.        echo'Usage: `basename$0` {master|backup|fault}' 166.        exit1167.    ;;168.esac启动服务
1.service keepalived start# 在两个节点上都需要启动keepalived双主模型启动
DSC0001.jpg
Haproxy部署
安装配置
01.yum -yinstallhaproxy#两节点都需部署02.vi/etc/haproxy/haproxy.cfg#两节点配置一致03.global04.    log         127.0.0.1 local205.    chroot      /var/lib/haproxy06.    pidfile     /var/run/haproxy.pid07.    maxconn     400008.    user         haproxy09.    group       haproxy10.    daemon# 以后台程序运行;11.defaults12.    mode                   http#选择HTTP模式,即可进行7层过滤; 13.    log                     global14.    option                  httplog#可以得到更加丰富的日志输出; 15.    option                  dontlognull16.    option http-server-close#server端可关闭HTTP连接的功能; 17.    option forwardfor except 127.0.0.0/8#传递client端的IP地址给server端,并写入“X-Forward_for”首部中;18.    option                  redispatch19.    retries                 320.    timeout http-request    10s21.    timeout queue           1m22.    timeout connect         10s23.    timeout client          1m24.    timeout server          1m25.    timeout http-keep-alive 10s26.    timeout check           10s27.    maxconn                 3000028.listen stats29.    modehttp30.    bind0.0.0.0:1080# 统计页面绑定1080端口;31.    statsenable#开启统计页面功能; 32.    stats hide-version# 隐藏Haproxy版本号;33.    stats uri     /haproxyadmin?stats#自定义统计页面的访问uri; 34.    stats realm   Haproxy\ Statistics#统计页面密码验证时的提示信息; 35.    stats auth    admin:admin#为统计页面开启登录验证功能; 36.    stats adminifTRUE#若登录用户验证通过,则赋予管理功能;37.frontend http-in38.    bind*:8039.    mode http40.    log global41.    option httpclose42.    option logasap43.    option dontlognull44.    capture request  header Host len 2045.    capture request  header Referer len 6046.    acl url_static       path_beg       -i /static /images /javascript /stylesheets47.    acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js .html48.    use_backend static_serversifurl_static#符合ACL规则的,请求转入后端静态服务器49.    default_backend dynamic_servers#默认请求转入后端动态服务器 50.backend static_servers51.    balance roundrobin52.    server imgsrv1 192.168.0.25:80 check maxconn 6000#静态服务器,可配置多台,还可设置权重weight;53.backend dynamic_servers54.    balancesource#对于动态请求利用source调度算法,可一定程度上实现session保持;但最好利用cookie绑定的方式实现session保持55.    server websrv1 192.168.0.35:80 check maxconn 1000#动态服务器,可配置多台,还可设置权重weight;启动服务
1.service haproxy start# 两节点都需要启动Nginx部署
见博客“如何测试Nginx的高性能”http://www.it165.net/admin/html/201405/2928.html

访问验证
Haproxy统计页面测试
DSC0002.jpg
DSC0003.gif DSC0004.jpg QTNTdG14REFCVy14MHUxTUYwMjA5LmpwZw==&quot; border=&quot;0&quot; height=&quot;400&quot; hspace=&quot;0&quot; src=&quot;http://www.it165.net/uploadfile/files/2014/0504/20140504094357372.jpg&quot;title=&quot;统计页面展示.png&quot; vspace=&quot;0&quot; width=&quot;600&quot; />
动静分离测试

DSC0005.jpg
DSC0006.jpg
高可用测试

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141343-1-1.html 上篇帖子: Keepalived 在openstack的实施 下篇帖子: 手把手教程: CentOS 6.5 LVS + KeepAlived 搭建 负载均衡 高可用 集群
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表