Network policies for traffic control in Kubernetes - Time & Space Complexity
When using Kubernetes network policies, it is important to understand how the system processes rules as the number of policies grows.
We want to know how the time to enforce traffic control changes when more policies or pods are added.
Analyze the time complexity of the following network policy snippet.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-nginx
spec:
podSelector:
matchLabels:
app: nginx
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 80
This policy allows pods labeled 'frontend' to send TCP traffic on port 80 to pods labeled 'nginx'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each incoming packet against all matching network policies.
- How many times: For each packet, the system checks all relevant policies and their rules.
As the number of network policies and pods increases, the system must check more rules for each packet.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 policies | Checks 10 rules per packet |
| 100 policies | Checks 100 rules per packet |
| 1000 policies | Checks 1000 rules per packet |
Pattern observation: The number of checks grows roughly linearly with the number of policies.
Time Complexity: O(n)
This means the time to process traffic grows linearly as the number of network policies increases.
[X] Wrong: "Adding more policies won't affect traffic processing time much because they run in parallel."
[OK] Correct: Even if policies are processed efficiently, each packet still needs to be checked against all relevant rules, so more policies mean more checks.
Understanding how network policies scale helps you design systems that keep traffic secure without slowing down communication.
"What if we changed the policy to select pods by namespace instead of labels? How would the time complexity change?"
Practice
Solution
Step 1: Understand NetworkPolicy role
Kubernetes NetworkPolicy is designed to control how pods communicate with each other and with other network endpoints.Step 2: Identify correct function
It specifically allows or blocks traffic between pods and namespaces to improve security.Final Answer:
To control traffic flow between pods and namespaces -> Option AQuick Check:
NetworkPolicy controls pod traffic = A [OK]
- Confusing NetworkPolicy with pod scheduling
- Thinking NetworkPolicy manages storage
- Assuming NetworkPolicy monitors resources
Solution
Step 1: Recall podSelector syntax
The correct syntax usesmatchLabelsunderpodSelectorwith key-value pairs properly indented.Step 2: Compare options
podSelector: matchLabels: app: frontend correctly usespodSelector: matchLabels: app: frontend. podSelector: matchLabels: {app: frontend} uses braces which is invalid YAML here.Final Answer:
podSelector: matchLabels: app: frontend -> Option AQuick Check:
Use matchLabels with key: value YAML format [OK]
- Using incorrect YAML braces {} in podSelector
- Writing labels as key=value instead of key: value
- Omitting matchLabels keyword
spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 3306Solution
Step 1: Analyze podSelector and ingress rules
The policy selects pods with labelrole: db. It allows ingress only from pods withrole: frontendon TCP port 3306.Step 2: Interpret traffic allowed
Only pods labeled frontend can connect to db pods on port 3306. Other traffic is blocked.Final Answer:
Only pods with label role=frontend can access db pods on TCP port 3306 -> Option CQuick Check:
Ingress from frontend pods on port 3306 = C [OK]
- Assuming all pods can access db pods
- Confusing source and destination pod labels
- Ignoring port restrictions
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend
spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- namespaceSelector:
matchLabels:
name: frontend-ns
ports:
- protocol: TCP
port: 3306
What is the likely error?Solution
Step 1: Check namespaceSelector label key
Namespace labels usually use the keykubernetes.io/metadata.namefor the namespace name, not justname.Step 2: Understand impact on traffic
Using incorrect label key means no namespaces match, so ingress is blocked.Final Answer:
Namespace label key should be 'kubernetes.io/metadata.name' not 'name' -> Option BQuick Check:
Correct namespace label key fixes traffic block = D [OK]
- Using wrong label key for namespaceSelector
- Thinking protocol TCP is unsupported
- Misunderstanding port data type requirements
Solution
Step 1: Identify podSelector and allowed source
The policy must select pods withrole: dband allow ingress only from pods in namespace 'frontend'. This requiresnamespaceSelectorwith correct label key.Step 2: Check port and protocol correctness
Port must be 5432 TCP. spec: podSelector: matchLabels: role: db ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: frontend ports: - protocol: TCP port: 5432 uses correct protocol TCP and correct namespace label keykubernetes.io/metadata.name.Step 3: Verify other options
spec: podSelector: matchLabels: role: db ingress: - from: - podSelector: matchLabels: role: frontend ports: - protocol: TCP port: 5432 uses podSelector for frontend pods, not namespaceSelector, so it restricts source pods incorrectly. spec: podSelector: matchLabels: role: frontend ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: db ports: - protocol: TCP port: 5432 selects wrong pods and namespaces. spec: podSelector: matchLabels: role: db ingress: - from: - namespaceSelector: matchLabels: name: frontend ports: - protocol: UDP port: 5432 uses wrong protocol UDP and wrong namespace label key.Final Answer:
spec: podSelector: matchLabels: role: db ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: frontend ports: - protocol: TCP port: 5432 -> Option DQuick Check:
Correct podSelector, namespaceSelector, TCP port 5432 = A [OK]
- Using podSelector instead of namespaceSelector for source
- Wrong namespace label key
- Using UDP instead of TCP for port 5432
Ready to prove it?
Pass the test and this pattern moves unknown
