在 CentOS 上部署 Kubernetes(k8s) 时,网络配置 是核心环节之一,主要包括 主机网络、内核参数、容器网络(CNI)、Service 网络、Pod 网络。下面按 生产可用的标准流程 给你讲清楚。
cat /etc/centos-release
✅ 推荐:
| 类型 | 网段 |
|---|---|
| Node 节点 IP | 192.168.10.0/24 |
| Pod 网段 | 10.244.0.0/16 |
| Service 网段 | 10.96.0.0/16 |
⚠️ Pod 和 Service 网段不能和宿主机网络冲突
systemctl disable firewalld --now
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
swapoff -a
sed -i '/swap/d' /etc/fstab
cat <<EOF > /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
✅ 必须加载 br_netfilter:
modprobe br_netfilter
yum install -y containerd
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
修改:
[plugins."io.containerd.grpc.v1.cri"]
SystemdCgroup = true
启动:
systemctl enable containerd --now
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/repodata/repomd.xml.key
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
K8s 本身不实现网络,必须安装 CNI 插件
| CNI | 特点 |
|---|---|
| Calico | 生产首选,支持网络策略 |
| Flannel | 简单,适合入门 |
| Cilium | 高性能,eBPF |
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--service-cidr=10.96.0.0/12
--pod-network-cidr必须和你选的 CNI 一致
mkdir -p $HOME/.kube
cp /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml
Calico 默认使用:
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
⚠️ Flannel 默认也是 10.244.0.0/16
在 Master 上获取 token:
kubeadm token create --print-join-command
在 Node 上执行:
kubeadm join 192.168.10.xx:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx
kubectl get nodes
kubectl get pods -A
测试 Pod 网络:
kubectl run test --image=busybox -it --rm -- sh
ping 10.244.x.x
kubectl get pods -n kube-system
journalctl -u kubelet
kubectl get svc -n kube-system
kubectl get pods -A | grep cni
✅ 使用 Calico + BGP
✅ 固定 Node IP
✅ 不使用 swap
✅ 内核参数必须开
✅ 避免 Pod 网段与内网冲突
如果你愿意,我可以:
你现在用的是 CentOS 7 还是 Stream?单节点还是多节点?