Table of Contents
- 1 Introduction
- 2 Step-by-Step Guide to Setting Up a Blue Green Deployment in Kubernetes
- 2.1 Step 1: Define Deployments for Blue and Green Environments
- 2.2 Step 2: Create Services
- 2.3 Step 3: Deploy the Blue Environment
- 2.4 Step 4: Test the Blue Environment
- 2.5 Step 5: Deploy the Green Environment
- 2.6 Step 6: Test the Green Environment
- 2.7 Step 7: Switch Traffic to Green
- 2.8 Step 8: Monitor the Deployment
- 2.9 Step 9: Cleanup
- 3 Summary
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!