Skip to content

Kubernetes Local Development with Minikube: A Step-by-Step Guide

Sure, here's a step-by-step guide for running Kubernetes locally using Minikube:

Prerequisites

A computer running macOS, Linux, or Windows VirtualBox or another virtualization provider installed (required for Minikube)

Step 1: Install Minikube

Minikube is a tool that makes it easy to run Kubernetes on your local machine. To install Minikube, follow these steps:

Download the latest version of Minikube from the official website: https://minikube.sigs.k8s.io/docs/start/ Install Minikube using your operating system's package manager or by following the instructions provided on the website.

Step 2: Start Minikube

Once you have installed Minikube, you can start it by running the following command:

sh
minikube start

This will start a local Kubernetes cluster using VirtualBox as the virtualization provider. Minikube will download the necessary Kubernetes components and create a virtual machine to run them.

Step 3: Define your application's Kubernetes resources

Next, you will need to define your application's Kubernetes resources in YAML files. These files typically define deployments, services, and any other resources required by your application.

Here's an example of a simple deployment that runs an NGINX web server:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

You can save this YAML file to a file named nginx.yaml on your local machine.

Step 4: Apply your Kubernetes resources

Once you have defined your application's Kubernetes resources in YAML files, you can apply them to your local Kubernetes cluster using the following command:

sh
kubectl apply -f nginx.yaml

This will create the necessary deployments, services, and other resources in your local Kubernetes cluster.

Step 5: Monitor your application

You can monitor the status of your application using the following command:

sh
kubectl get pods

This will show you the status of your NGINX deployment, including the number of running and ready pods.

Step 6: Access your application

To access your application, you will need to expose it using a service. You can create a service using the following YAML file:

yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: NodePort

You can save this YAML file to a file named nginx-service.yaml on your local machine.

To create the service, run the following command:

sh
kubectl apply -f nginx-service.yaml

This will create a service that exposes your NGINX deployment on a randomly-assigned NodePort.

To access your application, you can use the IP address of your Minikube cluster and the assigned NodePort. You can find the IP address and NodePort using the following commands:

sh
minikube ip
kubectl get services

Step 7: Make changes to your application

To make changes to your application, simply edit your YAML files and reapply them using kubectl apply -f. Kubernetes will automatically update your resources with the changes you've made.

Step 8: Delete your Kubernetes resources

When you're finished testing your application, you can delete your Kubernetes resources using the following command:

sh
kubectl delete -f nginx.yaml
kubectl delete -f nginx-service.yaml

This will delete the NGINX deployment and service from your local Kubernetes cluster.

Conclusion

Running Kubernetes locally using Minikube is a great way to test your applications before deploying them to a production environment. With just a few simple commands, you can create a local Kubernetes cluster, define your application's Kubernetes resources, and deploy and test your application. By following this guide, you should now have a basic understanding of how to run Kubernetes locally using Minikube.