متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 131Kubernetes · Workloads

How to add liveness and readiness probes

Without probes, Kubernetes sends traffic to a Pod that is still starting, and keeps a frozen app running forever. Probes fix both: the readiness probe says "send me traffic now", the liveness probe says "I am stuck — restart me", and the startup probe protects slow starters. Wrong probes are worse than none (restart loops, outages during load). This how-to sets all three correctly.

  • 5 الدروس
  • 2 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

131.1

The quick answer

deployment.yaml (part)
          ports:
            - containerPort: 8000
          startupProbe:
            httpGet: { path: /healthz, port: 8000 }
            periodSeconds: 5
            failureThreshold: 30
          readinessProbe:
            httpGet: { path: /ready, port: 8000 }
            periodSeconds: 5
            failureThreshold: 3
          livenessProbe:
            httpGet: { path: /healthz, port: 8000 }
            periodSeconds: 10
            failureThreshold: 3
            timeoutSeconds: 2

131.2

Step by step

  1. What each probe does:
ProbeWhen it failsUse
startupProbeContainer is restarted (after all tries). Other probes wait until it succeeds.Slow-starting apps (Java, migrations): here up to 30 × 5 s = 150 s.
readinessProbePod is removed from the Service — no restart."Can I serve traffic now?" (DB reachable, caches warm).
livenessProbeContainer is restarted."Am I stuck forever?" (deadlock). Keep it simple.
  1. Two endpoints in the app:
  • /healthz (liveness): only checks the process itself — never the database. If the DB is down, restarting all Pods does not help and makes it worse.
  • /ready (readiness): may check dependencies the Pod needs to serve.
  1. Types of probes: httpGet (2xx/3xx = OK), tcpSocket (port open), exec (a command returns 0), grpc.
example (TCP probe for a database)
          readinessProbe:
            tcpSocket: { port: 5432 }
            periodSeconds: 10
  1. Timing: liveness should be slower to fail than readiness (more failureThreshold or a longer periodSeconds), so a busy Pod first leaves the load balancer and only gets restarted if it really stays stuck.
  2. Watch them work:
kubectl describe pod api-6d8f9c7b5d-x2kq9 -n shop | grep -E 'Liveness|Readiness|Startup|Unhealthy'
kubectl get endpoints api -n shop
kubectl get pods -n shop -w

A Pod with a failing readiness probe shows 0/1 READY and is not in the endpoints list.

131.3

Check that it worked

During a rolling update, new Pods get traffic only after they are 1/1 READY (no errors for users), and if you make the app hang on purpose in a test, the liveness probe restarts it after period × failureThreshold seconds.

131.4

Common problems

ProblemCauseFix
Restart loop right after startLiveness fails before the app is up.Add a startupProbe; do not use liveness to wait for start.
All Pods restart when the DB is downLiveness checks the database.Liveness only checks the process; DB checks go into readiness.
Pods restart under heavy loadLiveness timeout too short (timeoutSeconds: 1).Longer timeout, more failures allowed; a light endpoint.
0/1 READY foreverReadiness path/port wrong or a dependency down.kubectl describe pod shows the probe error; test the URL inside the Pod.
Probe works locally but not in the clusterApp listens on 127.0.0.1.Listen on 0.0.0.0 (the kubelet calls the Pod IP).
Exec probes use much CPUHeavy command every few seconds.Use httpGet/tcpSocket, longer period.

الأوامر في هذا الدرس

131.5

Go further

1What happens when a readiness probe fails, and when a liveness probe fails?

Readiness: the Pod gets no traffic (no restart). Liveness: the container is restarted.

2Why should a liveness probe never check the database?

If the database is down, restarting every Pod does not help and adds load; that check belongs in readiness.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل