Introduce Ingress Basics and Show a Simple Ingress YAML Example
In this step, you'll learn about Kubernetes Ingress, a powerful way to manage external access to services in a Kubernetes cluster.
What is Ingress?
Ingress is an API object that manages external access to services in a Kubernetes cluster, typically HTTP. Ingress provides:
- Load balancing: Distributes traffic to multiple backend services
- SSL/TLS termination: Handles secure connections
- Name-based virtual hosting: Routes requests to different services based on the hostname
- Path-based routing: Routes requests to different services based on the URL path
Ingress consists of two components:
- Ingress Resource: A Kubernetes API object that defines the routing rules
- Ingress Controller: The implementation that enforces the rules defined in the Ingress Resource
Note: This lab provides only a basic introduction to Ingress. In production environments, Ingress configurations can be much more complex, including advanced routing, authentication, rate limiting, and more.
Let's enable the Ingress addon in Minikube:
minikube addons enable ingress
Example output:
💡 ingress is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
🔉 ingress was successfully enabled
Create a deployment for two sample applications:
kubectl create deployment web1 --image=nginx:alpine
kubectl create deployment web2 --image=httpd:alpine
Expose these deployments as services:
kubectl expose deployment web1 --port=80 --type=ClusterIP --name=web1-service
kubectl expose deployment web2 --port=80 --type=ClusterIP --name=web2-service
Create an Ingress YAML file:
nano ingress-example.yaml
Add the following Ingress configuration:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /web1
pathType: Prefix
backend:
service:
name: web1-service
port:
number: 80
- path: /web2
pathType: Prefix
backend:
service:
name: web2-service
port:
number: 80
Key components of this Ingress configuration:
- metadata.annotations: Specific configurations for the Ingress controller
- spec.rules: Define how traffic is routed to services
- path: URL path that will be matched
- pathType: How the path should be matched (Prefix, Exact, or ImplementationSpecific)
- backend.service: The service and port to route traffic to
Apply the Ingress configuration:
kubectl apply -f ingress-example.yaml
Verify the Ingress resource:
kubectl get ingress
Example output:
NAME CLASS HOSTS ADDRESS PORTS AGE
example-ingress nginx * 192.168.49.2 80 1m
Check Ingress details:
kubectl describe ingress example-ingress
Example output will show routing rules and backend services.
Testing the Ingress:
## Get the Minikube IP
minikube ip
## Test access to the services through Ingress
curl $(minikube ip)/web1
curl $(minikube ip)/web2
Each command should return the default page from the respective web server.
In production environments, Ingress can be configured with:
- Multiple hostname-based rules
- TLS certificates for HTTPS
- Authentication mechanisms
- Rate limiting
- Custom timeout configurations
- Session affinity
- And many more advanced features
For more comprehensive coverage of Ingress, refer to the Kubernetes documentation and consider exploring dedicated Ingress controller documentation like NGINX Ingress or Traefik.