What if a simple rule could stop hackers from sneaking into your system unnoticed?
Why Network policies for security in Kubernetes? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a busy office where everyone talks freely, but some conversations should only happen in private rooms. Without clear rules, anyone can overhear sensitive talks.
Manually controlling who can talk to whom in a network is like trying to watch every conversation in a crowded office. It's slow, easy to miss mistakes, and risky because sensitive data might leak.
Network policies act like smart doors and guards that only let the right people into the right rooms. They automatically control traffic between parts of your system, keeping everything safe without constant watching.
Allow all traffic between pods without restrictions
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-only-frontend-to-backend
spec:
podSelector:
matchLabels:
role: backend
ingress:
- from:
- podSelector:
matchLabels:
role: frontendWith network policies, you can confidently protect your system by controlling exactly who talks to whom, reducing risks and improving security.
In a company, only the finance team's computers can access the payroll database. Network policies enforce this rule automatically, preventing accidental or malicious access.
Manual network control is slow and risky.
Network policies automate secure communication rules.
This keeps your system safe and easier to manage.
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
