依赖安装
yum install -y wget gcc pcre-static pcre-devel
下载haproxy
wget https://www.haproxy.org/download/1.9/src/haproxy-1.9.7.tar.gz
解压haproxy
tar -zxvf haproxy-1.9.7.tar.gz
编译安装
cd haproxy-1.9.7
make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cp -rf /root/haproxy-1.9.7/examples/errorfiles /usr/local/haproxy/
设置日志输出
vi /etc/rsyslog.conf
添加#19
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 127.0.0.1
local2.* /var/log/haproxy.log
重启日志服务生效
systemctl restart rsyslog
创建haproxy配置文件输出
mkdir -p /usr/local/haproxy/conf/
vi /usr/local/haproxy/conf/haproxy.cfg
global
log 127.0.0.1 local2 info
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
maxconn 4096
#user haproxy
#group haproxy
daemon
stats timeout 2m
nbproc 1
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 51200
#stats UI
listen stats
mode http
bind 0.0.0.0:1080
maxconn 10
stats enable
stats refresh 10s
stats uri /haproxystats
stats hide-version
stats realm Haproxy\ Statistics
stats auth admin:admin
bind-process 1
stats admin if TRUE
errorfile 400 /usr/local/haproxy/errorfiles/400.http
errorfile 403 /usr/local/haproxy/errorfiles/403.http
errorfile 408 /usr/local/haproxy/errorfiles/408.http
errorfile 500 /usr/local/haproxy/errorfiles/500.http
errorfile 502 /usr/local/haproxy/errorfiles/502.http
errorfile 503 /usr/local/haproxy/errorfiles/503.http
errorfile 504 /usr/local/haproxy/errorfiles/504.http
# http frontend config
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 30
capture request header Referer len 60
default_backend httpservers
# http backend config
backend httpservers
mode http
balance roundrobin
option redispatch
option forwardfor
option httpclose
server web1 192.168.31.74:9818 check inter 1000
server web2 192.168.31.61:2011 check inter 1000
# msql frontend config
frontend msql-in
bind *:3306
mode tcp
log global
option dontlognull
default_backend mysql_server
# mssql backend config
backend mysql_server
mode tcp
balance roundrobin #leastconn roundrobin
option log-health-checks
server mysql_1 192.168.31.37:3306 weight 1 check
server mysql_2 192.168.31.50:3306 weight 1 check
添加配置文件软连接
mkdir -p /etc/haproxy/
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/
全局变量
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
拷贝开机启动文件
cp -r /root/haproxy-1.9.7/examples/haproxy.init /etc/rc.d/init.d/haproxy
添加脚本执行权限
chmod +x /etc/rc.d/init.d/haproxy
设置开机启动
chkconfig --add haproxy
chkconfig haproxy on
启动haproxy服务
systemctl start haproxy
配置文件语法是否正确
haproxy -f /usr/local/haproxy/conf/haproxy.cfg -c
页面测试默认账户admin/admin
http://192.168.31.66:1080/haproxystats