方案概述
K8S部署方案对比:
| 维度 | RKE1 (Legacy) | RKE2 (Next Gen) | K3s (Lightweight) | KubeKey (kk) |
|---|---|---|---|---|
| 核心定位 | 基于 Docker 的传统发行版 | 专注于安全与合规的政府级发行版 | 边缘计算/开发环境首选 | KubeSphere 生态专用安装器 |
| 容器运行时 | 强依赖 Docker | 原生 Containerd (符合 K8s 趋势) | 原生 Containerd | 灵活 (Docker/Containerd) |
| 部署形态 | K8s 组件运行在 Docker 容器内 | 静态二进制文件 + Static Pod | 单一二进制文件 (All-in-one) | Go 编写的自动化部署工具 |
| 证书管理 | 依赖 Rancher 维护,逻辑较重 | 自动轮转,且支持自定义证书管理 | 自动管理,最轻量 | 依赖 Ansible 逻辑,流程较长 |
| 主要优势 | 社区方案成熟,老用户多 | 默认 CIS 加固、FIPS 合规、无 Docker 损耗 | 资源开销极低,秒级启动 | 一键集成 KubeSphere 全家桶 |
| 局限性 | 架构臃肿,受 Docker 生命周期影响 | 相比 K3s 略重,更侧重服务器场景 | 裁剪了部分 API,不适合特大规模 | 逻辑与 KubeSphere 深度绑定 |
K8S管理方案选型:
| 维度 | Rancher (基础设施中心) | KubeSphere (业务赋能中心) |
|---|---|---|
| 技术侧重 | 侧重"基座"与"连接" | 侧重"业务"与"交付" |
| 核心能力 | 解决集群怎么装、怎么扩、多云怎么接。 | 解决业务怎么发、怎么看、怎么管。 |
| 监控深度 | 提供基础的集群资源监控(Node/Pod)。 | 多维度监控(应用级指标、自定义 Dashboard、多租户配额)。 |
| 日志方案 | 支持采集发送至第三方(ES/Kafka)。 | 内置日志系统(实时日志检索、落盘日志审计、多租户隔离查询)。 |
| DevOps | 简单的流水线支持。 | 全栈 DevOps(Jenkins 引擎、S2I 源码到镜像、图形化流水线)。 |
| 微服务治理 | 相对薄弱,需手动配置。 | 原生集成 Istio(灰度发布、流量治理、可视化拓扑图)。 |
| 典型角色 | 运维工程师 (SRE):关注底座稳不稳。 | 研发/测试/产品:关注代码发布快不快,链路清不清晰。 |
方案组合: RKE2 (集群引擎) + KubeSphere (管理平台)
技术总结:
- 底层选 RKE2: 拿到了一个去 Docker 化 、极致安全 、维护简单的 K8s 标准集群。它比 RKE1 更先进,比 K3s 更适合作为私有云的核心生产环境。
- 上层选 KubeSphere: 在 RKE2 提供的稳定底座上,套了一层开发者友好的"壳"。通过 KubeSphere 强大的 UI 和功能插件(监控、日志、DevOps),弥补了 Rancher 原生在业务治理层面的短板。
在线部署
- 必须科学环境, 否则玩不转
环境配置
配置系统代理
bash
export proxy_url=http://172.16.0.2:7893
echo "export https_proxy=$proxy_url" >> /etc/profile
source /etc/profile
配置yum源
bash
rm -f /etc/yum.repos.d/CentOS-Linux-*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
rm -rf /var/cache/dnf
dnf clean all && dnf makecache
dnf install wget -y
安装docker-ce
bash
# 查看docker版本
dnf list docker-ce --showduplicates | sort -r
# 安装docker
dnf install docker-ce-26.1.3-1.el8 -y
# 安装docker-compose
wget "https://wget.la/https://github.com/docker/compose/releases/download/v2.40.3/docker-compose-$(uname -s)-$(uname -m)"
chmod +x docker-compose-Linux-x86_64
mv docker-compose-Linux-x86_64 /usr/bin/docker-compose
配置docker
yaml
# 创建docker配置目录和数据目录
mkdir -p /etc/docker/
mkdir -p /data/docker/
# 配置docker服务
export harbor_url=172.16.0.101:8005
cat > /etc/docker/daemon.json <<EOF
{
"max-concurrent-downloads": 30,
"data-root": "/data/docker",
"log-opts": {"max-size":"10m", "max-file":"3"},
"insecure-registries":["${harbor_url}"]
}
EOF
# 配置docker代理
export proxy_url=http://172.16.0.2:7893
mkdir -p /etc/systemd/system/docker.service.d
cat <<EOF > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=${proxy_url}"
Environment="HTTPS_PROXY=${proxy_url}"
Environment="NO_PROXY=localhost,127.0.0.1,172.16.0.0/16,10.42.0.0/16,10.43.0.0/16"
EOF
启动验证
bash
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
docker info
- 配置Harbor仓库是为了方便后面上传镜像
- 也可以配置docker镜像加速器, 但不推荐, k3s拉镜像容易出问题
yaml{ "registry-mirrors": [ "https://docker.xuanyuan.me", "https://docker.1ms.run", "https://docker.1panel.live", "https://docker.m.daocloud.io", "https://wget.la" ],
节点初始化
bash
yum install -y device-mapper-persistent-data lvm2
yum install -y iptables iptables-services ipvsadm container-selinux
# 关闭swap分区
swapoff -a
# 编辑/etc/firewalld/firewalld.conf配置, 注释掉 FirewallBackend=nftables和FirewallBackend=iptables
sed -i 's/^FirewallBackend=nftables/#&/' /etc/firewalld/firewalld.conf
sed -i 's/^FirewallBackend=iptables/#&/' /etc/firewalld/firewalld.conf
# 加载内核模块
(
cat << EOF
systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables
systemctl stop ip6tables
systemctl disable ip6tables
systemctl stop nftables
systemctl disable nftables
modprobe br_netfilter
modprobe ip_tables
modprobe iptable_nat
modprobe iptable_filter
modprobe iptable_mangle
modprobe iptable_mangle
modprobe ip6_tables
modprobe ip6table_nat
modprobe ip6table_filter
modprobe ip6table_mangle
modprobe ip6table_mangle
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack
EOF
)>> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
sh /etc/rc.d/rc.local
# 查看加载的内核模块
lsmod
echo 'ip_tables' >> /etc/modules
# 查看防火墙状态, 验证是否关闭
systemctl status iptables
systemctl status ip6tables
systemctl status nftables
systemctl status firewalld
# 查看模块信息
modinfo iptable_nat
modinfo ip6table_nat
# 配置内核参数
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables=1" >> /etc/sysctl.conf
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
sysctl -p
# ipv6相关错误可以忽略
# 查看模块
ls /lib/modules/`uname -r`/kernel/net/ipv6/netfilter/
ls /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/
部署Rancher
docker-compose方式部署
yaml
mkdir /data/rancher
cd /data/rancher
export proxy_url=http://172.16.0.2:7893
cat > docker-compose.yml << EOF
services:
rancher:
image: rancher/rancher:v2.13.3-amd64
container_name: rancher
privileged: true
restart: unless-stopped
ports:
- "443:443"
environment:
AUDIT_LEVEL: "3"
CATTLE_BOOTSTRAP_PASSWORD: "Rancher123456"
HTTP_PROXY: ${proxy_url}
HTTPS_PROXY: ${proxy_url}
NO_PROXY: localhost,127.0.0.1,172.16.0.0/16,10.42.0.0/16,10.43.0.0/16,cattle-system.svc,.cluster.local
volumes:
- ./rancher-data:/var/lib/rancher
- ./images:/images
EOF
启动验证
bash
docker-compose up -d
# 查看pod
docker-compose exec rancher kubectl get pods -A
# 查看镜像
docker-compose exec rancher crictl images
第一次部署
AUDIT_LEVEL先配置3, 后面部署可以改为1或者0
0 (默认):禁用。不记录任何审计日志。 1 (Metadata):只记录元数据。记录请求的 user、timestamp、resource、verb(操作类型,如 GET/POST),但不记录具体发送了什么数据。 2 (Request):记录请求体。在级别 1 的基础上,增加记录用户发送的 Request Body。 3 (Request & Response):全记录。这是最高级别,不仅记录用户发了什么,还记录服务器返回了什么 (Response Body)。
Rancher部署K8S
Rancher-server创建集群
1. 取消metrics
2. 按规划配置好各节点角色
节点加入集群
bash
# 执行加入命令, 主机后面加入节点IP: --node-name 172.16.0.101
curl --insecure -fL https://172.16.0.101/system-agent-install.sh | sudo sh -s - --server https://172.16.0.101 --label 'cattle.io/os=linux' --token xxxx --ca-checksum xxxxc --etcd --controlplane --node-name 172.16.0.101
# 如果卡在拉取镜像, 需要配置环境变量, 加入代理
mkdir -p /etc/systemd/system/rancher-system-agent.service.d
export proxy_url=http://172.16.0.2:7893
cat <<EOF > /etc/systemd/system/rancher-system-agent.service.d/proxy.conf
[Service]
Environment="HTTP_PROXY=${proxy_url}"
Environment="HTTPS_PROXY=${proxy_url}"
Environment="NO_PROXY=localhost,127.0.0.1,172.16.0.0/16,10.42.0.0/16,10.43.0.0/16"
EOF
systemctl daemon-reload
systemctl restart rancher-system-agent
验证节点状态
bash
# 查看服务
systemctl list-units --type=service | grep -E "rancher|rke2"
# 查看日志
journalctl -u rancher-system-agent.service -f
journalctl -u rke2-server -f
journalctl -u rke2-agent -f
# 查看证书
ls -l /var/lib/rancher/rke2/server/tls/
小技巧
bash
# 查看镜像原始命令非常臃肿
/var/lib/rancher/rke2/bin/crictl \
--runtime-endpoint unix:///run/k3s/containerd/containerd.sock \
images
/var/lib/rancher/rke2/bin/ctr \
--address /run/k3s/containerd/containerd.sock \
--namespace k8s.io \
image ls
# 配置crictl命令
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/k3s/containerd/containerd.sock
image-endpoint: unix:///run/k3s/containerd/containerd.sock
timeout: 10
debug: false
EOF
echo "export PATH=$PATH:/var/lib/rancher/rke2/bin" >> /etc/profile
source /etc/profile
crictl images
# 或者使用别名方式
cat > ~/.bashrc << EOF
# 关于crictl和ctr
alias kimg='/var/lib/rancher/rke2/bin/crictl --runtime-endpoint unix:///run/k3s/containerd/containerd.sock images'
alias kctr='/var/lib/rancher/rke2/bin/ctr --address /run/k3s/containerd/containerd.sock --namespace k8s.io'
EOF
# 生效
source ~/.bashrc
部署kubesphere
4.x版本
bash
# 配置kubectl
cp /var/lib/rancher/rke2/bin/kubectl /usr/bin/kubectl
mkdir -p ~/.kube
vim ~/.kube/config
# 粘贴config内容
# 安装helm
wget https://get.helm.sh/helm-v3.19.0-linux-amd64.tar.gz
tar -zxf helm-v3.19.0-linux-amd64.tar.gz
mv linux-amd64/helm /usr/bin/helm
# 部署kubesphere核心组件
chart=oci://hub.kubesphere.com.cn/kse/ks-core
version=1.2.4
helm upgrade --install -n kubesphere-system --create-namespace ks-core $chart --debug --wait --version $version --reset-values --take-ownership
# 查看部署进度
kubectl get pods -n kubesphere-system
# 部署成功后, 会显示登录IP, 账号口令
# 申请license
https://kubesphere.com.cn/apply-license/
版本验证
bash# 下载版本测试 helm pull oci://hub.kubesphere.com.cn/kse/ks-core --version 1.2.4 helm show chart ks-core-1.2.4.tgz