haproxy代理数据库

发布时间 2023-07-19 10:54:09作者: 不懂123

安装部署

cp haproxy-systemd-wrapper  /usr/sbin/
cp haproxy /usr/sbin

chmod +x /usr/sbin/haproxy
chmod +x /usr/sbin/haproxy-systemd-wrapper
mkdir -p /etc/haproxy

vi /etc/haproxy/haproxy.cfg

useradd haproxy
chown -R haproxy:haproxy /etc/haproxy

mkdir /var/lib/haproxy
chown -R haproxy:haproxy /var/lib/haproxy




vi /etc/sysconfig/haproxy

# Add extra options to the haproxy daemon here. This can be useful for
# specifying multiple configuration files with multiple -f options.
# See haproxy(1) for a complete list of options.
OPTIONS=""




vi /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
EnvironmentFile=/etc/sysconfig/haproxy
ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed

[Install]
WantedBy=multi-user.target




systemctl daemon-reload
systemctl restart haproxy
安装步骤

 

配置文件说明

#---------------------------------------------------------------------
# 全局设定部分
#---------------------------------------------------------------------
global

defaults
        log     global
        mode    tcp
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000

#---------------------------------------------------------------------
# HAPROXY状态页面
#---------------------------------------------------------------------
listen admin_stats
   stats       enable
   # 监听端口
   bind        *:12345
   mode        http
   option      httplog
   log         global
   maxconn     10
   # 刷新间隔
   stats       refresh          30s
   # 页面路径
   stats       uri              /
   stats       realm            haproxy
   # 访问认证
   stats       auth             admin:password
   stats       hide-version

#---------------------------------------------------------------------
# TCP转发及负载均衡及简单TCP可用性校验
# weight权重;check inter检测频率;rise 1一次可用恢复;fall 2两次失败降级
#---------------------------------------------------------------------
frontend tidb-in
    bind     *:10000
    maxconn  80000
    default_backend tidb

backend tidb
    #使用HTTP对URI路径可用性进行检测
    #option httpchk
    #http-check send meth GET uri /check.html
    #http-check expect status 200

    server tidb-1    10.30.92.70:4000    maxconn 40480  weight 10  check inter 10s  rise 1 fall 2
    server tidb-2    10.30.92.71:4000    maxconn 40480  weight 10  check inter 10s  rise 1 fall 2
    server tidb-3    10.30.92.73:4000    maxconn 44440  weight 10  check inter 10s  rise 1 fall 2
haproxy.cfg

     

      

       负载均衡配置

       

     haproxy可以不需要虚拟vip,可以在多台主机上同时部署haproxy并且进行同样的配置,这样客户端无论连接哪台haproxy主机都能把请求转发到后台的服务主机上进行处理