1.HAProxy安装:
yum install haproxy -y
2.设置权限允许haproxy进行连接:
查看: getsebool haproxy_connect_any # 默认因该是off
设置:setsebool -P haproxy_connect_any 1 # 开启
3.编辑/etc/haproxy/haproxy.cfg配置:
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#监控
listen admin_stats
bind *:38098 #监听端口
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 Proxy\ Haproxy #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
# mysql 负载:
frontend mysql
bind *:33060 # 客户端访问端口
mode tcp
#log global
option tcplog
default_backend mysqlservers
backend mysqlservers
balance leastconn
server dbsrv1 node3:3306 check port 3306 rise 1 fall 2 maxconn 300 # 真实mysql服务器
4.启动
systemctl start haproxy
5.查看启动状态:
systemctl status haproxy