Kubernetes Workloads Practice Lab - neerajk555/Kubernetes GitHub Wiki

Kubernetes Workloads Practice Lab

This lab provides a step-by-step guide to deploy and manage all major Kubernetes workload controllers:

  • ReplicaSet
  • Deployment
  • DaemonSet
  • StatefulSet
  • Job
  • CronJob

Each step includes what it does, YAML file, commands, and verification.


Pre-requisites

  • A running Kubernetes cluster (Minikube, Kind, or any K8s cluster)
  • kubectl installed and configured
  • Optional: Create a separate namespace for the lab to keep resources organized
kubectl create namespace k8s-lab
kubectl config set-context --current --namespace=k8s-lab

What this does:

Creates an isolated namespace k8s-lab for this lab so all resources are grouped together.


Step 1: Deploy a ReplicaSet

A ReplicaSet ensures a specified number of identical pods are always running.

replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: lab-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: rs-nginx
  template:
    metadata:
      labels:
        app: rs-nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25
          ports:
            - containerPort: 80

Commands

kubectl apply -f replicaset.yaml
kubectl get rs
kubectl get pods

What this does:

  • Creates a ReplicaSet named lab-replicaset which ensures 3 nginx pods are always running.

Verification

  • Check that 3 pods are running with the label app=rs-nginx.

Step 2: Deploy a Deployment

A Deployment manages ReplicaSets and provides rolling updates and rollback features.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lab-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: dep-nginx
  template:
    metadata:
      labels:
        app: dep-nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25
          ports:
            - containerPort: 80

Commands

kubectl apply -f deployment.yaml
kubectl get deployments
kubectl rollout status deployment/lab-deployment
kubectl get pods

What this does:

  • Creates a Deployment named lab-deployment with 3 nginx pods.

  • Automatically manages a ReplicaSet and allows rolling updates.

Verification

  • Ensure Deployment and pods are running, and rollout status shows successful deployment.

Step 3: Deploy a DaemonSet

A DaemonSet ensures that one pod runs on every node (or selected nodes).

daemonset.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: lab-daemonset
spec:
  selector:
    matchLabels:
      app: ds-nginx
  template:
    metadata:
      labels:
        app: ds-nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25
          ports:
            - containerPort: 80

Commands

kubectl apply -f daemonset.yaml
kubectl get ds
kubectl get pods -o wide

What this does:

  • Ensures one nginx pod runs on each node in the cluster.
  • Automatically adds/removes pods when nodes join or leave the cluster.

Verification

  • kubectl get pods -o wide will show one pod per node.

Step 4: Deploy a StatefulSet

A StatefulSet manages stateful applications with stable pod names and persistent storage.

First, create a headless service:

statefulset-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: lab-sts-service
spec:
  clusterIP: None
  selector:
    app: sts-nginx
  ports:
    - port: 80
      name: http

Then, create the StatefulSet:

statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: lab-statefulset
spec:
  serviceName: "lab-sts-service"
  replicas: 2
  selector:
    matchLabels:
      app: sts-nginx
  template:
    metadata:
      labels:
        app: sts-nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25
          ports:
            - containerPort: 80
  volumeClaimTemplates:
    - metadata:
        name: sts-storage
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

Commands

kubectl apply -f statefulset-svc.yaml
kubectl apply -f statefulset.yaml
kubectl get sts
kubectl get pods

What this does:

  • Creates a headless service for network identity.
  • Deploys 2 nginx pods with stable names (lab-statefulset-0, lab-statefulset-1).
  • Automatically provisions PersistentVolumeClaims for each pod.

Verification

  • Check pods are created in order and PVCs are bound:

kubectl get pvc

Step 5: Run a Job

A Job runs a task to completion.

job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: lab-job
spec:
  template:
    spec:
      containers:
        - name: busybox
          image: busybox
          command: ["echo", "Job completed successfully!"]
      restartPolicy: Never
  backoffLimit: 2

Commands

kubectl apply -f job.yaml
kubectl get jobs
kubectl get pods
kubectl logs <job-pod-name>

What this does:

  • Runs a one-time pod that prints a message and then exits.
  • Job ensures completion even if the pod fails (up to backoffLimit).

Verification

  • Logs should show Job completed successfully!

Step 6: Run a CronJob

A CronJob runs Jobs on a schedule like Linux cron.

cronjob.yaml

apiVersion: batch/v1
kind: CronJob
metadata:
  name: lab-cronjob
spec:
  schedule: "*/1 * * * *"  # every 1 minute
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: busybox
              image: busybox
              command: ["echo", "Hello from CronJob"]
          restartPolicy: OnFailure

Commands

kubectl apply -f cronjob.yaml
kubectl get cronjobs
kubectl get jobs
kubectl get pods

What this does:

  • Schedules a job to run every minute.

  • Automatically creates Jobs and Pods at the defined interval.

Verification

  • Check logs of created pods:

kubectl logs <cronjob-pod-name>


Cleanup

kubectl delete -f cronjob.yaml
kubectl delete -f job.yaml
kubectl delete -f statefulset.yaml
kubectl delete -f statefulset-svc.yaml
kubectl delete -f daemonset.yaml
kubectl delete -f deployment.yaml
kubectl delete -f replicaset.yaml
kubectl delete namespace k8s-lab

What this does:

  • Deletes all lab resources and cleans up the namespace.
  • This completes the Kubernetes Workload Controllers Lab with detailed step explanations.