📖 Scenario: You are working in a team that manages applications running on Kubernetes clusters. To keep the applications running smoothly, you need to understand how networking works inside Kubernetes. This helps you ensure that different parts of your application can talk to each other and to the outside world.
🎯 Goal: Learn the basics of Kubernetes networking by creating a simple data structure that represents pods and their IP addresses, then configure a network policy to allow traffic only between certain pods, and finally display which pods can communicate with each other.
📋 What You'll Learn
Create a dictionary representing pods with their IP addresses
Add a list of allowed pod names to simulate a network policy
Write code to find which pods can communicate based on the policy
Print the communication pairs
💡 Why This Matters
🌍 Real World
Kubernetes networking is essential to control traffic flow between application parts, improving security and reliability.
Create a dictionary called pods with these exact entries: 'frontend': '10.0.0.1', 'backend': '10.0.0.2', 'database': '10.0.0.3'
Kubernetes
Hint
Think of pods as a phone book where each pod name has its phone number (IP address).
2
Add a list of allowed pods for network policy
Create a list called allowed_pods containing the pod names 'frontend' and 'backend' to simulate a network policy allowing communication only between these pods
Kubernetes
Hint
This list acts like a guest list for who can talk to each other.
3
Find which pods can communicate based on the policy
Create a list called allowed_communications that contains tuples of pod pairs (source, destination) where both pods are in allowed_pods. Use a for loop with variables src and dst to iterate over allowed_pods twice and include pairs where src != dst
Kubernetes
Hint
Think of this as making pairs of friends who can call each other, but not themselves.
4
Print the allowed pod communications
Write a print statement to display the allowed_communications list
Kubernetes
Hint
This shows which pods can talk to each other according to the network policy.
Practice
(1/5)
1. Why is networking important in Kubernetes clusters?
easy
A. It stores data persistently for applications.
B. It schedules Pods to nodes automatically.
C. It manages user authentication and permissions.
D. It connects different parts of applications and users.
Solution
Step 1: Understand Kubernetes networking role
Kubernetes networking allows communication between application parts and external users.
Step 2: Differentiate networking from other functions
Storage, authentication, and scheduling are handled by other Kubernetes components, not networking.
Final Answer:
It connects different parts of applications and users. -> Option D
Quick Check:
Networking = Connect apps and users [OK]
Hint: Networking links app parts and users in Kubernetes [OK]
Common Mistakes:
Confusing networking with storage
Mixing networking with authentication
Thinking scheduling is networking
2. Which of the following is the correct way to expose a Pod using a Service in Kubernetes?
easy
A. kubectl service create mypod --port=80
B. kubectl expose pod mypod --port=80 --target-port=8080 --type=ClusterIP
C. kubectl create pod mypod --expose --port=80
D. kubectl expose service mypod --port=80
Solution
Step 1: Identify correct command to expose a Pod
The command 'kubectl expose pod' is used to expose a Pod as a Service with specified ports and type.
Step 2: Check syntax correctness
kubectl expose pod mypod --port=80 --target-port=8080 --type=ClusterIP uses correct syntax with --port, --target-port, and --type flags. Other options use invalid commands or flags.
Final Answer:
kubectl expose pod mypod --port=80 --target-port=8080 --type=ClusterIP -> Option B
Quick Check:
Expose Pod = kubectl expose pod [OK]
Hint: Use 'kubectl expose pod' with ports and type [OK]
Common Mistakes:
Using 'kubectl create pod' to expose
Trying to expose a service instead of a pod
Missing required flags like --port
3. Given the following NetworkPolicy YAML snippet, what traffic is allowed?
A. Only frontend Pods can access backend Pods on TCP port 80.
B. No traffic is allowed to backend Pods.
C. Backend Pods can access frontend Pods on TCP port 80.
D. All Pods can access backend Pods on any port.
Solution
Step 1: Analyze podSelector and ingress rules
The policy selects Pods with label role=backend and allows ingress only from Pods with role=frontend on TCP port 80.
Step 2: Understand traffic direction and port restrictions
Only traffic from frontend Pods to backend Pods on TCP port 80 is allowed; other traffic is denied.
Final Answer:
Only frontend Pods can access backend Pods on TCP port 80. -> Option A
Quick Check:
NetworkPolicy limits ingress to frontend on port 80 [OK]
Hint: Check podSelector and ingress from labels and ports [OK]
Common Mistakes:
Assuming all Pods can access backend
Confusing ingress with egress
Ignoring port restrictions
4. You created a NetworkPolicy to allow traffic only from Pods with label app=frontend to Pods with label app=backend. However, backend Pods are still accessible from other Pods. What is the likely cause?
medium
A. The NetworkPolicy is applied in the wrong namespace.
B. The NetworkPolicy is missing a podSelector, so it applies to all Pods.
C. The backend Pods do not have the label app=backend.
D. The NetworkPolicy does not specify any ingress rules, so all traffic is allowed.
Solution
Step 1: Verify label matching on backend Pods
If backend Pods lack the label app=backend, the NetworkPolicy does not select them, so it does not restrict their traffic.
Step 2: Check other possible causes
NetworkPolicy without ingress rules denies all traffic, missing podSelector applies to all Pods, wrong namespace means policy doesn't affect target Pods.
Final Answer:
The backend Pods do not have the label app=backend. -> Option C
Quick Check:
Labels must match for NetworkPolicy to apply [OK]
Hint: Check Pod labels match NetworkPolicy selectors [OK]
Common Mistakes:
Assuming NetworkPolicy applies without matching labels
Ignoring namespace differences
Thinking missing ingress means allow all
5. You want to ensure that your multi-tier app in Kubernetes allows only the frontend Pods to communicate with backend Pods, and backend Pods to communicate with database Pods, but no other traffic. Which combination of Kubernetes networking features should you use?
hard
A. Use Services to expose Pods and NetworkPolicies to restrict traffic between tiers.
B. Use only Services with type LoadBalancer for all Pods.
C. Use NetworkPolicies to expose Pods externally and Services for internal traffic.
D. Use Ingress resources to control Pod-to-Pod communication.
Solution
Step 1: Understand role of Services and NetworkPolicies
Services provide stable network endpoints to reach Pods, while NetworkPolicies control which Pods can communicate with each other.
Step 2: Apply correct features for multi-tier communication
Use Services to expose frontend, backend, and database Pods internally. Use NetworkPolicies to restrict traffic so only allowed Pods communicate.
Final Answer:
Use Services to expose Pods and NetworkPolicies to restrict traffic between tiers. -> Option A