zoukankan      html  css  js  c++  java
  • Haproxy负载均衡与高可用

    HAPROXY简介
         HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种负载均衡解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
    2、HAProxy的特点是:
    1、HAProxy支持虚拟主机。
    2、HAProxy的优点能够补充Nginx的一些缺点,比如支持Session的保持,Cookie的引导;同时支持通过获取指定的url来检测后端服务器的状态。
    3、HAProxy跟LVS类似,本身就只是一款负载均衡软件;单纯从效率上来讲HAProxy会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的。
    4、HAProxy支持TCP协议的负载均衡转发,可以对MySQL读进行负载均衡,对后端的MySQL节点进行检测和负载均衡,可以用LVS+Keepalived对MySQL主从做负载均衡。
    5、HAProxy负载均衡策略非常多,HAProxy的负载均衡算法现在具体有如下8种:
      ① roundrobin,表示简单的轮询,这个不多说,这个是负载均衡基本都具备的;
      ② static-rr,表示根据权重,建议关注;
      ③ leastconn,表示最少连接者先处理,建议关注;
      ④ source,表示根据请求源IP,这个跟Nginx的IP_hash机制类似,我们用其作为解决session问题的一种方法,建议关注;
      ⑤ ri,表示根据请求的URI;
      ⑥ rl_param,表示根据请求的URl参数’balance url_param’ requires an URL parameter name;
      ⑦ hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
      ⑧ rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。
    3、haproxy 配置中分成五部分内容详解
    global:参数是进程级的,通常是和操作系统相关。这些参数一般只设置一次,如果配置无误,就不需要再次进行修改
    defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件
    frontend:接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend
    backend:后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器
    Listen Fronted和backend的组合体
    4、haproxy配置项的介绍
    ###########全局配置#########
    global
     log 127.0.0.1 local0           #日志输出配置,所有日志都记录在本机系统日志,通过local0输出
     log 127.0.0.1 local1 notice         #notice为日志级别,通常有24个级别(error warringinfo debug)
     nbproc 1                    #设置进程数量,通常是CPU核心数或者2倍
     pidfile /etc/haproxy/haproxy.pid       #haproxy 进程PID文件
     maxconn 4096               #最大连接数(需考虑ulimit-n限制 )
     #chroot /usr/share/haproxy             #chroot运行路径
     uid 99                 #用户uid
     gid 99                 #用户gid
     daemon                  #守护进程模式,以后台服务形式允许
     #debug                                 #haproxy 调试级别,建议只在开启单进程的时候调试
    ########默认配置############
    defaults
     log global                 #定义日志为global配置中的日志定义
     mode http                   #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
     option httplog              #日志类别,采用http日志格式记录日志
     #option  dontlognull             #不记录健康检查日志信息             
     retries 3              #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
     #option  forwardfor             #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端i
     #option  httpclose                     #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
     maxconn 4096               #最大连接数
     contimeout 5000            #连接超时时间
     clitimeout 50000           #客户端超时时间
     srvtimeout 50000           #服务器超时时间
     #timeout check 2000              #心跳检测超时
     #timeout http-keep-alive10s         #默认持久连接超时时间
     #timeout http-request   10s         #默认http请求超时时间
     #timeoutqueue          1m           #默认队列超时时间
      ########统计页面配置########
    listen admin_stats
       bind 0.0.0.0:1080            #设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称
       mode http                 #http的7层模式
       option httplog               #采用http日志格式
       #log 127.0.0.1 local0 err         #错误日志记录
      maxconn 10                  #默认的最大连接数
      stats refresh 30s             #统计页面自动刷新时间
      stats uri /stats                                 #统计页面url
      stats realm Crushlinux Haproxy     #统计页面密码框上提示文本
      stats auth admin:admin         #设置监控页面的用户和密码:admin,可以设置多个用户名
      stats hide-version                #隐藏统计页面上HAProxy的版本信息
      #stats admin if TRUE             #设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本)
    ########设置haproxy 错误页面#####
      errorfile 403 /home/haproxy/haproxy/errorfiles/403.http
      errorfile 500 /home/haproxy/haproxy/errorfiles/500.http
      errorfile 502 /home/haproxy/haproxy/errorfiles/502.http
      errorfile 503 /home/haproxy/haproxy/errorfiles/503.http
      errorfile 504 /home/haproxy/haproxy/errorfiles/504.http
    ########frontend前端配置##############
    bibind *:80   #这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。
      acl web hdr(host) -i www.abc.com   #acl后面是规则名称,-i是要访问的域名,
      acl img hdr(host) -i img.abc.com     #如果访问www.abc.com这个域名就分发到下面的webserver 的作用域。
                           #如果访问img.abc.com.cn就分发到imgserver这个作用域。
      use_backend webserver if web
      use_backend imgserver if img
    ########backend后端配置##############
    backend webserver      #webserver作用域
      mode http
      balance roundrobin   #banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数
      option httpchk /index.html HTTP/1.0    #健康检查,检测文件,如果分发到后台index.html访问不到就不再分发给它
      server web1 192.168.200.103:80 cookie 1 weight 1 check inter 2000 rise 2 fall 3
      server web2 192.168.200.104:80 cookie 2 weight 1 check inter 2000 rise 2 fall 3
      #cookie 1表示serverid为1,check inter 1500 是检测心跳频率
      #rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
      backend imgserver
      mode http
      option httpchk /index.php
      balance roundrobin
      server img01 192.168.200.105:80 check inter 2000 fall 3
      server img02 192.168.200.106:80 check inter 2000 fall 3
    ########tcp配置#################
    listen test1
      bind 0.0.0.0:90
      mode tcp
      option tcplog #日志类别,采用tcplog
      maxconn 4086
      #log 127.0.0.1 local0 debug
      server s1 192.168.200.107:80 weight 1
      server s2 192.168.200.108:80 weight 1
    5、基于Haproxy构建负载均衡集群
    1、安装配置Haproxy
    安装Haproxy依赖包及源码包编译安装
    [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
    [root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
    [root@localhost ~]# cd /usr/src/haproxy-1.4.24/
    [root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install
    建立haproxy的配置目录及文件
    [root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
    [root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
    haproxy 配置文件修改
    [root@localhost ~]# vim /etc/haproxy/haproxy.cfg
    # this config needs haproxy-1.1.28 or haproxy-1.2.1
    global
    #   log 127.0.0.1   local0
    #   log 127.0.0.1   local1 notice
        log /dev/log    local0 info
        log /dev/log    local0 notice
        maxconn 4096
        uid 99
        gid 99
        daemon
    defaults
        log global
        mode    http
        option  httplog
        retries 3
        maxconn 4096
        contimeout  5000
        clitimeout  50000
        srvtimeout  50000
    listen  webcluster 0.0.0.0:80
        option  httpchk GET /index.html
        balance roundrobin
        server  inst1 192.168.200.113:80 check inter 2000 fall 3
        server  inst1 192.168.200.114:80 check inter 2000 fall 3
    listen admin_stats
        bind 0.0.0.0:8000
        mode http
        option httplog
        maxconn 100
        stats refresh 30s
        stats uri /stats
        stats realm Crushlinux Haproxy
            stats auth admin:admin
        stats hide-version 
    准备服务自启动脚本
    [root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
    [root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
    [root@localhost ~]# chmod +x /etc/init.d/haproxy
    [root@localhost ~]# /etc/init.d/haproxy start
    Starting haproxy:                                          [确定]
    2、安装配置Web服务Nginx  
    Nginx1,Nginx2配置相同,
    [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
    [root@localhost ~]# useradd -M -s /sbin/nologin nginx
    [root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
    [root@localhost ~]# cd /usr/src/nginx-1.6.2
    [root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
    [root@localhost nginx-1.6.2]# cd /usr/local/nginx/html/
    nginx1 :
    [root@localhost ~]# echo "111111" > /usr/local/nginx/html/index.html
    [root@localhost ~]# /usr/local/nginx/sbin/nginx
    nginx2 :
    [root@localhost ~]# echo "222222" > /usr/local/nginx/html/index.html
    [root@localhost ~]# /usr/local/nginx/sbin/nginx
    6、基于Haporxy+Keepalived构建高可用负载均衡集群
    1、准备四台虚拟机两台nginx 两台haproxy
    2、两台nginx
    [root@Nginx-1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
    [root@Nginx-1 ~]# useradd -M -s /sbin/nologin  nginx
    [root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
    [root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2
    [root@Nginx-1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
    [root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/
    nginx1 :
    [root@localhost ~]# echo "111111" > /usr/local/nginx/html/index.html
    [root@localhost ~]# /usr/local/nginx/sbin/nginx
    nginx2 :
    [root@localhost ~]# echo "222222" > /usr/local/nginx/html/index.html
    [root@localhost ~]# /usr/local/nginx/sbin/nginx
     
    安装Haproxy两台机器配置一致:
    [root@Haproxy-1 ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
    [root@Haproxy-1 ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
    [root@Haproxy-1 ~]# cd /usr/src/haproxy-1.4.24/
    [root@Haproxy-1 haproxy-1.4.24]# make TARGET=linux26 && make install
    建立haproxy的配置目录及文件
    [root@Haproxy-1 haproxy-1.4.24]# mkdir /etc/haproxy
    [root@Haproxy-1 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
     haproxy配置文件的修改:
    [root@Haproxy-1 ~]# vim /etc/haproxy/haproxy.cfg
    # this config needs haproxy-1.1.28 or haproxy-1.2.1
    global
    #   log 127.0.0.1   local0
    #   log 127.0.0.1   local1 notice
        log /dev/log    local0 info
        log /dev/log    local0 notice
        maxconn 4096
        uid 99
        gid 99
        daemon
    defaults
        log global
        mode    http
        option  httplog
        retries 3
        maxconn 4096
        contimeout  5000
        clitimeout  50000
        srvtimeout  50000
    listen  webcluster 0.0.0.0:80
        option  httpchk GET /index.html
        balance roundrobin
        server  inst1 192.168.200.113:80 check inter 2000 fall 3
        server  inst1 192.168.200.114:80 check inter 2000 fall 3
    listen admin_stats
        bind 0.0.0.0:8000
        mode http
        option httplog
        maxconn 100
        stats refresh 30s
        stats uri /stats
        stats realm Crushlinux Haproxy
            stats auth admin:admin
        stats hide-version
    创建自启动脚本
    [root@Haproxy-1 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
    [root@Haproxy-1 ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
    [root@Haproxy-1 ~]# chmod +x /etc/init.d/haproxy
    [root@Haproxy-1 ~]# /etc/init.d/haproxy start
    Starting haproxy:                                          [确定]
     
    客户端访问测试:
    使用不同浏览器访问
    结果不同即为成功
     
    编译安装keepalived服务
    [root@Haproxy-1 ~]# yum -y install keepalived
    [root@Haproxy-1 ~]# vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
            notification_email {
                    acassen@firewall.loc
            }
            notification_email_from Alexandre.Cassen@firewall.loc
            smtp_server 192.168.200.1
            smtp_connect_timeout 30
            router_id LVS_DEVEL
            vrrp_skip_check_adv_addr
            vrrp_strict
            vrrp_garp_interval 0
            vrrp_gna_interval 0
    }
    vrrp_script chk_http_port {
            script "/root/check_haproxy.sh"
            interval 2
            weight -20
    }
    vrrp_instance VI_1 {
            state MASTER
            interface ens32
            virtual_router_id 51
            priority 100
            advert_int 1
            authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.200.254
        }
        track_script {
            chk_http_port
        }
    }
     第二台Haproxy配置keepalibed 主配置文件
    [root@Haproxy-2 ~]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
            notification_email {
                    acassen@firewall.loc
            }
            notification_email_from Alexandre.Cassen@firewall.loc
            smtp_server 192.168.200.1
            smtp_connect_timeout 30
            router_id LVS_DEVEL
            vrrp_skip_check_adv_addr
            vrrp_strict
            vrrp_garp_interval 0
            vrrp_gna_interval 0
    }
    vrrp_script chk_http_port {
            script "/root/check_haproxy.sh"
            interval 2
            weight -20
    }
    vrrp_instance VI_1 {
            state BACKUP
            interface ens32
            virtual_router_id 51
            priority 90
            advert_int 1
            authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.200.254
        }
        track_script {
            chk_http_port
        }
    }          
    两台机器上都配置haproxy检测脚本
    [root@Haproxy-1 ~]# cat /etc/keepalived/check_haproxy.sh
    #!/bin/bash
    num=`ps -C haproxy --no-header |wc -l`
    if [ $num -eq 0 ]
    then
        /etc/init.d/haproxy start
        sleep 3
        if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
        then
            systemctl stop keepalived
        fi
    fi
    [root@Haproxy-1 ~]# chmod +x /root/check_haproxy.sh
    [root@Haproxy-1 ~]# service keepalived start
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    参考文献:https://www.cnblogs.com/crushlinux/p/6738982.htm

  • 相关阅读:
    L1-046. 整除光棍
    判断素数
    L1-025. 正整数A+B
    L1-023. 输出GPLT
    L1-020. 帅到没朋友
    L1-016. 查验身份证
    L1-011. A-B
    UVa 400 Unix Is命令
    Uva 136 丑数
    The Preliminary Contest for ICPC Asia Xuzhou 2019 K. Center
  • 原文地址:https://www.cnblogs.com/elin989898/p/11955540.html
Copyright © 2011-2022 走看看