版本: V1.0日期: [YYYY-MM-DD]编制: [姓名/部门]
1 高可用方案概述
1.1 什么是 K8s 高可用
Kubernetes 高可用(HA)是指通过多节点部署核心组件,确保控制平面在单点甚至多点故障时仍能持续提供服务,保障集群不中断运行。
核心目标:
- 控制平面零单点:Master 节点(API Server、etcd、Controller Manager、Scheduler)多副本部署
- 数据持久可靠:etcd 集群通过 Raft 共识算法实现数据强一致性
- 流量 负载均衡:API Server 和 Ingress 流量通过负载均衡器均匀分发
- 故障自动恢复:节点宕机自动漂转,业务无感知
1.2 高可用层级
| 层级 | 组件 | 高可用方案 |
|---|---|---|
| 数据存储 | etcd | 3/5/7 节点 Raft 集群 |
| 控制平面 | API Server + Controller Manager + Scheduler | 多 Master + 负载均衡 |
| 流量入口 | Ingress | Nginx Ingress 多副本 + 外部 LB |
| 计算节点 | Worker Node | 多节点 + 自动调度 + Pod 反亲和 |
| 管理平台 | Rancher / 控制台 | 多副本部署 + HA 模式 |
2 架构设计方案
2.1 三种主流高可用方案对比
| 方案 | 架构 | 适用规模 | 优点 | 缺点 |
|---|---|---|---|---|
| Keepalived + HAProxy | VIP + L4/L7 负载均衡 | 中小集群(<250节点) | 部署简单、灵活支持 TCP/HTTP | 主节点单活跃,其余备份闲置 |
| LVS + Keepalived | L4 调度 + VIP 漂移 | 大型集群(1000+ PV) | 高性能,适合超大规模 | LVS 成本投入,自身也需 Keepalived |
| Node 侧代理 | 每节点部署 Nginx/OpenResty 反向代理 | 任意规模 | 避免单点 LB,利用节点负载 | 维护成本高,节点扩容后维护量大 |
2.2 推荐方案:Keepalived + HAProxy
选择理由:
- HAProxy 同时支持 L4 TCP + L7 HTTP 两种模式,非常灵活
- 广泛应用于中大型集群规模
- 实现 API Server 的 TCP 模式负载均衡
- 支持 HTTP 到 HTTPS 的强制跳转
scss
┌─────────────────────┐
│ DNS / 域名解析 │
└──────────┬──────────┘
│
┌──────────▼──────────┐
│ VIP (虚拟IP) │
│ Keepalived 主备 │
└──┬──────────┬───────┘
│ │
┌────────▼──┐ ┌───▼────────┐
│ HAProxy 1 │ │ HAProxy 2 │
└─────┬─────┘ └─────┬──────┘
│ │
┌──────────┼──────────┬───┼──────────┐
▼ ▼ ▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐
│Master1 │ │Master2 │ │Master3 │ │... │
│API-Svr │ │API-Svr │ │API-Svr │ │ │
└───┬────┘ └───┬────┘ └───┬────┘ └────────┘
│ │ │
└──────────┴──────────┘
│
┌──────▼──────┐
│ etcd 集群 │
│ (Raft) │
└─────────────┘
2.3 部署场景分类
场景一:中小型 集群 (< 100 节点)
管理平台与业务应用共用一套高可用集群及 etcd,统一部署,简化运维。
场景二:大型 集群 (> 250 节点)
管理平台(如 Rancher Server)与下游业务集群分离,各自独立高可用,互不影响。
3 硬件配置推荐
3.1 基础配置清单
| 角色 | CPU (核) | 内存 (GB) | 根磁盘 (GB) | 数据磁盘 (GB) | 数量 | 说明 |
|---|---|---|---|---|---|---|
| 控制节点 (Master) | 8 | 16 | 100 | 500 | 3 | 最少 3 节点,可 5/7 扩展 |
| 计算节点 (Worker) | 16 | 32 | 100 | 500 | ≥2 | 根据业务量调整 |
| 负载均衡 ( LB ) | 4 | 8 | 300 | - | 2 | HAProxy + Keepalived |
| 镜像仓库 | 4 | 16 | 100 | 500 | 1 | Harbor / 私有仓库 |
3.2 资源配置原则
- Master 节点:控制平面资源消耗相对固定,3 节点是 etcd 最小高可用配置
- Worker 节点:根据业务负载弹性扩展,CPU 和内存是主要瓶颈
- etcd 存储:建议 SSD,IOPS 直接影响集群性能
- 网络:节点间延迟 < 2ms,带宽 ≥ 1Gbps
4 负载均衡器部署
4.1 Keepalived 安装配置
4.1.1 安装
bash
# 在线安装
yum install -y keepalived
# 离线安装(制作 RPM 包)
yum install --downloadonly --downloaddir=/data/rpm keepalived
rpm -ivh /data/rpm/*.rpm
4.1.2 配置(主节点 LB1)
bash
cat > /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
router_id lb-master
}
vrrp_instance VI-kube-master {
state MASTER
priority 110
dont_track_primary
interface <网卡名> # 如 ens192
virtual_router_id 95
advert_int 1
authentication {
auth_type PASS
auth_pass <密码>
}
virtual_ipaddress {
<VIP地址>/24 # 如 10.0.0.100/24
}
}
EOF
4.1.3 配置(备节点 LB2)
typescript
cat > /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
router_id lb-backup
}
vrrp_instance VI-kube-master {
state BACKUP
priority 90 # 低于主节点
dont_track_primary
interface <网卡名>
virtual_router_id 95
advert_int 1
authentication {
auth_type PASS
auth_pass <密码>
}
virtual_ipaddress {
<VIP地址>/24
}
}
EOF
⚠️ 注意 :同一网络若有多个 Keepalived 实例,需修改
virtual_router_id避免冲突。
4.1.4 启动与验证
bash
# 启动并设置开机自启
systemctl enable keepalived && systemctl restart keepalived
# 验证 VIP 漂移
# 1. 主节点执行 ip addr,确认 VIP 在主节点
# 2. 停掉主节点 keepalived:systemctl stop keepalived
# 3. 备节点执行 ip addr,确认 VIP 已漂移
# 4. 重启主节点,VIP 自动回切
4.2 HAProxy 安装配置
4.2.1 安装
yum install -y haproxy
4.2.2 配置(两台 LB 相同)
bash
cat > /etc/haproxy/haproxy.cfg << 'EOF'
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats timeout 30s
user haproxy
group haproxy
daemon
nbproc 1
defaults
log global
timeout connect 5000
timeout client 500000
timeout server 500000
#---------------------------------------------------------------------
# 监控页面
#---------------------------------------------------------------------
listen admin_status
bind 0.0.0.0:8888
mode http
log 127.0.0.1 local3 err
stats refresh 5s
stats uri /stats
stats realm HAProxy\ Statistics
stats auth admin:<密码>
stats hide-version
stats admin if TRUE
#---------------------------------------------------------------------
# API Server 负载均衡(L4 TCP)
#---------------------------------------------------------------------
listen k8s_api
bind *:6443
mode tcp
balance roundrobin
option tcp-check
server master1 <Master1_IP>:6443 check inter 5s fall 3 rise 2
server master2 <Master2_IP>:6443 check inter 5s fall 3 rise 2
server master3 <Master3_IP>:6443 check inter 5s fall 3 rise 2
#---------------------------------------------------------------------
# Ingress 流量(L4 TCP 443)
#---------------------------------------------------------------------
frontend https_ingress
bind *:443
mode tcp
default_backend https_web_server
backend https_web_server
mode tcp
balance roundrobin
stick-table type ip size 200k expire 30m
stick on src
server node1 <Node1_IP>:443 check
server node2 <Node2_IP>:443 check
server node3 <Node3_IP>:443 check
#---------------------------------------------------------------------
# HTTP 强制跳转 HTTPS
#---------------------------------------------------------------------
frontend http_frontend
bind *:80
mode http
redirect scheme https if !{ ssl_fc }
option httpclose
option forwardfor
EOF
4.2.3 启动与验证
bash
systemctl enable haproxy && systemctl restart haproxy
# 验证
telnet <VIP地址> 6443
telnet <VIP地址> 443
# 监控页面:http://<VIP地址>:8888/stats
5 高可用集群部署
5.1 环境准备
5.1.1 系统要求
| 项目 | 要求 |
|---|---|
| 操作系统 | [CentOS 7.6+ / Anolis OS 7.9+ / Ubuntu 20.04+] |
| 内核版本 | ≥ 4.19 |
| Docker / Containerd | Docker 19.03+ / Containerd 1.4+ |
| 网络 | 节点间互通,端口 6443/2379-2380/10250/10251/10252 开放 |
5.1.2 基础配置(所有节点)
bash
# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# 关闭 SELinux
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-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
5.2 容器运行时安装
安装 Docker
bash
# 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装
yum install -y docker-ce-19.03.4 docker-ce-cli-19.03.4 containerd.io
# 配置
cat > /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": ["https://<镜像加速器>"],
"insecure-registries": ["<私有仓库地址>"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
# 启动
systemctl enable docker && systemctl start docker
创建管理用户
xml
useradd <用户名>
passwd <用户名>
groupadd docker
usermod -aG docker <用户名>
5.3 SSH 免密登录
bash
# 在部署机上生成密钥
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# 分发到各节点
for ip in <Master_IPs> <Worker_IPs>; do
ssh-copy-id <用户名>@$ip
done
5.4 使用 RKE 部署 K8s 集群
5.4.1 安装 RKE
bash
chmod +x rke_linux-amd64
mv rke_linux-amd64 /usr/bin/rke
5.4.2 集群配置文件 cluster.yml
yaml
nodes:
# Master 节点(controlplane + etcd)
- address: <Master1_IP>
user: <用户名>
role: ["controlplane", "etcd"]
- address: <Master2_IP>
user: <用户名>
role: ["controlplane", "etcd"]
- address: <Master3_IP>
user: <用户名>
role: ["controlplane", "etcd"]
# Worker 节点
- address: <Node1_IP>
user: <用户名>
role: ["worker"]
- address: <Node2_IP>
user: <用户名>
role: ["worker"]
# 私有镜像仓库
private_registries:
- url: <私有仓库地址>
user: <用户名>
password: "<密码>"
is_default: true
# 服务配置
services:
etcd:
backup_config:
enabled: true
interval_hours: 6
retention: 15
# Ingress 配置
ingress:
provider: nginx
options:
use-forwarded-headers: "true"
5.4.3 创建集群
csharp
# 执行部署
rke up
# 成功后生成 kube_config_cluster.yml
# 查看节点
kubectl --kubeconfig kube_config_cluster.yml get nodes
# 查看 Pod
kubectl --kubeconfig kube_config_cluster.yml get pods --all-namespaces
5.5 部署管理平台(可选)
5.5.1 安装 Helm
bash
tar -zxvf helm-v3.x.x-linux-amd64.tar.gz
cp linux-amd64/helm /usr/bin/
helm version
5.5.2 配置 SSL 证书
ini
# 创建命名空间
kubectl create namespace cattle-system
# 创建 TLS 密钥
kubectl -n cattle-system create secret tls tls-rancher-ingress \
--cert=<证书.pem> \
--key=<证书.key>
5.5.3 部署管理平台
ini
# 添加仓库
helm repo add <仓库名> <仓库URL>
# 渲染模板
helm template <实例名> <chart文件> \
--output-dir . \
--namespace cattle-system \
--set hostname=<域名> \
--set ingress.tls.source=secret \
--set systemDefaultRegistry=<私有仓库>
# 应用
kubectl -n cattle-system apply -R -f ./rancher
# 验证
kubectl get pods -n cattle-system
6 etcd 备份与恢复
6.1 自动备份
在 cluster.yml 中配置:
yaml
services:
etcd:
backup_config:
enabled: true
interval_hours: 6 # 备份间隔
retention: 15 # 保留份数
- 备份文件存放于
/opt/rke/etcd-snapshots - 支持 S3 外部存储
6.2 S3 外部存储备份
yaml
services:
etcd:
backup_config:
enabled: true
s3backupconfig:
access_key: "<AccessKey>"
secret_key: "<SecretKey>"
bucket_name: "<桶名>"
endpoint: "<S3端点>"
region: "<区域>"
6.3 手动备份
perl
# 手动快照
rke etcd snapshot-save \
--name snapshot_$(date +%Y-%m-%d_%H%M%S).db \
--config cluster.yml
# 定时任务
echo '30 02 * * * /usr/bin/rke etcd snapshot-save --name snapshot_$(date +%Y-%m-%d_%H:%M:%S).db --config /path/cluster.yml >> /path/backup.log' | crontab -
6.4 恢复
css
# 从快照恢复
rke etcd snapshot-restore \
--name snapshot_2024-01-01_023001.db \
--config cluster.yml
⚠️ 恢复注意事项:恢复操作会重建集群,请确保已做好数据保护和业务停机通知。
7 故障排查
7.1 常见问题
| 问题 | 原因 | 解决方法 |
|---|---|---|
kubectl 连接被拒绝 |
kubeconfig 未配置或路径错误 | 将 .kube/config 放到正确路径 |
| VIP 不漂移 | Keepalived 配置冲突或网络不通 | 检查 virtual_router_id、防火墙 |
| HAProxy 后端不健康 | Master 节点 API Server 未启动 | 检查 API Server 日志和端口 |
| etcd 集群不可用 | 多数节点宕机 | 至少保留 (N/2+1) 节点在线 |
| Pod 调度失败 | Worker 节点资源不足或污点 | 检查节点状态、资源、污点标记 |
7.2 诊断命令
perl
# 检查节点状态
kubectl get nodes -o wide
# 检查系统 Pod
kubectl get pods -n kube-system
# 检查 etcd 健康
kubectl exec -n kube-system etcd-<node> -- etcdctl endpoint health
# 检查 API Server 日志
journalctl -u kubelet -f
# 检查 Keepalived 状态
ip addr show | grep <VIP>
# 检查 HAProxy 状态
curl http://<VIP>:8888/stats
8 安全加固
| 项目 | 措施 |
|---|---|
| API Server | 启用 RBAC,限制匿名访问 |
| etcd | 启用 TLS 加密,绑定内网接口 |
| 网络策略 | 使用 NetworkPolicy 限制 Pod 间通信 |
| 镜像安全 | 私有仓库 + 镜像签名 + 漏洞扫描 |
| 审计日志 | 启用 API Server 审计日志 |
| 证书管理 | 定期轮换 TLS 证书 |
9 附录
附录 A:端口规划
| 组件 | 端口 | 说明 |
|---|---|---|
| API Server | 6443 | HTTPS API |
| etcd | 2379 | 客户端通信 |
| etcd | 2380 | 节点间通信 |
| Kubelet | 10250 | API |
| Controller Manager | 10257 | HTTPS |
| Scheduler | 10259 | HTTPS |
| HAProxy 监控 | 8888 | Stats 页面 |
附录 B:参考文档
-
Kubernetes 官方高可用指南
-
etcd 运维最佳实践
-
HAProxy 配置参考
-
Keepalived 配置参考
本文档为通用 模板 ,使用前请将 [X] / <占位符> 替换为实际环境信息。