02. 共同认知:云原生哲学与物流底座
02. Common Ground: Cloud Native
Philosophy & Foundations
云原生(Cloud
Native)本质上是一种利用云计算优势来设计、构建和运行应用程序的现代方法论。它不是简单地把传统应用搬到云上,而是从根本上重新思考软件如何在云环境中高效、可靠地工作。核心哲学可以总结为“弹性、自动化和可移植性”:应用程序应该像云一样,能自动扩展、快速恢复、自愈,并且不受底层硬件或环境的限制。
Cloud Native is a modern methodology designed to leverage cloud advantages. It's a fundamental
rethinking of software efficiency and reliability. The core philosophy: Elasticity,
Automation, and Portability—allowing apps to scale, recover, and self-heal independently
of underlying hardware.
弹性 (Elasticity)
Elasticity
随包裹量(负载)自动增减快递员(Pod)数量,确保资源利用率最优化。
Auto-scale courier (Pod) counts based on package volume (load) for resource
optimization.
自动化 (Automation)
Automation
全流程机器决策与生命周期管理,彻底消除人工误操作引入的系统性风险。
Machine-driven lifecycle management, eliminating systemic risks from manual
errors.
可移植性 (Portability)
Portability
解耦底层硬件,一套标准架构无论在私有云还是公有云,表现始终如一。
Decoupled from hardware; consistent performance across private and public
clouds.
标准化流水线
(The Pipeline)
Standardized Pipeline
云原生应用通过精确、结构化的流程构建,就像搭乐高积木:每个部分模块化,确保了可重复性与可审计性。实际中,借助 Helm 或 Operator,整个过程从代码提交到上线只需几分钟。
Cloud Native apps are built via structured processes, much like Lego: every part is modular,
ensuring repeatability and auditability. With Helm or Operators, the entire path from commit to
live takes only minutes.
🏗️
构建 (Build)
Build
用 Docker 编写 Dockerfile
构建镜像并推送到仓库
Write Dockerfile
Build & Push
Images
📜
配置 (Config)
Config
编写声明式 YAML 文件
定义网络与安全策略
Declarative YAML
Define Network &
Security
🚀
部署 (Deploy)
Deploy
CI/CD 自动化管道
测试并部署到环境
CI/CD Pipelines
Test & Deploy to
Runtime
⚙️
运行 (Run)
Run
监控系统状态
实现自动扩缩与自愈
Monitor Status
Auto-scale &
Self-heal
Kubernetes 与 Pod:分布式指挥系统
Kubernetes & Pods: The Distributed Orchestration System
作为现代容器化工作负载的标准管理平台,Kubernetes 负责维护系统的期望状态。它通过控制平面(Control Plane)进行全局决策,并将任务分发给工作节点(Worker Nodes)。
As the standard platform for containerized workloads, Kubernetes maintains the desired state of the system, making global decisions via the Control Plane and distributing tasks to Worker Nodes.
原子单元:Pod
The Atomic Unit: Pod
Pod 是 K8s 的最小调度单位,它封装了一个或多个容器并共享网络命名空间(NetNS)。这意味着 Pod 内的所有容器共用相同的 IP 和 MAC 地址,通过 localhost 即可高效通信。
A Pod is the smallest unit in K8s, encapsulating containers that share a Network Namespace (NetNS). All containers in a Pod share the same IP/MAC address and communicate via localhost.
易失性特征 (Ephemeral)
Ephemeral Nature
Pod 是临时的。它们随业务需求被频繁创建或销毁。由于每个 Pod 都会获得唯一的集群 IP,但重建后 IP 会发生变化,这种“易失性”对网络寻址提出了巨大挑战。
Pods are temporary and rescheduled based on demand. While each gets a unique IP, this IP changes upon recreation, posing a significant challenge for stable network addressing.
服务 (Service):稳定的访问入口
Service: The Stable Access Portal
为了应对 Pod IP 的不确定性,Kubernetes 引入了 Service 抽象层。它通过标签选择器(Label Selectors)动态关联一组后端 Pod,并对外暴露统一的入口。
To handle Pod IP instability, Kubernetes uses the Service abstraction. It leverages Label Selectors to associate with a set of back-end Pods, providing a unified access point.
负载均衡与虚拟 IP
Load Balancing & VIP
Service 提供长久稳定的虚拟 IP(ClusterIP)。无论后端 Pod 如何漂移,访问者的流量都会被均匀分发到健康的实例上,确保了服务发现的连续性。
Services provide stable Virtual IPs (VIPs). Regardless of Pod rescheduling, traffic is evenly distributed to healthy instances, ensuring continuous service discovery.
暴露类型
Exposure Types
除了内部访问的 ClusterIP,还支持通过 NodePort(节点端口)或 LoadBalancer(云负载均衡器)将服务安全地公开给外部世界。
Beyond internal ClusterIP, services can be exposed via NodePort or LoadBalancer to the external world securely.
CNI:容器网络插件规范
CNI: Container Network Interface Specification
Kubernetes 本身不提供网络连通性,而是通过 CNI 规范 赋予插件管理权。插件负责处理网络接口的建立、IP 分配及策略执行。
Kubernetes itself doesn't provide networking; it relies on the CNI specification to allow plugins to manage interface establishment, IP allocation, and policy enforcement.
连通性与 IPAM
Connectivity & IPAM
利用 veth pair 技术建立容器与宿主机的隧道。IPAM 机制充当“DHCP 服务器”,确保每个 Pod 在 PodCIDR 网段中拥有唯一的身份标识。
Utilizing veth pair technology to tunnel containers to hosts. IPAM acts as a "DHCP server," ensuring each Pod has a unique identity within the PodCIDR range.
生命周期指令集
Lifecycle Operations
标准定义了四个核心动作:ADD(配置接口/IP)、DEL(释放资源)、CHECK(配置检查)及 VERSION(规范匹配)。
The spec defines four core actions: ADD (setup interface/IP), DEL (release resources), CHECK (verification), and VERSION (compatibility).
📜 Kubernetes 网络模型:帝国的基本法
📜 The Kubernetes Network Model: The Constitution
在引入任何 CNI 插件之前,必须理解 Kubernetes 强制执行的三条核心原则,这确保了 Pod 像虚拟机或物理机一样易于管理:
- IP 唯一性:每个 Pod 拥有集群内唯一的 IP 地址。
- IP Uniqueness: Each Pod gets its own unique cluster-wide IP address.
- 直接通信:Pod 间通信无需经过地址转换 (NAT)。
- Direct Communication: Pods communicate without the use of proxies or NAT.
- 节点互通:宿主机上的 Agent (如 Kubelet) 必须能访问该节点的所有 Pod。
- Node Connectivity: Agents on a node can communicate with all pods on that node.
通信模型:网络任务的本质
Networking Model: The Essence of Tasks
在任何系统中,网络的核心任务只有三点:寻址(我要去哪)、转发(怎么过去)以及规则(我能去吗)。根据通信边界,我们将其分为:
In any system, networking revolves around Addressing (Where?), Forwarding (How?), and Rules (Can I?). Based on boundaries, we categorize traffic as:
⬅️ 东西向流量 (East-West)
⬅️ East-West Traffic
集群内部服务间的对话。这是微服务架构的基石,流量巨大且对延迟极度敏感,通常在扁平的二层或三层网络中直接寻址。
Communication between services within the cluster. This foundation of microservices is high-volume and latency-sensitive, using direct addressing.
⬆️ 南北向流量 (North-South)
⬆️ North-South Traffic
外部用户与内部服务间的进出口通信。由 Ingress 管理入站流量的安全边界,Egress 负责服务访问外部资源的安全审计。
Traffic between external users and internal services. Ingress manages security boundaries, while Egress handles outbound auditing.
CNI 与 Kubernetes 的协作生命周期
CNI & Kubernetes Collaboration Lifecycle
CNI 与 Kubernetes 的协作是一个高度标准化的自动化流程。由于系统本身不提供内置的网络实现,它通过 CNI 规范将网络配置的控制权委派给插件,确保每个 Pod 都能在瞬息万变的集群中获得唯一的身份标识,并构建全互联的通信平面。
The collaboration between CNI and Kubernetes is a highly standardized automated process. Since the system does not provide built-in networking, it delegates configuration control to plugins via the CNI specification, ensuring every Pod receives a unique identity and forms a fully interconnected communication plane.
1. 触发阶段 (Trigger)
1. Trigger Phase
调度与唤醒:当 Pod 被调度至特定节点后,该节点上的 Kubelet 进程会监听到调度事件。Kubelet 并不直接操作容器网络,而是通过 CRI(容器运行时接口)调用 containerd 或 CRI-O 来准备 Pod 的执行环境。
Scheduling & Awakening: Once a Pod is assigned to a node, the local Kubelet detects the event. Instead of handling networking directly, it uses the CRI to invoke containerd or CRI-O to prepare the execution environment.
2. 调用阶段 (Invoke)
2. Invoke Phase
配置指令发送:在 Pod 基础设施层启动后,容器运行时会读取预设的 CNI 配置文件,并向相应的 CNI 插件发送 ADD 指令。指令中包含了 Pod 名称、命名空间以及至关重要的网络配置元数据。
Configuration Command: After the infrastructure layer is up, the container runtime reads CNI config files and sends an ADD command to the plugin, carrying metadata like Pod name, namespace, and network specs.
3. 执行阶段 (Execution)
3. Execution Phase
链路构建与分配:这是最核心的步骤。插件会执行以下动作:
• 创建 Network Namespace 实现逻辑隔离。
• 建立 veth pair 打通数据链路。
• 调用 IPAM 模块分配唯一的 Pod IP。
• 在内核中加载 eBPF 程序或过滤规则执行策略。
Link Building & Allocation: The core execution step:
• Creating Network Namespaces for isolation.
• Establishing veth pairs for the data link.
• Calling IPAM to allocate a unique Pod IP.
• Loading eBPF programs or filter rules in the kernel.
4. 反馈阶段 (Feedback)
4. Feedback Phase
状态同步上线:配置完成后,插件将包含 IP 地址和接口信息的 JSON 结果反馈给运行时。Kubelet 随后向 API Server 更新 Pod 状态。至此,Pod 状态由 Pending 转为 Running,正式接收业务流量。
State Sync & Launch: Upon completion, the plugin returns a JSON result with IP and interface info. Kubelet then updates the API Server. The Pod transitions from Pending to Running, ready for traffic.
5. 销毁阶段 (Destroy)
5. Destroy Phase
资源回收清理:当 Pod 生命周期终结时,运行时会发送 DELETE 指令。CNI 插件随即执行反向操作:回收 IP 地址、删除宿主机上的虚拟接口,并清理内核中的转发规则,确保系统资源彻底释放。
Resource Reclamation: At the end of the lifecycle, a DELETE command is sent. The CNI plugin performs reverse operations: reclaiming IPs, deleting virtual interfaces, and clearing kernel forwarding rules.