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

كيف 31Kubernetes · kubectl

How to find why a Kubernetes Pod does not start

A deploy went out and the Pod is Pending, ImagePullBackOff or CrashLoopBackOff. You do not need to guess: Kubernetes writes the reason into the Pod status and the events. This how-to shows the three commands that find almost every cause, a table of all common statuses with their fix, and how to check the node when the Pod can not be scheduled at all.

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

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

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

31.1

The quick answer

kubectl get pods -n shop
NAME                   READY   STATUS             RESTARTS      AGE
api-6d8f9c7b5d-x2kq9   0/1     CrashLoopBackOff   5 (40s ago)   4m
kubectl describe pod api-6d8f9c7b5d-x2kq9 -n shop
kubectl logs api-6d8f9c7b5d-x2kq9 -n shop --previous
kubectl get events -n shop --sort-by=.lastTimestamp | tail -20

describe → read the Events at the bottom and the State / Last State of the container. logs --previous shows why the last run crashed.

31.2

Step by step

  1. Read the STATUS column and look it up:
StatusMeaningUsual fix
PendingNo node was chosen yet.Events say why: not enough CPU/memory, taint, nodeSelector/affinity, PVC not bound.
ContainerCreating (long)Node chosen, the container can not start yet.Volume mount, Secret/ConfigMap missing, CNI (network) problem — see events.
ImagePullBackOff / ErrImagePullThe image can not be pulled.Name or tag wrong, registry needs imagePullSecrets, registry not reachable.
CreateContainerConfigErrorA referenced Secret, ConfigMap or key does not exist.Create it, or fix the name in the Pod spec.
CrashLoopBackOffThe container starts and dies again and again.kubectl logs --previous; app error, wrong command, missing env, failing probe.
OOMKilled (in Last State)It used more memory than its limit.Raise resources.limits.memory or fix the leak.
Init:0/1, Init:CrashLoopBackOffAn init container did not finish.kubectl logs POD -c INIT-NAME.
Running but 0/1 readyThe readiness probe fails.Probe path/port wrong, or the app is slow — check describe.
  1. Describe the Pod — the bottom part is the story:
kubectl describe pod api-6d8f9c7b5d-x2kq9 -n shop | sed -n '/Last State/,/Ready/p;/Events/,$p'
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
Events:
  Warning  BackOff  12s (x20 over 4m)  kubelet  Back-off restarting failed container api
  1. Get the exact reason in one line (good for scripts and tickets):
kubectl get pod api-6d8f9c7b5d-x2kq9 -n shop -o jsonpath='{.status.containerStatuses[0].state}{"\n"}{.status.containerStatuses[0].lastState.terminated.reason}{"\n"}'
  1. Pending? Check the nodes. Are there free resources, and does the Pod tolerate the taints?
kubectl describe node worker1 | grep -A8 'Allocated resources'
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints
kubectl get pvc -n shop

The scheduler event 0/3 nodes are available: 3 Insufficient memory means the Pod requests more than any node has free — lower the request or add nodes.

  1. Look at the owner — a broken Deployment change often shows in the ReplicaSet:
kubectl rollout status deploy/api -n shop
kubectl get rs -n shop
kubectl describe rs api-6d8f9c7b5d -n shop | tail

31.3

Check that it worked

After the fix, the new Pod is Running and 1/1 ready, and the restart count stays the same:

kubectl get pods -n shop -w
kubectl rollout status deploy/api -n shop
deployment "api" successfully rolled out

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

31.4

Common problems

ProblemCauseFix
Failed to pull image … not foundTypo in the image name or the tag does not exist.Check the tag in the registry; use the full name registry/project/app:tag.
pull access denied / unauthorizedPrivate registry, no pull secret.Create a docker-registry Secret and add imagePullSecrets.
secret "db-cred" not foundThe Secret is missing in this namespace.Secrets are per namespace — create it in shop.
0/3 nodes are available: … untolerated taintAll nodes have taints the Pod does not tolerate.Add a toleration, or schedule on other nodes.
pod has unbound immediate PersistentVolumeClaimsNo StorageClass / no free volume.kubectl get pvc,sc; set a StorageClass or create the PV.
CrashLoopBackOff, no logs at allThe process exits before it writes (wrong command or entrypoint).Check command/args; kubectl debug with a copy and sh.
Liveness probe failed and restartsThe probe kills a slow-starting app.Add a startupProbe or a longer initialDelaySeconds.

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

31.5

Go further

1A Pod is Pending. Where do you find the reason?

In the events: kubectl describe pod (bottom) or kubectl get events — usually resources, taints, affinity or an unbound PVC.

2A container is in CrashLoopBackOff. Which log do you need?

The log of the previous run: kubectl logs POD --previous.

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