How to use Minikube for Kubernetes local development

Updated: January 30, 2024 By: Guest Contributor Post a comment

Introduction

Developing for Kubernetes can be a complex task, as it often requires standing up a Kubernetes cluster. However, Minikube makes this process much more manageable by simulating a Kubernetes cluster on a local machine. In this tutorial, you’ll learn the basics of setting up Minikube and using it to create a local Kubernetes development environment. You’ll learn how to deploy a simple application, expose services, and explore advanced features for your local Kubernetes development.

Prerequisites

Before we get started, you will need to install:

  • Minikube
  • kubectl
  • VirtualBox or Docker (depending on your virtualization preference)

Installation

First, ensure you have Minikube installed. Installation guidelines can be found at the official Minikube documentation. Typically, installation involves running a simple command, like so:

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/

Next, verify your installation with:

minikube version

You will also need kubectl, which is the command-line tool allowing you to run commands against Kubernetes clusters. Verify kubectl is installed by running:

kubectl version --client

Starting Minikube

To start your first Minikube cluster, simply use the following command:

minikube start

After a brief moment, Minikube will start a virtual machine (VM) and a Kubernetes cluster will be running inside it.

Basic Kubectl Commands

With your cluster running, let’s run through some basic kubectl commands to interact with it.

Checking Cluster Info

kubectl cluster-info

You should see output like the following:

Kubernetes master is running at https://127.0.0.1:32769
KubeDNS is running at https://127.0.0.1:32769/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

Viewing Nodes

kubectl get nodes

This will show you all the nodes in your cluster:

NAME       STATUS   ROLES    AGE    VERSION
test       Ready    master   10m    v1.14.2

Creating Deployments

Deployments on Kubernetes create and manage Pods. Here’s how you create a deployment running NGINX.

kubectl create deployment nginx --image=nginx

Check the deployment:

kubectl get deployments

Exposing Services

Services in Kubernetes are an abstraction which define a logical set of Pods and a policy by which to access them. We can expose our NGINX deployment as a service:

kubectl expose deployment nginx --type=NodePort --port=80

Use minikube service to open the service in your browser:

minikube service nginx

Persistent Volumes

Persistent Volumes are a method for managing storage in Kubernetes. Here is how you might set up a Persistent Volume Claim (PVC), and then attach it to a Pod.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-example
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Apply this YAML with kubectl apply -f pvc.yaml, and then you can use it in a Pod’s volume settings.

Advanced Minikube Features

Minikube supports a set of Add-ons which can be enabled or disabled in the environment. For example, you may want to enable the Metrics Server add-on with minikube addons enable metrics-server.

Tunneling

Sometimes you want to expose your local Kubernetes services to the public internet. You can do this by executing:

minikube tunnel

Dashboard

To access the Kubernetes dashboard, a web-based Kubernetes user interface, you can run:

minikube dashboard

This command will open the dashboard in your default web browser.

Cleaning Up

When you’re done, you can stop Minikube:

minikube stop

To delete your Minikube cluster:

minikube delete

Conclusion

This tutorial has provided a glance into Minikube capabilities, emphasizing hands-on guides for both beginners and experienced users alike. The importance of such a tool in local Kubernetes development is significant, as it offers an efficient flow in coding, deploying, and testing phases.