📖 Scenario: You are managing a Kubernetes cluster that hosts multiple web services. You want to route incoming HTTP requests to different services based on the URL path. For example, requests to /app1 should go to service-app1, and requests to /app2 should go to service-app2.
🎯 Goal: Create a Kubernetes Ingress resource that uses path-based routing to send traffic to the correct backend services based on the request path.
📋 What You'll Learn
Create a basic Ingress resource with metadata and spec
Add a default backend service for unmatched paths
Add path rules to route /app1 to service-app1 and /app2 to service-app2
Use the networking.k8s.io/v1 API version and proper pathType
💡 Why This Matters
🌍 Real World
Path-based routing is common in Kubernetes to host multiple applications on the same IP and port, directing users to the right service based on URL paths.
💼 Career
Understanding Ingress and path-based routing is essential for Kubernetes administrators and DevOps engineers managing multi-service applications.
Progress0 / 4 steps
1
Create the basic Ingress resource skeleton
Create a Kubernetes Ingress resource named example-ingress in the default namespace. Use the API version networking.k8s.io/v1. Include metadata with the name and namespace, and an empty spec section.
Kubernetes
Hint
Start with the basic Ingress YAML structure including apiVersion, kind, metadata, and an empty spec.
2
Add a default backend service
Inside the spec section, add a defaultBackend that points to the service named default-service on port 80. Use the correct structure for service and port fields.
Kubernetes
Hint
The defaultBackend routes requests that don't match any path rules. Use the service and port keys properly.
3
Add path-based routing rules
Inside the spec, add an ingressClassName set to nginx. Then add a rules list with one rule for host example.com. Under this rule, add an http section with paths that route /app1 to service service-app1 port 80 and /app2 to service service-app2 port 80. Use pathType: Prefix for both paths.
Kubernetes
Hint
Use ingressClassName to specify the Ingress controller. Define rules with host and http.paths for routing.
4
Display the complete Ingress YAML
Print the complete Ingress YAML manifest to verify the configuration. Use a command like cat ingress.yaml assuming the file is named ingress.yaml.
Kubernetes
Hint
Use cat ingress.yaml to display the full YAML manifest content.
Practice
(1/5)
1. What does path-based routing in Kubernetes Ingress do?
easy
A. It sends traffic to different services based on URL paths.
B. It balances traffic evenly between all nodes.
C. It encrypts traffic between pods.
D. It schedules pods on specific nodes.
Solution
Step 1: Understand Ingress role
Kubernetes Ingress manages external access to services, often HTTP.
Step 2: Identify path-based routing function
Path-based routing directs requests to services depending on the URL path.
Final Answer:
It sends traffic to different services based on URL paths. -> Option A
Quick Check:
Path-based routing = directing traffic by URL path [OK]
Hint: Remember: path-based routing matches URL paths to services [OK]
Common Mistakes:
Confusing path-based routing with load balancing
Thinking it schedules pods
Assuming it encrypts traffic
2. Which is the correct way to specify a path in a Kubernetes Ingress rule for path-based routing?
easy
A. url: /app
urlType: Prefix
B. route: /app
routeType: Exact
C. servicePath: /app
serviceType: Exact
D. path: /app
pathType: Prefix
Solution
Step 1: Recall Ingress rule syntax
Ingress rules use path and pathType keys to define routing.
Step 2: Match correct keys and values
Correct keys are path and pathType, with values like '/app' and 'Prefix'.
Final Answer:
path: /app
pathType: Prefix -> Option D
Quick Check:
Use path and pathType keys for routing [OK]
Hint: Look for 'path' and 'pathType' keys in Ingress rules [OK]
Common Mistakes:
Using wrong keys like 'route' or 'url'
Mixing up pathType values
Missing pathType field
3. Given this Ingress snippet, which service will receive traffic for URL path '/shop/cart'?
Paths must start with a leading slash '/' to match URLs correctly.
Step 2: Identify missing slash
The path is 'api' instead of '/api', so it won't match requests starting with '/api'.
Final Answer:
Missing leading slash in path value -> Option C
Quick Check:
Paths need leading '/' for correct matching [OK]
Hint: Always start path with '/' in Ingress rules [OK]
Common Mistakes:
Omitting leading slash in path
Using wrong pathType values
Assuming port number must be string
5. You want to route traffic so that:
- '/app' goes to 'app-service'
- '/app/admin' goes to 'admin-service'
Which Ingress path rules correctly implement this path-based routing?