Kubernetes Ingress Chronicles
Ingress is a powerful Kubernetes resource that helps manage how external HTTP and HTTPS traffic gets routed to services within your cluster. Think of it as a traffic cop for your Kubernetes services, deciding which requests get routed where based on rules you define.
What Exactly is Ingress?
At its core, Ingress allows you to expose your HTTP and HTTPS routes from outside your Kubernetes cluster to the services running inside it. It gives you control over how traffic should be directed based on rules specified in the Ingress resource.
Why Do We Need Ingress?
Without Ingress, managing external access to your services can become cumbersome, especially as your application grows. Ingress simplifies this by allowing you to define routing rules, handle SSL/TLS termination, and even provide load balancing—all in one place.
The Ingress Resource
The following yaml code is how we define Ingress.
#service/networking/minimal-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx-example
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80
Breaking It Down:
apiVersion, kind, metadata, and spec: Essential fields for defining an Ingress.
annotations: These provide additional configuration options depending on the Ingress controller. For example, the
nginx.ingress.kubernetes.io/rewrite-target
annotation helps rewrite URLs before they reach your service.spec.rules: This section defines the routing rules. For HTTP(S) traffic, you can specify paths and which services should handle requests that match those paths.
Setting Up Ingress on Minikube with NGINX Ingress Controller
Would you be ready to put your knowledge into action? Let’s set up a simple Ingress on Minikube with the NGINX Ingress Controller.
1. Start Minikube
First, ensure you have Minikube installed and running:
minikube start
Enable the Ingress controller
To enable the NGINX Ingress controller, run the following command:
minikube addons enable ingress
Verify that the NGINX Ingress controller is running
kubectl get pods -n ingress-nginx
The output is similar to:
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-g9g49 0/1 Completed 0 11m
ingress-nginx-admission-patch-rqp78 0/1 Completed 1 11m
ingress-nginx-controller-59b45fb494-26npt 1/1 Running 0 11m
Deploy a hello, world app
Create a Deployment using the following command:
kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0
The output should be :
deployment.apps/web created
Verify that the Deployment is in a Ready state:
kubectl get deployment web
The output should be similar to:
NAME READY UP-TO-DATE AVAILABLE AGE
web 1/1 1 1 53s
Expose the Deployment:
kubectl expose deployment web --type=NodePort --port=8080
The output should be:
service/web exposed
Verify the Service is created and is available on a node port:
kubectl get service web
The output is similar to:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web NodePort 10.104.133.249 <none> 8080:31637/TCP 12m
Visit the Service via NodePort, using the minikube service command. Follow the instructions for your platform:
For MacOS-
# The command must be run in a separate terminal.
minikube service web --url
Output is similar to:
http://127.0.0.1:62445
! Because you are using a Docker driver on Darwin, the terminal needs to be open to run it.
From a different terminal, invoke the URL obtained in the output of the previous step:
curl http://127.0.0.1:62445
Wrapping Up
In this guide, we’ve explored how Ingress can simplify managing external access to your Kubernetes services. By setting up Ingress with Minikube and the NGINX Ingress Controller, you’ve seen how to route traffic and effectively expose services. Keep experimenting and diving deeper to master Kubernetes Ingress and enhance your cloud-native skills!