在 Debian 上配置 Kubernetes(K8s)集群,常见且稳定的方式是使用 kubeadm。下面给你一套从零到可用集群的完整流程(适用于 Debian 10/11/12,也适用于大多数基于 Debian 的系统)。
| 节点 | 主机名 | 角色 |
|---|---|---|
| 192.168.1.10 | master | control-plane |
| 192.168.1.11 | node1 | worker |
| 192.168.1.12 | node2 | worker |
✅ 建议:
- Master ≥ 2 CPU,≥ 2G 内存
- Worker ≥ 1 CPU,≥ 1G 内存
- 所有节点 root 或 sudo 权限
# 1. 修改主机名
hostnamectl set-hostname master
# node 上改为 node1、node2
# 2. 关闭 swap(K8s 必须)
swapoff -a
sed -i '/swap/d' /etc/fstab
# 3. 关闭防火墙(或放行端口)
systemctl disable --now ufw
# 4. 设置内核参数
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | 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
sysctl --system
apt update
apt install -y containerd
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
修改:
SystemdCgroup = true
重启:
systemctl restart containerd
systemctl enable containerd
apt-get install -y apt-transport-https ca-certificates curl gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/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.28/deb/ /" |
tee /etc/apt/sources.list.d/kubernetes.list
apt update
apt install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--apiserver-advertise-address=192.168.1.10
✅ 成功后你会看到类似输出:
kubeadm join 192.168.1.10:6443 --token xxxx \
--discovery-token-ca-cert-hash sha256:xxxx
⚠️ 保存该命令!
mkdir -p $HOME/.kube
cp /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
验证:
kubectl get nodes
推荐使用 Flannel 或 Calico
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
等待:
kubectl get pods -n kube-system
在所有 worker 节点执行 master 输出的 join 命令:
kubeadm join 192.168.1.10:6443 \
--token xxxx \
--discovery-token-ca-cert-hash sha256:xxxx
kubectl get nodes
示例输出:
NAME STATUS ROLES AGE VERSION
master Ready control-plane 10m v1.28.x
node1 Ready <none> 5m v1.28.x
node2 Ready <none> 5m v1.28.x
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
kubelet: failed to run Kubelet: running with swap on is not supported
✅ 解决:
swapoff -a
containerd 必须是:
SystemdCgroup = true
✅ Debian 上配置 K8s 核心步骤:
kubeadm initkubeadm join如果你需要:
可以继续问我,我可以直接给你定制方案 ✅