Deploying Your First Application on Kubernetes Using Minikube

Deploying Your First Application on Kubernetes Using Minikube

Kubernetes CNI Plugin workflow

Introduction

Kubernetes has become the de facto standard for container orchestration, allowing developers to efficiently deploy, manage, and scale applications. Minikube provides an easy way to set up a local Kubernetes cluster for testing and learning. In this blog, we will explore the step-by-step process of deploying your first application on Kubernetes using Minikube. We will cover the fundamental concepts of Kubernetes, the installation process, pod creation, deployments, and auto-scaling.


Understanding Kubernetes Architecture

Before diving into Minikube, it's crucial to understand how Kubernetes organizes and manages applications. Let's break it down:

1. Containers

Containers provide a lightweight and portable environment for applications to run consistently across various environments. They package code, dependencies, and runtime into a single unit.

2. Pods

A Pod is the smallest deployable unit in Kubernetes. It encapsulates one or more containers that share networking and storage.

3. Deployments

A Deployment ensures that a specified number of Pods are running at all times. It supports self-healing, rolling updates, and rollback capabilities.

4. ReplicaSets

ReplicaSets manage the desired number of identical pods to ensure high availability and fault tolerance.

5. Services

Services expose a set of pods to the network, ensuring stable access despite changes in pod IP addresses.


Prerequisites

To follow along, ensure you have the following installed:

  • Windows with WSL2 (Ubuntu) or a Linux system

  • kubectl (Kubernetes CLI)

  • Minikube (Local Kubernetes Cluster Manager)

Installing kubectl

kubectl is the command-line tool for interacting with Kubernetes clusters.

Install kubectl on Linux (x86-64):

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Verify Installation:

kubectl version --client

Installing Minikube

Minikube is a lightweight Kubernetes cluster that runs locally.

Install Minikube:

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Start Minikube:

minikube start

Check Minikube Status:

minikube status

Verify Nodes:

kubectl get nodes

Deploying Pods on Kubernetes

Pods are the basic building blocks in Kubernetes. Let's deploy an Nginx pod.

Creating a Pod Configuration File

nano nginx-pod.yaml

Add the following content:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80

Deploying the Pod

kubectl apply -f nginx-pod.yaml

Verify Pod Status

kubectl get pods

Check Logs and Debug

kubectl logs nginx
kubectl describe pod nginx

Upgrading to Deployments

Deployments allow for scalability and automatic recovery from failures.

Creating a Deployment

nano nginx-deployment.yaml

Add the following content:

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

Deploying the Application

kubectl apply -f nginx-deployment.yaml

Scaling the Deployment

Increase the replicas in nginx-deployment.yaml:

spec:
  replicas: 5

Apply the changes:

kubectl apply -f nginx-deployment.yaml

Checking Auto-Healing

Delete a pod and observe the auto-recovery:

kubectl delete pod <pod_name>

The system will automatically recreate the deleted pod.


Exposing Your Application

To access the application externally, expose it as a service.

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

Get the service details:

kubectl get svc

Find the Minikube IP and access the application:

minikube service nginx-deployment --url

Open the URL in a browser to verify the deployment.


Summary

  • Containers package applications with dependencies.

  • Pods are the smallest deployable unit in Kubernetes.

  • Deployments manage pods with self-healing and scaling.

  • Minikube allows running Kubernetes locally for development.

By following this guide, you have successfully deployed your first application on Kubernetes using Minikube! Now, experiment with rolling updates, resource limits, and monitoring for a production-ready setup.

Stay tuned for more Kubernetes tutorials!