fix: correct node list, add HAProxy & Tailscale key sections

This commit is contained in:
luochen570
2026-05-24 09:48:16 +08:00
parent e2ab520a89
commit 0c7c204edf

View File

@@ -1,11 +1,12 @@
---
date: '2026-05-24T09:30:00+08:00'
date: '2026-05-24T10:00:00+08:00'
draft: false
title: 'k3s 高可用集群安装与配置(国内镜像加速版)'
tags:
- k3s
- kubernetes
- tailscale
- haproxy
- 运维
categories:
- 技术
@@ -13,27 +14,108 @@ categories:
## 前言
本文记录在多台服务器上部署 k3s 高可用集群的完整过程包括镜像加速配置、Tailscale VPN 组网、嵌入式镜像仓库等
本文记录在多台服务器上部署 k3s 高可用集群的完整过程。集群架构为 **3 Server + 1 Agent**,通过 Tailscale 组建 overlay 网络,只有一台节点具有公网 IP其余节点通过 HAProxy 代理访问
<!--more-->
## 环境准备
## 网络拓扑
### 节点规划
```
公网
8.152.2.199
┌──────┴──────┐
│ agent-ali │ ← 唯一暴露公网的节点
│ (HAProxy) │ 运行 HAProxy 代理 k3s API
└──────┬──────┘
│ Tailscale (100.100.1.x)
┌──────────────┼──────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ server-jz│ │server-jz2│ │server-zen│
│ 德国 │ │ 德国 │ │ 德国 │
│ CP+etcd │ │ CP+etcd │ │ CP+etcd │
└──────────┘ └──────────┘ └──────────┘
```
| 节点角色 | 节点名 | IP | 系统 |
|---------|--------|----|------|
| Server (etcd + control-plane) | server-jz2 | 100.100.1.13 | Debian 13 |
| Server (etcd + control-plane) | server-jz | 100.100.1.12 | Debian 13 |
| Server (etcd + control-plane) | server-zen | 100.100.1.11 | Debian 13 |
| Agent | agent-ali | 100.100.1.10 | Debian 12 |
| Agent | agent-uc | 100.100.1.101 | Debian 12 |
- **只有 `agent-ali` 有公网 IP**,其余节点均通过 Tailscale 内网互通
- **HAProxy** 运行在 agent-ali 上,将外部的 k3s API 请求负载均衡到 3 个 Server 节点
- 所有节点通过 **Tailscale VPN** 组网(`100.100.x.x` 网段)
所有节点通过 Tailscale VPN 组网100.100.x.x 网段)。
## 节点规划
### 安装 Server 节点
| 节点角色 | 节点名 | Tailscale IP | 公网 | 位置 |
|---------|--------|-------------|:----:|:----:|
| Server (etcd + control-plane) | server-jz | 100.100.1.12 | ❌ | 德国 |
| Server (etcd + control-plane) | server-jz2 | 100.100.1.13 | ❌ | 德国 |
| Server (etcd + control-plane) | server-zen | 100.100.1.11 | ❌ | 德国 |
| Agent | agent-ali | 100.100.1.10 | ✅ 阿里云 | 中国 |
#### 第一个 Server 节点(初始化集群)
## 前置准备
### 获取 Tailscale 密钥
在 [Tailscale 管理后台](https://login.tailscale.com/admin/settings/keys) 生成一个 **Auth Key**
1. 进入 **Settings → Keys**
2. 点击 **Generate auth key**
3. 勾选 **Reusable**(可重用)和 **Ephemeral**(可选)
4. 复制生成的密钥,格式为 `tskey-auth-xxxxx`
该密钥用于节点自动加入 Tailscale 网络,无需手动登录。
### Server 节点:依赖安装
所有 Server 节点需要安装 Tailscale
```bash
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up --auth-key=tskey-auth-xxxxx
```
确认节点上线:
```bash
tailscale status
```
## 第一步:在 agent-ali 上配置 HAProxy
在 agent-ali 上安装 HAProxy将外部流量转发到 3 个 Server 节点的 k3s API
```bash
# 安装 HAProxy
sudo apt update && sudo apt install -y haproxy
```
配置 `/etc/haproxy/haproxy.cfg`
```haproxy
frontend k3s-frontend
bind *:6443
mode tcp
option tcplog
default_backend k3s-backend
backend k3s-backend
mode tcp
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s
server server-zen 100.100.1.11:6443 check
server server-jz 100.100.1.12:6443 check
server server-jz2 100.100.1.13:6443 check
```
启动 HAProxy
```bash
sudo systemctl enable --now haproxy
```
这样外部的 `kubectl` 只需要连接 `agent-ali:6443`,由 HAProxy 转发到健康的 Server 节点。
## 第二步:初始化第一个 Server 节点
```bash
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
@@ -47,26 +129,30 @@ sh -s server \
--advertise-address=100.100.1.13 \
--tls-san=100.100.1.13 \
--tls-san=8.152.2.199 \
--vpn-auth="name=tailscale,joinKey=tskey-xxx"
--vpn-auth="name=tailscale,joinKey=tskey-auth-xxxxx"
```
关键参数说明:
- `--embedded-registry`启用内置镜像仓库zot本地缓存镜像
- `--cluster-init`:初始化集群(仅第一个节点需要)
- `--data-dir /www/k3s`:指定数据目录
- `--tls-san`:添加 TLS 证书的 SAN包括内网 IP 和公网 IP
- `--vpn-auth`:集成 Tailscale VPN
| 参数 | 说明 |
|------|------|
| `INSTALL_K3S_MIRROR=cn` | 使用国内镜像源 |
| `--embedded-registry` | 启用内置镜像仓库zot本地缓存镜像 |
| `--cluster-init` | 初始化集群(仅第一个节点需要) |
| `--data-dir /www/k3s` | 指定数据目录 |
| `--tls-san` | 添加证书 SAN包含公网 IP8.152.2.199 |
| `--vpn-auth` | 集成 Tailscale 自动加入网络 |
安装完成后获取节点 Token
安装完成后获取节点 Token
```bash
cat /www/k3s/server/node-token
sudo cat /www/k3s/server/node-token
```
#### 其余 Server 节点加入
## 第三步:加入其余 Server 节点
```bash
# server-jz
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
INSTALL_K3S_MIRROR=cn \
K3S_NODE_NAME=server-jz \
@@ -77,12 +163,9 @@ sh -s server \
--server https://100.100.1.13:6443 \
--node-ip=100.100.1.12 \
--advertise-address=100.100.1.12 \
--vpn-auth="name=tailscale,joinKey=tskey-xxx"
```
--vpn-auth="name=tailscale,joinKey=tskey-auth-xxxxx"
同样的方式加入第三个 Server 节点:
```bash
# server-zen
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
INSTALL_K3S_MIRROR=cn \
K3S_NODE_NAME=server-zen \
@@ -93,25 +176,10 @@ sh -s server \
--server https://100.100.1.13:6443 \
--node-ip=100.100.1.11 \
--advertise-address=100.100.1.11 \
--vpn-auth="name=tailscale,joinKey=tskey-xxx"
--vpn-auth="name=tailscale,joinKey=tskey-auth-xxxxx"
```
### 安装 Agent 节点
#### 海外 Agent
```bash
curl -sfL https://get.k3s.io | \
K3S_NODE_NAME=agent-uc \
K3S_URL=https://100.100.1.13:6443 \
K3S_TOKEN=K101fe... \
sh -s - agent \
--node-ip=100.100.1.101 \
--node-external-ip=100.100.1.101 \
--vpn-auth="name=tailscale,joinKey=tskey-xxx"
```
#### 国内 Agent
## 第四步:加入 Agent 节点
```bash
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
@@ -122,28 +190,31 @@ K3S_TOKEN=K101fe... \
sh -s - agent \
--node-ip=100.100.1.10 \
--node-external-ip=100.100.1.10 \
--vpn-auth="name=tailscale,joinKey=tskey-xxx"
--vpn-auth="name=tailscale,joinKey=tskey-auth-xxxxx"
```
## 配置 Kubernetes 访问
## 配置 kubeconfig
Server 节点的配置复制到本地:
从任意 Server 节点复制 kubeconfig 到本地:
```bash
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
```
# 复制配置文件
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
通过环境变量指定配置:
# 将 server 地址改为 HAProxy 的公网地址
kubectl config set-cluster default --server=https://8.152.2.199:6443
```bash
# 验证
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl get pods --all-namespaces
helm ls --all-namespaces
```
## 配置镜像加速(国内源)
> HAProxy 会将请求负载均衡到 3 个 Server 节点,即使单个 Server 宕机也不影响 API 访问。
在中国大陆部署时,镜像拉取是最大的痛点。通过配置 `registries.yaml` 可以解决:
## 配置镜像加速
在中国大陆部署时,镜像拉取是最大的痛点。通过配置 `registries.yaml` 解决:
```bash
sudo mkdir -p /etc/rancher/k3s
@@ -184,11 +255,14 @@ mirrors:
- "https://ghcr.chenby.cn"
- "https://ghcr.luochen570.top"
EOF
sudo systemctl restart k3s # 重启 Server
sudo systemctl restart k3s-agent # 重启 Agent
```
每个镜像源都配置了多个备用 endpoint按优先级依次尝试。
每个节点都需要配置,然后重启 k3s 服务:
```bash
sudo systemctl restart k3s # Server 节点
sudo systemctl restart k3s-agent # Agent 节点
```
## 卸载节点
@@ -202,8 +276,6 @@ sudo systemctl restart k3s-agent # 重启 Agent
## 验证集群
集群安装完成后,可以运行以下命令验证:
```bash
kubectl get nodes
kubectl get pods --all-namespaces
@@ -211,10 +283,10 @@ kubectl get pods --all-namespaces
## 总结
通过以上步骤,我们成功搭建了一个 3 Server + 2 Agent 的 k3s 高可用集群,特点包括
最终集群架构
1. **高可用**3 个控制平面节点,任单节点故障不影响集群
2. **国内加速**配置了多个国内镜像源,解决拉取超时问题
3. **嵌入式镜像仓库**:启用 `--embedded-registry`,提升镜像分发效率
4. **Tailscale 组网**:通过 VPN 实现跨地域节点互联
5. **灵活的 Agent 部署**:国内和海外节点使用不同的安装源
1. **高可用**3 个控制平面节点 + HAProxy 负载均衡,任单节点故障不影响集群
2. **安全隐藏**只有 agent-ali 暴露公网 IPServer 节点通过 Tailscale 内网访问
3. **国内加速**:内置镜像源列表,解决拉取超时
4. **嵌入式镜像仓库**:启用 `--embedded-registry`,跨节点分发更快
5. **Tailscale 组网**:跨地域节点自动互联,无需手动配置 WireGuard