πŸš€ Run Kubernetes Locally: Deploy Your App with Kind in Minutes!

DockerCloudNativeDevopsKindKubernetes

Wednesday, January 22, 2025

k8s

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:

  1. One control plane node – The brain of the cluster, responsible for managing the cluster state.
  2. 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
deploy

πŸ“‘ 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! πŸŽ‰