Ingress resource definition in Kubernetes - Time & Space Complexity
We want to understand how the time to process an Ingress resource changes as it grows.
Specifically, how does adding more rules affect the work Kubernetes does?
Analyze the time complexity of the following Ingress resource definition.
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
- path: /app2
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
This Ingress routes requests to different services based on URL paths.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Kubernetes checks each path rule in the Ingress to find a match for incoming requests.
- How many times: It examines each path rule one by one until it finds a match or finishes all rules.
As the number of path rules increases, the time to find the right backend grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 path checks |
| 100 | About 100 path checks |
| 1000 | About 1000 path checks |
Pattern observation: The work grows directly with the number of path rules.
Time Complexity: O(n)
This means the time to process the Ingress grows linearly with the number of path rules.
[X] Wrong: "Adding more path rules won't affect performance because Kubernetes handles all paths instantly."
[OK] Correct: Kubernetes must check each path rule to find a match, so more rules mean more checks and more time.
Understanding how resource size affects processing time helps you design scalable Kubernetes configurations and shows you think about system behavior beyond just writing code.
"What if the Ingress used a more efficient matching method like a prefix tree? How would the time complexity change?"
Practice
Ingress resource?Solution
Step 1: Understand Ingress role
An Ingress resource defines rules to route external traffic to services inside the cluster.Step 2: Compare with other resources
ConfigMaps store config, PersistentVolumes manage storage, and scheduling is done by the scheduler, not Ingress.Final Answer:
To route external HTTP(S) traffic to internal services based on rules -> Option AQuick Check:
Ingress routes external traffic [OK]
- Confusing Ingress with ConfigMap or PersistentVolume
- Thinking Ingress schedules pods
- Assuming Ingress manages storage
/app path to a service named my-service on port 80?Solution
Step 1: Check apiVersion and kind
Correct apiVersion for Ingress is networking.k8s.io/v1 and kind is Ingress.Step 2: Validate backend service and port
apiVersion: networking.k8s.io/v1\nkind: Ingress\nmetadata:\n name: example-ingress\nspec:\n rules:\n - http:\n paths:\n - path: /app\n pathType: Prefix\n backend:\n service:\n name: my-service\n port:\n number: 80 uses correct new syntax with service name and port number 80 matching the question.Final Answer:
YAML with apiVersion networking.k8s.io/v1, kind Ingress, path /app, service my-service, port 80 -> Option BQuick Check:
Correct Ingress YAML uses networking.k8s.io/v1 and service port number [OK]
- Using old apiVersion v1 for Ingress
- Using serviceName/servicePort instead of service.name/port.number
- Wrong port number in backend
- Defining Service instead of Ingress
web-service?
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /web
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
Solution
Step 1: Identify host and path in rules
The rule specifies host example.com and path /web with pathType Prefix.Step 2: Understand pathType Prefix meaning
Prefix means any path starting with /web will match, not just exactly /web.Final Answer:
Traffic to host example.com and path starting with /web -> Option AQuick Check:
Host and prefix path routing = Traffic to host example.com and path starting with /web [OK]
- Assuming path matches exactly instead of prefix
- Ignoring host field and assuming any host
- Confusing pathType Prefix with Exact
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: bad-ingress
spec:
rules:
- http:
paths:
- path: /app
pathType: Prefix
backend:
serviceName: app-service
servicePort: 80
What is the likely cause of the problem?Solution
Step 1: Check backend syntax in Ingress spec
The YAML uses old syntax with serviceName and servicePort which is deprecated in networking.k8s.io/v1.Step 2: Confirm correct syntax
Correct syntax uses service.name and service.port.number fields under backend.service.Final Answer:
Using deprecated backend syntax serviceName and servicePort instead of service.name and service.port.number -> Option CQuick Check:
Use service.name and service.port.number in Ingress backend [OK]
- Using old backend syntax causing Ingress controller to ignore rules
- Assuming missing host always breaks routing
- Confusing apiVersion correctness
- Forgetting metadata name
service-a and service-b, on the same IP but different paths /a and /b. Which Ingress YAML snippet correctly defines this?Solution
Step 1: Check apiVersion and kind
All options use correct apiVersion and kind for Ingress.Step 2: Validate backend syntax and multiple paths
apiVersion: networking.k8s.io/v1\nkind: Ingress\nmetadata:\n name: multi-service-ingress\nspec:\n rules:\n - http:\n paths:\n - path: /a\n pathType: Prefix\n backend:\n service:\n name: service-a\n port:\n number: 80\n - path: /b\n pathType: Prefix\n backend:\n service:\n name: service-b\n port:\n number: 80 correctly uses service.name and service.port.number with two paths under one rule without host, matching the requirement.Step 3: Compare pathType usage
Prefix pathType allows routing all requests starting with /a or /b, which is typical for path-based routing.Final Answer:
Ingress with two paths /a and /b routing to service-a and service-b using correct backend syntax -> Option DQuick Check:
Multiple paths with service.name and port.number = apiVersion: networking.k8s.io/v1\nkind: Ingress\nmetadata:\n name: multi-service-ingress\nspec:\n rules:\n - http:\n paths:\n - path: /a\n pathType: Prefix\n backend:\n service:\n name: service-a\n port:\n number: 80\n - path: /b\n pathType: Prefix\n backend:\n service:\n name: service-b\n port:\n number: 80 [OK]
- Using deprecated backend syntax serviceName/servicePort
- Missing one of the paths or services
- Using pathType Exact which may limit routing
- Defining only one path instead of two
Ready to prove it?
Pass the test and this pattern moves unknown
