HAProxy配置文件分成五个部分,主要介绍常用配置
1.global:设置全局配置参数,主要是进程、操作系统相关的配置
2.defaults:配置默认参数,这些参数可以被用到frontend、backend、listen组件
3.frontend:接收请求的前端虚拟节点,可以添加相应的规则匹配到后端backend
4.backend:后端真实服务器集群配置
5.listen:frontend和backend的组合
global组件
global daemon user haproxy group haproxy nbproc 4 maxconn 20000 ulimit-n 16384 pidfile /var/run/haproxy.pid log 127.0.0.1 local0 info
|
defaults组件
defaults mode http log 127.0.0.1 local0 error retries 3 option httplog option redispatch option abortonclose option dontlognull option httpclose timeout connect 5000 timeout client 5000 timeout server 5000 timeout check 5000
|
frontend组件
frontend myweb bind 0.0.0.0:80 mode http log global option forwardfor acl web_static_req /*.(css|jpg|png|jpeg|js|gif)$ acl realserver_req nbsrv(static_server) lt 1 default_backend web_server use_backend web_server if realserver_req use_backend static_server if web_static_req
|
backend组件
backend web_server mode http balance roundrobin option httpchk GET /test/index.php check表示接受健康检测 inter表示健康检测间隔 rise表示检测成功多少次正常才算正常 fail表示检测多少次失败才算失败 weight表示分发权重 maxconn表示最大连接 server server1 172.18.0.2:3306 check inter 2000 rise 3 fall 3 weight 1 maxconn 2000 server server2 172.18.0.3:3306 check inter 2000 rise 3 fall 3 weight 1 maxconn 2000 server server3 172.18.0.4:3306 check inter 2000 rise 3 fall 3 weight 1 maxconn 2000
|
listen组件
listen admin_stats bind 0.0.0.0:8888 mode http log global stats refresh 30s stats uri /dbs stats realm Global\ statistics stats auth admin:admin stats hide-version
listen proxy-mysql bind 0.0.0.0:3306 mode tcp balance roundrobin option tcplog option mysql-check user haproxy option tcpka server MySQL_1 192.168.37.100:3306 check weight 1 maxconn 2000 server MySQL_2 192.168.37.101:3306 check weight 1 maxconn 2000 server MySQL_3 192.168.37.102:3306 check weight 1 maxconn 2000
|