Kubernetes ingress example with minikube

Using Minikube is a great way to experiment with Kubernetes locally, and it supports Ingress out of the box. Here’s a step-by-step example to set up Ingress with Minikube:

Step 1: Start Minikube

Ensure that Minikube is installed, and start a Minikube cluster:

minikube start

Step 2: Enable Ingress Addon

Enable the Ingress addon for Minikube:

minikube addons enable ingress

NOTE:
You can either enable Ingress Addon or deploy the latest ingress-controllers for Minikube as below.

kubectl create -f https://raw.githubusercontent.com/Kong/kubernetes-ingress-controller/v2.12.0/deploy/single/all-in-one-dbless.yaml

Step 3: Deploy A Sample Application

Create a simple deployment and service for a sample application. Save the following YAML as sample-deployment.yaml:

package main
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
      - name: sample-app
        image: nginx:alpine
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: sample-service
spec:
  selector:
    app: sample-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

 Apply the configuration:

kubectl apply -f sample-deployment.yaml

Step 4: Create an Ingress Resource

Save the following YAML as sample-ingress.yaml:

package main
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sample-ingress
spec:
  rules:
  - host: sample.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: sample-service
            port:
              number: 80

Note: Add sample.local to your /etc/hosts file to map it to Minikube’s IP address. You can get Minikube’s IP with minikube ip.

Apply the Ingress configuration:

kubectl apply -f sample-ingress.yaml

Step 5: Access the Application

Open your web browser and navigate to http://sample.local. You should see the default NGINX welcome page.

Step 6: Cleanup

When you’re done experimenting, clean up your Minikube environment:

minikube stop
minikube delete

This example demonstrates the basic setup of Ingress with Minikube. In a real-world scenario, you might want to use a more complex application and configure additional Ingress options. Additionally, make sure your local machine supports the .local domain resolution. If not, you can use the Minikube IP directly.

Leave a Reply

Your email address will not be published. Required fields are marked *