• haproxy工具,负载均衡配置,反向代理配置,动静分离,高可用等等


    haproxy

    haproxy概述
    1.介绍
    Haproxy是一个开源的、高性能的基于tcp和http应用代理的高可用的、负载均衡服务软件,
    支持双机热备(heartbeat)
    支持高可用(heartbeat、)
    支持负载均衡(lvs、nginx、squid)
    支持虚拟主机、基于tcp和http的应用代理、图形界面查看信息等功能。
    配置简单、维护方便
    拥有很好的对服务器节点的健康检查功能
    当其代理的后端服务器出现问题的时候,haproxy会自动的将该故障服务器摘除,当故障的服务器恢复侯,haproxy还会自动将该服务器自动加入进来提供服务
    
    2.haproxy适用场景
    Haproxy特别适用于那些高负载,访问量很大,但又需要会话保持及七层应用代理的业务应用,haproxy运行在今天的普通的服务器硬件上,几乎不需要进行任何的优化就可以支持数亿万计的并发连接,并且它的运行模式使得它可以很简单、轻松、安全的整合到各种已有的网站架构中,同时,haproxy的代理模式,可以使得所有应用服务器不会暴露到公共网络上,即后面的节点服务器不需要公网ip地址
    
    从1.3版本起,haproxy软件引入了frontend,backend的功能,
    frontend(acl 规则匹配) 可以让运维管理人员根据任意http请求头内容做规则匹配,然后把请求定向到相关的backend(这个是事先定义好的多个server pools),等待前端把请求转过来的服务器组),
    
    通过frontend和backend,我们可以很容易的实现haproxy的各种七层应用代理功能。
    
    
    3.Haproxy支持两种主要代理模式
    
    1)基于四层的tcp应用代理(例如:可用于邮件服务、内部协议通信服务器,mysql、https服务等)
    2)基于7层的http代理,在4层tcp代理模式下,haproxy仅在客户端和服务器之间进行流量转发,但是在7层http代理模式下,haproxy会分析应用层协议,并且能通过允许、拒绝、交换、增加、删除、修改请求或者回应制定内容来控制协议
    
    官方站点 https://www.haproxy.com/  https://www.haproxy.org/
    
    LVS在企业应用中抗负载能力很强,但存在不足,LVS不支持正则处理,不能实现动静分离;
    对于大型网站,LVS的实施配置复杂,维护成本相对较高;Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件,适用于负载大的Web站点;
    运行在硬件上可支持数以万计的并发连接的连接请求;
    
    
    
    4.Haproxy L4 负载均衡用用架构拓扑
    
    Haproxy软件的四层tcp应用代理非常优秀,且配置非常方便,比lvs和nginx配置简单很多,首先,配置haproxy不需要再rs(real server)端做任何特殊配置(只要对应服务开启就ok)就可以实现应用代理,其次,haproxy的配置语法和增加虚拟主机功能等也比lvs/nginx简单
    
    客户机-----》haproxy------》web服务器------》mysql数据库-----》web服务器------》haproxy----》客户机
    
    
    说明:由于haproxy软件采用的是类nat模式(本质不同)的应用代理,数据包来去都会经过haproxy,因此,在流量特别大的情况下(门户级别的流量吧),其效率和性能不如lvs的dr模式负载均衡
    
    
    
    5.HAPROXY L7负载均衡应用架构拓扑图
    
    
    Haproxy软件最大的优势在于其7层的根据url请求头应用过滤的功能以及session会话功能,在门户网站的高并发生产架构中,haproxy软件一般在4层lvs负载均衡软件的下一层,或者像haproxy官方推荐的也可以挂在硬件负载均衡althon、ns、f5、a10下适用,其表现非常好,从2009年起taobao网,京东商城的业务也大面积的适用了haproxy做未7层cache应用代理
    
    下面是haproxy的layer7层代理的拓扑结构图
    
    客户端----》lvs、f5工作在5层-----》haproxy7层------》web服务器
    说明:lvs支持大并发,haproxy负责7层
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    haproxy 配置文件解析
    Haproxy支持的调度算法
    1.Round Robin(RR) 轮询调度
    2.Least Connections(LC) 最少连接数
    3.Source Hashing(SH) 基于来源访问调度算法
    
    Haproxy配置文件详解
    1、Haproxy配置文件通常分为三个部分
    
    global:全局配置
    defaults:默认配置
    listen:应用组件配置
    
    2、global配置参数
    
    log127.0.0.1 lcal0:配置日志记录,local0为日志设备,默认存放到系统日志
    log127.0.0.1 loca1 notice: notice为日志级别,通常有24个级别
    maxconn4096:最大连接数
    uid 99:用户 uid
    gid 99:用户 gid
    
    3、defaults配置项配置默认参数
    
    log global:定义日志为global配置中的日志定义
    mode http:模式为http option
    httplog:采用http日志格式记录日志
    retries 3:检查节点服务器失败连续达到三次则认为节点不可用
    maxconn2000:最大连接数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    haproxy 实战

    haproxy配置过程
    1.haproxy配置负载均衡和反向代理
          1.两台web服务器,一台做负载均衡和反向代理
          2.关闭防火墙,配置yum源
          3.安装haproxy软件,配置/etc/haproxy/haproxy.cfg文件
          4.重启haproxy
          5.配置web服务器httpd或者nginx服务,可以显示出网页
          
    2.查看状态信息的配置
          1.修改主配置文件添加内容如下:
             defaults
                 stats enable
                 stats uri /haproxy
                 stats auth admin:123456
                 mode       http
          2.重启haproxy服务
          3.测试
    3.haproxy健康检查功能
           1.基于tcp端口的健康检查
               1.修改主配置文件补全配置结果
                 backend webserver
                        server web1 192.168.0.12:80 check inter 5000 fall 5 2
                        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2
               2.重启haproxy
           2.基于URL的健康检查
               1.设置基于URI健康检查配置
                 backend webserver
                        option httpchk HEAD /checkstatus.html HTTP/1.0
                        server web1 192.168.0.12:80 check inter 5000 fall 5 rise 2
                        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2
               2.在两台web服务器下创建checkstatus.html文件
               3.重启haproxy
    4.haproxy高可用配置
    backup 参数,表示只有其它主机全部down掉之后,backup所在的主机才会生效
    高可用:一个web提供服务,另外一个处于备份等待状态
    负载均衡:每个web 服务器都会提供服务
           1.修改主配置文件,添加backup
                 backend webserver
                        option httpchk HEAD /checkstatus.html HTTP/1.0
                        server web1 192.168.0.12:80 check inter 5000 fall 5 rise 2
                        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2 backup
           2.重启haproxy  
    5.获取客户端ip地址
           1.在haproxy配置文件里加入如下参数
               backend webserver
                    option forwardfor
                    option httpchk HEAD /checkstatus.html HTTP/1.0   
                    server web1 192.168.0.12:80 check inter 5000 fall 5 rise 2
                    server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2 backup
           2)在nginx web服务器里添加日志记录功能
                   #此配置在web服务器上的nginx配置文件
                   #可配置到http、server、location中
                   #配置需要识别的IP来源   负载均衡的IP
               set_real_ip_from  192.168.0.11;
           3.重启web服务器nginx服务
               # nginx -s reload 
    ========================httpd记录真实ip配置如下=======================
    如果是web服务是httpd的话,在listen 80 这行下面添加
    RemoteIPHeader X-Forwarded-For
    
    将195行的日志中的%h替换成%{X-Forwarded-For}i
    4)测试,在windows上访问
    在192.168.0.12上去查看日志(注意:基于url的健康检查会产生很多日志)
    可以看到上图中,日志记录的不是haproxy服务器的ip地址而是192.168.1.11windows主机的ip地址
    
    6.动静分离实验(负载均衡+反向代理)
    实现haproxy基于url地址目录做7层跳转实现动静分离配置
    根据目录进行过滤转发       
           1.修改/etc/haproxy/haproxy.cfg文件
           2.重启haproxy
           3.配置两台web服务器
           
    7.keepalived+haproxy 高可用集群
      haproxy的高可用是对web服务器进行backup处理
      keepalived的高可用是备份haproxy,解决了单点故障的问题
         1.关闭防火墙,配置yum源
         2.安装haproxy,安装keepalived
         3.配置主用节点haproxy,配置备用节点haproxy
         4.配置主用节点keepalived,配置备用节点keepalived
         5.开启主keepalived,后开启备用keepalived
         6.配置两台web服务器
         
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    haproxy负载均衡和反向代理
    安装haproxy软件
    [root@hd1 ~]# yum -y install haproxy
    
    备份主目录
    [root@hd1 ~]# cd /etc/haproxy/
    [root@hd1 haproxy]# cp haproxy.cfg haproxy.cfg.bak
    
    
    修改配置文件,将60行以后的配置删除后,添加如下内容:
    
    [root@hd1 haproxy]# vi haproxy.cfg
    frontend  mywebs *:80
        default_backend  webserver
    backend webserver
        server   web1 192.168.8.138:80 check
        server   web2 192.168.8.139:80 check
    
    
    haproxy配置文件解释
    
    global :全局配置参数段,主要用来控制haproxy启动前的进程及系统相关设置
    defaults:配置一些默认参数,如果frontend,backend,listen等段未设置则使用defaults段配置
    listen:监听
    frontend:用来匹配接收客户所请求的域名uri等,并针对不同的匹配,做不同的请求处理。
    Backend;定义后端服务器集群,以及对后段服务器的一些权重、队列、连接数等选项的设置
    
    
    保存退出之后,重启
    [root@hd1 haproxy]# systemctl start haproxy.service 
                   
    [root@hd1 haproxy]# ss -ntl |grep -w 80
    LISTEN     0      128          *:80                       *:*  
    
    
    接着
    在192.168.8.138上和192.168.8.139上安装nginx web服务
    1.先安装nginx源
    [root@hd3 yum.repos.d]# cat nginx.repo 
    [nginx]
    baseurl = http://nginx.org/packages/centos/7/$basearch/
    enabled = 1
    gpgcheck = 0
    name = nginx repo
    [root@hd2 yum.repos.d]# cat nginx.repo 
    [nginx]
    baseurl = http://nginx.org/packages/centos/7/$basearch/
    enabled = 1
    gpgcheck = 0
    name = nginx repo
    2.两台服务器上yum 安装nginx
    
    yum -y install nginx 
    
    配置首页步骤略过
    
    
    
    测试一下
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    haproxy查看状态信息
    查看状态信息
    
    修改主配置文件添加内容如下:
    [root@hd1 haproxy]# vi haproxy.cfg
    defaults
        stats enable
        stats uri /haproxy
        stats auth admin:123456
        mode       http
    
    重启haproxy
    [root@hd1 haproxy]# systemctl restart haproxy
    
    账号Admin
    密码123456
    通过浏览器访问状态页面
    192.168.1.129/haproxy
    
    Queue
    Cur: current queued requests //当前的队列请求数量
    Max:max queued requests     //最大的队列请求数量
    Limit:           //队列限制数量
    Session rate(每秒的连接回话)列表:
    scur: current sessions        //每秒的当前回话的限制数量
    smax: max sessions           //每秒的新的最大的回话量
    slim: sessions limit           //每秒的新回话的限制数量
    Sessions 
    Total:            //总共回话量
    Cur:             //当前的回话
    Max: //最大回话 
    Limit: //回话限制
    Lbtot: total number of times a server was selected //选中一台服务器所用的总时间
    Bytes
    In: //网络的字节数输入总量  
    Out: //网络的字节数输出总量
    Denied
    Req: denied requests//拒绝请求量
    Resp:denied responses //拒绝回应
    Errors
    Req:request errors             //错误请求
    Conn:connection errors          //错误的连接
    Resp: response errors (among which srv_abrt)  ///错误的回应
    Warnings
    Retr: retries (warning)                      //重新尝试
    Redis:redispatches (warning)               //再次发送
    Server列表:
    Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
    LastChk:    持续检查后端服务器的时间
    Wght: (weight) : 权重
    Act: server is active (server), number of active servers (backend) //活动链接数量
    Bck: server is backup (server), number of backup servers (backend) //backup:备份的服务器数量
    Down:          //后端服务器连接后都是down的数量
    Downtime: downtime: total downtime (in seconds)    //总的downtime 时间
    Throttle: warm up status                          //设备变热状态
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    Haproxy健康检查功能
    基于tcp端口的健康检查
    基于tcp端口的健康检查
    使用这种检测方式,haproxy 只会去检查后端sever 服务的对应端口,根据前面的经验我们知道这并不能保证节点服务的完全可用,但却可以作为辅助的检查手段,不是非常严格业务环境下也是可用的
    
    
    修改主配置文件补全配置结果
    [root@hd1 haproxy]# vi haproxy.cfg
    backend webserver
        
        server web1 192.168.0.12:80 check inter 5000 fall 5 2
        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2
    
    [root@hd1 haproxy]# systemctl restart   haproxy
    
    提示:
    1)inter 5000 fall 5 表示每5秒检查一次,一共检查5次。如果有问题就会摘掉出问题的机器。
    2)如果结尾不加inter 5000 fall 5,则默认每2秒检查一次,一共检查3次。如果有问题就会摘掉出问题的机器。
    4)The default
    values are the following ones :
      - inter : 2000  意思是不加该参数,正常情况默认每两秒检查一次
      - rise  : 2  意思是不加该参数,在RealServer宕机后恢复前,检查2次OK,认为其复活,并加入到群组中
      - fall  : 3  意思是不加该参数,检查3此后,认为RS宕机,剔除集群组
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    基于URL的健康检查
    基于URL的健康检查
    
    1)设置基于URI健康检查配置
    backend webserver
        option httpchk HEAD /checkstatus.html HTTP/1.0
        server web1 192.168.0.12:80 check inter 5000 fall 5 rise 2
        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2
    
    刷新状态页面,发现web1和web2都变色了
    解决办法在web1和web2 网页根目录下创建checkstatus.html
    [root@node3 html]# touch checkstatus.html
    [root@node2 html]# touch checkstatus.html
    
    
    生产环境健康检查参数的设置
    max_fails=2  最大尝试失败的次数,默认是1,0表示禁止失败尝试,企业场景中 京东1次,蓝汛10次 
    
    fail_timemout=20s 
    
    强调:本节内容适用于所有负载均衡软件的健康检查(lvs、nginx、squid、)
    
    1》常规业务可以使用基于tcp的方式做健康检查,我们在keepalived健康检查是就是这样做的,虽然,keepalived也支持url健康检查
    2》由于基于url的方式做健康检查相对来说也比较容易配置,推荐使用url的方式
    3》实际生产环境下,最重要的关键是,我们设置的check.html能否真正的代表rs上的业务状态,即 访问check.html正常,整个业务是否能正常,这个是需要我们运维及开发人员去分析的 
    4》对于用户体验比较高的业务,可以请开发人员设置更加深入的健康检查文件,例如:check.jsp等,这个健康检查可以深入到数据库,存储以及各个接口,如果之一出现问题就摘除,实际工作中不是越高级越好,根据业务需求达到要求就可以
    5》健康检查的频率,次数都要做相应的调整,例如:1秒检查一次,不成功的话 立刻摘除
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    haproxy的高可用–不需要keepalived
    Haproxy 的高可用相关生产功能参数
    
    backup 参数,表示只有其它主机全部down掉之后,backup所在的主机才会生效
    高可用:一个web提供服务,另外一个处于备份等待状态
    负载均衡:每个web 服务器都会提供服务
    
    
    案例:修改主配置文件如下:
    
    backend webserver
        option httpchk HEAD /checkstatus.html HTTP/1.0
        server web1 192.168.0.12:80 check inter 5000 fall 5 rise 2
        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2 backup
     
    [root@hd1 haproxy]# systemctl  restart  haproxy 
    
    测试发现,访问的是0.12,那0.13作为高可用不提供服务
    
    停止web1的服务,再次测试,发现0.13提供web服务了。
    [root@node2 html]# nginx -s stop
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    获取真实ip地址
    实现haproxy下的rs(web服务器)无法记录客户端ip地址的问题?
    
    1)在haproxy配置文件里加入如下参数
    
    backend webserver
        option forwardfor
        option httpchk HEAD /checkstatus.html HTTP/1.0   
        server web1 192.168.0.12:80 check inter 5000 fall 5 rise 2
        server web2 192.168.0.13:80 check inter 5000 fall 5 rise 2 backup
    2)在nginx web服务器里添加日志记录功能
    #此配置在web服务器上的nginx配置文件
    #可配置到http、server、location中
    #配置需要识别的IP来源   负载均衡的IP
    set_real_ip_from  192.168.0.11;
    
    [root@node2 conf]# nginx -s reload 
    ========================httpd记录真实ip配置如下=======================
    如果是web服务是httpd的话,在listen 80 这行下面添加
    RemoteIPHeader X-Forwarded-For
    
    将195行的日志中的%h替换成%{X-Forwarded-For}i
    3)在windows上访问
    在192.168.0.12上去查看日志(注意:基于url的健康检查会产生很多日志)
    可以看到上图中,日志记录的不是haproxy服务器的ip地址而是192.168.1.11windows主机的ip地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    haproxy 动静分离
    实现haproxy基于url地址目录做7层跳转实现动静分离配置
    根据目录进行过滤转发
    1.修改后的内容如下
    注:这里是用的apache网站配置的haproxy路径
    root@hd1 haproxy]# vi haproxy.cfg 
    frontend www   *:80
           acl wm_static path_beg /nginx/
           acl wm_php    path_beg /php/
           acl wm_java   path_beg /java/
           use_backend   nginxpools if wm_static
           use_backend   phppools  if wm_php
           default_backend nginxpools
    backend  nginxpools
           balance leastconn
           option httpclose
           option forwardfor
           server web01 192.168.1.13:80 check port 80 inter 1000 fall 1 
    backend  phppools
           balance leastconn
           option httpclose
           option forwardfor
           server web02 192.168.1.14:80 check port 80 inter 1000 fall 1
    注 :这里是nginx对应的haproxy配置
    root@hd1 haproxy]# vi 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 http
          bind *:80
          acl url_static       path_end -i .html     
          acl url_php          path_end -i .php      
          use_backend nginx if url_static
          use_backend php if url_php
          default_backend php
    
    #---------------------------------------------------------------------
    # round robin balancing between the various backends
    backend php
          option  httpchk GET /index.php
          server  php1 192.168.100.12:80   cookie id1  check inter 2000 rise 2 fall 3
    backend nginx
          option  httpchk GET /index.html
          server  nginx1 192.168.100.13:80   cookie id1  check inter 2000 rise 2 fall 3
    
    
    
    
    
    2.重新启动haproxy这个服务
    
    [root@hd1 haproxy]# systemctl restart haproxy
    
    接下来配置 192.168.1.13、192.168.1.14为apache网站 (yum安装)
    
    在192.168.1.13的网站根目录之下创建一个nginx目录和子首页
    [root@hd3 ~]# cd /var/www/html
    [root@hd3 html]# mkdir nginx
    [root@hd3 html]# cd nginx/
    [root@hd3 nginx]# echo "wo shi 1.13 de nginx" >index.html
    
    
    在192.168.1.14上同理 
    [root@hd4 ~]# cd /var/www/html
    [root@hd4 html]# mkdir php
    [root@hd4 html]# cd php
    [root@hd4 php]# echo "wo shi 1.14 php" >index.html    
    
    
    测试php
    
    
    
    更好的配置如下:
    frontend www
          bind *:80
          maxconn 5000
          mode http
          log global
          option httplog
          option httpclose
          option forwardfor
          log         global
          default_backend default       
          acl url_static path_beg  -i /static  /img /javascript 
          acl url_static path_end  -i .jpg .gif .png .css .js .html 
          acl host_static hdr_beg(host)  -i img. video. download. ftp. imags.         acl url_php path_end     -i .php
          acl url_jsp path_end     -i .jsp .do
          use_backend static_pool if  url_static or host_static
          use_backend php_pool    if  url_php
          use_backend tomcat_pool if  url_jsp
    backend static_pool
          option  httpchk GET /index.html
          server static1 192.168.1.12:80 cookie id1  check inter 2000 rise 2 fall 3
    backend php_pool
          option  httpchk GET /info.php
          server php1 192.168.1.13:80 cookie id1 check inter 2000 rise 2 fall 3
    backend tomcat_pool
          option  httpchk GET /index.jsp
          server tomcat1 192.168.1.14:8086 cookie id2 check inter 2000 rise 2 fall 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158

    keepalived+haproxy高可用集群

    haproxy+keepalived实现高可用集群
    1.可靠性和稳定性非常好,可以和硬件级的负载均衡设备F5相媲美。
    2.最高可同时维护40000-50000个并发连接,单位时间内处理的最大请求数为20000个。
    3.支持8种负载均衡算法,支持会话保持;支持虚拟主机功能;支持连接拒绝,全透明代理并且有一个功能强大的服务器状态监控界面。
    4.拥有功能强大的ACL支持。
    5.用haproxy构建群集的时候,比如后方代理两个http,如果haproxy宕机,后方的http正常运行网站也是瘫痪状态,这就造成了单点故障。
    
    这时keepalived就登场了,keepalived基于vrrp协议,两台主机之间生成一个虚拟的ip,我们称漂移ip,漂移ip由主服务器承担,一但主服务器宕机,备份服务器就会抢占漂移ip,继续工作,有效的解决了群集中的单点故障。两者相结合,可靠稳定。
    1 在192.168.1.12上安装haproxy,停止httpd服务,删除httpd软件
    [root@hd2 ~]# yum -y install haproxy
    2 配置192.168.1.11和192.168.1.12为haproxy (配置相同)
    frontend  mywebs *:80
        default_backend  webserver
        backend webserver
        server   web1 192.168.1.13:80 check
    server   web2 192.168.1.14:80 check
    
    重启haproxy服务
    [root@hd1 ~]# systemctl restart haproxy 
    [root@hd2 ~]# systemctl restart haproxy 
    
    配置192.168.1.11为主用keepalived,192.168.1.12为备用
    [root@hd1 ~]# yum -y install keepalived
    
    清空配置文件,重新设置如下
    [root@hd1 ~]# cat /etc/keepalived/keepalived.conf 
    ! Configuration File for keepalived
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL1
    }
    vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.199
        }
    }
    
    配置备用节点
    [root@hd2 haproxy]# yum -y install keepalived
    [root@hd2 haproxy]# cat /etc/keepalived/keepalived.conf 
    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL2
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface ens33
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.199
        }
    }
    
    启动两台服务器的keeaplived
    [root@hd1 ~]# systemctl restart keepalived
    [root@hd2 ~]# systemctl restart keepalived
    
    查看vip地址
    
    [root@hd1 ~]# ip addr |grep 1.199
    inet 192.168.1.199/32 scope global ens33
    
    
    测试是否正常
    [root@hd1 ~]# curl 192.168.1.199
    停掉服务测试是否正常
    [root@hd2 ~]# curl 192.168.1.199
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
  • 相关阅读:
    五分钟,看懂vue项目会用到哪些ES6常用语法
    Coursera自动驾驶1.1-1.2——自动驾驶基础和软硬件架构
    基于分布式系统结构下Nacos配置中心的应用
    射频与微波综合测试仪-4958手持式微波综合测试仪
    《Mycat分布式数据库架构》之Mycat管理
    《计算机网络》第一章学习随笔
    问题解决——SSH时出现WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!(转)
    【重识云原生】第六章容器6.4.2.3节——Pod使用(下)
    2020,XLNet: Generalized Autoregressive Pretraining for Language Understanding
    【Java SE】类和对象
  • 原文地址:https://blog.csdn.net/qq_48975137/article/details/132906357