下载最新版本haproxy
wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.19.tar.gz/sha512/f62b0a18f19295986d26c5c60b6b1ad55850a175bed67a359282cc37a4c630a0c2be51d608226b4316f2e69c3008c20a1cb91ce10f86311219355973a050e65b/haproxy-1.8.19.tar.gz
tar -xvf haproxy-1.8.19.tar.gz && cd haproxy-1.8.19
查看对应内核版本
uname -r
安装下列对应中的版本编译安装
#编译安装
make TARGET=linux2628 PREFIX=/data/haproxy
make install PREFIX=/data/haproxy
useradd -u 40002 haproxy
cp /data/haproxy/sbin/haproxy /usr/sbin/
mkdir -p /data/haproxy/conf
cat > /data/haproxy/conf/haproxy.cfg << EOF
#全局配置
global
#设置日志
log 127.0.0.1 local3 info
chroot /data/haproxy
#用户与用户组
user haproxy
group haproxy
#守护进程启动
daemon
#最大连接数
maxconn 4000
#默认配置
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
#web状态页
listen stats
bind 0.0.0.0:1080 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息
#监听3000端口至后端服务器3306端口
listen mysql-server
bind 0.0.0.0:3000
option tcplog
mode tcp
#负载均衡算法(#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数)
balance roundrobin
#check inter 2000 是检>测心跳频率,rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用
server mysql rm-bp1497zxp8px5j81d.mysql.rds.aliyuncs.com:3306 check inter 5s rise 2 fall 3
EOF
cp /data/haproxy-1.8.19/examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy && sleep 2 && sed -i 's/etc\/$BASENAME/data\/$BASENAME\/conf/g' /etc/init.d/haproxy
systemctl daemon-reload && systemctl enable haproxy && systemctl start haproxy