0
0
Kubernetesdevops~7 mins

Blue-green deployments in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Blue-green deployment is a way to update applications with almost no downtime. It keeps two versions of the app running: one live (blue) and one new (green). You switch traffic to the green version when ready, so users see no interruption.
When you want to update your app without making users wait or see errors.
When you need to test a new app version in production safely before full release.
When you want to quickly roll back to the old version if the new one has problems.
When your app must be available 24/7 without downtime during updates.
When you want to reduce risks of deployment failures by switching traffic smoothly.
Config File - blue-green-deployment.yaml
blue-green-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-blue
  labels:
    app: my-app
    version: blue
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
      version: blue
  template:
    metadata:
      labels:
        app: my-app
        version: blue
    spec:
      containers:
      - name: my-app-container
        image: my-app:blue
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-green
  labels:
    app: my-app
    version: green
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
      version: green
  template:
    metadata:
      labels:
        app: my-app
        version: green
    spec:
      containers:
      - name: my-app-container
        image: my-app:green
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
    version: blue
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP

This file defines two deployments: my-app-blue and my-app-green. Each runs a different version of the app labeled by version. The Service routes traffic to the blue version initially by selecting pods with label version: blue. To switch traffic, you update the Service selector to version: green.

Commands
This command creates both blue and green deployments and the service routing to blue. It sets up the environment for blue-green deployment.
Terminal
kubectl apply -f blue-green-deployment.yaml
Expected OutputExpected
deployment.apps/my-app-blue created deployment.apps/my-app-green created service/my-app-service created
Check that pods for both blue and green versions are running correctly before switching traffic.
Terminal
kubectl get pods -l app=my-app -o wide
Expected OutputExpected
NAME READY STATUS RESTARTS AGE IP NODE my-app-blue-6f7d8c9f7b-abc12 1/1 Running 0 1m 10.244.1.5 node1 my-app-blue-6f7d8c9f7b-def34 1/1 Running 0 1m 10.244.1.6 node2 my-app-green-7g8h9i0j1k-lmn56 1/1 Running 0 1m 10.244.2.7 node3 my-app-green-7g8h9i0j1k-opq78 1/1 Running 0 1m 10.244.2.8 node4
-l - Filter pods by label
-o wide - Show extra pod details like IP and node
Switch the service selector to route traffic from blue pods to green pods. This changes which version users access without downtime.
Terminal
kubectl patch service my-app-service -p '{"spec":{"selector":{"app":"my-app","version":"green"}}}'
Expected OutputExpected
service/my-app-service patched
Verify that the service endpoints now point to green pods after switching traffic.
Terminal
kubectl get endpoints my-app-service
Expected OutputExpected
NAME ENDPOINTS AGE my-app-service 10.244.2.7:80,10.244.2.8:80 2m
Key Concept

If you remember nothing else from this pattern, remember: run two app versions side-by-side and switch user traffic by changing the service selector to avoid downtime.

Common Mistakes
Not running the green deployment before switching the service selector
Switching traffic to a non-existent or unhealthy green deployment causes downtime or errors.
Always deploy and verify the green version pods are running and ready before switching the service selector.
Forgetting to update the service selector labels exactly to match the green deployment labels
If labels do not match, the service will have no endpoints and users cannot reach the app.
Double-check the service selector labels match the green deployment pod labels exactly.
Deleting the blue deployment immediately after switching traffic
If the green version has issues, you cannot quickly roll back to blue without the pods running.
Keep the blue deployment running until you confirm the green version works well, then delete blue.
Summary
Create two deployments with different labels for blue and green versions of the app.
Use a Kubernetes Service to route traffic to the blue version initially.
Switch traffic to the green version by patching the service selector to the green labels.
Verify pods and service endpoints before and after switching to ensure smooth deployment.