Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Network policies for security
📖 Scenario: You are managing a Kubernetes cluster for a small company. You want to control which pods can communicate with each other to improve security.
🎯 Goal: Create a Kubernetes NetworkPolicy that allows only pods with a specific label to communicate with each other inside the same namespace.
📋 What You'll Learn
Create a NetworkPolicy YAML manifest named allow-same-label.yaml
The policy should select pods with label role: frontend
Allow ingress traffic only from pods with label role: frontend
Deny all other ingress traffic by default
💡 Why This Matters
🌍 Real World
NetworkPolicies help secure Kubernetes clusters by controlling pod communication, reducing attack surface.
💼 Career
Understanding NetworkPolicies is essential for Kubernetes administrators and DevOps engineers to enforce security best practices.
Progress0 / 4 steps
1
Create a pod selector with label role: frontend
Create a YAML manifest for a NetworkPolicy named allow-same-label.yaml with a podSelector that selects pods with the label role: frontend. Start with the apiVersion, kind, metadata, and spec fields including the podSelector.
Kubernetes
Hint
Use podSelector with matchLabels to select pods with label role: frontend.
2
Add ingress rule to allow traffic from pods with label role: frontend
Add an ingress rule under spec that allows traffic only from pods with the label role: frontend. Use from with podSelector inside the ingress rule.
Kubernetes
Hint
Use ingress with a list containing from that has a podSelector matching role: frontend.
3
Set policy type to Ingress to deny all other traffic
Add policyTypes under spec and set it to - Ingress to ensure all other ingress traffic is denied by default.
Kubernetes
Hint
Adding policyTypes: [Ingress] tells Kubernetes to deny all ingress traffic except what is allowed.
4
Apply the NetworkPolicy and verify it
Run the command kubectl apply -f allow-same-label.yaml to apply the NetworkPolicy. Then run kubectl get networkpolicy allow-same-label -o yaml to display the applied policy.
Kubernetes
Hint
Use kubectl apply -f allow-same-label.yaml to create the policy and kubectl get networkpolicy allow-same-label -o yaml to verify it.
Practice
(1/5)
1. What is the main purpose of a Kubernetes NetworkPolicy?
easy
A. To update container images automatically
B. To schedule pods on specific nodes
C. To manage storage volumes for pods
D. To control which pods can communicate with each other
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 D
Quick Check:
NetworkPolicy controls pod communication = A [OK]
Hint: NetworkPolicy controls pod communication, not scheduling or storage [OK]
Common Mistakes:
Confusing NetworkPolicy with pod scheduling
Thinking NetworkPolicy manages storage
Assuming NetworkPolicy updates images
2. Which of the following is the correct way to specify a pod selector in a NetworkPolicy YAML?
easy
A. podSelector: labels: role: frontend
B. podSelector:
matchLabels:
role: frontend
C. podSelector: role=frontend
D. podSelector: role: frontend
Solution
Step 1: Recall podSelector syntax
In NetworkPolicy YAML, podSelector uses matchLabels to select pods by labels.
Step 2: Match correct YAML format
podSelector:
matchLabels:
role: frontend correctly uses podSelector with matchLabels syntax.
Final Answer:
podSelector:
matchLabels:
role: frontend -> Option B
Quick Check:
Correct podSelector uses matchLabels = C [OK]
Hint: Use matchLabels map inside podSelector for correct syntax [OK]
Common Mistakes:
Using incorrect YAML indentation
Omitting matchLabels key
Writing labels without proper mapping
3. Given this NetworkPolicy snippet, what traffic is allowed?
C. The port in the policy is 8080 but nginx listens on port 80
D. The protocol TCP is not supported in NetworkPolicy
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 C
Quick Check:
Port mismatch blocks traffic = D [OK]
Hint: Check port numbers match service and policy exactly [OK]
Common Mistakes:
Ignoring port mismatch
Assuming protocol TCP is unsupported
Thinking metadata name affects traffic
5. You want to create a NetworkPolicy that allows pods labeled role=frontend to access pods labeled app=nginx on port 80, but blocks all other traffic. Which YAML snippet correctly achieves this?
Step 1: Identify pods to protect and allowed sources
The policy must select pods with app: nginx and allow ingress only from pods with role: 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 uses podSelector for nginx pods and allows ingress from frontend pods on TCP port 80.
Step 3: Confirm other options are incorrect
The snippet that selects role: frontend in podSelector but has from app: nginx reverses source and destination; the snippet using egress and to controls outgoing traffic; the snippet using namespaceSelector selects entire namespaces instead of specific pods.