容器中的网络策略
在 Kubernetes 中实施网络策略,控制 Pod 之间的流量,提高安全性和隔离性
容器中的网络策略 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are Network Policies?
In Kubernetes, Network Policies are like firewalls for your pods. They control how groups of pods communicate with each other and with external network endpoints.
Think of them as a security layer that defines which connections are allowed or denied, enhancing isolation and security.
Why Use Network Policies?
Without Network Policies, all pods in a Kubernetes cluster can communicate with each other by default. This can be a significant security risk!
- Isolation: Prevent unauthorized access between different application tiers (e.g., frontend talking directly to a sensitive database).
- Security: Reduce the attack surface by only allowing necessary connections.
- Compliance: Help meet regulatory requirements for network segmentation.
How Network Policies Function
Network Policies work by selecting specific pods and then defining rules for allowed inbound (ingress) and outbound (egress) traffic for those pods.
They are enforced by the cluster's Container Network Interface (CNI) plugin (like Calico or Cilium). If no policy selects a pod, all traffic to/from it is allowed by default.
Policy Structure: The Basics
A Network Policy is a Kubernetes resource defined in YAML. It specifies:
podSelector: Which pods the policy applies to.policyTypes: Whether the policy affects ingress, egress, or both.ingress/egressrules: The specific allowed connections.
Here's the basic YAML structure:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-policy
namespace: default
spec:
# ... rules go here ...Targeting Pods with Selectors
The podSelector is crucial. It uses labels to identify the pods that this policy will apply to.
If podSelector is empty {}, the policy applies to ALL pods in its namespace. If omitted, the policy applies to no pods.
The policyTypes field specifies if the policy governs `Ingress`, `Egress`, or both. This helps the network plugin know which traffic directions to enforce.
spec:
podSelector:
matchLabels:
app: my-app
tier: backend
policyTypes:
- Ingress
- EgressControlling Inbound Traffic (Ingress)
Ingress rules define what traffic is allowed to enter the selected pods. If an ingress rule is present, only traffic matching that rule is permitted; all other ingress traffic is denied.
You can specify allowed traffic based on:
from: The source of the traffic (e.g., IP block, namespace, or other pods).ports: Specific destination ports on the selected pods.
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 80Controlling Outbound Traffic (Egress)
Egress rules define what traffic is allowed to leave the selected pods. Similar to ingress, if an egress rule is present, only traffic matching that rule is permitted; all other egress traffic is denied.
You can specify allowed traffic based on:
to: The destination of the traffic (e.g., IP block, namespace, or other pods).ports: Specific source ports on the selected pods.
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- protocol: TCP
port: 5432Example: Default Deny Ingress
A common security practice is to implement a "default deny" policy. This means no traffic is allowed to a pod unless explicitly permitted.
To achieve this for ingress traffic, create a policy that selects the desired pods but has an empty ingress rule list. This explicitly denies all incoming traffic.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
namespace: default
spec:
podSelector:
matchLabels:
app: sensitive-app
policyTypes:
- Ingress
ingress: [] # An empty list means no ingress is allowedExample: Allow Specific Ingress
After a default deny, you can add more specific policies to allow necessary traffic. Here, we allow backend pods to receive traffic on port 80 from frontend pods.
This policy targets pods with app: backend and permits ingress from pods labeled app: frontend on TCP port 80.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: default
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 80Policy Check
Consider a pod with label app: database. You want to block all incoming traffic to it, except from pods with label app: backend.
Recap: Network Policies
You've learned about Kubernetes Network Policies!
- They provide firewall-like rules for pods.
- They use
podSelectorto target pods and defineingress/egressrules. - By default, all pods can communicate; policies enforce restrictions.
- They are essential for securing and isolating containerized applications.
Keep practicing with different policy rules to master container security!
常见问题解答
「容器中的网络策略」课时是免费的吗?
是的 — 「容器中的网络策略」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。
「容器中的网络策略」这节课中我会学到什么?
在 Kubernetes 中实施网络策略,控制 Pod 之间的流量,提高安全性和隔离性 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Networking & TCP/IP for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「容器中的网络策略」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?
能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。