A Kubernetes Network Policy is a set of rules that controls how pods communicate with each other and with other network endpoints. It acts like a firewall inside the cluster to allow or block traffic.
Click to reveal answer
beginner
Which Kubernetes resource is used to define traffic rules between pods?
The NetworkPolicy resource is used to define rules that specify allowed inbound and outbound traffic for pods based on labels and ports.
Click to reveal answer
intermediate
What does the podSelector field do in a Network Policy?
The podSelector field selects which pods the policy applies to by matching labels. Only pods matching this selector are affected by the policy rules.
Click to reveal answer
intermediate
How does a Network Policy affect traffic if no policies are applied to a pod?
If no Network Policies select a pod, then all traffic to and from that pod is allowed by default. Policies only restrict traffic when applied.
Click to reveal answer
beginner
What is the difference between ingress and egress rules in Network Policies?
Ingress rules control incoming traffic to pods, while egress rules control outgoing traffic from pods.
Click to reveal answer
What does a Kubernetes Network Policy primarily control?
ACPU usage of pods
BNetwork traffic between pods
CStorage allocation for pods
DPod scheduling on nodes
✗ Incorrect
Network Policies control how pods communicate by allowing or blocking network traffic.
Which field in a Network Policy specifies which pods the policy applies to?
ApodSelector
BnamespaceSelector
CpolicyTypes
Dmetadata
✗ Incorrect
The podSelector field selects pods by labels to apply the policy rules.
If a pod has no Network Policies applied, what is the default behavior for its traffic?
AOnly ingress traffic is allowed
BAll traffic is blocked
CAll traffic is allowed
DOnly egress traffic is allowed
✗ Incorrect
Without any Network Policies, pods accept all traffic by default.
What does an ingress rule in a Network Policy control?
ANode network settings
BOutgoing traffic from pods
CPod resource limits
DIncoming traffic to pods
✗ Incorrect
Ingress rules specify what incoming traffic is allowed to pods.
Which Kubernetes resource would you create to block all traffic to a pod except from a specific namespace?
ANetworkPolicy with namespaceSelector in ingress
BPodSecurityPolicy
CConfigMap
DServiceAccount
✗ Incorrect
NetworkPolicy with a namespaceSelector in ingress rules can restrict traffic to pods from specific namespaces.
Explain how Kubernetes Network Policies control pod communication and why they are important.
Think of Network Policies as rules like a gatekeeper for pod traffic.
You got /4 concepts.
Describe the difference between ingress and egress rules in Network Policies with an example for each.
Ingress is like who can visit your house; egress is where you can go out.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a Kubernetes NetworkPolicy?
easy
A. To control traffic flow between pods and namespaces
B. To schedule pods on specific nodes
C. To manage storage volumes for pods
D. To monitor pod resource usage
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 A
Quick Check:
NetworkPolicy controls pod traffic = 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 monitors resources
2. Which of the following is the correct syntax to specify a pod selector in a NetworkPolicy YAML?
easy
A. podSelector: matchLabels: app: frontend
B. podSelector: labels: app=frontend
C. podSelector: app: frontend
D. podSelector: matchLabels: {app: frontend}
Solution
Step 1: Recall podSelector syntax
The correct syntax uses matchLabels under podSelector with key-value pairs properly indented.
B. Namespace label key should be 'kubernetes.io/metadata.name' not 'name'
C. Protocol TCP is not supported in NetworkPolicy
D. podSelector is missing in the ingress rule
Solution
Step 1: Check namespaceSelector label key
Namespace labels usually use the key kubernetes.io/metadata.name for the namespace name, not just name.
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 B
Quick Check:
Correct namespace label key fixes traffic block = D [OK]
Hint: Use correct namespace label key: kubernetes.io/metadata.name [OK]
Common Mistakes:
Using wrong label key for namespaceSelector
Thinking protocol TCP is unsupported
Misunderstanding port data type requirements
5. You want to allow all pods in namespace 'frontend' to access pods labeled 'role=db' only on port 5432 TCP, but block all other traffic. Which NetworkPolicy YAML snippet correctly achieves this?
hard
A. spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- namespaceSelector:
matchLabels:
name: frontend
ports:
- protocol: UDP
port: 5432
B. spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 5432
C. spec:
podSelector:
matchLabels:
role: frontend
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: db
ports:
- protocol: TCP
port: 5432
D. spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: frontend
ports:
- protocol: TCP
port: 5432
Solution
Step 1: Identify podSelector and allowed source
The policy must select pods with role: db and allow ingress only from pods in namespace 'frontend'. This requires namespaceSelector with 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 key kubernetes.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.