Back to articles

What happens between kubectl apply and a running pod

kubectl apply returns, and at that moment nothing is running: one object has been written to a database. This article follows a Deployment through every hand-over that comes next, from admission and the controllers to the scheduler, the kubelet, probes and EndpointSlices, with the place each stage can stall and the command that shows it.

kubectl apply -f deployment.yaml prints deployment.apps/web created and returns. At that moment nothing is running: no pod exists, no node has been chosen, no image has been pulled. One object has been written to a database. Everything after that is done by a chain of independent components, each watching the API for something to do, and each able to stall without the others noticing. When a rollout hangs, the question is never "why doesn't Kubernetes start my pod" but "which link in the chain is waiting, and for what".

Who this is for: engineers who deploy to Kubernetes and want to debug a stuck rollout by reasoning, not by trying commands until one shows something.

What you walk away with: the full sequence for a Deployment, from the client to a pod receiving traffic, with the place each stage can stall and the command that shows it.

Versions: Kubernetes v1.37.

The sequence at a glance

# Stage Component Typical stall Where to look
1 Build and send the request kubectl Invalid manifest, apply conflict kubectl's own output
2 Authentication, authorisation API server 401, 403 kubectl's own output
3 Admission and validation API server, webhooks, policies Rejected or timed-out request kubectl's output; for pods, events on the ReplicaSet
4 Persist API server, etcd Slow or unavailable etcd API server latency, timeouts
5 Deployment to ReplicaSet to Pods kube-controller-manager Quota or admission rejects the pods kubectl describe rs
6 Choose a node kube-scheduler Pod Pending, FailedScheduling kubectl describe pod
7 Sandbox, network, volumes kubelet, container runtime, CNI, CSI ContainerCreating Pod events
8 Pull images kubelet, runtime ErrImagePull, ImagePullBackOff Pod events
9 Init containers, then containers kubelet, runtime Init:0/1, CrashLoopBackOff kubectl logs, container status
10 Probes and readiness kubelet Running but 0/1 ready Pod conditions, Unhealthy events
11 Endpoints EndpointSlice controller, kube-proxy Service has no ready endpoints kubectl get endpointslices

Stages 1 to 4 are synchronous: you get the answer in your terminal. Everything from stage 5 on happens after kubectl has returned, and failures there are reported only in object status and events. Events are kept for one hour by default (--event-ttl), so read them before they expire; the events guide explains how.

1. kubectl: two kinds of apply

kubectl reads the manifest and sends a request. What it sends depends on the apply mode.

Client-side apply is what plain kubectl apply does; in kubectl v1.37 the --server-side flag still defaults to false. kubectl fetches the live object, compares your file, the live object and the copy of your previous file stored in the kubectl.kubernetes.io/last-applied-configuration annotation (which the documentation calls legacy), and computes a patch.

Server-side apply (kubectl apply --server-side) sends your manifest as it is, and the API server does the merge. The server records which field manager owns which field in metadata.managedFields. If your manifest sets a field that another manager owns to a different value, the request fails with a conflict instead of silently overwriting it. You can then drop the field from your manifest, or take ownership with --force-conflicts.

The difference shows when two parties write one field, for instance your manifest and a HorizontalPodAutoscaler both setting replicas. Client-side, the last writer wins and nobody is told; server-side, you get an error that names the other manager.

kubectl apply --server-side --dry-run=server -f deployment.yaml   # through admission and validation, nothing stored
kubectl get deployment web --show-managed-fields -o yaml          # who owns which field

Where it stalls: it doesn't; it fails, visibly. A manifest that doesn't parse, an unknown kind or an apply conflict comes back as an error in the terminal.

2. API server: who are you, and may you

Authentication comes first and establishes an identity from the request: a client certificate, a bearer token, an OIDC token. If no authenticator accepts the request, the answer is 401 Unauthorized.

Authorisation asks whether that identity may perform this verb on this resource in this namespace. With RBAC, some Role or ClusterRole bound to the user or one of its groups must allow it. If not, the answer is 403 Forbidden, with a message that names the user, the verb and the resource (reading those messages).

kubectl auth whoami
kubectl auth can-i create deployments -n shop

Note what is checked: permission to create a Deployment. The pods are created later by a controller under its own identity, so the user does not need permission to create pods.

3. Admission: the object may still change, or be refused

An authorised write then passes through admission: two phases, with validation by the API server between them.

  1. Mutating admission. Built-in plugins, mutating admission policies and mutating webhooks may modify the object: set defaults, inject a sidecar, add a toleration.
  2. Object validation. The API server validates the resulting object for its type.
  3. Validating admission. Built-in plugins, validating admission policies and validating webhooks may reject the object. They cannot change it.

Among the plugins enabled by default in v1.37 are LimitRanger, ServiceAccount, PodSecurity, Priority, DefaultTolerationSeconds and ResourceQuota. This is where a LimitRange fills in missing requests and where a quota is charged.

Where it stalls: a webhook that does not answer holds the request until its timeout, 10 seconds by default, and then its failurePolicy decides whether the request is rejected or let through. Admission webhooks and how they fail covers this in depth.

One subtlety for a Deployment: policies written for pods have nothing to check yet. The Deployment can be admitted while the pods it will create are going to be refused; that failure surfaces at stage 5; your terminal gets at most a Pod Security warning.

4. Persist: the object becomes real

The API server serialises the object and writes it to etcd. The write is committed once a majority of etcd members has persisted it; then the API server answers 201 Created and kubectl prints its one line.

Every downstream component holds a watch on the resources it cares about and receives the new object as a watch event: components react to state, they are not invoked.

Where it stalls: if etcd is slow or has lost quorum, writes are slow or fail. The API server's etcd_request_duration_seconds metric shows it.

5. Controllers: Deployment, ReplicaSet, Pod

The Deployment controller, in kube-controller-manager, sees the new Deployment and creates a ReplicaSet for its pod template. It computes a hash of the template and adds it as the pod-template-hash label to the ReplicaSet, its selector and its pods. On an update it creates a new ReplicaSet and shifts replicas from old to new within maxSurge and maxUnavailable, both 25% by default.

The ReplicaSet controller sees a ReplicaSet with fewer pods than desired and creates Pod objects from the template. Each pod gets a generated name, an owner reference to the ReplicaSet, and an empty spec.nodeName. Each creation is an API request of its own that goes through stages 2 to 4 again, under the controller's identity. This is the moment pod-level admission runs: Pod Security, quota, webhooks that match pods.

Where it stalls: if pod creation is rejected, no pod ever exists, and kubectl get pods shows nothing. The Deployment sits at 0/3. The evidence is one level down:

kubectl rollout status deployment/web
kubectl describe deployment web     # conditions: Available, Progressing, ReplicaFailure
kubectl describe replicaset -l app=web   # FailedCreate events: "Error creating: <API server's message>"

A ReplicaFailure condition with reason FailedCreate on the Deployment, and the matching events on the ReplicaSet, carry the API server's message: a quota that is exceeded, a Pod Security violation, a webhook that could not be called. If the rollout makes no progress for progressDeadlineSeconds, 600 by default, the Deployment's Progressing condition gets the reason ProgressDeadlineExceeded. Kubernetes reports that; it does not roll back for you.

6. Scheduler: a node is chosen

The pods now exist with phase Pending and no node, which is what the scheduler watches for. For each pod it filters out the nodes that cannot run it (insufficient requested resources, untolerated taints, affinity rules, volume constraints), scores the rest, picks the best and binds the pod: the node's name is written into the pod through the API, with a Scheduled event. How the scheduler picks a node explains the filtering and scoring.

Where it stalls: when no node passes the filters the pod stays Pending, and the scheduler records a FailedScheduling event that counts the nodes rejected per reason. The pending pods decision tree works through the causes; see also priority and preemption.

kubectl describe pod <pod>      # FailedScheduling events at the bottom

7. Kubelet: sandbox, network, volumes

The kubelet on the chosen node watches for pods bound to its node.

First the kubelet runs its own admission check. A pod rejected here goes to phase Failed, which can happen under node pressure. Then the node prepares the pod:

When all of that is done, the kubelet sets the PodReadyToStartContainers condition to True. The condition is stable in v1.37, and it splits "the node could not prepare the pod" from "the containers have a problem": image pulls and container creation come after it.

Where it stalls: the pod shows ContainerCreating, or Init:0/N if it has init containers, with PodReadyToStartContainers at False. The causes are in the pod's events: FailedMount or FailedAttachVolume for a Secret or ConfigMap volume whose source does not exist or a volume that cannot attach or mount, FailedCreatePodSandBox when the runtime or the CNI plugin fails, for example a CNI plugin that is not running yet on a new node or has run out of addresses.

kubectl get pod <pod> -o jsonpath='{range .status.conditions[*]}{.type}={.status}{"  "}{end}{"\n"}'
kubectl describe pod <pod>      # events at the bottom

8. Image pull

For each container the kubelet checks, according to imagePullPolicy, whether the image must be pulled, and asks the runtime to do it, with credentials from imagePullSecrets if there are any. When the field is omitted the policy is IfNotPresent, or Always if the tag is :latest or the image has neither tag nor digest. By default the kubelet pulls one image at a time per node (serializeImagePulls: true), so a pod can wait behind another pod's large image.

Where it stalls: ErrImagePull after a failed attempt, then ImagePullBackOff while the kubelet waits before retrying, with a delay that grows to at most 300 seconds. The message of the Failed event says which kind of failure: the tag does not exist, the registry refused the credentials, the registry cannot be reached, a rate limit. ImagePullBackOff explained takes these apart. A slow pull is not an error: the pod stays in ContainerCreating with a Pulling event and no Pulled event after it.

9. Init containers, then containers

Init containers run one at a time, in order, and each must exit successfully before the next starts. An init container with restartPolicy: Always is a sidecar: it is started in its place in the order, and once it has started the kubelet moves on while it keeps running. When the last init container is done, the Initialized condition becomes True, and the kubelet creates and starts the main containers.

Where it stalls: Init:1/3 means one of three init containers is done and the second is not: waiting for a dependency, or failing and being restarted (Init:CrashLoopBackOff). A container whose configuration cannot be built, for instance because env refers to a Secret or ConfigMap that does not exist, waits in CreateContainerConfigError. A main container whose process exits is restarted with an exponential back-off (10s, 20s, 40s and so on, capped at 300 seconds), which the pod shows as CrashLoopBackOff.

kubectl logs <pod> -c <init-container>
kubectl logs <pod> --previous       # output of the instance that crashed
kubectl get pod <pod> -o jsonpath='{range .status.containerStatuses[*]}{.name}{"\t"}{.state}{"\t"}{.lastState.terminated.reason}{"\n"}{end}'

CrashLoop forensics and investigating OOMKilled continue from here.

10. Probes: from Running to Ready

Phase Running means all containers have been created and at least one is running, starting or restarting. It does not mean the application can serve; probes, run by the kubelet, decide that:

The pod's Ready condition becomes True when ContainersReady is true and every condition listed in spec.readinessGates is true as well. Readiness gates let an external controller hold a pod back.

Where it stalls: kubectl get pods shows Running with 0/1 in the READY column. kubectl describe pod shows Unhealthy events with the probe's failure: connection refused, an HTTP status, a timeout. A container with no readiness probe counts as ready as soon as it is running, which avoids this stall by sending traffic to an application that may not be listening yet. Health probes done right covers the settings.

For a Deployment, readiness also drives the rollout: a new pod counts as available only once it has been ready for minReadySeconds (0 by default), and old pods are removed as new ones become available.

11. EndpointSlices: traffic arrives

The EndpointSlice controller watches Services and pods. For every pod matching a Service's selector it maintains an endpoint in an EndpointSlice labelled kubernetes.io/service-name=<service>, with three conditions: serving follows the pod's Ready condition, terminating is set once the pod is being deleted, and ready is in effect "serving and not terminating". kube-proxy, or whatever implements Services in your cluster, watches EndpointSlices and programs each node to send traffic to ready endpoints.

Where it stalls: the pod is Ready and the Service answers with connection refused or times out. Two things to check first: the Service's selector not matching the pod's labels, which gives no endpoints, and the Service's targetPort not matching the port the container listens on, which gives an endpoint pointing at a closed port.

kubectl get endpointslices -l kubernetes.io/service-name=web -o wide
kubectl get pods -l app=web --show-labels

Removal works the same way in reverse, which is why graceful shutdown matters during upgrades.

Summary

kubectl apply stores a wish. Controllers, the scheduler, the kubelet, probes and the EndpointSlice controller each take it one step further. Each hand-over goes through the API and leaves a trace: a condition, a status, an event. A stuck rollout is the first hand-over that did not happen. Find the last object that looks right; the component that should have acted on it is where to look.