← Back to Sessions

Session 1: Kubernetes Deployments

Learn Deployments, Scaling & ReplicaSets

Hands-On Lab Guide

Lab Overview

Application Architecture

In this lab, we'll deploy a full-stack MERN application to Kubernetes. The application consists of three microservices:

  • Frontend: React application (Port 80)
  • Backend: Node.js/Express API (Port 5000)
  • Database: MongoDB (Port 27017)

Initial Setup

1. Verify Cluster Access

First, verify you can access the Kubernetes cluster:

Check nodes
kubectl get nodes
Expected Output:
NAME              STATUS   ROLES           AGE   VERSION
ip-10-1-1-10      Ready    control-plane   1d    v1.28.0
ip-10-1-1-20      Ready    worker          1d    v1.28.0
Check all pods
kubectl get pods -A

2. Clone the Repository

Clone the demo application repository:

Clone repository
git clone git@github.com:sasidatta/docker_demo.git
cd docker_demo/k8s
💡 Tip: If you need to set up SSH keys for GitHub, generate a key with ssh-keygen and add it to your GitHub account.

Namespaces

Understanding Namespaces

Namespaces provide a virtual cluster within a physical cluster. They're useful for:

  • Isolating resources between teams or projects
  • Applying resource quotas and policies
  • Organizing resources logically
⚠️ Important: Namespaces are a virtual construct - pods from different namespaces still share the same network space by default!

Create Your Namespace

Create namespace
kubectl create namespace <your-name>
Set default namespace
kubectl config set-context --current --namespace=<your-name>
💡 Pro Tip: Setting the default namespace saves you from typing --namespace with every command!
Verify namespace
kubectl get namespaces

Deployments

What is a Deployment?

A Deployment is a higher-level abstraction that manages Pods through ReplicaSets. It provides:

  • Self-healing: Automatically restarts failed pods
  • Scaling: Easy horizontal scaling of replicas
  • Rolling updates: Zero-downtime deployments
  • Rollback: Revert to previous versions

Deploy Frontend

Apply frontend deployment
kubectl apply -f frontend.yaml
Check deployment status
kubectl get deployments
Expected Output:
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
frontend   1/1     1            1           30s

Deploy Backend

Apply backend deployment
kubectl apply -f backend.yaml

Scaling Deployments

Scale your deployment dynamically:

Scale to 2 replicas
kubectl scale deployment backend --replicas=2
Verify scaling
kubectl get pods
Expected Output:
NAME                        READY   STATUS    RESTARTS   AGE
backend-6b5f7b8889-abc12    1/1     Running   0          1m
backend-6b5f7b8889-def34    1/1     Running   0          10s

Pods

Understanding Pods

A Pod is the smallest deployable unit in Kubernetes. Key concepts:

  • Pods hold one or more containers (typically one)
  • Containers in a pod share network and storage
  • Pods are ephemeral - they can be created and destroyed
  • Each pod gets its own IP address

Declarative vs Imperative

Declarative (YAML manifest)

Create pod from YAML
kubectl apply -f busybox.yaml

Imperative (Command line)

Create pod imperatively
kubectl run nginx --image=nginx:latest --restart=Never

Pod Lifecycle & Restart Policy

Check restart policy
kubectl get pod busybox-pod -o yaml | grep -i restart
Output:
restartPolicy: Always
restartCount: 1
Restart Policies:
  • Always - Always restart the container (default)
  • OnFailure - Restart only if container exits with error
  • Never - Never restart the container

View Pod Details

Get pods with details
kubectl get pods -o wide
Output shows: Pod name, status, IP address, node placement
NAME           READY   STATUS    IP            NODE
frontend-abc   1/1     Running   10.244.1.30   ip-10-1-1-20
Delete pod
kubectl delete pod busybox-pod nginx

Pod vs Deployment

Feature Pod Deployment
Self-healing ❌ No ✅ Yes
Scaling ❌ Manual ✅ Automatic
Rolling updates ❌ No ✅ Yes
Use case Testing, debugging Production workloads

ReplicaSets

Understanding ReplicaSets

ReplicaSets ensure a specified number of pod replicas are running at all times. They:

  • Are managed by Deployments (you rarely create them directly)
  • Use label selectors to identify pods
  • Maintain the desired replica count
Relationship: Deployment → ReplicaSet → Pods

Inspect ReplicaSets

List ReplicaSets
kubectl get rs
Describe ReplicaSet
kubectl describe replicaset frontend-6b5f7b8889
💡 Labels: ReplicaSets use labels like app=frontend to identify which pods they manage.

Debugging & Troubleshooting

Common kubectl Commands

Get pod logs
kubectl logs <pod-name>
Describe pod (detailed info)
kubectl describe pod <pod-name>
Execute command in pod
kubectl exec <pod-name> -- /bin/sh
Interactive shell in pod
kubectl exec -it <pod-name> -- /bin/sh
⚠️ Note: Some containers don't have /bin/bash, use /bin/sh instead!

Container Runtime (crictl)

On worker nodes, use crictl instead of docker:

List containers
crictl ps
Stop container
crictl stop <container-id>
💡 Self-Healing: When you stop a container, Kubernetes automatically restarts it based on the restart policy!

Common Issues

ImagePullBackOff

Cause: Cannot pull container image

Solution: Check image name, registry access, and credentials

CrashLoopBackOff

Cause: Container keeps crashing

Solution: Check logs with kubectl logs

Pending Pods

Cause: Cannot schedule pod (resources, node selector, etc.)

Solution: Use kubectl describe pod to see events

Best Practices

Security & Network Policies

By default, all pods can communicate with each other. Use Network Policies to restrict traffic:

  • Implement least-privilege access
  • Restrict ingress/egress traffic
  • Use namespace isolation
  • Apply pod security standards
⚠️ Production Tip: Never allow unrestricted pod-to-pod communication in production!

Resource Management

  • Always set resource requests and limits
  • Use horizontal pod autoscaling (HPA)
  • Monitor resource usage
  • Implement proper health checks (liveness/readiness probes)

Deployment Strategies

  • Rolling Update: Gradual replacement (default)
  • Recreate: Stop all, then start new (downtime)
  • Blue/Green: Full environment switch
  • Canary: Gradual traffic shift