K8s 多主集群部署实战指南
这篇文章整理的是 Kubernetes 多主集群的基础部署思路,包含控制平面、工作节点和负载均衡层的建议角色划分。相比单 Master 架构,多主集群更适合需要高可用接入点和控制平面冗余的场景。
角色规划
| 角色 | 数量 | 推荐主机名 | 建议配置 | 关键组件 |
|---|---|---|---|---|
| Master | 3 | k8s-master-01/02/03 | 2C/4G | API Server, Etcd, Controller, Scheduler |
| Worker | 3 | k8s-node-01/02 | 2C/4G | Kubelet, Containerd/Docker, Proxy |
| Load Balancer | 2 | k8s-lb-01/02 | 1C/1G | HAProxy, Keepalived |
| VIP | 1 | - | - | 虚拟 IP(用于高可用接入点) |
这个规划的核心目标很明确:
- 3 台 Master 负责控制平面高可用。
- 2 台 LB 提供统一的
6443访问入口。 - 1 个 VIP 作为集群外部接入地址。
基础环境准备
所有节点在安装前,都建议先完成系统层面的统一配置。
关闭防火墙和 SELinux
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config禁用 Swap
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab加载内核模块
cat > /etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter配置内核参数
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
EOF
sysctl --system设置时间同步
yum install -y chrony
systemctl enable --now chronyd
chronyc sources -v设置主机名解析
所有节点都要维护一致的 /etc/hosts,避免在早期阶段因为 DNS 不完善导致节点间通信异常。
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
10.0.5.10 lb-vip
10.0.5.1 k8s-master-01
10.0.5.2 k8s-master-02
10.0.5.3 k8s-master-03
10.0.5.4 k8s-node-01
10.0.5.5 k8s-node-02
10.0.5.6 k8s-node-03
10.0.5.7 k8s-lb-01
10.0.5.8 k8s-lb-02
EOF负载均衡层部署
多主集群的访问入口通常不会直接暴露到某一台 Master,而是通过 HAProxy 和 Keepalived 做成统一入口。
HAProxy
适用机器:k8s-lb-01、k8s-lb-02
安装
yum install -y haproxy生成配置
这里的核心是把三台 Master 的 6443 端口都纳入转发池。
cat > /etc/haproxy/haproxy.cfg <<EOF
global
log /dev/log local0
maxconn 20000
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
timeout connect 5s
timeout client 50s
timeout server 50s
frontend k8s-api
bind *:6443
default_backend k8s-api
backend k8s-api
balance roundrobin
option tcp-check
server k8s-master-01 10.0.5.1:6443 check port 6443 inter 5s fall 3 rise 2
server k8s-master-02 10.0.5.2:6443 check port 6443 inter 5s fall 3 rise 2
server k8s-master-03 10.0.5.3:6443 check port 6443 inter 5s fall 3 rise 2
frontend k8s-metrics
bind *:10250
default_backend kubelet-metrics
backend kubelet-metrics
balance roundrobin
option tcp-check
server k8s-master-01 10.0.5.1:10250 check
server k8s-master-02 10.0.5.2:10250 check
server k8s-master-03 10.0.5.3:10250 check
EOF启动并验证
systemctl enable --now haproxy
ss -lntp | grep haproxyKeepalived
适用机器:k8s-lb-01、k8s-lb-02
安装
yum install -y keepalived配置
先确认实际网卡名称,例如 ens33 或 eth0,再修改配置里的 interface 字段。
k8s-lb-01:
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
router_id k8s-lb-01
}
vrrp_script check_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 3
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.5.10
}
track_script {
check_haproxy
}
}
EOFk8s-lb-02:
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
router_id k8s-lb-02
}
vrrp_script check_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 3
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.5.10
}
track_script {
check_haproxy
}
}
EOF验证思路
Keepalived 的重点不是“服务启动了”,而是 VIP 是否真的漂移正常。建议检查这些点:
- 主节点上 VIP 是否已经绑定。
- 停掉主节点
haproxy或keepalived后,VIP 是否切到备节点。 - 通过 VIP 访问
6443时,是否能稳定转发到任意 Master。
小结
这部分内容的重点其实是“把集群入口先做好”。在多主架构里,如果 LB 和 VIP 没有先理顺,后面的 kubeadm init、节点加入和 kubeconfig 配置都会绕不开入口地址问题。先把基础环境、主机名解析、HAProxy 和 Keepalived 配齐,后续控制平面部署会顺很多。