π Run Kubernetes Locally: Deploy Your App with Kind in Minutes!
Wednesday, January 22, 2025

If you've ever wanted to spin up a lightweight Kubernetes (K8s) cluster on your local machine without the hassle, Kind (Kubernetes in Docker) is your best friend. This guide will take you from zero to a fully running K8s cluster in no time! Ready? Letβs dive in! π₯
π Step 1: Install Docker
First things first, Kubernetes needs a container runtime, and Docker is a solid choice. Install it using the following commands:
# Update package lists and install dependencies sudo apt-get update sudo apt-get install ca-certificates curl # Add Docker's official GPG key sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc # Add Docker repository echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # Install Docker sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
π¦ Step 2: Install Kind
Now, letβs get Kind installed so we can create a K8s cluster within Docker.
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.24.0/kind-linux-amd64 chmod +x ./kind sudo mv ./kind /usr/local/bin/kind
π Step 3: Configure and Create Your Cluster
π§ Define the Kind Cluster Configuration
Create a file named kind-config.yml with the following content:
kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: - role: control-plane kubeadmConfigPatches: - | kind: InitConfiguration nodeRegistration: kubeletExtraArgs: node-labels: "ingress-ready=true" extraPortMappings: - containerPort: 80 hostPort: 80 protocol: TCP - containerPort: 443 hostPort: 443 protocol: TCP - role: worker - role: worker
π€ What Does This Configuration Do?
This configuration file defines a Kind Kubernetes Cluster with three nodes:
- One control plane node β The brain of the cluster, responsible for managing the cluster state.
- Two worker nodes β Where your applications will actually run.
Key Features:
- Ingress-Ready Control Plane:
The kubeadmConfigPatches section adds a label (ingress-ready=true) to the control-plane node, preparing it for an ingress controller. - Port Forwarding for HTTP and HTTPS:
The extraPortMappings section ensures that requests sent to port 80 (HTTP) and port 443 (HTTPS) on your local machine are forwarded to the control-plane node, making it easy to access applications running in the cluster.
This setup gives you a simple but effective Kubernetes cluster inside Docker, perfect for local development and testing.
ποΈ Create the Cluster
Run the following command to apply the configuration and spin up your cluster:
kind create cluster --config kind-config.yml

π‘ Step 4: Install kubectl
Now, install kubectl, the CLI tool for managing Kubernetes clusters:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo mv kubectl /usr/local/bin/ sudo chmod +x /usr/local/bin/kubectl
π Step 5: Deploy an Ingress Controller
To expose your applications, you'll need an Ingress Controller. Deploy one using:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/kind/deploy.yaml
π’ Step 6: Deploy Your Web App to Kubernetes
π Create the Web App Deployment File
Create a file named k8s_webapp.yml with the following content:
apiVersion: apps/v1 kind: Deployment metadata: name: web-app spec: replicas: 1 selector: matchLabels: app: web-app template: metadata: labels: app: web-app spec: containers: - name: web-app image: nginx:latest # Replace with your web app image ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: web-app-service spec: selector: app: web-app ports: - protocol: TCP port: 80 targetPort: 80 --- apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: web-app-ingress spec: rules: - host: web-app.local http: paths: - path: / pathType: Prefix backend: service: name: web-app-service port: number: 80
π Deploy Your App to the Cluster
kubectl apply -f k8s_webapp.yml
π Step 7: Access Your App
To access your app in the browser, map the hostname to localhost by adding the following line to /etc/hosts:
127.0.0.1 web-app.local
Now, open your browser and navigate to http://web-app.local to see your deployed app! π
π― Wrapping Up
Youβve successfully set up a Kubernetes cluster using Kind, deployed a web app, and exposed it with an Ingress Controller! π
This setup is perfect for local development and testing before moving to production. If youβre diving deeper into Kubernetes, you can now experiment with scaling, networking, and more!
Happy coding! π