Skip to content

๐Ÿš€ Kubernetes Deployments

A Deployment is the most commonly used controller in Kubernetes. It manages ReplicaSets, allowing for:

  • Declarative updates
  • Rollbacks
  • Scaling
  • Easy rollout of changes

โœ… Why Use a Deployment?

Unlike a raw ReplicaSet, a Deployment offers high-level operations:

  • Rollout new versions with zero downtime
  • Automatically create new ReplicaSets
  • Roll back to previous versions

๐Ÿ“ฆ Create a Deployment

Imperative Command

kubectl create deployment nginx --image=nginx

With Replicas

kubectl create deployment nginx --image=nginx --replicas=4

If --replicas is not accepted, scale it after:

kubectl scale deployment nginx --replicas=4

โœ๏ธ Generate YAML Without Creating (Dry Run)

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml

You can then customize and apply it:

kubectl apply -f nginx-deployment.yaml

๐Ÿงพ Sample Deployment YAML

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

๐Ÿ” Inspect & Manage Deployments

kubectl get deployments
kubectl describe deployment <name>
kubectl edit deployment <name>
kubectl delete deployment <name>

๐Ÿ” Rollouts & Rollbacks

View rollout status

kubectl rollout status deployment <name>

Roll back to previous version

kubectl rollout undo deployment <name>

โŒ Common Deployment Issues

Pods stuck in ImagePullBackOff

kubectl describe pod <pod-name>
# Check the Events section

Example Error

spec:
  containers:
  - name: app
    image: nonexistent-image  # leads to ImagePullBackOff

๐Ÿง  Key Differences from ReplicaSet

Feature Deployment ReplicaSet
Manages RS? โœ… Yes โŒ No
Rollbacks โœ… Yes โŒ No
Rolling updates โœ… Yes โŒ Manual only
Recommended? โœ… Production default ๐Ÿšซ Not preferred

โœ… Summary

  • Deployment is your go-to controller for managing stateless apps
  • Use kubectl apply with YAML for full control
  • Supports rollback, rollout, and declarative scaling
  • Use kubectl rollout status to monitor updates
  • Use Deployments unless you have a stateful or special-case workload