Linux 上配置 Kubernetes 网络的实操指南
一 前置条件与网络模型
swapoff -a 并注释 /etc/fstab 中的 swap 行),否则 kubelet 可能无法正常调度。/etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_forward = 1sysctl --system二 使用 kubeadm 初始化并指定 Pod 网段
kubeadm init --pod-network-cidr=10.244.0.0/16mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configkubeadm init 输出中的 token 与 hash):
kubeadm join <APISERVER_IP>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>三 部署主流 CNI 网络插件
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/v0.22.0/Documentation/kube-flannel.ymlnet-conf.json 的 "Backend":{"Type":"vxlan"} 改为 "host-gw" 可提升性能(要求二层直连)。curl -O https://docs.projectcalico.org/v3.26/manifests/calico.yamlCALICO_IPV4POOL_CIDR: "10.244.0.0/16"(与 kubeadm init --pod-network-cidr 保持一致)。IP_AUTODETECTION_METHOD: "interface=eth0"(将 eth0 替换为实际主网卡名)。helm repo add cilium https://helm.cilium.io/helm install cilium cilium/cilium --version 1.14.0 --namespace kube-system --set k8sServiceHost=<APISERVER_IP> --set k8sServicePort=6443--set kubeProxyReplacement=true。kubectl get pods -n kube-flannel、kubectl get pods -n calico-system、kubectl get pods -n kube-system -l k8s-app=cilium)。四 验证与常见问题排查
kubectl get nodes、kubectl get pods -Akubectl run nginx --image=nginx --port=80kubectl get pod nginx -o jsonpath='{.status.podIP}'kubectl run test-pod --image=busybox --rm -it -- wget -qO- http://<NGINX_POD_IP>