Step-by-Step Guide to Setting Up Rolling Updates in Kubernetes with Nginx

Introduction

In the realm of Kubernetes, ensuring zero downtime during application updates is crucial. Rolling Updates in Kubernetes provide a seamless way to update the application’s pods without affecting its availability. In this guide, we’ll walk through setting up rolling updates for an Nginx deployment in Kubernetes, ensuring your services remain uninterrupted.

Preparation

Before proceeding, ensure you have Kubernetes and kubectl installed and configured. This guide assumes you have basic knowledge of Kubernetes components and YAML syntax.

Deployment and Service Configuration

First, let’s understand the components of our .yaml file which configures both the Nginx deployment and service:

Deployment Configuration

  • apiVersion: apps/v1 indicates the API version.
  • kind: Deployment specifies the kind of Kubernetes object.
  • metadata: Defines the name of the deployment.
  • spec: Describes the desired state of the deployment:
    • selector: Ensures the deployment applies to pods with the label app: nginxdeployment.
    • revisionHistoryLimit: The number of old ReplicaSets to retain.
    • progressDeadlineSeconds: Time to wait before indicating progress has stalled.
    • minReadySeconds: Minimum duration a pod should be ready without any of its containers crashing, for it to be considered available.
    • strategy: Specifies the strategy used to replace old pods with new ones. Here, it’s set to RollingUpdate.
    • replicas: Number of desired pods.
    • template: Template for the pods the deployment creates.
    • containers: Specifies the Nginx container and its settings, such as image and ports.

Service Configuration

  • apiVersion: v1 indicates the API version.
  • kind: Service specifies the kind of Kubernetes object.
  • metadata: Defines the name of the service.
  • spec: Describes the desired state of the service:
    • selector: Maps the service to the deployment.
    • ports: Specifies the port configuration.
Step-by-Step Guide to Setting Up Rolling Updates in Kubernetes with Nginx

Implementing Rolling Updates in Kubernetes

To apply these configurations and initiate rolling updates, follow these steps:

Step 1. Create or update your deployment and service file named nginx-deployment-service.yaml with the content below.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginxdeployment
  revisionHistoryLimit: 3
  progressDeadlineSeconds: 300
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  replicas: 3
  template:
    metadata:
      labels:
        app: nginxdeployment
    spec:
      containers:
      - name: nginxdeployment
        # image: nginx:1.22
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginxservice
spec:
  selector:
    app: nginxdeployment
  ports:
    - protocol: TCP
      port: 80

Step 2. Apply the configuration using the command:

kubectl apply -f nginx-deployment-service.yaml

Step 3. To update the Nginx image or make other changes, modify the nginx-deployment-service.yaml file, and then reapply it. Kubernetes will handle the rolling update according to your strategy specifications.

Monitoring and Troubleshooting:

Monitor the update process using:

kubectl rollout status deployment/nginx-deployment

Check the status of your pods with:

kubectl get pods

If you need to revert to a previous version due to an issue, use:

kubectl rollout undo deployment/nginx-deployment

Conclusion

Rolling updates are essential for maintaining application availability and user satisfaction. By following this guide, you’ve learned how to set up and manage rolling updates for an Nginx deployment in Kubernetes. As you continue to work with Kubernetes, remember that careful planning and monitoring are key to successful deployment management. I hope will this your helpful. Thank you for reading the DevopsRoles page!

, ,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.