Kubernetes Day 3 - pranavkumarpk01/MD-DevOps GitHub Wiki

βš™οΈ Kubernetes Deployment, ReplicaSet & ReplicationController – Full Guide

In Kubernetes, managing Pods manually is not scalable. Controllers like Deployment, ReplicaSet, and ReplicationController ensure that the desired state of Pods is maintained automaticallyβ€”even if Pods crash or nodes fail.


πŸ“Œ Summary of Concepts

Controller Purpose Current Status
ReplicationController Ensures specified number of identical Pods Deprecated (use Deployment)
ReplicaSet Newer version of ReplicationController Used by Deployment internally
Deployment Manages ReplicaSet and enables rolling updates Most commonly used

πŸ” ReplicationController

βœ… Use Case

Ensures a specified number of Pod replicas are running at all times.

πŸ“„ YAML Example

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
spec:
  replicas: 2
  selector:
    app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.21
          ports:
            - containerPort: 80

🧠 Explanation

Field Meaning
replicas: 2 Runs two copies of the Pod
selector Matches Pods that should be managed
template Pod template to create new Pods
image: nginx:1.21 Container image to use
containerPort Port inside the container

πŸ” ReplicaSet

ReplicaSet is the next-gen ReplicationController, supporting set-based selectors (e.g., in, notin).

πŸ“„ YAML Example

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

🧠 Explanation

Field Meaning
replicas: 3 Desired number of Pods
matchLabels Ensures only Pods with app: nginx are managed
template Pod spec to create when needed

πŸš€ Deployment (Most Recommended)

A Deployment manages ReplicaSets which in turn manage Pods. It supports:

  • Rolling updates
  • Rollbacks
  • Scaling
  • Version tracking

πŸ“„ Full Deployment YAML with Explanation

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.21
          ports:
            - containerPort: 80

🧠 Field-by-Field Breakdown

Field Purpose
apiVersion: apps/v1 API version for Deployment
kind: Deployment Declares this is a Deployment object
metadata.name Name of the Deployment
metadata.labels Labels applied to Deployment (used for grouping)
spec.replicas: 3 Desired number of Pod replicas
spec.selector.matchLabels Selector to identify Pods managed by this Deployment
spec.strategy Deployment strategy for updates
rollingUpdate.maxUnavailable: 1 Allows one Pod to be down during update
rollingUpdate.maxSurge: 1 Allows one extra Pod during update
template.metadata.labels Labels for Pods created via this template
containers.image: nginx:1.21 Container image version
containerPort: 80 Port exposed by container

πŸ”§ Common Commands

# Apply a deployment YAML
kubectl apply -f deployment.yaml

# View Deployments
kubectl get deployments

# View ReplicaSets
kubectl get rs

# View Pods
kubectl get pods

# View detailed info
kubectl describe deployment nginx-deployment

# Scale the deployment
kubectl scale deployment nginx-deployment --replicas=5

# Trigger a rollout (e.g., change image)
kubectl set image deployment/nginx-deployment nginx=nginx:1.22

# Check rollout status
kubectl rollout status deployment/nginx-deployment

# Rollback if needed
kubectl rollout undo deployment/nginx-deployment

# Delete the deployment
kubectl delete deployment nginx-deployment

βš™οΈ Deployment Lifecycle

  1. You kubectl apply -f deployment.yaml
  2. Kubernetes creates a ReplicaSet
  3. ReplicaSet maintains desired Pods
  4. Deployment watches and updates the ReplicaSet when needed (e.g., version change)

πŸ“˜ Best Practices

  • Use Deployment instead of managing Pods or ReplicaSets directly
  • Use proper labels and selectors to avoid misconfigurations
  • Use RollingUpdate for smooth version changes
  • Monitor rollouts using kubectl rollout status
  • Use readinessProbes and livenessProbes in production
  • Keep manifests in Git (GitOps)

πŸ§ͺ Test It Yourself

# Apply Deployment
kubectl apply -f nginx-deployment.yaml

# Scale
kubectl scale deployment nginx-deployment --replicas=5

# Update Image
kubectl set image deployment/nginx-deployment nginx=nginx:1.22

# Rollback
kubectl rollout undo deployment nginx-deployment

πŸ”„ Difference Table

Feature Deployment ReplicaSet ReplicationController
Rolling Updates βœ… Yes ❌ No ❌ No
Rollback Support βœ… Yes ❌ No ❌ No
Use in Production βœ… Recommended ❌ Only internal ❌ Deprecated
Controller for Pods Indirect (via RS) Direct Direct

🎯 Conclusion:
Always use Deployments in modern Kubernetes for managing your apps. They provide the abstraction and control needed to safely roll out changes, scale apps, and recover from errors.