How to Set Up Blue Green Deployment in Kubernetes: A Step-by-Step Guide

Introduction

Setting up a Blue Green deployment in Kubernetes is an effective strategy to minimize downtime and reduce risks during application updates. This guide will walk you through the process step by step.

Prerequisites

Before you begin, ensure you have the following:

  • A running Kubernetes cluster.
  • kubectl configured to interact with your cluster.

Step-by-Step Guide to Setting Up a Blue Green Deployment in Kubernetes

Step 1: Define Deployments for Blue and Green Environments

Create two separate deployments for your Blue and Green environments. Each deployment will have its own set of pods.

Blue Deployment (blue-deployment.yaml)

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

Green Deployment (green-deployment.yaml)

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

Step 2: Create Services

Create a service that will switch between the Blue and Green environments.

Service (my-app-service.yaml)

apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
version: blue # Initially pointing to Blue
ports:
- protocol: TCP
port: 80
targetPort: 80

Step 3: Deploy the Blue Environment

Apply the Blue deployment and service configurations.

kubectl apply -f blue-deployment.yaml
kubectl apply -f my-app-service.yaml

Step 4: Test the Blue Environment

Verify that the Blue environment is working correctly by accessing the service.

Step 5: Deploy the Green Environment

Apply the Green deployment configuration.

kubectl apply -f green-deployment.yaml

Step 6: Test the Green Environment

Verify that the Green environment is working correctly by accessing the pods directly or through a temporary service.

Step 7: Switch Traffic to Green

Update the service to point to the Green deployment.

kubectl patch service my-app-service -p '{"spec":{"selector":{"version":"green"}}}'

Step 8: Monitor the Deployment

Ensure that the Green environment is working correctly by monitoring logs and application metrics.

Step 9: Cleanup

If everything is working fine, you can scale down or delete the Blue deployment.

kubectl delete -f blue-deployment.yaml

Summary

By following these steps, you can achieve a Blue Green deployment setup in Kubernetes, allowing you to switch between two identical environments with minimal downtime and risk. This approach ensures a seamless transition during application updates, providing a reliable method to manage deployments.  I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. 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.