Skip to main content

Command Palette

Search for a command to run...

Day 49: Kubernetes Deployments - Application Management Fundamentals ๐Ÿš€

Updated
โ€ข10 min read
Day 49: Kubernetes Deployments - Application Management Fundamentals ๐Ÿš€

Welcome back! ๐Ÿ‘‹ Day 49 of the 100 Days Cloud DevOps Challenge, and today we're diving into Kubernetes Deployments! This is where container orchestration truly begins - managing application lifecycle at scale. Let's orchestrate! ๐ŸŽฏ

๐ŸŽฏ The Mission - Create Kubernetes Deployment

๐Ÿ“‹ TASK TICKET #DEV-8049 - Kubernetes Deployment Creation
Priority: HIGH | Type: K8s Application Management
Server: Jump Host | Cluster: Kubernetes

REQUIREMENTS:
1. Deployment Name: httpd
2. Application: httpd web server
3. Container Image: httpd:latest (tag required)
4. Tool: kubectl (pre-configured)

DEPLOYMENT SPECS:
- Resource Type: Deployment
- Replicas: Default (1)
- Image Pull Policy: Always
- Container Port: 80

VERIFICATION:
- Deployment created successfully
- Pods running
- Application accessible

This is Kubernetes application orchestration! ๐Ÿšข

๐Ÿค” Why Kubernetes Deployments?

Without Deployments:

  • โŒ Manual container management

  • โŒ No automatic recovery

  • โŒ Complex scaling operations

  • โŒ Risky updates (downtime)

  • โŒ No rollback capability

With Deployments:

  • โœ… Declarative application state

  • โœ… Self-healing (auto-restart)

  • โœ… Easy scaling (replicas)

  • โœ… Rolling updates (zero downtime)

  • โœ… Automatic rollback

Architecture:

Deployment (httpd)
    โ†“ Creates
ReplicaSet
    โ†“ Manages
Pods (httpd containers)
    โ†“ Runs
Application (Apache)

๐Ÿ› ๏ธ Complete Implementation

Step 1: Access and Verify Environment

# SSH to jump host (credentials provided by environment)
ssh user@jump_host

# Verify kubectl configured
kubectl version --short

Expected output:

Client Version: v1.28.0
Server Version: v1.28.0

Verify cluster access:

# Check cluster info
kubectl cluster-info

# List nodes
kubectl get nodes

Expected output:

NAME           STATUS   ROLES           AGE   VERSION
controlplane   Ready    control-plane   10d   v1.28.0
node01         Ready    <none>          10d   v1.28.0

Cluster accessible! โœ…

Step 2: Create Deployment (Imperative Method)

# Create deployment using kubectl command
kubectl create deployment httpd --image=httpd:latest

Expected output:

deployment.apps/httpd created

Command breakdown:

  • kubectl create deployment = Create deployment resource

  • httpd = Deployment name

  • --image=httpd:latest = Container image with tag

Deployment created! ๐ŸŽ‰

Step 3: Verify Deployment

# Check deployment status
kubectl get deployments

Expected output:

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
httpd   1/1     1            1           30s

Status indicators:

  • READY: 1/1 (1 replica running out of 1 desired)

  • UP-TO-DATE: 1 (replicas with latest config)

  • AVAILABLE: 1 (replicas ready to serve)

View detailed deployment info:

# Detailed view
kubectl get deployment httpd -o wide

Expected output:

NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   1/1     1            1           1m    httpd        httpd:latest   app=httpd

Describe deployment:

# Full deployment details
kubectl describe deployment httpd

Expected output:

Name:                   httpd
Namespace:              default
CreationTimestamp:      Wed, 25 Dec 2024 10:00:00 +0000
Labels:                 app=httpd
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=httpd
Replicas:               1 desired | 1 updated | 1 total | 1 available
StrategyType:           RollingUpdate
Pod Template:
  Labels:  app=httpd
  Containers:
   httpd:
    Image:        httpd:latest
    Port:         <none>
    Environment:  <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable

Step 4: Check Pods

# List pods created by deployment
kubectl get pods

Expected output:

NAME                     READY   STATUS    RESTARTS   AGE
httpd-7d6c9f8b9c-x7k2m   1/1     Running   0          1m

Pod status:

  • READY: 1/1 (container ready)

  • STATUS: Running (pod active)

  • RESTARTS: 0 (no crashes)

View pod details:

# Describe specific pod
kubectl describe pod httpd-7d6c9f8b9c-x7k2m

Check pod logs:

# View application logs
kubectl logs httpd-7d6c9f8b9c-x7k2m

Expected output:

AH00558: httpd: Could not reliably determine the server's fully qualified domain name
[Wed Dec 25 10:00:00.123456 2024] [mpm_event:notice] [pid 1:tid 140] AH00489: Apache/2.4.58 (Unix) configured
[Wed Dec 25 10:00:00.123456 2024] [core:notice] [pid 1:tid 140] AH00094: Command line: 'httpd -D FOREGROUND'

Apache running! โœ…

Step 5: Check ReplicaSet

# View ReplicaSet created by deployment
kubectl get replicasets

Expected output:

NAME               DESIRED   CURRENT   READY   AGE
httpd-7d6c9f8b9c   1         1         1       2m

ReplicaSet details:

  • DESIRED: 1 (pods wanted)

  • CURRENT: 1 (pods exist)

  • READY: 1 (pods ready)

Describe ReplicaSet:

kubectl describe replicaset httpd-7d6c9f8b9c

Step 6: Complete Verification

# Comprehensive check
echo "=== Deployment Verification ==="
echo "1. Deployment Status:"
kubectl get deployment httpd

echo -e "\n2. ReplicaSet Status:"
kubectl get rs -l app=httpd

echo -e "\n3. Pod Status:"
kubectl get pods -l app=httpd

echo -e "\n4. Deployment Details:"
kubectl describe deployment httpd | grep -A 5 "Image:"

Expected output:

=== Deployment Verification ===
1. Deployment Status:
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
httpd   1/1     1            1           3m

2. ReplicaSet Status:
NAME               DESIRED   CURRENT   READY   AGE
httpd-7d6c9f8b9c   1         1         1       3m

3. Pod Status:
NAME                     READY   STATUS    RESTARTS   AGE
httpd-7d6c9f8b9c-x7k2m   1/1     Running   0          3m

4. Deployment Details:
    Image:        httpd:latest
    Port:         <none>
    Host Port:    <none>

All checks passed! โœ…

๐Ÿ” Alternative: Declarative Method (YAML)

For production, use YAML manifests:

# Create deployment YAML
cat > httpd-deployment.yaml << 'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd
  labels:
    app: httpd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:latest
        ports:
        - containerPort: 80
EOF

# Apply deployment
kubectl apply -f httpd-deployment.yaml

YAML breakdown:

apiVersion: apps/v1          # API version for Deployments
kind: Deployment             # Resource type
metadata:
  name: httpd                # Deployment name
  labels:
    app: httpd               # Deployment label
spec:
  replicas: 1                # Number of pod copies
  selector:
    matchLabels:
      app: httpd             # Select pods with this label
  template:                  # Pod template
    metadata:
      labels:
        app: httpd           # Pod label (must match selector)
    spec:
      containers:
      - name: httpd          # Container name
        image: httpd:latest  # Container image with tag
        ports:
        - containerPort: 80  # Container port (for documentation)

Verify YAML deployment:

# Check deployment
kubectl get deployment httpd -o yaml

๐Ÿ’ก Understanding Kubernetes Deployments

Deployment Hierarchy

Deployment: httpd
โ””โ”€โ”€ Manages โ†’ ReplicaSet: httpd-7d6c9f8b9c
    โ””โ”€โ”€ Creates โ†’ Pod: httpd-7d6c9f8b9c-x7k2m
        โ””โ”€โ”€ Runs โ†’ Container: httpd (httpd:latest)

Flow:
1. You create: Deployment
2. Deployment creates: ReplicaSet
3. ReplicaSet creates: Pods
4. Pods run: Containers

Why This Hierarchy?

Deployment:

  • Manages updates and rollbacks

  • Declares desired state

  • Controls ReplicaSet versions

ReplicaSet:

  • Maintains pod count

  • Creates/deletes pods

  • Ensures availability

Pod:

  • Smallest deployable unit

  • Runs container(s)

  • Has unique IP

Self-Healing Demo

# Delete pod (watch it recreate)
kubectl delete pod httpd-7d6c9f8b9c-x7k2m

# Immediately check
kubectl get pods -w

Output:

NAME                     READY   STATUS        RESTARTS   AGE
httpd-7d6c9f8b9c-x7k2m   1/1     Terminating   0          5m
httpd-7d6c9f8b9c-n9p4q   0/1     Pending       0          0s
httpd-7d6c9f8b9c-n9p4q   0/1     ContainerCreating   0     1s
httpd-7d6c9f8b9c-n9p4q   1/1     Running       0          3s

New pod automatically created! That's self-healing! ๐Ÿ”„

๐ŸŽ“ Quick Interview Questions

Q: What's the difference between a Pod and a Deployment?

A: Pod is the smallest unit (runs containers), Deployment is a controller that manages Pods. Pods are ephemeral and not self-healing. Deployments provide declarative updates, scaling, rollback, and self-healing via ReplicaSets.

Q: Why do we need ReplicaSets if we have Deployments?

A: Deployments manage ReplicaSets to enable rolling updates. When you update a Deployment, it creates a new ReplicaSet with the new version while scaling down the old one. This enables zero-downtime deployments and easy rollbacks.

Q: What happens if you manually delete a Pod in a Deployment?

A: The ReplicaSet immediately creates a new Pod to maintain the desired replica count. This is Kubernetes' self-healing mechanism - the actual state always converges to the desired state.

Q: How do you scale a Deployment?

A: Three ways: (1) kubectl scale deployment httpd --replicas=3, (2) kubectl edit deployment httpd (change replicas), (3) Update YAML and kubectl apply -f. All methods update the ReplicaSet, which creates/deletes Pods accordingly.

Q: What's the difference between image:latest and image:v1.0?

A: latest is a moving tag (unpredictable, can break production). Specific tags like v1.0 are immutable (predictable, reproducible). Best practice: Always use specific version tags in production for stability and rollback capability.

๐Ÿš€ Common Deployment Operations

Scaling Deployments

# Scale to 3 replicas
kubectl scale deployment httpd --replicas=3

# Verify scaling
kubectl get deployment httpd
kubectl get pods

Expected output:

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
httpd   3/3     3            3           10m

NAME                     READY   STATUS    RESTARTS   AGE
httpd-7d6c9f8b9c-x7k2m   1/1     Running   0          10m
httpd-7d6c9f8b9c-n9p4q   1/1     Running   0          5s
httpd-7d6c9f8b9c-m3r8t   1/1     Running   0          5s

Updating Deployments

# Update image version
kubectl set image deployment/httpd httpd=httpd:2.4

# Watch rollout status
kubectl rollout status deployment/httpd

Expected output:

Waiting for deployment "httpd" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "httpd" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "httpd" rollout to finish: 1 old replicas are pending termination...
deployment "httpd" successfully rolled out

Rollback Deployments

# View rollout history
kubectl rollout history deployment/httpd

# Rollback to previous version
kubectl rollout undo deployment/httpd

# Rollback to specific revision
kubectl rollout undo deployment/httpd --to-revision=1

Pause and Resume Rollouts

# Pause rollout (for batch changes)
kubectl rollout pause deployment/httpd

# Make multiple changes
kubectl set image deployment/httpd httpd=httpd:2.4
kubectl set resources deployment/httpd -c=httpd --limits=cpu=200m,memory=512Mi

# Resume rollout (apply all changes)
kubectl rollout resume deployment/httpd

๐ŸŒŸ kubectl Commands Reference

Deployment management:

kubectl create deployment NAME --image=IMAGE    # Create
kubectl get deployments                         # List all
kubectl get deployment NAME                     # Get specific
kubectl describe deployment NAME                # Details
kubectl delete deployment NAME                  # Delete
kubectl edit deployment NAME                    # Edit live

Scaling:

kubectl scale deployment NAME --replicas=N      # Scale
kubectl autoscale deployment NAME --min=2 --max=5 --cpu-percent=80  # Auto-scale

Updates and rollbacks:

kubectl set image deployment/NAME CONTAINER=IMAGE:TAG   # Update image
kubectl rollout status deployment/NAME          # Watch rollout
kubectl rollout history deployment/NAME         # View history
kubectl rollout undo deployment/NAME            # Rollback
kubectl rollout restart deployment/NAME         # Restart pods

Debugging:

kubectl logs deployment/NAME                    # View logs
kubectl logs deployment/NAME --previous         # Previous container logs
kubectl exec -it deployment/NAME -- bash        # Shell into container

๐ŸŽฏ Real-World Scenarios

Scenario 1: Deploy Multi-Replica Web App

# Create deployment with 3 replicas
kubectl create deployment webapp --image=nginx:1.21 --replicas=3

# Expose via LoadBalancer
kubectl expose deployment webapp --port=80 --type=LoadBalancer

# Check status
kubectl get deployment,pods,service

Scenario 2: Zero-Downtime Update

# Initial deployment
kubectl create deployment api --image=myapp:v1.0 --replicas=5

# Update to v2.0 (rolling update)
kubectl set image deployment/api api=myapp:v2.0

# Monitor rollout
kubectl rollout status deployment/api

# If issues, rollback
kubectl rollout undo deployment/api

Scenario 3: Resource-Constrained Deployment

# Create deployment with resource limits
kubectl create deployment backend --image=node:16
kubectl set resources deployment backend -c=backend --limits=cpu=500m,memory=512Mi --requests=cpu=250m,memory=256Mi

Scenario 4: Environment-Specific Config

# Create deployment with env vars
kubectl create deployment app --image=python:3.9
kubectl set env deployment/app ENV=production DB_HOST=postgres.default.svc.cluster.local

๐Ÿ”ง Troubleshooting

Issue 1: Pods stuck in Pending

# Check pod events
kubectl describe pod POD_NAME

# Common causes:
# - Insufficient resources
# - Image pull issues
# - Node selector mismatch

# Solution: Check node resources
kubectl top nodes
kubectl describe nodes

Issue 2: ImagePullBackOff

# Check pod status
kubectl get pods
kubectl describe pod POD_NAME

# Common causes:
# - Wrong image name/tag
# - Private registry auth missing
# - Network issues

# Solution: Verify image
docker pull httpd:latest  # Test locally

Issue 3: CrashLoopBackOff

# View logs
kubectl logs POD_NAME
kubectl logs POD_NAME --previous  # Previous crash logs

# Common causes:
# - Application errors
# - Missing dependencies
# - Wrong configuration

# Debug interactively
kubectl exec -it POD_NAME -- sh

Issue 4: Deployment not updating

# Check rollout status
kubectl rollout status deployment/httpd

# Check ReplicaSets
kubectl get rs

# Force recreate
kubectl rollout restart deployment/httpd

๐Ÿ“Š Deployment Strategies

RollingUpdate (Default):

strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 1        # Extra pods during update
    maxUnavailable: 1  # Pods that can be down

Recreate (Downtime):

strategy:
  type: Recreate  # Kill all, then create new

Blue-Green (Manual):

# Deploy v2 alongside v1
kubectl create deployment app-v2 --image=myapp:v2

# Switch service to v2
kubectl patch service app -p '{"spec":{"selector":{"version":"v2"}}}'

# Delete v1
kubectl delete deployment app-v1

Canary (Manual):

# Keep v1 with 9 replicas
kubectl scale deployment app-v1 --replicas=9

# Deploy v2 with 1 replica
kubectl create deployment app-v2 --image=myapp:v2 --replicas=1

# Both serve traffic (10% to v2)
# If good, scale v2 to 10, delete v1

๐ŸŽ‰ Final Thoughts

You've successfully created your first Kubernetes Deployment! This is the foundation of container orchestration.

What you accomplished: โœ… Created Deployment via kubectl
โœ… Verified resource hierarchy
โœ… Understood self-healing
โœ… Learned deployment operations
โœ… Mastered kubectl commands

Real-world impact:

  • Automatic scaling (handle traffic spikes)

  • Zero-downtime updates (deploy without interruption)

  • Self-healing (automatic pod recovery)

  • Declarative management (infrastructure as code)

  • Version control (rollback capability)

Skills acquired:

  • Kubernetes deployment creation

  • Resource verification techniques

  • kubectl command mastery

  • Deployment troubleshooting

  • Self-healing understanding

This is production Kubernetes! ๐Ÿ’ช


Day: 49/100
Challenge: KodeKloud Cloud DevOps
Date: December 24, 2025
Topic: Kubernetes Deployments

What's your Kubernetes deployment strategy? Share your orchestration experiences! ๐Ÿš€

More from this blog

๐Ÿš€ DevOps Challenge- KodeKloud Solutions

73 posts