K8s 网络架构总览
K8s 网络架构总览
K8s 网络模型(四个基本保证)
Kubernetes 本身不实现网络,只规定了一个”大家都得遵守”的模型(由 CNI 插件落地,如 Cilium/Calico/Flannel):
1. 每个 Pod 有自己独立的 IP(Pod 内所有容器共享这个 IP 和网络命名空间)
2. 任意两个 Pod 可以直接用对方 IP 通信,无需 NAT(不管同节点还是跨节点)
3. Node 可以用 Pod IP 直接访问 Pod(无需经过 NAT)
4. Pod 看到的自己的 IP,跟别的 Pod/Node 看到它的 IP 一致(无 IP 伪装)
一句话:Pod 是”一等网络公民”,有真实 IP、能被直接路由。这就把 K8s 网络和”传统容器端口映射”彻底区分开了。
整体分层
┌──────────────────────── 应用层 ────────────────────────┐
│ Ingress / Gateway API(南北向入口,L7 路由) │
│ Service Mesh(Istio/Cilium mesh,东西向 L7) │
└────────────────────────────┬───────────────────────────┘
┌──────────▼──────────┐
│ Service(虚拟 VIP) │ ← kube-proxy / KPR 实现
└──────────┬──────────┘
┌──────────────────────── 网络层 ────────────────────────┐
│ NetworkPolicy(L3/L4 防火墙,由 CNI 落地) │
│ Pod 网络(CNI 给 IP、建 veth、配路由) │
│ DNS(CoreDNS,Service 名→ClusterIP) │
└────────────────────────────┬───────────────────────────┘
┌──────────▼──────────┐
│ 底层网络(节点 L2/L3)│ ← 物理/云网络、BGP、隧道
└─────────────────────┘
数据包路径:Pod → Pod
这是”底层网络架构”的核心,必须清楚同节点和跨节点的差异。
同节点(Pod-A → Pod-B,都在 Node1)
Pod-A netns host netns Pod-B netns
┌──────────┐ veth-A ┌────────────────┐ veth-B ┌──────────┐
│ 10.1.1.5 │────────▶│ bridge/cilium_ │────────▶│ 10.1.1.6 │
│ │ │ host (路由转发) │ │ │
└──────────┘ └────────────────┘ └──────────┘
路由:10.1.1.6/32 dev veth-B(本机直连)
- 包从 veth-A 出 Pod,进 host 的桥/路由
- host 查路由发现 10.1.1.6 是另一端的 veth-B,直接转发
- 不过节点物理网卡,无封装、无 NAT,延迟最低
跨节点(Pod-A@Node1 → Pod-B@Node2)
Node1 Node2
Pod-A(10.1.1.5) Pod-B(10.2.2.6)
│ veth │ veth
▼ ▲
host 路由: 10.2.2.0/24 via Node2 │
│ │
▼ │
[ Native Routing ] 直接路由到 Node2 eth0 ─┘
或
[Tunnel 模式 ] 包被 VXLAN/Geneve 封装 → 从 Node1 eth0 发往 Node2 eth0 → 解封
- Native Routing:底层网络(BGP/云路由表)知道 10.2.2.0/24 在 Node2,包直接路由过去
- Tunnel:Pod 包被封进节点 IP 的隧道里,底层只看到节点间通信,看不到 Pod IP
具体走哪条,由 CNI 决定。Cilium 见 ../../cilium/Cilium 网络与路由模式,Calico 见 ../特性详解/CNI 网络插件对比与排障。
Service:Pod 之上的稳定抽象
Pod IP 会变(扩缩容、重建),所以引入了 Service = 稳定 VIP + 负载均衡到一组 backend Pod:
Client Pod → Service VIP (10.96.x.x:80)
│ kube-proxy / KPR 做 DNAT/LB
▼
Endpoint (Pod IP:80)
- Service 由
kube-apiserver维护,Endpoints/EndpointSlices由 endpoints-controller 根据 Pod 就绪状态自动更新 - 转发实现有三种:iptables(旧)、IPVS(较新)、eBPF/KPR(Cilium)——详见 K8s Service 模型与转发实现
DNS:Service 名如何解析
Pod 里 `curl http://api.default.svc.cluster.local`
→ 查 CoreDNS(ClusterIP,通常在 kube-dns 服务后)
→ CoreDNS 按 K8s 服务记录返回 ClusterIP
→ 再走 Service 转发到 backend
- CoreDNS 监听 Service/Endpoint 变化,动态生成 DNS 记录
- 搜索域:
default.svc.cluster.local→svc.cluster.local→cluster.local(所以api也能解析成api.default.svc) - 详见 K8s Service 模型与转发实现
南北向入口:Ingress 与 Gateway API
外部流量
→ LoadBalancer / NodePort(四层进集群)
→ Ingress Controller / Gateway(七层路由到 Service)
→ Service → Pod
- Ingress:老标准,L7 路由(host/path → Service),实现有 nginx/Contour/Traefik
- Gateway API:新标准,角色分离(GatewayClass/GDP/Gateway/HTTPRoute),Cilium/Istio/Envoy 都实现——见 ../gateway-api/Gateway API 概述
东西向安全:NetworkPolicy
默认:Pod 间全通(无策略时)
NetworkPolicy:按标签限制谁能访问谁(L3/L4)
Cilium 扩展:L7 + FQDN + Host Firewall(零信任)——见 [[../../cilium/Cilium 网络策略与零信任]]
一张图串起全部
外部请求
│
▼
LoadBalancer / Ingress / Gateway API ──(L7)──┐
│ │
▼ ▼
Service (VIP) ──kube-proxy/KPR──▶ Endpoint(Pod IP)
│ │
│ NetworkPolicy 在此层裁决(CNI 落地) │
▼ ▼
CoreDNS(名字解析) Pod 网络(CNI:veth/路由/隧道)
│ │
└────────── 底层节点网络(L2/L3/BGP/隧道)──┘
关联知识
- K8s Service 模型与转发实现 — Service 四类型、iptables/IPVS/eBPF 对比、CoreDNS
- ../../cilium/Cilium 知识总览 — 用 eBPF 落地这套模型的现代实现
- ../特性详解/CNI 网络插件对比与排障 — Flannel/Calico/Cilium 怎么各自落地 Pod 网络
- ../特性详解/nftables kube-proxy 详解 — kube-proxy iptables/nftables 实现细节
- ../gateway-api/Gateway API 概述 — 南北向入口新标准
- ../特性详解/Istio 服务网格详解 — 东西向 L7 网格
学习时间
| 阶段 | 时间 | 备注 |
|---|---|---|
| 网络架构 | 2026-07-16 | 完成:四保证、分层、Pod-Pod 数据包路径、Service/DNS/Ingress 串联 |