How to Setup a Kubernetes Cluster with Minikube
Tags: kubernetes, cluster, devops
Published: 2024-02-06 05:58:08

For users that are interested in trying out Kubernetes or developing with it on a regular basis, Minikube runs a single-node Kubernetes cluster in a VM on their laptop.

With Minikube, new Kubernetes developers can master the basics, try out new ideas, and become familiar with the Kubernetes environment.

Rather than using a cloud-based Kubernetes cluster, Minikube allows you to run Kubernetes on local machines. This is useful for testing and development purposes, as well as for learning Kubernetes concepts and features.

In this tutorial, you will learn how to set up a Kubernetes cluster on a local machine.

## Prerequisites

Although this tutorial will explain each process in detail, it is recommended that you have a basic understanding of Kubernetes and its concepts. 

You will also need to have the following installed on your local machine:

- [Docker](https://docs.docker.com/engine/install/): Docker acts as the container runtime and provides the necessary infrastructure for Minikube to create and manage containers within the local Kubernetes cluster.
- [Kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl): This is the command-line tool used to interact with the local Kubernetes cluster created by Minikube. It allows you to run commands against Kubernetes clusters to deploy applications, inspect and manage cluster resources, and view logs.
- Hardware requirements: Minikube requires a minimum of 2 CPUs, 2GB of RAM, and 20GB of disk space to run a single-node Kubernetes cluster.

### Step 1 — Installing Minikube

The OS used in this tutorial is Ubuntu 20.04, but you can install Minikube on other operating systems as well.

Installing Minikube requires downloading the Minikube binary and installing it on your local system.

Minikube has binary versions for many operating systems and architectures. You can find the binary for any system type by checking the [Minikube releases page](https://minikube.sigs.k8s.io/docs/start/).

To find out your system architecture type, run the following command:

```bash
uname -i
```

To download the Minikube binary for a `x86_64` architecture Ubuntu system, run the following command:

```bash
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb

sudo dpkg -i minikube_latest_amd64.deb
```

The output will look like this:

```bash
Selecting previously unselected package minikube.
(Reading database ... 65066 files and directories currently installed.)
Preparing to unpack minikube_latest_amd64.deb ...
Unpacking minikube (1.32.0-0) ...
Setting up minikube (1.32.0-0) ...
```

Run the following command to verify that Minikube has been installed:

```bash
minikube version
```

### Step 2 — Starting Minikube

When you start Minikube, it creates a single-node Kubernetes cluster inside a VM on your local machine.

In this case, Minikube will use the Docker container runtime to create and manage containers within the local Kubernetes cluster.

To start Minikube, run the following command:

```bash
minikube start
```

The output will look like this:

```bash
😄  minikube v1.32.0 on Ubuntu 22.04
✨  Automatically selected the docker driver. Other choices: none, ssh
📌  Using Docker driver with root privileges
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
💾  Downloading Kubernetes v1.28.3 preload ...
    > preloaded-images-k8s-v18-v1...:  403.35 MiB / 403.35 MiB  100.00% 138.82
    > gcr.io/k8s-minikube/kicbase...:  453.90 MiB / 453.90 MiB  100.00% 140.38
🔥  Creating docker container (CPUs=2, Memory=7900MB) ...
🐳  Preparing Kubernetes v1.28.3 on Docker 24.0.7 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying Kubernetes components...
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
```

To check the status of the Minikube cluster, run the following command:

```bash
minikube status
```

This will show you the status of the Minikube cluster:

```bash
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
```

This status simply shows that the Minikube cluster is running and the Kubernetes components are up and running.

### Step 3 — Interact with the Kubernetes Cluster

At this point, Minikube has started a single-node Kubernetes cluster inside a VM on your local machine.

It also configured the Kubernetes command-line tool, `kubectl`, to interact with the local Kubernetes cluster.

To view the nodes in the cluster, run the following command:

```bash
kubectl get nodes
```

You should see only one node - the Minikube node:

```bash
NAME       STATUS   ROLES           AGE   VERSION
minikube   Ready    control-plane   77s   v1.28.3
```

### Step 4 — Deploy an Application

Now that you have a running Kubernetes cluster, you can deploy an application to it.

This tutorial will deploy a Kubernetes sample application called `hello-minikube`.

This application is a simple web server that serves a static web page containing the text "Hello, Minikube!".

To deploy the `hello-minikube` application, run the following command:

```bash
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
```

The command will create a deployment called `hello-minikube` and use the `k8s.gcr.io/echoserver:1.10` image to create the pods.

### Step 5 — Expose the Deployment

Exposing a deployment means making it accessible from outside the Kubernetes cluster.

To expose the `hello-minikube` application, run the following command:

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

This command creates a service of type `NodePort` that exposes the `hello-minikube` deployment on a port accessible from outside the cluster.

### Step 6 — Access the Application

To access the `hello-minikube` application, you need to find the URL to access the service.

To find the URL, run the following command:

```bash
minikube service hello-minikube --url
```

The output will look like this:

```bash
http://<ip>:<port>
```

You can access the application by visiting the URL in your web browser.

You can also use the `curl` command to access the application from the terminal.

```bash
curl http://<ip>:<port>
```

### Step 7 — Stopping Minikube

When you are done working with the Minikube cluster, you can stop it by running the following command:

```bash
minikube delete
```

The output will look like this:

```bash
🔥  Deleting "minikube" in docker ...
🔥  Deleting container "minikube" ...
🔥  Removing /home/ubuntu/.minikube/machines/minikube ...
💀  Removed all traces of the "minikube" cluster.
```

This command will stop the Minikube cluster and remove all traces of it from your local machine.

## Conclusion

In this tutorial, you learned how to set up a Kubernetes cluster on your computer with Minikube.

You also learned how to deploy an application to the cluster and access it from outside the cluster.

Your journey with Kubernetes has just begun. You are now ready to explore more advanced Kubernetes and Minikube features.