首页 > 代码库 > haproxy

haproxy

安装

yum install -y haproxy
yum install -y socat

提示:建议源码安装,socat用于访问socket

 

配置

 

global  # 全局配置
    log         127.0.0.1 local2  # centos7 /etc/rsyslog.conf日志配置
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
	stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin  
# 监控unix socket 权限600,级别admin
	
defaults  # 默认配置
    mode                    http   # http 七层
    log                     global  # 日志记录使用global中定义
    option                  httplog  # 记录http日志,建议关闭
    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
	
listen stats  # 监听
    mode http
    bind 0.0.0.0:8888
    stats enable
    stats uri     /haproxy-status   # 监听URL
    stats auth    haproxy:saltstack  # 用户名、密码
	
frontend frontend_www_test_com  # 前端配置名称
    bind 192.168.137.11:80  # VIP
    mode http  # http协议
    option httplog
    log global  # 日志输出
    default_backend backend_www_test_com  # 跳转后端名称
	
backend backend_www_example_com  # 后端配置名称
    option forwardfor header X-REAL-IP
    option httpchk HEAD / HTTP/1.0  # 监控检查
    balance source  # 负载均衡算法,源IP hash
    server web-node1  192.168.137.21:8080 check inter 3000 rise 30 fall 10  
    # 参数: check启用监控检查, inter 3000健康检查的时间间隔(毫秒),rise 30检查服务连续可以次数,加入这台服务,fall 10检查服务连续不可用的次数,剔除这台服务
    server web-node2  192.168.137.22:8080 check inter 3000 rise 30 fall 10

 

  • 指定后端节点进入维护模式
echo "disable server backend_www_example_com/web-node1" |grep socat stdio /var/lib/haproxy/haproxy.sock

 技术分享

  • 指定后端节点进入访问模式
echo "enable server backend_www_example_com/web-node1" |grep socat stdio /var/lib/haproxy/haproxy.sock

技术分享

 

haproxy