Network policies for security in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to enforce network policies changes as the number of policies or pods grows.
How does adding more policies or pods affect the work Kubernetes does to secure the network?
Analyze the time complexity of the following Kubernetes 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
This policy allows pods with label role: frontend to send traffic to pods labeled app: nginx.
Look at what Kubernetes does repeatedly when applying network policies.
- Primary operation: Checking each pod against policy selectors to see if it is allowed or denied.
- How many times: For each pod, Kubernetes checks all relevant policies and their rules.
As the number of pods and policies grows, the checks increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 pods, 5 policies | ~50 checks |
| 100 pods, 20 policies | ~2000 checks |
| 1000 pods, 50 policies | ~50,000 checks |
Pattern observation: The number of checks grows roughly with the product of pods and policies.
Time Complexity: O(pods × policies)
This means the work to enforce network policies grows proportionally as you add more pods and policies.
[X] Wrong: "Adding more policies won't affect performance much because they run independently."
[OK] Correct: Each pod must be checked against all policies, so more policies mean more checks and more work.
Understanding how network policy enforcement scales helps you design secure and efficient Kubernetes clusters.
"What if network policies used IP blocks instead of pod selectors? How would that change the time complexity?"
Practice
NetworkPolicy?Solution
Step 1: Understand NetworkPolicy role
A NetworkPolicy defines rules about pod communication inside a Kubernetes cluster.Step 2: Identify main function
It controls which pods can send or receive network traffic to improve security.Final Answer:
To control which pods can communicate with each other -> Option DQuick Check:
NetworkPolicy controls pod communication = A [OK]
- Confusing NetworkPolicy with pod scheduling
- Thinking NetworkPolicy manages storage
- Assuming NetworkPolicy updates images
Solution
Step 1: Recall podSelector syntax
In NetworkPolicy YAML, podSelector usesmatchLabelsto select pods by labels.Step 2: Match correct YAML format
podSelector: matchLabels: role: frontend correctly usespodSelectorwithmatchLabelssyntax.Final Answer:
podSelector: matchLabels: role: frontend -> Option BQuick Check:
Correct podSelector uses matchLabels = C [OK]
- Using incorrect YAML indentation
- Omitting matchLabels key
- Writing labels without proper mapping
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
Solution
Step 1: Analyze podSelector and ingress rules
The policy selects pods with labelapp: nginxand allows ingress only from pods withrole: frontendon TCP port 80.Step 2: Interpret allowed traffic
Only pods labeledrole=frontendcan connect to nginx pods on TCP port 80; other traffic is blocked.Final Answer:
Only pods with label role=frontend can access nginx pods on TCP port 80 -> Option AQuick Check:
Ingress from role=frontend on port 80 = B [OK]
- Assuming all pods can access nginx
- Confusing source and destination labels
- Ignoring port restrictions
role=frontend still cannot access app=nginx pods on port 80. What is wrong?
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: 8080
Solution
Step 1: Compare port in policy with actual service port
The policy allows ingress on TCP port 8080, but nginx usually listens on port 80.Step 2: Identify mismatch causing blocked traffic
Because the port does not match nginx's listening port, traffic is blocked despite correct podSelector.Final Answer:
The port in the policy is 8080 but nginx listens on port 80 -> Option CQuick Check:
Port mismatch blocks traffic = D [OK]
- Ignoring port mismatch
- Assuming protocol TCP is unsupported
- Thinking metadata name affects traffic
role=frontend to access pods labeled app=nginx on port 80, but blocks all other traffic. Which YAML snippet correctly achieves this?Solution
Step 1: Identify pods to protect and allowed sources
The policy must select pods withapp: nginxand allow ingress only from pods withrole: frontend.Step 2: Check ingress rules and ports
spec: podSelector: matchLabels: app: nginx ingress: - from: - podSelector: matchLabels: role: frontend ports: - protocol: TCP port: 80 correctly usespodSelectorfor nginx pods and allows ingress from frontend pods on TCP port 80.Step 3: Confirm other options are incorrect
The snippet that selectsrole: frontendin podSelector but has fromapp: nginxreverses source and destination; the snippet usingegressandtocontrols outgoing traffic; the snippet usingnamespaceSelectorselects entire namespaces instead of specific pods.Final Answer:
spec: podSelector: matchLabels: app: nginx ingress: - from: - podSelector: matchLabels: role: frontend ports: - protocol: TCP port: 80 -> Option AQuick Check:
Correct podSelector and ingress from frontend pods = A [OK]
- Mixing up podSelector labels
- Using egress instead of ingress
- Using namespaceSelector instead of podSelector
