博客
关于我
使用haproxy搭建web群集
阅读量:755 次
发布时间:2019-03-23

本文共 2359 字,大约阅读时间需要 7 分钟。

haproxy群集调度配置详细指南

前置知识点

1. HTTP请求

通过URL访问网站,使用协议是HTTP协议。HTTP请求分为GET和POST方式。当访问某个URL时,会返回状态码,状态码分为2XX(如200、201、202)、3XX(如301、302)和4XX、5XX(如401、403、404、500、501等)。

2. 负载均衡常用调度算法

负载均衡有多种调度算法,常见的有:

  • RR(Round Robin轮询):轮询调度,逐一分配访问请求。

    • 例如:节点a、b、c,第一个请求到a,第二个到b,第三个到c,第四个回到a。
  • LC(Least Connections最小连接数):根据后端节点连接数动态分配前端请求。

    • 优点:连接数最少的节点处理新请求,节点连接数动态调整。
  • SH(Source Hashing基于来源调度):基于来源IP或Cookie进行调度。

    • 适用于有会话记录的场景,但可能引起某些节点访问量过大。
  • 3. 常见Web群集调度器

    目前主要分为软件和硬件两类:

    • 软件:如LVS、Haproxy、Nginx等开源工具,Haproxy性能最佳但配置复杂。
    • 硬件:如F5、梭子鱼、绿盟等,常用于企业级负载均衡。

    Haproxy群集调度器配置示例

    1. 安装Nginx服务器

    # 安装依赖包
    yum -y install pcre-devel zlib-devel
    # 创建用户组并进入目录
    useradd -s /sbin/nologin nginx
    tar zxf nginx-1.12.0.tar.gz
    cd /usr/src/nginx-1.12.0
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    nginx -t
    echo "192.168.1.10" > /usr/local/nginx/html/index.html
    nginx

    2. 搭建Nginx2

    # 参考上述步骤操作
    #Awareness: Nginx默认路径:/usr/local/nginx/html

    3. 编译安装Haproxy

    # 安装依赖包
    yum -y install pcre-devel bzip2-devel
    # 安装Haproxy
    tar zxf haproxy-1.5.19.tar.gz -C /usr/src
    cd /usr/src/haproxy-1.5.19
    make TARGET=linux26 && make install

    4. Haproxy配置

  • 创建配置文件目录
  • mkdir -p /etc/haproxy
    cp examples/haproxy.cfg /etc/haproxy/
    1. 配置Haproxy文件
    2. global
      log /dev/log local0 info
      log /dev/log local0 notice
      maxconn 4096
      uid 99
      gid 99
      daemon
      defaults
      log global
      mode http
      option httplog
      option dontlognull
      retries 3
      redispatch
      maxconn 2000
      timeout connect 5000
      timeout client 50000
      timeout server 50000
      listen webserver 0.0.0.0:80
      option httpchk GET /index.html
      balance roundrobin
      server inst1 192.168.1.10:80 check inter 2000 fall 3
      server inst2 192.168.1.20:80 check inter 2000 fall 3

      5. 创建自启动脚本

      # 添加启动脚本
      cp examples/haproxy.init /etc/init.d/haproxy
      ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
      chmod +x /etc/init.d/haproxy
      # 启动服务
      chkconfig --add /etc/init.d/haproxy
      /etc/init.d/haproxy start
      # 停止firewalld
      systemctl stop firewalld

      6. 测试Haproxy

      # 重启服务确保配置生效
      /etc/init.d/haproxy restart

      7. Haproxy日志优化

    3. 修改日志目录
    4. vim /etc/rsyslog.d/haproxy.conf
      v /programname==haproxy and $syslogseverity-text==info /var/log/haproxy/haproxy-info.log
      v /programname==haproxy and $syslogseverity-text==notice /var/log/haproxy/haproxy-notice.log
      systemctl restart rsyslog.service
      /etc/init.d/haproxy restart

      8. 查看日志

      cat /var/log/haproxy/haproxy-info.log

    转载地址:http://tgizk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现IIR 滤波器算法(附完整源码)
    查看>>
    Objective-C实现IIR数字滤波器(附完整源码)
    查看>>
    Objective-C实现insertion sort插入排序算法(附完整源码)
    查看>>
    Objective-C实现integer partition整数分区算法(附完整源码)
    查看>>
    Objective-C实现integerPartition整数划分算法(附完整源码)
    查看>>
    Objective-C实现interpolation search插值搜索算法(附完整源码)
    查看>>
    Objective-C实现Interpolation search插值查找算法(附完整源码)
    查看>>
    Objective-C实现intersection交集算法(附完整源码)
    查看>>
    Objective-C实现intro sort内省排序算法(附完整源码)
    查看>>
    Objective-C实现inverse matrix逆矩阵算法(附完整源码)
    查看>>
    Objective-C实现inversions倒置算法(附完整源码)
    查看>>
    Objective-C实现isalpha函数功能(附完整源码)
    查看>>
    Objective-C实现islower函数功能(附完整源码)
    查看>>
    Objective-C实现isPowerOfTwo算法(附完整源码)
    查看>>
    Objective-C实现isupper函数功能(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现iterating through submasks遍历子掩码算法(附完整源码)
    查看>>
    Objective-C实现iterative merge sort迭代归并排序算法(附完整源码)
    查看>>
    Objective-C实现jaccard similarity相似度无平方因子数算法(附完整源码)
    查看>>