HAProxy 部署

42 阅读1分钟

部署步骤

  1. apt 安装
# 安装 HAProxy
apt-get update
apt-get install -y haproxy

# 查看版本
haproxy -v

2. 修改默认的配置文件 /etc/haproxy/haproxy.cfg 内容

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    # 统计socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# Default settings
#---------------------------------------------------------------------
defaults
    mode                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option                  redispatch
    retries                 3
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# HAProxy 统计页面配置(可选)
#---------------------------------------------------------------------
listen stats
    bind *:9000
    mode http
    stats enable
    stats uri /haproxy-stats
    stats auth admin:admin123
    stats refresh 30s
    stats realm HAProxy\ Statistics

#---------------------------------------------------------------------
# Kubernetes API Server 前端配置
#---------------------------------------------------------------------
frontend kubernetes-apiserver
    bind *:6443
    mode tcp
    option tcplog
    default_backend kubernetes-master

#---------------------------------------------------------------------
# Kubernetes API Server 后端配置
#---------------------------------------------------------------------
backend kubernetes-master
    mode tcp
    balance roundrobin
    option tcp-check
    # 健康检查:连接到 6443 端口
    # Master 节点配置
    server server-03 192.168.174.131:6443 check inter 2000 rise 2 fall 3
    server server-04 192.168.174.133:6443 check inter 2000 rise 2 fall 3
    server server-05 192.168.174.134:6443 check inter 2000 rise 2 fall 3

3. 重启 haproxy 应用配置

systemctl restart haproxy

4. 验证部署成功

浏览器访问 http://192.168.174.128:9000/haproxy-stats

image.png