zoukankan      html  css  js  c++  java
  • haproxy实现负载均衡

    一.安装
    tar zxvf haproxy-1.4.22.tar.gz
    cd haproxy-1.4.22
    make TARGET=linux26 PREFIX=/usr/local/haproxy install ( 注意要大写)
    注:linux26就是linux内核的版本号可以用uname -r可查看 2.6 就可以写为linux26


    配置:

    cd /usr/local/haproxy
    vi haproxy.conf -->新建配置文件,以下是配置文件的模板在此基础上修改:


    global
    log 127.0.0.1 local0
    maxconn 4096 # 默认最大连接数
    chroot /usr/local/haproxy # chroot运行的路径
    uid 501 # 所属运行的用户uid
    gid 501 # 所属运行的用户组ID
    daemon # 以后台形式运行haproxy
    nbproc 1 # 进程数量(可以设置多个进程提高性能)
    pidfile /usr/local/haproxy/logs/haproxy.pid # haproxy的PID存放路径,启动进程的用户必须有权限访问些文件
    debug
    #ulimit -n 65535 # ulimit 的数据限制

    defaults
    log 127.0.0.1 local3
    mode http # 所处理的类别(7层 http;4层:tcp)
    option httplog # 日志类别http日志格式
    option httpclose # 每次请求完毕后主动关闭http通道
    option dontlognull # 不记录健康检查的日志信息
    option forwardfor # 如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
    option redispatch # serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    retries 2 # 2次连接失败就认为服务不可用,也可以通过后面设置
    maxconn 65535 # 最大连接数
    balance roundrobin # 默认的负载均衡的方式,轮询方式
    #balance source # 默认的负载均衡的方式,类似nginx的ip_hash
    #balance leastconn # 默认的负载均衡的方式,最小连接

    stats uri /haproxy-stats # 监控页面的url
    #stats refresh 30 # 统计页面刷新间隔
    contimeout 5000 # 连接超时
    clitimeout 50000 # 客户端超时
    srvtimeout 50000 # 服务器超时
    #timeout check 2000 # 心跳检测超时

    -------------------------------------------------------------------------------
    ####################监控页面的设置#######################
    listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称
    bind 0.0.0.0:65532 #监听端口
    mode http #http的7层模式
    log 127.0.0.1 local3 err #错误日志记录
    stats refresh 5s #每隔5秒自动刷新监控页面
    stats uri /admin?stats #监控页面的url
    stats realm itnihao itnihao #监控页面的提示信息
    stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名
    stats auth admin1:admin1 #监控页面的用户和密码admin1
    stats hide-version #隐藏统计页面上的HAproxy版本信息
    stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)


    errorfile 403 /etc/haproxy/errorfiles/403.http
    errorfile 500 /etc/haproxy/errorfiles/500.http
    errorfile 502 /etc/haproxy/errorfiles/502.http
    errorfile 503 /etc/haproxy/errorfiles/503.http
    errorfile 504 /etc/haproxy/errorfiles/504.http

    #################HAProxy的日志记录内容设置###################
    capture request header Host len 40
    capture request header Content-Length len 10
    capture request header Referer len 200
    capture response header Server len 40
    capture response header Content-Length len 10
    capture response header Cache-Control len 8


    --------------------------------------------------------------------------------

    ##########以上不用动,主要改以下部分:--->
    # listen 名称(叫什么都可以) *(绑定的IP地址):端口号(对外)
    # 数据库的
    listen mysql_proxy *:3307
    mode tcp # 要是网站就是写成 http
    # 负载均衡策略:
    # 最少链接:leastconn
    # 轮询: roundrobin 每个server根据权重依次被轮询
    # ip-hash: source 同一个IP地址就访问同到初步访问的服务器上
    # 默认为轮询方式
    balance leastconn
    # server 名字(叫什么都可以) ip主机:端口 权重(数值)
    server db1 192.168.2.104:3306 weight 1
    server db2 192.168.2.105:3306 weight 1

    # web服务器的
    listen http *:80
    server web1 192.168.2.10:80 weight 1
    server web1 192.168.2.11:80 weight 1

    #listen web_proxy *:80
    # server web1 192.168.2.104:80 check inter 2000 rise 2 fall 5
    # server web2 192.168.2.105:80 check inter 2000 rise 2 fall 5

    #######################################
    #cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义
    #option httpchk GET /index.html #心跳检测的文件
    #server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
    #服务器定义,cookie 1表示serverid为web1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,
    #fall 3是3次失败认为服务器不可用,weight代表权重
    #######################################

    启动:
    ulimit -SHn 65535
    /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf &

    关闭:
    pkill -9 haproxy

  • 相关阅读:
    取得窗口大小和窗口位置兼容所有浏览器的js代码
    一个简单易用的导出Excel类
    如何快速启动chrome插件
    网页表单设计案例
    Ubuntu下的打包解包
    The source file is different from when the module was built. Would you like the debugger to use it anyway?
    FFisher分布
    kalman filter
    Group delay Matlab simulate
    24位位图格式解析
  • 原文地址:https://www.cnblogs.com/ahwu/p/3818039.html
Copyright © 2011-2022 走看看