Skip to Content
Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition
book

Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

by Benjamin Muschko
January 2026
Intermediate to advanced
392 pages
4h 44m
Chinese
O'Reilly Media, Inc.
Content preview from Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

第20章 网络策略

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

分配给 Pod 的 IP 地址 在所有节点和命名空间中保持唯一性。这是通过在创建过程中为每个注册节点分配专用子网来实现的。当节点上创建新 Pod 时,容器网络接口 (CNI) 插件负责从分配的子网中租赁 IP 地址。因此,节点上的 Pod 可与集群内任意节点上运行的所有其他 Pod 无缝通信。

默认情况下,Kubernetes允许跨命名空间的无限制Pod间通信,这构成了重大安全风险( ),因为某个命名空间中被入侵的Pod可能访问其他命名空间中的敏感服务。

Kubernetes中的网络策略类似于防火墙规则,专门用于管控Pod间通信。这些策略包含针对单个或多个Pod的网络流量方向规则(入站和/或出站),涵盖同一命名空间内或跨命名空间的通信。此外,规则还定义了通信目标端口。这种精细化控制增强了安全性,并有效管理了Kubernetes集群内的流量流向。

网络策略操作

在Kubernetes集群中,任何Pod均可通过IP地址或DNS名称无限制地与其他Pod通信,甚至跨越命名空间。这种不受限制的Pod间通信不仅存在潜在安全风险,更会模糊架构的通信模型认知。网络策略通过定义规则来管控Pod的进出流量,如图20-1所示

Diagram illustrating network policies where Pod 1 can communicate with Pod 2, but Pods 2 and 3 have restricted communication.
图20-1. 网络策略定义Pod 的入站与出站流量

例如,没有充分理由允许运行在某个Pod中的后端应用程序直接与运行在另一个Pod中的前端应用程序通信。通信应由前端Pod引导至后端Pod。

安装网络策略控制器

网络策略必须配合网络策略控制器( )才能生效。该控制器负责评估网络策略定义的规则集合。Kubernetes文档中提供了各类网络策略控制器的配置指南。部分CNI( )如flannel( )未集成网络策略控制器,仅专注于提供基础的Pod间连接功能,这意味着API虽会接受NetworkPolicy资源,但完全不会执行相关策略。

Cilium作为CNI组件( )实现了网络策略控制器功能。您可在云服务商及本地Kubernetes集群中安装Cilium,具体安装指南请参阅相关说明文档。安装完成后,您应能在kube-system命名空间中找到至少两个运行Cilium及Cilium Operator的Pod:

$ kubectl get pods -n kube-system
NAME                               READY   STATUS    RESTARTS       AGE
cilium-k5td6                       1/1     Running   0              110s
cilium-operator-f5dcdcc8d-njfbk    1/1     Running   0              110s

此时可认为网络策略对象定义的规则将被评估。此外,您可使用 Cilium 命令行工具验证安装是否正确。

创建网络策略

标签选择 在定义网络策略 的适用范围时起着关键作用。 我们在其他场景(如部署和服务的配置)中已接触过该概念。此外,网络策略需定义允许或禁止的流量方向:入站流量 称为ingress,出站流量称为egress。针对这两类流量,可通过白名单方式允许特定来源(如Pod、IP地址或端口)。

网络策略不适用于服务

多数情况下,需通过服务 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kubernetes 上的生成式人工智能 (Chinese Edition)

Kubernetes 上的生成式人工智能 (Chinese Edition)

Roland Huß, Daniele Zonca
游戏化头脑风暴

游戏化头脑风暴

Dave Gray, Sunni Brown, James Macanufo

Publisher Resources

ISBN: 0642572314712