☑️Day 35: Understanding Deployment and Rollout in Kubernetes🚀

🔹Table of Contents :

  • Introduction

  • What is a Deployment in Kubernetes?

  • Key Deployment Commands

  • Hands-On Practice: Deploying nginx

    • Step-by-Step Guide to Deploy nginx Using a YAML File

    • Verify and Manage the Deployment

  • What is the Rollout Feature in Kubernetes?

  • YAML for Rolling Updates

  • Tracking Rollout Status & History

  • Real-Time Example: Apache Deployment & Rollout

  • Summary of Key Commands


What is a Deployment in Kubernetes?

A Deployment in Kubernetes is used to manage the creation and updating of pods in a scalable and structured way. A deployment ensures that a specified number of pod replicas are running at all times. It also manages rolling updates and rollbacks.

Components of a Deployment:

  1. Pod Template: Defines the containers, images, and configurations.

  2. Replicas: Number of pod copies that should be running.

  3. Update Strategy: Defines how the application should be updated (e.g., rolling updates).


Key Deployment Commands:

  • Create a Deployment:

      kubectl create deployment <deployment_name> --image=<image_name>
    

    Example:

      kubectl create deployment nginx-deployment --image=nginx:latest
    
  • Get Deployment Details:

      kubectl get deployments
    
  • Check Pods:

      kubectl get pods
    

Hands-On Practice: Deploying nginx

To solidify my understanding, I performed a deployment using nginx.

YAML for nginx Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
  • Apply YAML file:

      kubectl apply -f nginx-deployment.yaml
    
  • Verify Deployment:

      kubectl get deployments
    

This command ensures that three replicas of the nginx pod are running.


What is the Rollout Feature?

In Kubernetes, a Rollout is a way to update your application. It allows you to deploy new versions of your app without downtime by gradually replacing old pods with new ones. If anything goes wrong, you can rollback to a previous version, ensuring that your application remains stable.

Key Rollout Concepts:

  1. Rolling Update: Gradually replaces old versions of your application with the new ones.

  2. Rollback: If the new version is faulty, you can revert back to the previous stable version.

  3. Strategy: Kubernetes provides different strategies for updating your application, including RollingUpdate and Recreate.


YAML for Rolling Updates:

I created a new deployment with a custom rolling update strategy in the YAML file.

YAML for Rolling Update (deployment3.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  annotations:
    deployment.kubernetes.io/revision: "1"
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  minReadySeconds: 10
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80
  • Apply the YAML:

      kubectl apply -f deployment3.yaml
    
  • Check Rollout Status:

      kubectl rollout status deployment/nginx-deployment
    

Tracking Rollout Status & History

I also explored how to monitor the status of rollouts and view the history of deployments.

Rollout Commands:

  • Check Rollout Status:

      kubectl rollout status deployment/<deployment_name>
    
  • View Rollout History:

      kubectl rollout history deployment/nginx-deployment
    
  • Undo Rollout (Rollback):

      kubectl rollout undo deployment/nginx-deployment --to-revision=1
    

This command is incredibly useful if you want to revert to a previous, stable version of your application.


Real-Time Example: Apache Deployment & Rollout

To further understand the rollback feature, I deployed Apache and practiced the rollback commands.

YAML for Apache Deployment (deployment5.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apache-deployment
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - name: apache
        image: httpd:2
        ports:
        - containerPort: 80
  • Apply the Deployment:

      kubectl apply -f deployment5.yaml
    
  • Rollback to Previous Version:

      kubectl rollout undo deployment/apache-deployment --to-revision=1
    

Summary of Key Commands:

  • Create Deployment:

      kubectl create deployment <name> --image=<image>
    
  • Apply YAML File:

      kubectl apply -f <file.yaml>
    
  • Check Deployment:

      kubectl get deployments
    
  • Rolling Update:

      kubectl rollout status deployment/<name>
    
  • Rollback:

      kubectl rollout undo deployment/<name> --to-revision=1
    

By understanding deployments and rollouts, you can manage and scale your applications in a highly efficient way, ensuring high availability and smooth updates in production environments.

🚀Thanks for joining me on Day 35! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)
0
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti