Why Kubernetes networking matters - Performance Analysis
Kubernetes networking connects Pods via Services for reliable communication. We analyze how request routing time scales with the number of backend Pods (n).
Focus: Time to route one request through the Service.
Time complexity of request load-balancing in this Kubernetes Service (assuming modern IPVS mode in kube-proxy).
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 9376
Load-balances incoming traffic to Pods labeled 'app: my-app'.
Key operation in kube-proxy:
- Primary operation: Select one endpoint (Pod IP) from the list and forward request (hash or round-robin).
- How many times: Exactly once per request; selection is constant time.
Endpoint list size grows with n, but per-request selection does not.
| Number of Pods (n) | Routing Operations per Request |
|---|---|
| 10 | O(1): Hash/RR select 1 |
| 100 | O(1): Hash/RR select 1 |
| 1000 | O(1): Hash/RR select 1 |
Pattern: Constant time per request regardless of n.
Time Complexity: O(1)
Per-request routing time is constant; scales well with more Pods.
[X] Wrong: "Routing slows linearly with more Pods as it scans the list."
[OK] Correct: IPVS uses kernel-level hashing/round-robin for O(1) selection; endpoints sync incrementally.
Grasping K8s networking scaling demonstrates production-ready systems thinking and performance awareness.
"In legacy iptables mode, how does per-request complexity differ? (Hint: More rules, potential O(n) worst-case lookup)"
Practice
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 DQuick Check:
Networking = Connect apps and users [OK]
- Confusing networking with storage
- Mixing networking with authentication
- Thinking scheduling is networking
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 BQuick Check:
Expose Pod = kubectl expose pod [OK]
- Using 'kubectl create pod' to expose
- Trying to expose a service instead of a pod
- Missing required flags like --port
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend
spec:
podSelector:
matchLabels:
role: backend
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 80
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 AQuick Check:
NetworkPolicy limits ingress to frontend on port 80 [OK]
- Assuming all Pods can access backend
- Confusing ingress with egress
- Ignoring port restrictions
app=frontend to Pods with label app=backend. However, backend Pods are still accessible from other Pods. What is the likely cause?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 CQuick Check:
Labels must match for NetworkPolicy to apply [OK]
- Assuming NetworkPolicy applies without matching labels
- Ignoring namespace differences
- Thinking missing ingress means allow all
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 AQuick Check:
Services expose; NetworkPolicies restrict traffic [OK]
- Using LoadBalancer for internal Pod communication
- Confusing Ingress with Pod-to-Pod control
- Relying only on NetworkPolicies without Services
Ready to prove it?
Pass the test and this pattern moves unknown
