Ravindra BagaleCourses & study guides मराठी

Chapter 75: DevOps Projects Part 5: Kubernetes Cluster Deploy Ingress TLS HPA Probes StatefulSet Blue-Green

75.9 Project 39: Blue-green deployment

Friends, blue-green means two full versions and one Service switch. Today we run shop-blue and shop-green, point shop-active at blue, smoke green, cut over, then roll back with one patch.

What you will build in this project

  1. Deployments shop-blue and shop-green.
  2. Service shop-active selecting version.
  3. Cutover patch to green.
  4. Rollback patch back to blue.

1. Start from a real company story

At Razorpay in Bengaluru, a Friday rolling update mixed old and new payment code for eight minutes.

Some charges double-wrote. Rough cost: about ₹25 lakh in reconciliations.

Leela moved critical cutovers to blue-green: green soaks privately, then the Service flips once.

Today we practice that Service selector flip on kind.

shop-blueversion=blueshop-greenversion=greenshop-activeselector fliprollbackselector back
Picture 1: blue live → green ready → flip Service → rollback if needed

Ravindra Bagale's Tip

Many students delete blue the second green looks fine. Keep blue warm through a soak window. Keep that habit.

2. Start and prepare

Step 1: Why blue-green

Why: Full stacks. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 1: a1

Type this:

echo 'Blue-green keeps two full versions; switch Service selector when green is healthy'

You will see:

Blue-green keeps two full versions; switch Service selector when green is healthy

What it means: Why blue-green.

Example 2: a2

Type this:

echo 'Rollback is instant: point the Service back to blue labels'

You will see:

Rollback is instant: point the Service back to blue labels

What it means: Rollback benefit.

Example 3: a3

Type this:

echo 'Cost is double capacity during the cutover window'

You will see:

Cost is double capacity during the cutover window

What it means: Trade-off tip.

Step 2: Create blue

Why: Blue live. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: kubectl apply blue. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 4: b1

Type this:

mkdir -p /tmp/k8s39 && printf '%s\n' 'apiVersion: apps/v1' 'kind: Deployment' 'metadata: {name: shop-blue}' 'spec:' '  replicas: 2' '  selector: {matchLabels: {app: shop, version: blue}}' '  template:' '    metadata: {labels: {app: shop, version: blue}}' '    spec:' '      containers:' '      - name: web' '        image: nginx:1.27-alpine' '        ports: [{containerPort: 80}]' > /tmp/k8s39/blue.yaml && kubectl apply -f /tmp/k8s39/blue.yaml

You will see:

deployment.apps/shop-blue created

What it means: Blue Deployment created.

Example 5: b2

Type this:

kubectl wait --for=condition=available deploy/shop-blue --timeout=90s

You will see:

deployment.apps/shop-blue condition met

What it means: Blue Available.

Example 6: b3

Type this:

kubectl get pods -l version=blue --no-headers | wc -l

You will see:

2

What it means: Two blue Pods.

Step 3: Create Service on blue

Why: Service selects blue. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: kubectl apply svc. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 7: c1

Type this:

printf '%s\n' 'apiVersion: v1' 'kind: Service' 'metadata: {name: shop-active}' 'spec:' '  selector: {app: shop, version: blue}' '  ports: [{port: 80, targetPort: 80}]' > /tmp/k8s39/svc.yaml && kubectl apply -f /tmp/k8s39/svc.yaml

You will see:

service/shop-active created

What it means: Service points at blue.

Example 8: c2

Type this:

kubectl get endpoints shop-active -o jsonpath='{.subsets[0].addresses[*].ip}' | wc -w

You will see:

2

What it means: Two blue endpoints.

Example 9: c3

Type this:

kubectl get svc shop-active -o jsonpath='{.spec.selector.version}'; echo

You will see:

blue

What it means: Selector version is blue.

3. Build the main pieces

Step 4: Create green

Why: Green Ready. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: kubectl apply green. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 10: d1

Type this:

printf '%s\n' 'apiVersion: apps/v1' 'kind: Deployment' 'metadata: {name: shop-green}' 'spec:' '  replicas: 2' '  selector: {matchLabels: {app: shop, version: green}}' '  template:' '    metadata: {labels: {app: shop, version: green}}' '    spec:' '      containers:' '      - name: web' '        image: nginx:1.27-alpine' '        ports: [{containerPort: 80}]' '        env: [{name: COLOR, value: green}]' > /tmp/k8s39/green.yaml && kubectl apply -f /tmp/k8s39/green.yaml

You will see:

deployment.apps/shop-green created

What it means: Green Deployment created beside blue.

Example 11: d2

Type this:

kubectl wait --for=condition=available deploy/shop-green --timeout=90s

You will see:

deployment.apps/shop-green condition met

What it means: Green Available.

Example 12: d3

Type this:

kubectl get deploy shop-blue shop-green

You will see:

NAME READY
shop-blue 2/2
shop-green 2/2

What it means: Both colors Ready.

Step 5: Smoke green

Why: Private smoke. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: port-forward. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 13: e1

Type this:

echo 'Smoke green before cutover: port-forward deploy/shop-green and curl'

You will see:

Smoke green before cutover: port-forward deploy/shop-green and curl

What it means: Pre-cutover test tip.

Example 14: e2

Type this:

kubectl port-forward deploy/shop-green 8099:80 >/tmp/k8s39/pf.log 2>&1 & echo $! > /tmp/k8s39/pf.pid; sleep 1; curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8099/

You will see:

200

What it means: Green responds 200 privately.

Example 15: e3

Type this:

kill $(cat /tmp/k8s39/pf.pid); echo stopped

You will see:

stopped

What it means: Forward stopped.

Step 6: Cutover to green

Why: Selector green. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: kubectl patch. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 16: f1

Type this:

kubectl patch svc shop-active -p '{"spec":{"selector":{"app":"shop","version":"green"}}}'

You will see:

service/shop-active patched

What it means: Cutover: Service now selects green.

Example 17: f2

Type this:

kubectl get svc shop-active -o jsonpath='{.spec.selector.version}'; echo

You will see:

green

What it means: Selector version is green.

Example 18: f3

Type this:

kubectl get endpoints shop-active -o jsonpath='{.subsets[0].addresses[*].ip}' | wc -w

You will see:

2

What it means: Endpoints now green Pods.

4. Automate and verify

Step 7: Keep blue warm

Why: Soak. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo soak. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 19: g1

Type this:

kubectl get pods -l version=green -o name

You will see:

pod/shop-green-...
pod/shop-green-...

What it means: Green Pods are the active targets.

Example 20: g2

Type this:

kubectl get pods -l version=blue -o name

You will see:

pod/shop-blue-...
pod/shop-blue-...

What it means: Blue still running as instant rollback.

Example 21: g3

Type this:

echo 'Keep blue warm for a soak period before deleting it'

You will see:

Keep blue warm for a soak period before deleting it

What it means: Soak habit.

Step 8: Rollback to blue

Why: Selector blue. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: kubectl patch. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 22: h1

Type this:

kubectl patch svc shop-active -p '{"spec":{"selector":{"app":"shop","version":"blue"}}}'

You will see:

service/shop-active patched

What it means: Rollback: Service back to blue.

Example 23: h2

Type this:

kubectl get svc shop-active -o jsonpath='{.spec.selector.version}'; echo

You will see:

blue

What it means: Selector version is blue again.

Example 24: h3

Type this:

echo 'Rollback took one patch; no image rebuild required'

You will see:

Rollback took one patch; no image rebuild required

What it means: Why blue-green is loved in war rooms.

Step 9: Compare canary

Why: Vs canary. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo canary. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 25: i1

Type this:

echo 'Canary sends a fraction of traffic; blue-green sends all at once after proof'

You will see:

Canary sends a fraction of traffic; blue-green sends all at once after proof

What it means: Compare to canary.

Example 26: i2

Type this:

echo 'Project 40 will cover canary with Argo Rollouts'

You will see:

Project 40 will cover canary with Argo Rollouts

What it means: Bridge to next chapter.

Example 27: i3

Type this:

echo 'RollingUpdate is the default Deployment strategy; blue-green is a Service switch'

You will see:

RollingUpdate is the default Deployment strategy; blue-green is a Service switch

What it means: Compare to rolling.

5. Harden and accept

Step 10: Naming and cleanup

Why: Stable Service name. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo naming. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 28: j1

Type this:

kubectl get deploy -l app=shop -o custom-columns=NAME:.metadata.name,READY:.status.readyReplicas

You will see:

NAME READY
shop-blue 2
shop-green 2

What it means: Both Deployments still Ready.

Example 29: j2

Type this:

echo 'After soak: kubectl delete deploy shop-blue'

You will see:

After soak: kubectl delete deploy shop-blue

What it means: Cleanup old color after confidence.

Example 30: j3

Type this:

echo 'Name Services shop-active not shop-blue so cutovers do not rename clients'

You will see:

Name Services shop-active not shop-blue so cutovers do not rename clients

What it means: Naming habit.

Step 11: Endpoints proof

Why: Endpoints match. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: kubectl get ep. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 31: k1

Type this:

kubectl get endpoints shop-active -o yaml | grep -E 'ip:|pod-name' | head -6

You will see:

ip: 10.x
pod-name: shop-blue-...

What it means: Endpoints show which Pods are active.

Example 32: k2

Type this:

kubectl describe svc shop-active | grep -A3 Selector

You will see:

Selector: app=shop,version=blue

What it means: Describe confirms selector.

Example 33: k3

Type this:

echo 'Automate the patch in CI only after green smoke tests pass'

You will see:

Automate the patch in CI only after green smoke tests pass

What it means: CI tip.

Step 12: Acceptance checklist

Why: Leela signs off. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: All green. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 34: z1

Type this:

kubectl get svc shop-active -o jsonpath='{.spec.selector.version}'; echo

You will see:

blue

What it means: Lab left on blue after rollback demo.

Example 35: z2

Type this:

kubectl get deploy shop-blue shop-green -o jsonpath='{.items[*].status.readyReplicas}'; echo

You will see:

2 2

What it means: Both colors Ready for practice.

Example 36: z3

Type this:

echo 'Leela: PASS'

You will see:

Leela: PASS

What it means: Project 39 accepted.

Check yourself

Q1. Who fixed Razorpay cutover?

Leela

Q2. Active Service name?

shop-active

Q3. Blue label value?

blue

Q4. Green label value?

green

Q5. Cutover command family?

kubectl patch

Q6. Rollback method?

patch selector back

Friends, you flipped traffic with a Service selector and rolled back in one patch. Did you understand? Let us try it once more. Write the soak checklist you would require before deleting blue.

Lab: try at home

  1. Create blue Deployment and Service.
  2. Create green beside it.
  3. Smoke green privately.
  4. Patch selector to green.
  5. Patch back to blue for rollback.

Recap

  • Two full stacks.
  • Service name stays stable.
  • Selector is the switch.
  • Soak before delete.
  • Canary is the next pattern.

Lab

  1. Finish all nine projects on one kind cluster named shop-lab.
  2. Keep a notebook of kubectl get and describe outputs that proved each step.
  3. Never apply untested YAML to a shared staging cluster.
  4. After each project, write one paragraph: what broke and how you fixed it.
  5. Leave shop-lab running if you will continue with projects 40-48 next.

Practice task

Pick the HPA maxReplicas cap and the blue-green Service flip. Prove both on kind.

Ravindra Bagale's Tip

Prove every YAML on kind before staging. Set CPU requests before HPA. Keep blue warm through the soak window.

In short

  • kind before staging.
  • Service in front of Pods.
  • Secrets for passwords.
  • Ingress for hostnames.
  • cert-manager for TLS.
  • requests before HPA.
  • readiness before liveness aggression.
  • StatefulSet plus PVC for MySQL.
  • Soak before deleting blue.