📖 Scenario: You are managing a Kubernetes cluster that hosts multiple web applications. You want to customize the behavior of the Ingress resource to control how traffic is routed and handled by the Ingress controller.Ingress annotations allow you to add extra settings to your Ingress resources without changing the core specification. These settings can control things like SSL redirect, request timeout, and custom headers.
🎯 Goal: Learn how to add annotations to a Kubernetes Ingress resource to customize its behavior for your web applications.
📋 What You'll Learn
Create a basic Ingress resource with a host and service backend
Add an annotation to enable SSL redirect
Add an annotation to set a custom request timeout
Display the final Ingress YAML with annotations
💡 Why This Matters
🌍 Real World
Ingress annotations are widely used in Kubernetes to customize how traffic is handled by the Ingress controller without changing the core Ingress spec. This allows teams to quickly enable features like SSL redirect, timeouts, and security settings.
💼 Career
Understanding how to use Ingress annotations is important for DevOps engineers and Kubernetes administrators to manage application traffic, improve security, and optimize performance in production environments.
Progress0 / 4 steps
1
Create a basic Ingress resource
Create a Kubernetes Ingress resource YAML named my-ingress.yaml with the following exact content: apiVersion networking.k8s.io/v1, kind Ingress, metadata name my-ingress, spec with a rule for host example.com routing to service name my-service on port 80.
Kubernetes
Hint
Remember to include apiVersion, kind, metadata with name, and spec with rules for the host and backend service.
2
Add SSL redirect annotation
Add an annotation to the metadata section of my-ingress.yaml to enable SSL redirect by setting nginx.ingress.kubernetes.io/ssl-redirect to "true".
Kubernetes
Hint
Annotations go under metadata as a map of key-value pairs.
3
Add custom request timeout annotation
Add another annotation to my-ingress.yaml under metadata.annotations to set the request timeout by adding nginx.ingress.kubernetes.io/proxy-read-timeout with the value "60".
Kubernetes
Hint
Multiple annotations are listed under metadata.annotations as key-value pairs.
4
Display the final Ingress YAML
Print the complete content of my-ingress.yaml showing the Ingress resource with both annotations included.
Kubernetes
Hint
Use multiple print() statements to output each line exactly as in the YAML.
Practice
(1/5)
1. What is the main purpose of using annotations in a Kubernetes Ingress resource?
easy
A. To specify the IP address of the Ingress controller
B. To define the backend services for routing traffic
C. To customize the behavior of the Ingress controller without changing the main spec
D. To set resource limits for the Ingress pod
Solution
Step 1: Understand the role of annotations in Kubernetes
Annotations are key-value pairs used to add metadata or customize behavior without changing the main resource spec.
Step 2: Apply this to Ingress resources
In Ingress, annotations allow customization of controller behavior like SSL settings or rewrite rules without modifying the core routing rules.
Final Answer:
To customize the behavior of the Ingress controller without changing the main spec -> Option C
Quick Check:
Ingress annotations = customization without spec change [OK]
Hint: Annotations customize Ingress behavior without touching main config [OK]
Common Mistakes:
Confusing annotations with service definitions
Thinking annotations set IP addresses
Assuming annotations control pod resources
2. Which of the following is the correct syntax to add an annotation for enabling SSL redirect in a Kubernetes Ingress YAML?
easy
A. metadata:
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
B. spec:
annotations:
ssl-redirect: true
C. metadata:
labels:
nginx.ingress.kubernetes.io/ssl-redirect: true
D. annotations:
ssl-redirect: "true"
Solution
Step 1: Locate where annotations belong in Ingress YAML
Annotations must be under metadata.annotations as key-value pairs.
Step 2: Check correct key and value format
The key for SSL redirect in nginx ingress is nginx.ingress.kubernetes.io/ssl-redirect and the value should be a string "true".
Final Answer:
metadata:
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true" -> Option A
Quick Check:
Annotations under metadata.annotations with string values [OK]
Hint: Annotations go under metadata.annotations as string key-values [OK]
Common Mistakes:
Placing annotations under spec instead of metadata
5. You want to customize your Ingress to add a custom header X-Frame-Options: DENY for security. Which annotation and value should you add to achieve this with nginx ingress controller?
hard
A. nginx.ingress.kubernetes.io/custom-header: "X-Frame-Options=DENY"
B. nginx.ingress.kubernetes.io/add-headers: "X-Frame-Options: DENY"
C. nginx.ingress.kubernetes.io/headers: "X-Frame-Options: DENY"
D. nginx.ingress.kubernetes.io/configuration-snippet: "more_set_headers \"X-Frame-Options: DENY\";"
Solution
Step 1: Identify how to add custom headers in nginx ingress
The nginx ingress controller supports adding custom headers via the configuration-snippet annotation using nginx directives.
Step 2: Use the correct syntax for setting headers
The directive more_set_headers "X-Frame-Options: DENY"; correctly adds the header.
Final Answer:
nginx.ingress.kubernetes.io/configuration-snippet: "more_set_headers \"X-Frame-Options: DENY\";" -> Option D
Quick Check:
Use configuration-snippet with more_set_headers for custom headers [OK]
Hint: Use configuration-snippet with nginx directive for custom headers [OK]
Common Mistakes:
Using non-existent annotation keys
Incorrect syntax for header setting
Confusing add-headers with configuration-snippet
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.