温馨提示×

Debian K8s网络怎么配

小樊
35
2026-09-05 09:22:02
栏目: 智能运维

在 Debian 上配置 Kubernetes(K8s)网络,通常包含主机网络准备、容器网络插件(CNI)选择、Service/网络策略配置几个层面。下面按“实战流程”给你讲清楚。


一、Debian 主机网络基础准备

1. 系统要求

  • Debian 11/12(推荐)
  • 内核 ≥ 4.19
  • 关闭 swap
sudo swapoff -a
sudo sed -i '/swap/s/^/#/' /etc/fstab

2. 内核参数(必须)

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

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

3. 安装容器运行时(以 containerd 为例)

sudo apt install -y containerd
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd

二、Kubernetes 网络模型概述

K8s 网络要求:

  • 每个 Pod 有独立 IP
  • Node 之间 Pod 可直接通信
  • Service 是虚拟 IP(ClusterIP)
  • CNI 插件负责 Pod 网络

三、选择并安装 CNI 网络插件(重点)

方案 1:Calico(最常用,推荐)

适合生产、支持 NetworkPolicy

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

检查:

kubectl get pods -n kube-system

方案 2:Flannel(简单、轻量)

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

⚠ 不支持 NetworkPolicy


方案 3:Cilium(eBPF,高级)

helm repo add cilium https://helm.cilium.io/
helm install cilium cilium/cilium --namespace kube-system

四、Debian 节点网络注意事项

1. 网卡与 IP

  • 确保每个节点有稳定 IP
  • 多网卡时指定 --node-ip
# kubelet 配置
KUBELET_EXTRA_ARGS="--node-ip=192.168.1.10"

2. 防火墙(如启用)

放行:

  • 6443(API)
  • 10250(kubelet)
  • CNI 端口(如 Calico 179/BGP)

五、Service 与 DNS

CoreDNS 自动部署:

kubectl get pods -n kube-system | grep coredns

测试 DNS:

kubectl run test --image=busybox --rm -it -- sh
nslookup kubernetes.default

六、常见问题排查

kubectl get nodes
kubectl describe pod <pod>
ip route
iptables -L -n

七、推荐组合(Debian 生产)

✅ Debian 12
✅ containerd
✅ Calico
✅ Kubernetes 1.28+


如果你愿意,我可以:

  • 给你一步步从零部署 Debian K8s 集群
  • 或针对云 / 裸机 / 内网场景细化网络方案

0