Ravindra BagaleCourses & study guides मराठी

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

75.7 Project 37: Liveness and readiness probes

Friends, Running is not the same as Ready. Today we add HTTP liveness and readiness probes, break readiness on purpose, watch Endpoints drain, then restore them.

What you will build in this project

  1. Deployment with httpGet probes.
  2. Service Endpoints track readiness.
  3. Broken readiness drains Endpoints.
  4. Restored path brings Pods back.

1. Start from a real company story

At Freshworks in Chennai, a Java app took sixty seconds to boot. Liveness killed it every thirty seconds.

CrashLoopBackOff ate the release night. Rough cost: about ₹10 lakh in overtime and a missed window.

Neha added a startupProbe and softened liveness. Readiness kept bad Pods out of the Service.

Today we practice readiness and liveness the careful way.

readinessProbehttpGet /livenessProberestart if failbad pathEndpoints emptyfix pathEndpoints back
Picture 1: probe fails → not Ready → removed from Endpoints

Ravindra Bagale's Tip

Many students copy aggressive liveness probes that kill slow apps. Start soft. Prefer readiness to shed traffic. Keep that habit.

2. Start and prepare

Step 1: Why probes

Why: Probes separate alive and 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: 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 'Probes tell kubelet when a container is alive and ready for traffic'

You will see:

Probes tell kubelet when a container is alive and ready for traffic

What it means: Why probes exist.

Example 2: a2

Type this:

echo 'liveness restarts; readiness removes from Service Endpoints'

You will see:

liveness restarts; readiness removes from Service Endpoints

What it means: Difference in one line.

Example 3: a3

Type this:

echo 'startupProbe protects slow apps from liveness kills during boot'

You will see:

startupProbe protects slow apps from liveness kills during boot

What it means: startupProbe role.

Step 2: Write Deployment with probes

Why: YAML declares both probes. 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: printf. 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/k8s37 && printf '%s\n' 'apiVersion: apps/v1' 'kind: Deployment' 'metadata: {name: probe-demo}' 'spec:' '  replicas: 2' '  selector: {matchLabels: {app: probe-demo}}' '  template:' '    metadata: {labels: {app: probe-demo}}' '    spec:' '      containers:' '      - name: web' '        image: nginx:1.27-alpine' '        ports: [{containerPort: 80}]' '        readinessProbe:' '          httpGet: {path: /, port: 80}' '          initialDelaySeconds: 2' '          periodSeconds: 5' '        livenessProbe:' '          httpGet: {path: /, port: 80}' '          initialDelaySeconds: 10' '          periodSeconds: 10' > /tmp/k8s37/deploy.yaml && grep -E 'readinessProbe|livenessProbe' /tmp/k8s37/deploy.yaml

You will see:

readinessProbe:
livenessProbe:

What it means: Deployment YAML with both HTTP probes.

Example 5: b2

Type this:

kubectl apply -f /tmp/k8s37/deploy.yaml

You will see:

deployment.apps/probe-demo created

What it means: Applied probe-demo.

Example 6: b3

Type this:

kubectl wait --for=condition=available deploy/probe-demo --timeout=90s

You will see:

deployment.apps/probe-demo condition met

What it means: Both replicas Available.

Step 3: Apply and wait

Why: Available after apply. 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. 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:

kubectl expose deploy probe-demo --port=80 --name=probe-demo 2>/dev/null || true; kubectl get endpoints probe-demo

You will see:

NAME ENDPOINTS
probe-demo 10.x:80,10.y:80

What it means: Ready Pods appear in Endpoints.

Example 8: c2

Type this:

kubectl get pods -l app=probe-demo -o jsonpath='{range .items[*]}{.metadata.name} {.status.containerStatuses[0].ready}{"\n"}{end}'

You will see:

probe-demo-... true
probe-demo-... true

What it means: containerStatuses ready is true.

Example 9: c3

Type this:

echo 'If readiness fails, Endpoints empty even while Pod Running'

You will see:

If readiness fails, Endpoints empty even while Pod Running

What it means: Classic interview point.

3. Build the main pieces

Step 4: Endpoints and ready

Why: Endpoints follow readiness. 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 10: d1

Type this:

kubectl get deploy probe-demo -o yaml | grep -A4 readinessProbe

You will see:

httpGet:
  path: /
  port: 80

What it means: readiness hits HTTP GET /.

Example 11: d2

Type this:

kubectl get deploy probe-demo -o yaml | grep -A4 livenessProbe

You will see:

httpGet:
  path: /
  port: 80

What it means: liveness also hits HTTP GET /.

Example 12: d3

Type this:

echo 'Prefer httpGet for HTTP apps; use tcpSocket or exec when needed'

You will see:

Prefer httpGet for HTTP apps; use tcpSocket or exec when needed

What it means: Probe type tip.

Step 5: Inspect probe YAML

Why: httpGet on /. 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 yaml. 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 'Break readiness: kubectl set probe by editing path to /not-real'

You will see:

Break readiness: path /not-real

What it means: Lab idea to see Endpoints drain.

Example 14: e2

Type this:

kubectl patch deploy probe-demo --type json -p '[{"op":"replace","path":"/spec/template/spec/containers/0/readinessProbe/httpGet/path","value":"/not-real"}]' && sleep 8; kubectl get endpoints probe-demo

You will see:

NAME ENDPOINTS
probe-demo <none>

What it means: Bad readiness removes Pods from Endpoints.

Example 15: e3

Type this:

kubectl get pods -l app=probe-demo

You will see:

NAME READY STATUS
... 0/1 Running

What it means: Pod Running but not Ready.

Step 6: Break readiness

Why: Bad path drains. 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 deploy probe-demo --type json -p '[{"op":"replace","path":"/spec/template/spec/containers/0/readinessProbe/httpGet/path","value":"/"}]' && kubectl rollout status deploy/probe-demo --timeout=90s

You will see:

deployment "probe-demo" successfully rolled out

What it means: Readiness path restored.

Example 17: f2

Type this:

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

You will see:

2

What it means: Endpoints filled again.

Example 18: f3

Type this:

echo 'Never point liveness at a dependency like a database'

You will see:

Never point liveness at a dependency like a database

What it means: Anti-pattern tip.

4. Automate and verify

Step 7: Restore readiness

Why: Good path refills. 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 19: g1

Type this:

echo 'initialDelaySeconds gives boot time before first probe'

You will see:

initialDelaySeconds gives boot time before first probe

What it means: Delay meaning.

Example 20: g2

Type this:

echo 'periodSeconds is how often kubelet probes'

You will see:

periodSeconds is how often kubelet probes

What it means: Period meaning.

Example 21: g3

Type this:

echo 'failureThreshold times period equals roughly time-to-restart'

You will see:

failureThreshold times period equals roughly time-to-restart

What it means: Failure math tip.

Step 8: Timing fields

Why: delay and period matter. 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 timing. 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:

echo 'startupProbe with failureThreshold high protects slow JVMs'

You will see:

startupProbe with failureThreshold high protects slow JVMs

What it means: startupProbe use case.

Example 23: h2

Type this:

echo 'Disable liveness until startupProbe succeeds when both are set'

You will see:

Disable liveness until startupProbe succeeds when both are set

What it means: Interaction tip.

Example 24: h3

Type this:

echo 'exec probes must be cheap; heavy scripts hurt the node'

You will see:

exec probes must be cheap; heavy scripts hurt the node

What it means: exec caution.

Step 9: startupProbe tips

Why: startup for slow apps. 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 startup. 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:

kubectl describe pod -l app=probe-demo | grep -E 'Readiness|Liveness|Restart' | head -10

You will see:

Liveness: http-get ...
Readiness: http-get ...

What it means: Describe shows probe config and restarts.

Example 26: i2

Type this:

kubectl get pods -l app=probe-demo -o jsonpath='{.items[0].status.containerStatuses[0].restartCount}'; echo

You will see:

0

What it means: Healthy probes keep restartCount at 0.

Example 27: i3

Type this:

echo 'Rising restartCount with failing liveness means investigate logs'

You will see:

Rising restartCount with failing liveness means investigate logs

What it means: Debug tip.

5. Harden and accept

Step 10: Describe and logs

Why: Describe shows restarts. 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 describe. 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 logs -l app=probe-demo --tail=3

You will see:

GET / ...

What it means: Access logs prove probes or traffic.

Example 29: j2

Type this:

kubectl get events --field-selector reason=Unhealthy --sort-by=.lastTimestamp 2>/dev/null | tail -3 || echo 'no Unhealthy yet'

You will see:

no Unhealthy yet

What it means: Unhealthy events appear when probes fail.

Example 30: j3

Type this:

echo 'Tune probes in stage with real traffic shapes before prod'

You will see:

Tune probes in stage with real traffic shapes before prod

What it means: Tuning habit.

Step 11: Confirm final paths

Why: Paths healthy. 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: jsonpath. 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 deploy probe-demo -o jsonpath='{.spec.template.spec.containers[0].readinessProbe.httpGet.path}'; echo

You will see:

/

What it means: Readiness path healthy.

Example 32: k2

Type this:

kubectl get deploy probe-demo -o jsonpath='{.spec.template.spec.containers[0].livenessProbe.periodSeconds}'; echo

You will see:

10

What it means: Liveness period 10s.

Example 33: k3

Type this:

echo 'Optional cleanup: kubectl delete deploy probe-demo svc probe-demo'

You will see:

Optional cleanup: kubectl delete deploy probe-demo svc probe-demo

What it means: Cleanup tip.

Step 12: Acceptance checklist

Why: Neha 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 endpoints probe-demo -o jsonpath='{.metadata.name}'; echo

You will see:

probe-demo

What it means: Service Endpoints object exists.

Example 35: z2

Type this:

kubectl get deploy probe-demo -o jsonpath='{.status.readyReplicas}'; echo

You will see:

2

What it means: Two ready replicas.

Example 36: z3

Type this:

echo 'Neha: PASS'

You will see:

Neha: PASS

What it means: Project 37 accepted.

Check yourself

Q1. Who fixed Freshworks probes?

Neha

Q2. Removes from Service?

readiness

Q3. Restarts container?

liveness

Q4. Slow boot helper?

startupProbe

Q5. Lab deploy name?

probe-demo

Q6. HTTP path used?

/

Friends, you watched Endpoints drain when readiness failed. Did you understand? Let us try it once more. Add a startupProbe tip to your notebook for JVM apps.

Lab: try at home

  1. Write probes on a Deployment.
  2. Expose a Service.
  3. Break readiness path.
  4. Restore and confirm Endpoints.
  5. Read describe and restartCount.

Recap

  • Ready means in Endpoints.
  • Liveness restarts containers.
  • startupProbe protects boot.
  • Do not probe the database from liveness.
  • Tune in stage first.