Introduction
Kubernetes has revolutionized container orchestration, making it easier to manage, scale, and deploy applications. If you're new to Kubernetes, Minikube is a great way to get started. In this guide, we will walk through the process of setting up Minikube and deploying a simple application using Kubernetes.
Understanding the Basics
What Are Containers?
Containers are lightweight, portable, and self-sufficient environments that include everything needed to run an application. Technologies like Docker have made containers popular.
What Are Pods?
In Kubernetes, a Pod is the smallest deployable unit. A Pod can contain one or more containers that share networking and storage resources.
What Is a Deployment?
A Deployment is a higher-level Kubernetes object that manages a group of Pods. It ensures automatic recovery, rolling updates, and scalability.
Setting Up Your Kubernetes Environment
Prerequisites
Before we start, make sure you have the following installed:
Windows with WSL2 (or a Linux-based system)
kubectl (Kubernetes command-line tool)
Minikube (a tool to run Kubernetes locally)
Installing kubectl
Refer to the Kubernetes documentation for installation steps. On Linux, use the following commands:
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
kubectl version --client
Installing Minikube
Follow the Minikube documentation for installation instructions. To start a Minikube cluster, use:
minikube start
Verify that Minikube is running:
minikube status
Deploying an Application
Creating a Pod
Create a configuration file for a simple Nginx pod:
nano nginx-pod.yaml
Paste the following YAML:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Deploy the pod:
kubectl apply -f nginx-pod.yaml
kubectl get pods
Exposing the Pod
To access the application inside the cluster:
kubectl port-forward pod/nginx 8080:80
Then visit http://localhost:8080
in your browser.
Transitioning to Deployments
Pods alone do not offer resilience. If a Pod crashes, it won’t restart automatically. This is where Deployments come in.
Creating a Deployment
Create a new file for the Deployment:
nano nginx-deployment.yaml
Paste the following YAML:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f nginx-deployment.yaml
kubectl get deployments
kubectl get pods
Testing Auto-Healing
Delete a pod manually:
kubectl delete pod <pod_name>
Run the following command on second terminal:
kubectl get pods -w
Conclusion
We started with a simple container, transitioned to Pods, and then leveraged Deployments for auto-healing and scalability. Kubernetes provides powerful features, and Minikube is an excellent tool for learning. Experiment with different configurations and explore Kubernetes further!