Unlocking the Magic: Setting Up Ingress on Minikube with NGINX Ingress Controller đ
In the realm of Kubernetes, Ingress is the gateway to granting external access to services within a cluster. But every good gatekeeper needs a controller, and thatâs where the NGINX Ingress Controller comes into play. This article walks you through the process of setting up Ingress, using NGINX as your trusty gatekeeper, to elegantly route requests to different services, depending on the HTTP URI. So, fasten your seatbelts and embark on this journey to master Ingress on Minikube! đ
Before We Dive In đ
First things first, we assume youâre aboard the Minikube ship, cruising on a local Kubernetes cluster. If not, make sure to anchor your ship with Minikube. And, of course, ensure your trusty companion kubectl is configured to communicate with your cluster. If youâre not already on this voyage, you can create a Kubernetes cluster using Minikube or other Kubernetes playgrounds. đď¸
Also, make sure your Kubernetes server is version 1.19 or later. You can check this by running:
kubectl version
Setting Sail: Create a Minikube Cluster âď¸
If you havenât already set up your cluster locally, letâs make it happen by running:
minikube start
Your cluster is now born and ready for action. đ˘
Raise the Drawbridge: Enable the Ingress Controller đ°
Now, letâs talk business. To enable the NGINX Ingress controller, execute the following command:
minikube addons enable ingress
Now, for the grand reveal, letâs check if the NGINX Ingress controller is up and running:
kubectl get pods -n ingress-nginx
It may take a minute or so, but when itâs ready, youâll see pods standing at attention, ready to serve your Ingress. âď¸
Prepare to Set Sail: Deploy a Hello, World App đ
Ahoy there! Itâs time to embark on an adventure by deploying your first app. First, create a Deployment for your web service:
kubectl create deployment web - image=gcr.io/google-samples/hello-app:1.0
Then, expose the Deployment to the world:
kubectl expose deployment web - type=NodePort - port=8080
To ensure your web service is available on a node port, run:
kubectl get service web
Youâll receive information about your service, including the NodePort. âľď¸
Now, you can visit your Service via NodePort by running:
minikube service web - url
Time to put your Captainâs hat on and navigate to the provided URL. If you run âcurl http://minikube-IP:NodePortâ, youâll see a cheerful âHello, world!â message. Youâre now navigating the Kubernetes waters with your trusty Minikube and NGINX Ingress Controller as your crew. âď¸
Creating a Map: Create an Ingress đşď¸
Next on the list is creating a map, your Ingress, which will route traffic to your Service via a special domain, hello-world.info.
Hereâs an example manifest defining your Ingress, create example-ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: hello-world.info
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 8080
You can create the Ingress object by running:
kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml
Now, youâll see your Ingress manifest has been created. đ
Before you can set sail, make sure your IP address is ready:
kubectl get ingress
After a couple of minutes, an IPv4 address will appear in the ADDRESS column. đ´ââ ď¸
Testing the Waters: Testing Your Ingress đ
Finally, you can test your Ingress. Access the first version of your Hello World app using:
curl - resolve "hello-world.info:80:$(minikube ip)" -i http://hello-world.info
You should see a familiar âHello, world!â message.
Also you can check your browser:
Now that youâve successfully set up your first Ingress route and sailed the Kubernetes seas, letâs dive into more exciting adventures. Weâll show you how to explore multiple versions of your Hello World app using the same Ingress configuration. đď¸
Prepare for the Voyage: Add Entries to Your Hosts File
Before you set sail to explore different versions of your Hello World app, letâs make your journey even smoother. You can add entries to your computerâs /etc/hosts file to map domain names to Minikubeâs IP address. This will allow your web browser to easily access the Ingress routes. Hereâs how:
Optionally, find the external IP address reported by Minikube using:
minikube ip
Once you have the IP address, add a line similar to the following to the bottom of your computerâs /etc/hosts file. Please note that youâll need administrator access to edit this file:
minikubeip hello-world.info
Be sure to replace minikubeip with the IP address you obtained from Minikube. This step ensures that your web browser knows where to find the Ingress routes.
Create a Second Deployment: Welcome, Web2!
Our next adventure involves deploying another version of your Hello World app. Letâs create a new Deployment for web2 with version 2.0:
kubectl create deployment web2 - image=gcr.io/google-samples/hello-app:2.0
Youâll see confirmation that your web2 Deployment has been created. This is where the fun begins! đ
Now, expose your newly created web2 Deployment:
kubectl expose deployment web2 - port=8080 - type=NodePort
Youâll receive confirmation that your web2 service has been exposed, and youâre all set to explore the next version of the Hello World app. đ
Edit the Existing Ingress: Chart Your Course to Web2
The existing Ingress has served you well, but now itâs time to extend its capabilities. Edit the âexample-ingress.yamlâ manifest and add the following lines at the end
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: hello-world.info
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 8080
- path: /v2
pathType: Prefix
backend:
service:
name: web2
port:
number: 8080
With these lines, youâre telling Ingress to route traffic to /v2 to your new web2 service. Once youâve made the changes, apply them:
kubectl apply -f example-ingress.yaml
Youâll receive confirmation that your Ingress has been configured, and youâre ready for the next leg of your journey. đ
Test Your Ingress: The Grand Finale
Itâs time to put your newfound Ingress configuration to the test. Access the first version of the Hello World app using:
curl - resolve "hello-world.info:80:$(minikube ip)" -i http://hello-world.info
Youâll be greeted by the familiar âHello, world!â message, proudly displaying âVersion: 1.0.0â and the hostname of your web service.
But wait, thereâs more! You can also explore the second version of the Hello World app:
curl - resolve "hello-world.info:80:$(minikube ip)" -i http://hello-world.info/v2
This time, youâll be welcomed by a cheerful âHello, world!â message from the second version, proudly declaring âVersion: 2.0.0â and displaying the hostname of your web2 service. đ
And remember, if you completed the optional step to update your /etc/hosts file, you can also visit hello-world.info and hello-world.info/v2 from your web browser, making your exploration even more delightful. đ
With your Ingress configuration set up, youâre now the captain of your Kubernetes ship, charting courses to different versions of your Hello World app with ease. Your Kubernetes adventure has just become a treasure trove of possibilities, and youâre sailing towards exciting horizons. đ