Hello world on Minikube Kubernetes cluster
In this article , we are going to talk about minikube and we will deploy a sample hello world application on a local kubernetes cluster , provisioned with the help of Docker Desktop and minikube.
Minikube
- Minikube is a open source tool that allows you to run Kubernetes clusters locally on your machine.
- It runs a single-node Kubernetes cluster on your local machine, making it easy to experiment with Kubernetes without needing access to a cloud provider or a large infrastructure.
- It supports multiple virtualization technologies like VirtualBox, VMware, Hyper-V, and Docker.
- The concept of a “driver” in Minikube is essential for managing the underlying environment where the Kubernetes cluster runs.
Pre requisite for minikube
- 2 CPUs or more
- 2GB of free memory
- 20GB of free disk space
- Internet connection
- Container or virtual machine manager, such as: Docker, QEMU, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMware Fusion/Workstation
Refer this article to know more about Minikube Drivers.
In our case , we will be using Docker Desktop as a driver for installing a single cluster with minikube. when we use the Docker driver, Minikube will create Docker containers for each Kubernetes component, simplifying the setup and teardown process. This makes it easier for us to quickly spin up a Kubernetes cluster without needing to manually configure VMs or containers.
Let’s quickly jump on to how to run a minikube cluster and deploy a sample hello world application onto it.
Step 1: Installing docker desktop
Head on to this official page of docker for downloading the docker desktop for the desired OS https://docs.docker.com/desktop/. We will be using Windows OS.
- Download installer using the download button here https://docs.docker.com/desktop/install/windows-install/
- Docker Desktop Installer.exe will be downloaded in your system.
- Double click to and follow the instructions on the installation wizard to authorize the installer and proceed with the install.
- Once it is installed , you need to restart your system.
Step 2: Enable Kubernetes through docker desktop
We need to enable kubernetes through docker desktop. Open docker desktop app and head to gear icon on top right corner and look for Kubernetes section on the left side menu.
You will get and option to Enable Kubernetes. Check mark that option and wait for few seconds to enable Kubernetes and then click Apply and restart button in the bottom right corner.
Step 3: Adding kubernetes and yaml extenstion to VS code
We will be using VS code editor for our purpose. Installing kubernetes and yaml extension would make our lives easier. Go to Extensions section of your code editor and install these 2 extensions:
Step 4: Creating deployment yaml
We will pull a sample hello world image from Google Container Registry.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello #deployment name
spec:
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: gcr.io/google-samples/hello-app:1.0 #pulling hello world app image from gcr
# commenting out reqsource quota as we are not using it in this demo.
# resources:
# limits:
# memory: "128Mi"
# cpu: "500m"
# ports:
# - containerPort: <Port>
# using env variable to expose app outside the cluster through port 80
env:
- name: "PORT"
value: "80"
Once you save this deployment.yaml file , run the below command to deploy this manifest to our cluster.
$ kubectl apply -f ./deployment.yaml
You should get output like this :
deployment.apps/hello created
Step 5: Validating the deployment
Run the below command to check if deployment is ready.
$ kubectl get deployment
Once deployment is ready , it will show like this :
This deployment will create a pod which will have our app inside it in a container.
Run the below command to check the status of your pod:
$ kubectl get pods
Keep checking until your pod’s status shows as ‘Running’ state :
You can check the same in Docker Desktop as well:
Step 6: Port forwarding
This is additional step for giving our browser access to our hello world app.
What is Port forwarding?
In this context , port forwarding is a mechanism that allows us to access a Kubernetes resource (such as a pod) from our local machine. This is achieved by forwarding traffic from a local port (port 10001 in this case )on your machine to a port on a pod inside the Kubernetes cluster(port 80 in this case).
$ kubectl port-forward pods/hello-957dd97f6-4hl9t 10001:80 -n default
This command forwards the local port 10001 to the pod hello-957dd97f6-4hl9t
's port 80 in the default
namespace.
If we go ahead and hit localhost:10001 from our browser , we should be able to access our app from outside the cluster , i.e. through browser.
Bingo ! You have just deployed a sample hello world app on a local minikube cluster.