实现 Debian 上的 Kubernetes(k8s)高可用(HA)部署,核心目标是:控制平面(API Server、etcd、Controller Manager、Scheduler)多副本 + 负载均衡 + 多 Worker 节点。下面给你一套可直接落地的 HA 架构与步骤(基于 kubeadm,生产最常用)。
| 角色 | 数量 | 说明 |
|---|---|---|
| Load Balancer | 2(或 1+VIP) | 用于 API Server 高可用 |
| Control Plane(Master) | 3 | 运行 API Server / etcd |
| Worker Node | ≥2 | 运行业务 Pod |
推荐 etcd 使用 kubeadm 内置 etcd(堆叠模式)
cat /etc/debian_version
# 推荐 Debian 11 / 12
# 关闭 swap
swapoff -a
sed -i '/swap/d' /etc/fstab
# 内核参数
cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
modprobe br_netfilter
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
apt update
apt install -y containerd
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
# 使用 systemd cgroup
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
systemctl restart containerd
systemctl enable containerd
apt install -y apt-transport-https ca-certificates curl gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key |
gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] \
https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /" |
tee /etc/apt/sources.list.d/kubernetes.list
apt update
apt install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
✅ 推荐:Keepalived + HAProxy
apt install -y haproxy keepalived
# /etc/haproxy/haproxy.cfg
frontend k8s-api
bind 0.0.0.0:6443
mode tcp
default_backend k8s-api-backend
backend k8s-api-backend
mode tcp
balance roundrobin
server master1 192.168.1.11:6443 check
server master2 192.168.1.12:6443 check
server master3 192.168.1.13:6443 check
systemctl restart haproxy
# /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.1.100
}
}
systemctl restart keepalived
✅ API Server 访问地址:
https://192.168.1.100:6443
kubeadm init \
--control-plane-endpoint "192.168.1.100:6443" \
--upload-certs \
--pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
cp /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubeadm join 192.168.1.100:6443 \
--token <token> \
--discovery-token-ca-cert-hash sha256:<hash> \
--control-plane \
--certificate-key <cert-key>
✅ 此时你已有 3 个 API Server + 3 个 etcd
kubeadm join 192.168.1.100:6443 \
--token <token> \
--discovery-token-ca-cert-hash sha256:<hash>
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
kubectl get nodes
kubectl get pods -n kube-system
✅ 应看到:
✅ etcd 至少 3 节点
✅ API Server 必须 LB
✅ Worker ≥ 2
✅ 使用 Rook / Longhorn 做存储 HA
✅ 使用 Ingress + 多副本 Pod
✅ 定期备份 etcd
| 场景 | 方案 |
|---|---|
| 超大规模 | 外部 etcd 集群 |
| 云环境 | 云 LB(SLB / ELB) |
| 离线部署 | kubeadm + 私有镜像 |
| 安全 | kube-apiserver 证书 + RBAC |
如果你愿意,我可以:
直接告诉我你的 节点数量 & 网络环境 即可。