下面给你一个从 0 到 1 的 Debian + Kubernetes 自动化部署思路,覆盖工具选型、步骤示例、可落地方案,适合生产或测试环境。
| 方案 | 适用场景 | 特点 |
|---|---|---|
| kubeadm + Ansible | 最常用、可控 | 官方推荐,灵活 |
| Kubernetes the Hard Way | 学习/定制 | 手动多,不推荐生产 |
| Rancher / K3s | 轻量/边缘 | 一键部署 |
| Terraform + Cloud | 云环境 | 基础设施即代码 |
| Kubespray | 大规模集群 | 基于 Ansible,偏重 |
✅ 推荐:Debian + kubeadm + Ansible(最稳)
# 关闭 swap
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
# 内核参数
cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward=1
EOF
sysctl --system
# 加载模块
modprobe br_netfilter
apt update
apt install -y containerd
containerd config default > /etc/containerd/config.toml
systemctl restart containerd
apt install -y apt-transport-https ca-certificates curl
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/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.29/deb/ /" > /etc/apt/sources.list.d/kubernetes.list
apt update
apt install -y kubelet kubeadm kubectl
kubeadm init --pod-network-cidr=10.244.0.0/16
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubeadm join <master-ip>:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx
k8s-deploy/
├── inventory.ini
├── playbook.yml
└── roles/
├── common/
├── containerd/
└── k8s/
[masters]
master ansible_host=192.168.1.10
[workers]
node1 ansible_host=192.168.1.11
node2 ansible_host=192.168.1.12
- hosts: all
roles:
- common
- containerd
- hosts: masters
roles:
- k8s-master
- hosts: workers
roles:
- k8s-worker
- name: Disable swap
command: swapoff -a
- name: Remove swap from fstab
lineinfile:
path: /etc/fstab
regexp: 'swap'
state: absent
#!/bin/bash
ansible-playbook -i inventory.ini playbook.yml
✅ 生产级增强
Terraform 管理 Debian 虚拟机MetalLB 做 LoadBalancerCluster AutoscalerGitOps(ArgoCD / Flux)✅ 轻量方案
curl -sfL https://get.k3s.io | sh -
❌ swap 没关
❌ 时间不同步(务必 NTP)
❌ 防火墙未放行 6443/10250
❌ containerd 配置错误
我可以直接给你一套完整可跑的 Ansible 仓库。