Chapter 8: Deployments and Rollouts – Managing Application Updates

Introduction to Deployments

A Deployment in Kubernetes automates the process of managing application rollouts, scaling, and rollbacks. It ensures your desired application state is always met, making it an essential building block for production-grade Kubernetes environments.

Why Use Deployments?

  1. Declarative Updates: Specify the desired state, and Kubernetes handles the rest.
  2. Scaling Applications: Scale applications up or down seamlessly.
  3. Rolling Updates: Update applications with zero downtime.
  4. Rollback: Revert to a previous state if an update fails.

Key Concepts in Deployments

  1. ReplicaSet: Ensures a specified number of pod replicas are running.
  2. Rolling Updates: Gradually replace old versions with new ones.
  3. Rollback: Revert to a previous Deployment state in case of issues.

Step-by-Step Implementation

Step 1: Create a Deployment

Deployment YAML

Save this as deployment-example.yaml:

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

Explanation:

  • replicas: Number of Pod instances.
  • selector: Matches Pods with specific labels.
  • template: Defines Pod specifications.

Apply the Deployment:

kubectl apply -f deployment-example.yaml

Verify the Deployment:

kubectl get deployments
kubectl get pods

Access the Deployment details:

kubectl describe deployment nginx-deployment

Step 2: Scaling the Deployment

Scale Using kubectl

Increase replicas:

kubectl scale deployment nginx-deployment --replicas=5

Verify the new replica count:

kubectl get pods

Scale by Editing YAML

Modify the replicas field in deployment-example.yaml:

replicas: 5

Apply the updated Deployment:

kubectl apply -f deployment-example.yaml

Step 3: Rolling Updates

Update the Deployment

Update the image in the Deployment YAML:

containers:
- name: nginx
  image: nginx:1.21

Apply the update:

kubectl apply -f deployment-example.yaml

Verify the rolling update:

kubectl rollout status deployment nginx-deployment

View the Rollout History

Check Deployment history:

kubectl rollout history deployment nginx-deployment

Step 4: Rollback to a Previous Version

Simulate an Issue

Update to an invalid image:

kubectl set image deployment nginx-deployment nginx=nginx:invalid --record

Verify Pods fail to start:

kubectl get pods
kubectl describe pod <pod-name>

Rollback to a Stable Version

Rollback to the previous revision:

kubectl rollout undo deployment nginx-deployment

Verify the rollback:

kubectl rollout status deployment nginx-deployment

Step 5: Canary Deployments

A Canary Deployment gradually releases new versions to a subset of users to minimize risk.

Example

Save this as canary-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-canary
  labels:
    app: nginx
    version: canary
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      version: canary
  template:
    metadata:
      labels:
        app: nginx
        version: canary
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80

Apply the Canary Deployment:

kubectl apply -f canary-deployment.yaml

Verify the Canary Pods:

kubectl get pods -l version=canary

Monitoring and Troubleshooting Deployments

Check Pod Status:

kubectl get pods
kubectl describe pod <pod-name>

Inspect Events:

kubectl get events

Debugging Logs:

kubectl logs <pod-name>

Check Deployment Rollout:

kubectl rollout status deployment nginx-deployment

Best Practices

  1. Use Rolling Updates: Ensure zero downtime during updates.
  2. Monitor Resources: Keep an eye on CPU and memory usage.
  3. Leverage Canary Deployments: Reduce the risk of breaking changes.
  4. Automate Deployments: Use CI/CD pipelines for faster iterations.

Production Example: Deploying a Web App

Scenario

Deploy a production-ready web application with a rolling update strategy.

Web App Deployment YAML

Save this as webapp-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  replicas: 4
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: myregistry/webapp:v1.0
        ports:
        - containerPort: 8080

Apply the Deployment:

kubectl apply -f webapp-deployment.yaml

Configure Rolling Update

Set a rolling update strategy:

kubectl set image deployment webapp webapp=myregistry/webapp:v1.1 --record

Verify:

kubectl rollout status deployment webapp

Conclusion

In this chapter, you learned how to:

  1. Create, scale, and update Deployments.
  2. Perform rolling updates and rollbacks.
  3. Implement Canary Deployments for safer updates

Leave a Reply