Host-based routing in Kubernetes - Time & Space Complexity
We want to understand how the time to route requests grows as we add more hosts in Kubernetes.
How does the system handle more hosts and how does that affect routing speed?
Analyze the time complexity of the following Kubernetes Ingress configuration snippet.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: host1.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: service1
port:
number: 80
- host: host2.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: service2
port:
number: 80
This snippet routes requests to different services based on the host name in the request.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the host name against each rule in the Ingress rules list.
- How many times: Once per incoming request, the system compares the request host to each host rule until it finds a match.
As the number of host rules increases, the system checks more hosts to find the right one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 hosts | Up to 10 host checks per request |
| 100 hosts | Up to 100 host checks per request |
| 1000 hosts | Up to 1000 host checks per request |
Pattern observation: The number of checks grows linearly with the number of hosts.
Time Complexity: O(n)
This means the routing time grows directly in proportion to the number of host rules.
[X] Wrong: "Routing time stays the same no matter how many hosts are added."
[OK] Correct: Each new host adds another check, so more hosts mean more work to find the right route.
Understanding how routing scales helps you design systems that stay fast as they grow. This skill shows you can think about real-world system behavior.
What if the routing used a hash map to find hosts instead of checking each one? How would the time complexity change?
Practice
Solution
Step 1: Understand host-based routing concept
Host-based routing directs requests to services depending on the host name in the URL.Step 2: Compare options to the concept
Only To send traffic to different services based on the website address (host). matches this behavior; others describe different features.Final Answer:
To send traffic to different services based on the website address (host). -> Option DQuick Check:
Host-based routing = directing traffic by host [OK]
- Confusing host-based routing with load balancing
- Thinking it encrypts traffic automatically
- Assuming it monitors service health
Solution
Step 1: Review Kubernetes Ingress host-based routing syntax
The correct syntax uses 'rules' with 'host' and 'http.paths' specifying backend service details.Step 2: Match options to correct syntax
spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80 matches the official Kubernetes Ingress spec format for host-based routing.Final Answer:
spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80 -> Option BQuick Check:
Correct Ingress host syntax = spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80 [OK]
- Placing 'host' under metadata instead of rules
- Using deprecated 'serviceName' and 'servicePort' keys
- Confusing 'path' with 'host' field
spec:
rules:
- host: app.example.com
http:
paths:
- path: /app
pathType: Prefix
backend:
service:
name: app-service
port:
number: 8080
- path: /
pathType: Prefix
backend:
service:
name: default-service
port:
number: 80Solution
Step 1: Analyze the Ingress rules for host 'app.example.com'
The rule for host 'app.example.com' has two paths: '/app' and '/'.Step 2: Understand path matching priority
Requests with path starting '/app' go to 'app-service' on port 8080; all other paths go to 'default-service' on port 80.Final Answer:
Traffic to app.example.com with path starting /app goes to app-service; others go to default-service. -> Option AQuick Check:
Host + path routing = Traffic to app.example.com with path starting /app goes to app-service; others go to default-service. [OK]
- Assuming all traffic goes to first service listed
- Thinking duplicate hosts cause rejection
- Ignoring path prefixes in routing
spec:
rules:
- host: shop.example.com
http:
paths:
- path: /shop
backend:
serviceName: shop-service
servicePort: 80Solution
Step 1: Check Ingress backend service syntax
Modern Kubernetes Ingress requires 'service.name' and 'service.port.number' keys, not 'serviceName' and 'servicePort'.Step 2: Identify the error in YAML
The YAML uses deprecated keys causing routing failure.Final Answer:
The keys 'serviceName' and 'servicePort' are deprecated; use 'service.name' and 'service.port.number'. -> Option AQuick Check:
Use correct backend service keys = The keys 'serviceName' and 'servicePort' are deprecated; use 'service.name' and 'service.port.number'. [OK]
- Using old 'serviceName' and 'servicePort' keys
- Adding host under metadata instead of rules
- Changing path unnecessarily
Solution
Step 1: Understand host-based routing for multiple hosts
To route traffic by host, define separate rules for each host under spec.rules.Step 2: Compare options for best practice
Create two rules under spec.rules, each with a different host and paths pointing to their respective services. correctly uses two rules with different hosts and paths to route traffic properly.Final Answer:
Create two rules under spec.rules, each with a different host and paths pointing to their respective services. -> Option CQuick Check:
Multiple hosts need separate rules = Create two rules under spec.rules, each with a different host and paths pointing to their respective services. [OK]
- Using one host with path prefixes instead of separate hosts
- Creating multiple Ingresses unnecessarily
- Not specifying hosts and relying on paths only
Ready to prove it?
Pass the test and this pattern moves unknown
