Ingress vs LoadBalancer Service decision in Kubernetes - Performance Comparison
When choosing between Ingress and LoadBalancer Service in Kubernetes, it's important to understand how their processing time grows as the number of requests or services increases.
We want to know how the system's work changes when more traffic or services are added.
Analyze the time complexity of routing requests using an Ingress controller versus a LoadBalancer Service.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /app1
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
---
apiVersion: v1
kind: Service
metadata:
name: app1-loadbalancer
spec:
type: LoadBalancer
selector:
app: app1
ports:
- port: 80
targetPort: 8080
This snippet shows an Ingress routing multiple paths to services and a LoadBalancer Service exposing a single app directly.
- Primary operation: Ingress controller checks each incoming request against its list of rules to find the matching path and service.
- How many times: For each request, it may check multiple rules until it finds a match.
- LoadBalancer Service: Directly routes requests to one service without rule checks.
- Dominant operation: Ingress rule matching per request.
As the number of Ingress rules grows, the controller checks more rules per request, increasing work.
| Number of Rules (n) | Approx. Checks per Request |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of rules.
Time Complexity: O(n)
This means the time to route a request grows linearly with the number of Ingress rules.
[X] Wrong: "Ingress routing time stays the same no matter how many rules exist."
[OK] Correct: Each request must be checked against rules until a match is found, so more rules mean more checks and longer processing time.
Understanding how routing scales helps you design systems that handle growth smoothly. This skill shows you can think about system behavior beyond just writing code.
"What if the Ingress controller used a hash map for rules instead of checking them one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand service types
LoadBalancer Service exposes an app with its own external IP, making it directly accessible.Step 2: Compare with Ingress
Ingress routes multiple apps through one IP, so it's not for single app direct exposure.Final Answer:
LoadBalancer Service -> Option AQuick Check:
Single app direct IP = LoadBalancer Service [OK]
- Confusing Ingress with LoadBalancer for single app
- Choosing ClusterIP which is internal only
- Selecting NodePort which exposes on node ports, not external IP
Solution
Step 1: Check apiVersion and kind
Service resources use apiVersion: v1 and kind: Service.Step 2: Confirm service type
LoadBalancer type is set under spec.type to expose external IP.Final Answer:
YAML with kind Service and type LoadBalancer -> Option DQuick Check:
LoadBalancer service YAML uses kind Service and type LoadBalancer [OK]
- Using kind LoadBalancer instead of Service
- Setting type to Ingress which is invalid for Service
- Confusing NodePort with LoadBalancer
Solution
Step 1: Understand routing multiple apps on one IP
Ingress allows routing multiple apps through one IP using path or hostname rules.Step 2: Compare with LoadBalancer and others
LoadBalancer exposes one app per IP; NodePort uses node ports, not one IP; ClusterIP is internal only.Final Answer:
One Ingress resource with path rules -> Option BQuick Check:
Multiple apps, one IP = Ingress with path rules [OK]
- Using multiple LoadBalancers wasting IPs
- Choosing NodePort which needs manual port management
- Thinking ClusterIP exposes externally
Solution
Step 1: Check platform support for LoadBalancer
Some clusters (like bare metal) do not support automatic external IP assignment for LoadBalancer services.Step 2: Verify service type and controller
Spec.type must be LoadBalancer, not ClusterIP; Ingress controller is unrelated; NodePort is different service type.Final Answer:
Platform lacks LoadBalancer support -> Option CQuick Check:
No external IP = platform lacks LoadBalancer support [OK]
- Confusing ClusterIP with LoadBalancer type
- Assuming Ingress controller assigns LoadBalancer IP
- Mixing NodePort with LoadBalancer service
Solution
Step 1: Understand cost implications of LoadBalancer IPs
Each LoadBalancer service usually gets a separate external IP, increasing cloud costs.Step 2: Use Ingress for cost efficiency
Ingress lets multiple apps share one external IP with routing rules, saving costs.Final Answer:
One Ingress resource with single LoadBalancer IP -> Option AQuick Check:
Multiple apps, minimize cost = Ingress with one LoadBalancer IP [OK]
- Creating multiple LoadBalancers wasting IPs and money
- Using NodePort exposes nodes, less secure and complex
- ClusterIP cannot expose apps externally
Ready to prove it?
Pass the test and this pattern moves unknown
