Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Create a kind Cluster, Apply a Deployment and Service, and Reach the App with kubectl port-forward

Beginner40 minDocker · kind · kubectl

Course: DevOps · Chapter 15: Kubernetes Introduction · Lesson: 15.13 Lab: kind cluster, deploy, curl

Chapter 15 explains Pod, Deployment, Service and kubectl verbs; this lab runs them on your laptop.

Chala mitrano! Kubernetes shikayla cloud bill nako. kind ne laptop var cluster banvuya.

Suppose we are…

Suppose we are on an Instagram-style team moving from Docker Compose to Kubernetes. Before touching a cloud cluster that costs money, every developer needs a safe place to practise kubectl. kind runs a full Kubernetes cluster inside Docker on your laptop.

In this lab we create a cluster, deploy nginx with two replicas behind a Service, and reach it from the laptop with port-forward.

Goal of this lab

By the end you will have:

  • A running kind cluster named lab.
  • A Deployment with 2 ready Pods and a Service with endpoints.
  • A curl to localhost:8080 that returns the nginx page.

What you need

  • Docker running.
  • kind and kubectl installed (kind version, kubectl version --client).
  • 40 minutes.

Part 1: The cluster

  1. Create the cluster:

    kind create cluster --name lab
    kubectl cluster-info --context kind-lab
    kubectl get nodes
    

    What you should see: one node lab-control-plane with STATUS Ready.

Part 2: Deploy

  1. Save the Deployment and Service:

    web.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web
      labels: { app: web }
    spec:
      replicas: 2
      selector:
        matchLabels: { app: web }
      template:
        metadata:
          labels: { app: web }
        spec:
          containers:
            - name: web
              image: nginx:1.27
              ports:
                - containerPort: 80
              resources:
                requests: { cpu: 50m, memory: 64Mi }
                limits:   { cpu: 200m, memory: 128Mi }
              readinessProbe:
                httpGet: { path: /, port: 80 }
                periodSeconds: 5
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: web
    spec:
      selector: { app: web }
      ports:
        - port: 80
          targetPort: 80
    
  2. Apply it and watch the Pods start:

    kubectl apply -f web.yaml
    kubectl get pods -l app=web -w
    

    What you should see: two Pods going to Running with READY 1/1. Press Ctrl+C to stop watching.

  3. Look at the Service and its endpoints:

    kubectl get svc web
    kubectl get endpoints web
    

    What you should see: a ClusterIP and two Pod IPs on port 80 under endpoints.

Part 3: Reach it and read it

  1. Forward a laptop port to the Service (keep this terminal open):

    kubectl port-forward svc/web 8080:80
    
  2. In a second terminal:

    curl -s localhost:8080 | head -n 4
    

    What you should see: <title>Welcome to nginx!</title>

  3. Read events and logs:

    kubectl describe deploy web | tail -n 8
    kubectl logs -l app=web --tail 5
    

    What you should see: events about scaling the ReplicaSet, and nginx access-log lines from your curl.

  4. Kill one Pod and watch Kubernetes replace it:

    kubectl delete pod -l app=web --wait=false | head -n 1
    kubectl get pods -l app=web
    

    What you should see: a new Pod with a new name; the Deployment keeps 2 replicas.

Clean up

  1. Stop port-forward with Ctrl+C and delete the cluster:

    kind delete cluster --name lab
    kind get clusters
    

    What you should see: No kind clusters found.

Ravindra Bagale's Tip

Always write YAML and use kubectl apply -f. Commands like kubectl run are fine for a quick test, but YAML in Git is what a team can review.

Common mistakes

MistakeWhat happensFix
Service selector does not match Pod labelsendpoints is empty, curl failsMake selector equal to the template labels
ImagePullBackOffPods never startCheck the image name and tag with kubectl describe pod
Closing the port-forward terminalcurl gets connection refusedKeep it running in its own terminal
Deleting Pods to "restart" in productionShort outage if replicas=1Use kubectl rollout restart deploy/web

Self-check checklist

0 of 5 done

Try-at-home challenge

Scale to 4 replicas with kubectl scale deploy/web --replicas=4, then change it back by editing replicas in web.yaml and applying again. Which way would your team prefer, and why?

Samajla ka? Cluster on the laptop, YAML in Git, kubectl to read. Aata pudhe jaauya rolling updates.