一、使用HAProxy 为TiDB-Server 做负载均衡

安装 docker-compose

环境

  • IP: 192.168.180.46
  • 系统: CentOS 7
  • Core: 8核
  • HAProxy版本 2.0.6
服务器IPhostname
HAProxy192.168.180.46test1
TiDB-Server192.168.180.47test2
TiDB-Server192.168.180.48test3
TiDB-Server192.168.181.18test4

1、创建文件夹

mkdir -p /home/tidb/haproxy/config

2、配置haproxy.cfg

cat > /home/tidb/haproxy/config/haproxy.cfg << eric
global
  maxconn 10000                 # 最大同时10000连接
  daemon                        # 以daemon方式在后台运行

defaults
  log     127.0.0.1 local0 debug       # [emerg, alert, crit, err, warning, notice, info, debug]
  # mode http                   # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
  retries         3             # 连接后端服务器失败重试次数,超过3次后会将失败的后端服务器标记为不可用。
  timeout client  1h            # 客户端响应超时             1小时
  timeout server  1h            # server端响应超时           1小时
  timeout connect 1h            # 连接server端超时           1小时
  timeout check   10s           # 对后端服务器的检测超时时间 10秒

listen stats                    # 定义监控页面
  mode  http
  bind  *:1080                  # 绑定容器内的1080端口
  stats refresh 5s              # 每1秒更新监控数据
  stats uri /stats              # 访问监控页面的uri
  stats realm HAProxy\ Stats    # 监控页面的认证提示
  stats auth admin:654321       # 监控页面的用户名和密码

frontend tidb_front
  mode  tcp
  bind  *:4000                  # 监听容器内的4000端口
  default_backend tidb_back

backend tidb_back
  mode    tcp
  option  tcp-check
  balance roundrobin

  server TiDB-Server-48 192.168.180.47:4000 check inter 10s rise 3 fall 3 weight 1
  server TiDB-Server-49 192.168.180.48:4000 check inter 10s rise 3 fall 3 weight 1
eric

3、创建 docker-compose.yaml 文件

cat > /home/tidb/haproxy/docker-compose.yaml << eric
version: '3.1'

services:

  HAProxy:
    image: haproxy:2.0.6
    restart: always
    container_name: HAProxy
    ports:
      - 4600:4000 # 宿主机端口:容器内端口
      - 1080:1080
    volumes:
      # 容器与宿主机时间同步
      - /etc/localtime:/etc/localtime
      - ./config/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
    environment:
      TIME_ZONE: Asia/Shanghai
eric

查看管理界面: http://192.168.180.46:1080/stats
数据库连接地址: 192.168.180.46:4600

参照官方文档

Logo

一起探索未来云端世界的核心,云原生技术专区带您领略创新、高效和可扩展的云计算解决方案,引领您在数字化时代的成功之路。

更多推荐