HAProxy 节点和 RKE2 Server 节点是重合的(130 和 131 既是负载均衡器又是控制平面)。 这需要解决一个关键问题:避免 HAProxy 和 RKE2 的 API Server 在 6443 端口上冲突。
最优方案:
-
HAProxy 前端绑定 16443端口
*:16443。 -
RKE2 的 API Server 保持默认监听
0.0.0.0:6443。
下面是完整配置,适合直接使用。
环境确认
| 主机 | IP | 角色 | 安装组件 |
|---|---|---|---|
| node1 | 192.168.26.130 | RKE2 Server + LB 主 , k8s-control-plane | RKE2, HAProxy, Keepalived |
| node2 | 192.168.26.131 | RKE2 Server + LB 备, k8s-control-plane | RKE2, HAProxy, Keepalived |
| node3 | 192.168.26.132 | RKE2 Server, k8s-control-plane | RKE2 |
| worker01 | 192.168.26.110 | RKE2 Agent, k8s-worker | RKE2 |
| VIP | 192.168.26.100 | 外部入口 | 由 Keepalived 管理 |
1. 基础准备(在 node1 和 node2 上执行)
sudo apt update && sudo apt upgrade -y
sudo apt install -y haproxy keepalived
# 先停止服务,配置好再启动
sudo systemctl stop haproxy keepalived
sudo systemctl disable haproxy keepalived
防火墙设置(如有 UFW)
# VIP 通信使用的 VRRP 协议
sudo ufw allow in on eth33 proto vrrp # 按实际网卡名替换
sudo ufw allow 6443/tcp # API Server 端口
# 如果需要从外部访问 HAProxy 统计页
sudo ufw allow 8404/tcp
2. HAProxy 配置(node1 和 node2 完全一样)
编辑 /etc/haproxy/haproxy.cfg:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn 4096
defaults
log global
mode tcp # 默认四层,透传 API Server 的 TLS
option tcplog
timeout connect 5s
timeout client 30s
timeout server 30s
# ---------- API Server 前端(监听 16443) ----------
frontend k8s-api
bind *:16443 # haproxy监听16443
mode tcp
maxconn 2000
default_backend k8s-api-servers
# ---------- 后端真实 API Server 池 ----------
backend k8s-api-servers
mode tcp
balance roundrobin
option tcp-check # 简单检测端口是否可连
# 三个 RKE2 控制平面节点
server rke2-130 192.168.26.130:6443 check inter 2000 rise 2 fall 3
server rke2-131 192.168.26.131:6443 check inter 2000 rise 2 fall 3
server rke2-132 192.168.26.132:6443 check inter 2000 rise 2 fall 3
# ---------- 统计页面(可选) ----------
listen stats
bind *:8404
mode http
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:your_password
说明
-
bind *:16443绑定的是16443 -
后端列表中包含本机(130/131)自己,HAProxy 会将部分请求直接发给本机的 API Server,没有任何问题。
验证语法:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
3. Keepalived 配置
3.1 健康检查脚本(两台节点都要有)
创建 /etc/keepalived/check_haproxy.sh:
#!/bin/bash
# 检查进程是否存在
if pgrep -x "haproxy" > /dev/null; then
exit 0
else
exit 1
fi
给执行权限
sudo chmod +x /etc/keepalived/check_haproxy.sh
3.2 主节点(192.168.26.130)
编辑 /etc/keepalived/keepalived.conf:
! Configuration File for keepalived
global_defs {
router_id haproxy01
}
vrrp_script check_apiserver {
script "/etc/keepalived/check_apiserver.sh"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
# 状态,主节点为MASTER,从节点为BACKUP
state MASTER
# 修改为你自己网卡的名字
interface ens33
virtual_router_id 51
# MASTER当中使用101,BACKUP当中使用100
priority 101
authentication {
auth_type PASS
# 设置好你的密码,keepalived集群当中需要保证这个值的一致
auth_pass yourpasswd
}
virtual_ipaddress {
# 注意这里修改为你自己的虚拟IP地址
192.168.26.100
}
track_script {
check_apiserver
}
}
3.3 备节点(192.168.26.131)
编辑 /etc/keepalived/keepalived.conf:
! Configuration File for keepalived
global_defs {
router_id haproxy02
}
vrrp_script check_apiserver {
script "/etc/keepalived/check_apiserver.sh"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
# 状态,主节点为MASTER,从节点为BACKUP
state BACKUP
# 修改为你自己网卡的名字
interface ens33
virtual_router_id 51
# MASTER当中使用101,BACKUP当中使用100
priority 100
authentication {
auth_type PASS
# 设置好你的密码,keepalived集群当中需要保证这个值的一致
auth_pass yourpasswd
}
virtual_ipaddress {
# 注意这里修改为你自己的虚拟IP地址
192.168.26.100
}
track_script {
check_apiserver
}
}
切记
-
interface必须填写正确的物理网卡名称(如ens192、eth33)。 -
auth_pass两台节点完全一致。 -
virtual_router_id在局域网内不能冲突。
4. 启动与验证
启动服务(node1 和 node2)
sudo systemctl enable --now haproxy keepalived
检查 VIP 位置(在主节点上)
ip a show ens33 | grep 192.168.26.100
预期主节点有 VIP,备节点无。
测试 API Server 访问
从任意可路由到 VIP 的客户端执行:
curl -k https://192.168.26.100:16443/version
应返回 Kubernetes 版本 JSON。 后续配置 kubeconfig 时,将 server 地址填为 https://192.168.26.100:16443。
故障转移测试
-
在主节点
sudo systemctl stop haproxy,VIP 应漂移到 131。 -
curl再次验证,依然可用。 -
重启 HAProxy,VIP 自动回切。
5. RKE2 集群层面的注意事项
-
RKE2 在多 server 节点部署时,需配置一个固定的 外部负载均衡地址(也就是我们这里的 VIP)。
-
在 RKE2 的配置文件(如
/etc/rancher/rke2/config.yaml)中,可能需要指定advertise-address为本机物理 IP,而tls-san添加 VIP 地址,确保证书包含192.168.26.100。 -
RKE2 的 kubelet 等内部组件依然使用
127.0.0.1:6443的内置代理,完全不依赖外部 LB,因此即便 HAProxy 故障,集群内部通信不受影响。
你的环境已经完整覆盖了 RKE2 控制平面的高可用入口,且没有端口冲突。 如果后续需要在 80/443 上为应用流量(Ingress)添加负载均衡,只需要在 HAProxy 配置中额外增加 frontend/backend 即可。
安装RKE2 HA
在已有 HAProxy + Keepalived 的情况下,安装 RKE2 高可用集群只需在所有控制平面节点上启动 RKE2 Server,并把它们指向同一个负载均衡 VIP 即可。下面针对 Ubuntu 24.04 + 中国网络环境 给出完整步骤。
1. 节点环境说明
| 主机名 | IP | 角色 |
|---|---|---|
| node1 | 192.168.26.130 | RKE2 Server + HAProxy 主 k8s control-plane |
| node2 | 192.168.26.131 | RKE2 Server + HAProxy 备 k8s control-plane |
| node3 | 192.168.26.132 | RKE2 Server k8s control-plane |
| work01 | 192.168.26.110 | RKE2 agent k8s worker |
| VIP | 192.168.26.100 |
API Server 负载均衡入口 |
2. 基础环境准备(所有节点)
2.1 系统设置
# 关闭 swap(Kubernetes 要求)
sudo swapoff -a
sudo sed -i '/swap/d' /etc/fstab
# 确保内核模块加载
sudo modprobe overlay
sudo modprobe br_netfilter
# 设置 sysctl 参数
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
2.2 时间同步
sudo apt update
sudo apt install -y chrony
sudo systemctl enable --now chrony
2.3 修改主机名
hostnamectl hostname node1
hostnamectl hostname node2
hostnamectl hostname node3
3. 配置 RKE2 国内镜像加速(关键)
中国网络访问 docker.io、gcr.io 受限,RKE2 提供两种方式解决:
方式一:在线安装 + 国内镜像源(推荐)
RKE2 安装脚本支持 INSTALL_RKE2_MIRROR=cn,会自动从阿里云 OSS 下载二进制包。 同时,在 /etc/rancher/rke2/config.yaml 中设置 system-default-registry 指向阿里云镜像库。
方式二:离线安装(备选,略)
如果在线仍失败,可先手动下载 RKE2 的 tar.gz 和 images.lz4,然后分发到各节点。本文使用在线方式。
4. 安装第一个 RKE2 Server(node1:192.168.26.130)
4.1 创建配置文件
sudo mkdir -p /etc/rancher/rke2
编辑 /etc/rancher/rke2/config.yaml:
# 使用国内镜像仓库
system-default-registry: "registry.cn-hangzhou.aliyuncs.com"
# 在 TLS 证书中增加额外 SAN(负载均衡 VIP 和各节点 IP)
tls-san:
- "192.168.26.100" # VIP
- "192.168.26.130"
- "192.168.26.131"
- "192.168.26.132"
# 如果有域名也可以添加
# 生成一个随机 token,其他 Server 加入时需相同
token: my-rke2-secret-token # 请替换为随机字符串
# Server 监听地址(默认监听所有地址即可,无需特别设置)
# node-ip: 192.168.26.130 # 可选,建议不设,让 RKE2 自动选择
# 写入 kubeconfig 到指定路径(默认 /etc/rancher/rke2/rke2.yaml)
write-kubeconfig-mode: "0644"
4.2 执行安装
# 使用中国镜像安装脚本
curl -sfL https://rancher-mirror.rancher.cn/rke2/install.sh | INSTALL_RKE2_MIRROR=cn sh -
国际用户脚本:
# 国际用户,国内无法连接docker.io和github
curl -sfL https://get.rke2.io | sh -
安装完成后启动服务:
sudo systemctl enable rke2-server.service
sudo systemctl start rke2-server.service
4.3 等待 Server 就绪
# 查看日志(等待出现 "Cluster-Http-Server started")
sudo journalctl -u rke2-server -f
# 检查节点是否 Ready(可能需要等待20-30 分钟)
sudo /var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml get nodes
5. 加入第二个和第三个 RKE2 Server
5.1 在 node2 和 node3 上创建配置文件
/etc/rancher/rke2/config.yaml 内容与 node1 基本一致,只需修改 token 和增加 server 指向 VIP:
system-default-registry: "registry.cn-hangzhou.aliyuncs.com"
tls-san:
- "192.168.26.100"
- "192.168.26.130"
- "192.168.26.131"
- "192.168.26.132"
token: my-rke2-secret-token # 和 node1 相同
server: https://192.168.26.100:9345 # 使用 VIP 连接第一个 Server
write-kubeconfig-mode: "0644"
注意:
server地址必须是https://VIP:9345,这是 RKE2 的注册端口(非 6443)。VIP 此时应该已由 Keepalived 挂在主节点上,并且 HAProxy 不需要代理 9345,因为 RKE2 注册是直接到第一个 Server 的 9345,现在通过 VIP 即可到达主节点(目前 VIP 在主节点上)。后续若主节点故障,VIP 漂移后新加入的节点可能短暂失败,但实际上集群已形成后影响很小。稳妥起见可以配置 HAProxy 也代理 9345 端口,但不是必须。
5.2 安装 RKE2 并启动(node2、node3 均执行)
curl -sfL https://rancher-mirror.rancher.cn/rke2/install.sh | INSTALL_RKE2_MIRROR=cn sh -
sudo systemctl enable rke2-server.service
sudo systemctl start rke2-server.service
同样用 journalctl -u rke2-server -f 观察日志,直至节点加入集群。
6. 验证高可用集群
6.1 从任意节点查看节点状态
sudo /var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml get nodes
输出应显示三个节点均为 Ready,并且角色为 control-plane 或 master。
6.2 故障转移测试
-
在 node1 上
sudo systemctl stop haproxy或sudo poweroff,VIP 应自动漂移到 node2。 -
kubectl get nodes应仍然正常。 -
检查集群内 Pod 运行状态,确保 etcd 和 control-plane 容错。
7. (可选)添加 Worker 节点
若需要专用 Worker,在另一台机器上使用 agent 配置:
/etc/rancher/rke2/config.yaml:
system-default-registry: "registry.cn-hangzhou.aliyuncs.com"
token: my-rke2-secret-token
server: https://192.168.26.100:9345
然后安装 rke2-agent:
curl -sfL https://rancher-mirror.rancher.cn/rke2/install.sh | INSTALL_RKE2_MIRROR=cn INSTALL_RKE2_TYPE="agent" sh -
sudo systemctl enable rke2-agent.service
sudo systemctl start rke2-agent.service
8. Containerd 镜像仓库配置
Containerd 可以配置连接到私有镜像仓库,并使用它们在每个节点上提取私有镜像。
启动时,RKE2 将检查/etc/rancher/rke2/是否存在registries.yaml文件,并指示 containerd 使用该文件中定义的任何镜像仓库。如果你希望使用一个私有的镜像仓库,那么你将需要在每个使用镜像仓库的节点上以 root 身份创建这个文件。
注意,server 节点默认是可调度的。如果你没有 tainted server 节点,并在 server 上运行工作负载,请确保你也在每个 server 上创建registries.yaml文件。
mirrors:
docker.io:
endpoint:
- "https://harbor.local"
configs:
"harbor.local":
auth:
username: admin # this is the registry username
password: Harbor12345 # this is the registry password
tls:
insecure_skip_verify: true # may be set to true to skip verifying the registry's certificate 跳过tls验证
# cert_file: # path to the cert file used to authenticate to the registry
# key_file: # path to the key file for the certificate used to authenticate to the registry
# ca_file: # path to the ca file used to verify the registry's certificate
至此,基于 HAProxy + Keepalived 的 RKE2 高可用集群在环境下已搭建完成。
9. 卸载rke2(卸载整个k8s集群和rke2集群,慎用)
# 若为 Server 节点
sudo systemctl stop rke2-server
sudo systemctl disable rke2-server
sudo rke2-uninstall.sh
# 若为 Agent 节点
sudo systemctl stop rke2-agent
sudo systemctl disable rke2-agent
sudo rke2-agent-uninstall.sh # 部分版本存在此脚本
10、安装rancher UI
1、安装cert-manager
helm install \
cert-manager oci://quay.io/jetstack/charts/cert-manager \
--version v1.20.2 \
--namespace cert-manager \
--create-namespace \
--set crds.enabled=true
2、安装rancher UI
| 类别 | 添加仓库命令 | 仓库描述 |
|---|---|---|
| rancher-latest | helm repo add rancher-latest https://releases.rancher.com/server-charts/latest | 添加最新版本的 Rancher 的 Helm Chart 仓库。我们建议使用此仓库来测试新版本的 Rancher。 |
| rancher-stable | helm repo add rancher-stable https://releases.rancher.com/server-charts/stable | 添加较旧的,稳定的版本的 Rancher 的 Helm Chart 仓库。我们建议将此仓库用于生产环境。 |
| rancher-alpha | helm repo add rancher-alpha https://releases.rancher.com/server-charts/alpha | 添加 alpha 版本的 Rancher 的 Helm Chart 仓库,以预览即将发布的版本.不建议在生产环境中使用这些版本。我们不支持从 rancher alpha 仓库中的 chart 升级到任何其他版本 chart。 |
# 安装 rancher-latest
helm install rancher rancher-latest/rancher \
--namespace cattle-system \
--create-namespace \
--set hostname=rancher.local \
--set systemDefaultRegistry=registry.cn-hangzhou.aliyuncs.com
11、其他配置
11.1 基础命令配置
环境变量最后面添加下面内容
# rke2
export KUBECONFIG=/etc/rancher/rke2/rke2.yaml
export CRI_CONFIG_FILE=/var/lib/rancher/rke2/agent/etc/crictl.yaml
export PATH=/var/lib/rancher/rke2/bin:$PATH
# kubectl
if [ -f /etc/profile.d/bash_completion.sh ]; then
. /etc/profile.d/bash_completion.sh
fi
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k

56

被折叠的 条评论
为什么被折叠?



