Join our Discord Server
Karan Singh Karan is a highly experienced DevOps Engineer with over 13 years of experience in the IT industry. Throughout his career, he has developed a deep understanding of the principles of DevOps, including continuous integration and deployment, automated testing, and infrastructure as code.

Setting up a Local Kubernetes cluster using Minikube

4 min read

Kubernetes has emerged as a leading container orchestration platform, enabling organizations to manage and scale their applications efficiently. To experiment, develop, or test Kubernetes locally, Minikube comes to the rescue. Minikube is a lightweight tool that allows you to run a single-node Kubernetes cluster on your local machine. In this guide, we’ll explore the step-by-step process of setting up a local Kubernetes cluster using Minikube.

Step 1: Prerequisites

Before diving into the setup process, ensure that your local machine meets the following prerequisites:

  • Virtualization: Verify that virtualization is enabled on your machine. Minikube relies on virtualization technologies like VirtualBox, Hyper-V, or KVM to run the Kubernetes cluster.

  • Hypervisor: Choose a hypervisor that suits your environment. VirtualBox is a popular choice for most platforms, but alternatives like Hyper-V (Windows) or KVM (Linux) can also be used.

  • Minikube: Download and install the latest version of Minikube, which is available for various operating systems. Ensure that the Minikube binary is added to your system’s PATH.

Installing Minikube on your Macbook

I have Docker Desktop already installed on my system with Kubernetes enabled. Still I can install minikube side-by-side.

To install the latest minikube stable release on x86-64 macOS using binary download:

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64
sudo install minikube-darwin-amd64 /usr/local/bin/minikube
  • kubectl: Install the kubectl command-line tool, which is used to interact with the Kubernetes cluster. Ensure that kubectl is also added to your system’s PATH. Refer to the kubectl Cheat sheet.

Step 2: Starting Minikube

Once you’ve met the prerequisites, you’re ready to start the Minikube cluster. Follow these steps:

Open a terminal or command prompt and execute the following command to start Minikube:

minikube start
😄 minikube v1.30.1 on Darwin 13.2.1 (arm64)
✨ Using the docker driver based on existing profile
👍 Starting control plane node minikube in cluster minikube
🚜 Pulling base image ...
🤷 docker "minikube" container is missing, will recreate.
🔥 Creating docker container (CPUs=2, Memory=4000MB) ...
🐳 Preparing Kubernetes v1.26.3 on Docker 23.0.2 ...
🔗 Configuring bridge CNI (Container Networking Interface) ...
🔎 Verifying Kubernetes components...
▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟 Enabled addons: storage-provisioner, default-storageclass
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

This command initializes and starts the Minikube cluster using the default configuration. Minikube will automatically detect and use the appropriate hypervisor based on your environment.

Minikube will download the required ISO image and spin up a virtual machine to host the Kubernetes cluster. The first start may take some time as it needs to download the necessary components.

Once the cluster is up and running, you’ll see the confirmation message that Minikube is ready to use.

Step 3: Verifying the Cluster

To verify that the Minikube cluster is running correctly, execute the following command:

kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:51807
CoreDNS is running at https://127.0.0.1:51807/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

This command displays information about the Kubernetes cluster, including the cluster endpoint and the Kubernetes version. If everything is set up correctly, you should see the cluster details.

Step 4: Interacting with the Cluster

Now that the Minikube cluster is running, you can start interacting with it using kubectl. Here are a few essential kubectl commands to get you started:

List Nodes:

kubectl get nodes

This command displays information about the nodes in the cluster. In our case, it will show a single node running in the Minikube cluster.

Deployments:

kubectl get deployments

Use this command to list all the deployments in the cluster. Initially, there will be no deployments.

Pods:

kubectl get pods

This command lists all the pods running in the cluster. Since we have just started the cluster, no pods will be present initially.

Step 5: Running Applications

Now that the cluster is up and running, you can deploy applications and services. Let’s start by deploying a sample application:

Create a Deployment:

kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4

This command creates a deployment named “hello-minikube” using the “echoserver” image.

Expose the Deployment:

kubectl expose deployment hello-minikube --type=NodePort --port=8080

This command creates a service to expose the deployment using a NodePort type, which makes it accessible from outside the cluster.

Note:

If you wish to use Load Balancer instead of NodePort, follow the below steps:

To access a LoadBalancer deployment, use the “minikube tunnel” command. Here is an example deployment:

kubectl create deployment balanced --image=kicbase/echo-server:1.0
kubectl expose deployment balanced --type=LoadBalancer --port=8080

In another window, start the tunnel to create a routable IP for the ‘balanced’ deployment:

minikube tunnel

To find the routable IP, run this command and examine the EXTERNAL-IP column:

kubectl get services balanced

Your deployment is now available at <EXTERNAL-IP>:8080

Using Ingress Addon:

minikube addons enable ingress

The following example creates simple echo-server services and an Ingress object to route to these services.

kind: Pod
apiVersion: v1
metadata:
  name: foo-app
  labels:
    app: foo
spec:
  containers:
    - name: foo-app
      image: 'kicbase/echo-server:1.0'
---
kind: Service
apiVersion: v1
metadata:
  name: foo-service
spec:
  selector:
    app: foo
  ports:
    - port: 8080
---
kind: Pod
apiVersion: v1
metadata:
  name: bar-app
  labels:
    app: bar
spec:
  containers:
    - name: bar-app
      image: 'kicbase/echo-server:1.0'
---
kind: Service
apiVersion: v1
metadata:
  name: bar-service
spec:
  selector:
    app: bar
  ports:
    - port: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
    - http:
        paths:
          - pathType: Prefix
            path: /foo
            backend:
              service:
                name: foo-service
                port:
                  number: 8080
          - pathType: Prefix
            path: /bar
            backend:
              service:
                name: bar-service
                port:
                  number: 8080
---

Apply the contents

kubectl apply -f https://storage.googleapis.com/minikube-site-examples/ingress-example.yaml

Wait for ingress address

kubectl get ingress
NAME              CLASS   HOSTS   ADDRESS          PORTS   AGE
example-ingress   nginx   *       <your_ip_here>   80      5m45s

Note for Docker Desktop Users:


To get ingress to work you’ll need to open a new terminal window and run minikube tunnel and in the following step use 127.0.0.1 in place of <ip_from_above>.

Now verify that the ingress works

$ curl <ip_from_above>/foo
Request served by foo-app
...
$ curl <ip_from_above>/bar
Request served by bar-app
...

Access the Application:

minikube service hello-minikube

This command opens a browser window or prints the URL that can be used to access the deployed application. Click the URL or copy it into a web browser to see the running application.

Step 6: Cleaning up

Once you are done experimenting with the Minikube cluster, you can clean up the resources to free up system resources. Execute the following commands:

Delete the service:

kubectl delete service hello-minikube

Delete the deployment:

kubectl delete deployment hello-minikube

Stop the Minikube cluster:

minikube stop

This command stops the running Minikube cluster, but it keeps the virtual machine intact.

Delete the Minikube cluster:

minikube delete

This command deletes the Minikube cluster and removes the associated virtual machine.

Conclusion

Setting up a local Kubernetes cluster using Minikube is a straightforward process that allows you to experiment with Kubernetes concepts and deploy applications locally. Minikube provides a convenient and lightweight environment to develop and test your Kubernetes deployments. By following the steps outlined in this guide, you can quickly get started with Kubernetes on your local machine using Minikube and begin exploring the vast possibilities of container orchestration. Happy Kubernetting!


 
                        

Have Queries? Join https://launchpass.com/collabnix

Karan Singh Karan is a highly experienced DevOps Engineer with over 13 years of experience in the IT industry. Throughout his career, he has developed a deep understanding of the principles of DevOps, including continuous integration and deployment, automated testing, and infrastructure as code.
Join our Discord Server
Index