Kubernetes Ingress
An Ingress exposes HTTP and HTTPS routes from outside a Kubernetes cluster to Services, using host and path-based routing rules managed by an Ingress controller.
An Ingress is a Kubernetes API object that defines HTTP and HTTPS routing rules for traffic entering the cluster from outside. Instead of creating one LoadBalancer Service per application (each of which provisions a separate, costly cloud load balancer), you deploy a single Ingress controller (such as nginx-ingress or the AWS Load Balancer Controller) and route all HTTP/HTTPS traffic through it using Ingress rules based on hostname and URL path. An Ingress object on its own does nothing — it requires a running Ingress controller to implement the rules.
Ingress vs Service LoadBalancer
| Aspect | Service (LoadBalancer) |
Ingress |
|---|---|---|
| OSI layer | Layer 4 (TCP/UDP) | Layer 7 (HTTP/HTTPS) |
| Routing | By IP:port only | By hostname and/or URL path |
| TLS termination | Not built-in | Yes, using spec.tls |
| Cost | One cloud LB per Service | One cloud LB for all Ingress rules |
| Header manipulation | No | Yes (depends on controller) |
| Typical use | Non-HTTP workloads, direct TCP | All HTTP/HTTPS ingress traffic |
Ingress Controller
Kubernetes does not ship with a built-in Ingress controller. You must install one. Common choices:
- ingress-nginx (community) — the most widely used; runs an nginx reverse proxy
- AWS Load Balancer Controller — provisions an AWS ALB
- GKE Ingress — provisions a GCP HTTPS Load Balancer
- Traefik, HAProxy Ingress, Kong
Minimal Ingress Manifest
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx # must match the IngressClass installed
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-svc
port:
number: 80
Path-Based Routing (Multiple Services)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: multi-app-ingress
namespace: default
spec:
ingressClassName: nginx
rules:
- host: api.example.com
http:
paths:
- path: /users
pathType: Prefix
backend:
service:
name: users-svc
port:
number: 8080
- path: /orders
pathType: Prefix
backend:
service:
name: orders-svc
port:
number: 8080
TLS Termination
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: secure-ingress
namespace: default
spec:
ingressClassName: nginx
tls:
- hosts:
- app.example.com
secretName: nginx-tls # Secret of type kubernetes.io/tls
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-svc
port:
number: 80
pathType Values
pathType |
Matching Behavior |
|---|---|
Exact |
Matches the exact URL path only |
Prefix |
Matches the path and any subpaths; /api matches /api, /api/v1, /api/v2 |
ImplementationSpecific |
Matching depends on the Ingress controller implementation |
kubectl Commands
kubectl apply -f app-ingress.yaml
kubectl get ingress
# NAME CLASS HOSTS ADDRESS PORTS AGE
# app-ingress nginx app.example.com 34.102.10.200 80 2m
kubectl describe ingress app-ingress
# ... rules, backend, TLS, events
kubectl get ingressclass
# NAME CONTROLLER PARAMETERS AGE
# nginx k8s.io/ingress-nginx <none> 5d
Common Mistakes
- Missing or wrong
ingressClassName. Without a matchingingressClassName, the Ingress object is ignored by all controllers. From Kubernetes 1.18+,ingressClassNameis the standard way to associate an Ingress with a controller. Checkkubectl get ingressclassfor available classes. - Referencing Services in a different namespace. An
Ingressobject can only forward to Services in the same namespace. For cross-namespace routing, some controllers supportIngressRouteTCPor similar custom resources. - Forgetting that the Ingress controller must be installed. An
Ingressobject is just configuration. Without a running controller (such asingress-nginx), no traffic is actually routed. - Using
extensions/v1beta1ornetworking.k8s.io/v1beta1. These API versions were removed in Kubernetes 1.22. Always usenetworking.k8s.io/v1withpathTypespecified. - Omitting
pathType.pathTypeis required innetworking.k8s.io/v1. Omitting it will cause a validation error.
Best Practices
- Use
cert-managerwith Let's Encrypt to automate TLS certificate provisioning and renewal for Ingress resources. - Prefer a single Ingress resource with multiple rules over multiple Ingress resources to reduce configuration complexity.
- Use annotations carefully; they are controller-specific and tie you to a particular implementation.
- Apply rate limiting and authentication at the Ingress layer (using controller-specific annotations) rather than in every individual application.
- Use
pathType: Prefixfor API services andpathType: Exactfor specific endpoints to avoid unintended route matches.
Interview Questions
1. What is a Kubernetes Ingress and how does it differ from a Service of type LoadBalancer?
An Ingress operates at Layer 7 (HTTP/HTTPS) and routes traffic based on hostname and URL path, with TLS termination support. A LoadBalancer Service operates at Layer 4 (TCP/UDP) and gives each Service its own cloud load balancer. For HTTP workloads, Ingress is preferred because a single Ingress controller and one cloud load balancer serve all your HTTP applications, reducing cost and complexity.
2. What is an Ingress controller and why is it needed?
An Ingress object is just a configuration object in the Kubernetes API. It does nothing on its own. An Ingress controller is a Pod running in the cluster (such as ingress-nginx) that watches for Ingress objects and translates their rules into actual reverse-proxy configuration (e.g., nginx server blocks). Without a controller, Ingress rules have no effect.
3. What does ingressClassName do?
ingressClassName references an IngressClass resource that identifies which Ingress controller should implement the Ingress object. This allows multiple Ingress controllers to run in the same cluster (e.g., nginx for internal traffic and an AWS ALB controller for external traffic). If ingressClassName is omitted, the behavior depends on whether a default IngressClass is set.
4. What are the pathType options for Ingress path rules?
Exact matches only the specified path. Prefix matches the path and all of its subpaths (e.g., /api matches /api, /api/v1). ImplementationSpecific delegates matching logic to the Ingress controller. pathType is required in networking.k8s.io/v1; omitting it causes a validation error.
5. How does TLS termination work with an Ingress?
You create a Secret of type kubernetes.io/tls containing a TLS certificate and private key, then reference it in spec.tls[].secretName. The Ingress controller terminates the HTTPS connection at the load balancer/proxy level and forwards plain HTTP to the backend Service. Traffic inside the cluster between the controller and the backend is unencrypted unless you configure backend TLS separately.
6. Can an Ingress route to Services in a different namespace?
By default, no — an Ingress object can only reference Service backends in the same namespace. Some Ingress controllers provide extensions (like IngressRouteTCP in Traefik) for cross-namespace routing. Otherwise, create an Ingress resource in each namespace.
7. How would you set up Ingress with automatic TLS certificates?
Install cert-manager in the cluster and configure an Issuer or ClusterIssuer pointing to Let's Encrypt. Annotate the Ingress with cert-manager.io/cluster-issuer: letsencrypt-prod and specify spec.tls[].secretName. cert-manager detects the annotation, requests a certificate via the ACME challenge, and stores it in the specified Secret, which the Ingress controller then uses automatically.
Summary
- An
Ingressprovides HTTP/HTTPS layer-7 routing to cluster Services based on hostname and URL path rules. - It requires an Ingress controller (such as
ingress-nginx) to be running in the cluster; the object alone does nothing. - Use
networking.k8s.io/v1withingressClassNameandpathTypespecified — older API versions were removed in 1.22. - TLS termination is handled by referencing a
kubernetes.io/tlsSecret inspec.tls. - Prefer
Ingressover multipleLoadBalancerServices for HTTP workloads to reduce cloud load balancer costs.
Continue Learning
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.