Kubernetes Ingress is an API object that manages rules for external access to services within the cluster, typically HTTP and HTTPS routing. Ingress provides routing based on domain names and paths, TLS termination, and other features.
Purpose of Ingress
-
Routing Rules: Routes traffic to different Services based on domain names and paths
-
Load Balancing: Distributes traffic across multiple Service instances
-
SSL/TLS Termination: Handles HTTPS at the Ingress layer, simplifying backend configuration
-
Name-based Virtual Hosting: Supports multiple domain names pointing to the same cluster
-
Path Rewriting: Supports URL path rewriting
Ingress Controller
The Ingress Controller is the component that implements Ingress functionality. It monitors changes to Ingress resources and configures the load balancer.
Common Ingress Controllers
-
NGINX Ingress Controller:
- The most popular Ingress Controller
- Based on NGINX/OpenResty
- Rich features and excellent performance
- Supports advanced routing, rate limiting, authentication, etc.
-
Traefik:
- Cloud-native design
- Automatic service discovery
- Supports Let's Encrypt automatic certificates
- Simple configuration
-
HAProxy Ingress:
- Based on HAProxy
- High performance
- Supports advanced load balancing algorithms
-
Istio Gateway:
- Part of the service mesh
- Supports advanced traffic management
- Integrated with mTLS, traffic mirroring, etc.
-
AWS ALB Ingress Controller:
- Designed specifically for AWS
- Uses AWS Application Load Balancer
- Native integration with AWS services
Ingress Resource Examples
Basic Routing
yamlapiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: simple-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / 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
TLS Configuration
yamlapiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: tls-ingress spec: tls: - hosts: - secure.example.com secretName: tls-secret rules: - host: secure.example.com http: paths: - path: / pathType: Prefix backend: service: name: secure-service port: number: 443
Default Backend
yamlapiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: default-backend-ingress spec: defaultBackend: service: name: default-service port: number: 80 rules: - host: example.com http: paths: - path: /api pathType: Prefix backend: service: name: api-service port: number: 80
Ingress Annotations
Annotations are used to configure specific behaviors of the Ingress Controller.
Common NGINX Ingress Controller Annotations
- Rewrite Path:
yamlnginx.ingress.kubernetes.io/rewrite-target: /$2
- Enable SSL Redirect:
yamlnginx.ingress.kubernetes.io/ssl-redirect: "true"
- Rate Limiting Configuration:
yamlnginx.ingress.kubernetes.io/limit-rps: "10" nginx.ingress.kubernetes.io/limit-connections: "5"
- CORS Configuration:
yamlnginx.ingress.kubernetes.io/enable-cors: "true" nginx.ingress.kubernetes.io/cors-allow-origin: "*"
- Authentication Configuration:
yamlnginx.ingress.kubernetes.io/auth-type: basic nginx.ingress.kubernetes.io/auth-secret: basic-auth
- Custom Error Pages:
yamlnginx.ingress.kubernetes.io/custom-http-errors: "404,503"
Ingress vs Service
| Feature | Ingress | Service |
|---|---|---|
| Protocol | HTTP/HTTPS | TCP/UDP |
| Routing | Based on domain and path | Based on port |
| Load Balancing | L7 (Application Layer) | L4 (Transport Layer) |
| SSL Termination | Supported | Not supported |
| Use Case | Web applications | General services |
Ingress vs LoadBalancer
| Feature | Ingress | LoadBalancer |
|---|---|---|
| Cost | Low (shared IP) | High (one IP per Service) |
| Routing Capability | Strong (domain, path) | Weak (port only) |
| SSL Termination | Supported | Partially supported |
| Use Case | Multiple HTTP/HTTPS services | Few services or non-HTTP services |
Deploying Ingress Controller
Deploy NGINX Ingress Controller
bash# Add Helm repository helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx helm repo update # Install helm install ingress-nginx ingress-nginx/ingress-nginx \ --namespace ingress-nginx \ --create-namespace
Verify Installation
bashkubectl get pods -n ingress-nginx kubectl get svc -n ingress-nginx
Best Practices
-
Use Namespace Isolation: Deploy the Ingress Controller in a separate namespace
-
Configure Resource Limits: Set reasonable CPU and memory limits for the Ingress Controller
-
Enable Monitoring: Monitor performance metrics of the Ingress Controller
-
Use TLS: Configure TLS certificates for production environments
-
Configure Health Checks: Ensure health checks for backend Services are working properly
-
Optimize with Annotations: Configure appropriate annotations based on application requirements
-
Backup Configuration: Regularly backup Ingress configuration
-
Version Management: Track version updates of the Ingress Controller
Troubleshooting
- View Ingress Status:
bashkubectl get ingress kubectl describe ingress <ingress-name>
- View Ingress Controller Logs:
bashkubectl logs -n ingress-nginx <pod-name>
- Test DNS Resolution:
bashnslookup example.com
- Check Service and Endpoint:
bashkubectl get svc kubectl get endpoints
- Verify Certificate:
bashkubectl get secret tls-secret -o yaml