Configuration, probes and resources
ConfigMaps, Secrets, liveness and readiness probes wired to Actuator, requests and limits, and the QoS class you got.
A pod template says what image to run. Three other things decide whether the service actually works in the cluster: where its configuration and secrets come from, how the cluster learns whether it is alive and ready, and how much memory and CPU it is allowed — the last of which is the setting behind almost every "the pod keeps restarting" ticket.
ConfigMaps and Secrets
A ConfigMap is key-value configuration as a cluster object; a Secret is the same with base64 encoding and, if the cluster is set up for it, encryption at rest and tighter access control. Either reaches the container as environment variables or as files:
env:
- { name: SPRING_PROFILES_ACTIVE, valueFrom: { configMapKeyRef: { name: orders-config, key: profile } } }
- { name: DATABASE_URL, valueFrom: { secretKeyRef: { name: orders-db, key: url } } }
volumeMounts:
- { name: secrets, mountPath: /run/secrets, readOnly: true }
volumes:
- { name: secrets, secret: { secretName: orders-db } }Environment variables are read once at start; a mounted file updates in place when the ConfigMap changes (within a minute), which Spring can pick up with spring.config.import=configtree:/run/secrets/ and a refresh — and which is the argument for mounting secrets rather than injecting them, along with the Docker course's point that an environment variable is visible to every process and every kubectl describe. Base64 is not encryption: a Secret object committed to a Git repository is a plaintext credential with extra steps. The pattern that works is a secrets manager (Vault, the cloud's) synced into cluster Secrets by an operator, so the repository holds references and never values — the same rule as .env.example in this repository.
A change to a ConfigMap does not restart the pods that use it as environment variables. The usual trick is a checksum of the ConfigMap in a pod annotation, so a config change is a template change and rolls the Deployment.
Probes: three questions, three endpoints
The kubelet asks a pod three things, and getting the endpoints right is the difference between a cluster that heals and one that kills healthy pods:
| Probe | Question | On failure | Endpoint for Spring Boot |
|---|---|---|---|
| startup | has it finished starting? | keep waiting, up to failureThreshold × periodSeconds; the other probes are paused | /actuator/health/liveness |
| liveness | is it alive, or wedged? | restart the container | /actuator/health/liveness |
| readiness | can it take traffic now? | remove it from the Service's endpoints; no restart | /actuator/health/readiness |
startupProbe: { httpGet: { path: /actuator/health/liveness, port: 8080 }, periodSeconds: 5, failureThreshold: 30 }
livenessProbe: { httpGet: { path: /actuator/health/liveness, port: 8080 }, periodSeconds: 10, failureThreshold: 3 }
readinessProbe: { httpGet: { path: /actuator/health/readiness, port: 8080 }, periodSeconds: 5, failureThreshold: 2 }The Actuator lesson separated liveness from readiness for exactly this: a liveness check that includes the database restarts your pods when the database is slow, which turns a database incident into a restart storm on top of it. Liveness should fail only when the JVM itself is wedged; readiness should fail when a dependency the pod needs is unavailable, so traffic goes elsewhere while it recovers. The startup probe with a generous threshold is what lets a Spring Boot application take 60 seconds on a throttled CPU without the liveness probe killing it at 30; before startup probes existed, initialDelaySeconds did that job badly.
Graceful shutdown is the readiness probe run backwards: on SIGTERM Spring Boot fails readiness first (server.shutdown=graceful makes the readiness state REFUSING_TRAFFIC), the endpoint list drops the pod, in-flight requests finish, then the process exits. terminationGracePeriodSeconds (default 30) must exceed the shutdown timeout, and a preStop sleep of a few seconds covers the window before every kube-proxy has seen the endpoint change.
Requests and limits
resources:
requests: { cpu: "1", memory: "1Gi" }
limits: { memory: "1Gi" }A request is what the scheduler reserves: a pod is placed on a node with that much unallocated, and the CPU request is its guaranteed share under contention. A limit is what the kernel enforces: a memory limit is the cgroup limit the Java-in-a-container lesson's JVM reads and the kernel kills at; a CPU limit is a quota that throttles.
The two rules that follow, and that the community has argued to a consensus on: memory request equals limit, so the pod is never scheduled somewhere it will be killed for using what it needs; and a CPU request with no CPU limit, so the JVM sees a sensible processor count from the request and can burst into idle cores instead of being throttled at a quota — throttling produces the latency spikes the container lesson described, and a limit buys nothing on a node that is not contended. The exception is a multi-tenant node where one runaway pod must not starve the others; there, a limit is a fence, and a generous one.
QoS: who gets killed first
From requests and limits the cluster assigns a quality of service class, and it is the order of eviction when a node runs short:
- Guaranteed — every container has requests equal to limits for both CPU and memory. Evicted last.
- Burstable — some requests, not all equal to limits. The recommended shape above is here.
- BestEffort — no requests or limits at all. Evicted first, and scheduled anywhere, and throttled to nothing under contention. A pod with no resources block is a pod you have told the cluster you do not care about.
OOMKilled, and reading it
kubectl describe pod shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137. That is the kernel, not Java: the container's total memory hit the limit. The JVM's own OutOfMemoryError is a different event, exit code 1 with a stack trace, and it is why -XX:+ExitOnOutOfMemoryError matters — without it a heap-exhausted JVM stays up, fails no probe and serves nothing.
For OOMKilled, the checklist from the container lesson: is MaxRAMPercentage leaving room for non-heap; is direct memory or Metaspace growing (Native Memory Tracking); are there more threads than expected (each stack is off-heap); is the limit simply too small for the workload. kubectl top pod shows the working set against the limit, and a pod that sits at 95% of its limit is not fine, it is one allocation from a restart. Raise the limit and the request together, and find out why before the next one.
Under the hood: how the kubelet runs a probe, and how the kernel enforces a limit
The table above says what each probe means. What actually executes it, and what actually stops a container at its memory limit, are two separate mechanisms worth being precise about — both because they explain behaviour the table cannot, and because they are the two things a production incident forces you to reason about under pressure.
Probe execution: three different mechanisms wearing one config shape
httpGet, exec and tcpSocket look like three options in the same YAML block, but the kubelet runs them completely differently:
httpGetis an HTTP GET the kubelet process itself issues, from the node, straight topodIP:port/path— it never goes through the container's own network stack beyond the pod's IP, and it never goes through the Service, which is why a probe endpoint does not need a Service to exist at all. A response in the success range counts as a pass; anything else, including a connection refused because the process has not bound the port yet, counts as a failure.execruns the command inside the container's namespaces, through the container runtime's exec API (the same mechanismkubectl execuses) — the kubelet does not run the command itself, it asks the runtime to run it in that container, and reads the exit code:0is success, anything else is failure.tcpSocketjust attempts a TCP handshake againstpodIP:portand immediately closes it — no bytes sent, no response read, so it can pass against a port that is open but serving garbage.
All three run on their own timer per container per probe (periodSeconds, after an initial initialDelaySeconds), each with a hard timeoutSeconds (default one second) after which a slow response counts as a failure, not a retry. A single failure does not flip the probe's state: it takes failureThreshold consecutive failures (default three) to go from passing to failing, and — for liveness and startup — exactly one success to go back, which is why a liveness probe recovers the moment the app responds again but takes a few probe periods to notice it stopped.
Requests, limits and cgroups: two different kernel mechanisms, not one
A CPU request becomes a cgroup CPU shares value (cpu.shares under cgroup v1, cpu.weight under v2) — a relative scheduling weight the kernel's CFS scheduler only consults when CPUs are actually contended. It sets no ceiling: a pod can use far more than its request when the node has idle cycles, which is exactly the point of requesting honestly and not limiting.
A CPU limit, by contrast, becomes a CFS quota and period (cpu.cfs_quota_us / cpu.cfs_period_us, period defaulting to 100 ms). If a container's cgroup burns through its quota before the period ends, the kernel stops scheduling it for the rest of that period — full stop, regardless of whether the node has idle CPU elsewhere. This is why a CPU limit produces the container lesson's latency spikes even when average utilisation looks fine on a dashboard: throttling happens inside a 100 ms window, invisibly to any metric that only samples once a second, and a request handler that needed 15 ms of CPU can lose the rest of its period mid-request if the container had already spent its quota on something else in that same 100 ms slice. cpu.stat inside the cgroup (nr_periods, nr_throttled, throttled_time) is the ground truth for this, and it is what the Prometheus metric container_cpu_cfs_throttled_periods_total (from cAdvisor / kubelet) exposes cluster-wide — the real diagnostic for "the JVM is slow but CPU usage looks low."
A memory limit has no equivalent soft mechanism: it becomes the cgroup's hard memory ceiling (memory.limit_in_bytes under v1, memory.max under v2), and there is no throttling option for memory the way there is for CPU — a process cannot be told to "use memory more slowly." The instant the cgroup's usage would cross that ceiling, the kernel's OOM killer runs, scoped to that cgroup, and sends SIGKILL to the process it picks (usually the largest). That is the entire mechanism behind OOMKilled, exit 137: not Kubernetes deciding anything, the kernel enforcing a number it was given.
Why a limit with no request is a scheduling trap, not a missing value
It is tempting to think "a limit without a request" leaves the request unset, making the pod BestEffort. It does not: Kubernetes' API defaulting sets the request to equal the limit whenever a container specifies a limit and no request, for both CPU and memory. The pod is scheduled as if it needs exactly the limit, all the time — which means a generous limit set "just in case" now reserves that much capacity on a node even when the container typically uses a fraction of it, making the pod harder to place and wasting bin-packable capacity across the cluster. The failure mode is not an unscheduled resources block; it is a request nobody wrote down, chosen for you, and usually too high.
Try it yourself
Predict which mechanism kills the container, and which one just slows it
A container has resources: { requests: { cpu: "500m" }, limits: { memory: "512Mi" } } — no memory request written, no CPU limit written. Two separate things happen to it in production: (1) under a burst, its CPU usage spikes to 3 full cores' worth of demand for a few hundred milliseconds while the node has spare CPU; (2) later, a memory leak grows its heap until the process's total memory usage would exceed 512Mi.
Using the cgroup mechanics above, predict what happens in each case — is the container throttled, killed, or left alone — and what the pod's effective memory request actually is, given only a limit was written.
Why the CPU spike is untouched but the memory growth is fatal
No CPU limit was set, so there is no CFS quota configured for this container at all — its CPU usage is bounded only by its cpu.shares weight (from the 500m request) relative to other cgroups when CPUs are contended, and this scenario says the node has spare CPU, so the burst to 3 cores is simply scheduled and served; nothing throttles it. The memory case is different in kind, not degree: 512Mi is a hard memory.max/memory.limit_in_bytes ceiling with no soft mode, so the instant total usage would cross it the kernel's OOM killer sends SIGKILL to the container's process — OOMKilled, exit 137, no warning period, no throttling first. And because a memory limit was specified with no memory request, Kubernetes' defaulting set the effective memory request to 512Mi too (equal to the limit) — this container reserves the full 512Mi on its node from the moment it is scheduled, even though the leak means it only actually needs that much right before it is killed.
Misconceptions
- "A CPU limit smooths out spikes, it doesn't cause them." The opposite: a CFS quota is enforced per 100 ms period with a hard cutoff, so a container that front-loads its work within a period gets throttled for the remainder of that exact period even while overall usage looks low on any coarser-grained dashboard — the limit is what manufactures the latency spike, not what prevents one.
- "A resources block with only a limit and no request leaves the request unset." Kubernetes' API server fills the request in automatically, equal to the limit, for both CPU and memory when only a limit is given — the pod is never BestEffort in that case, it is scheduled as if it always needs the full limit.
Sources
- Kubernetes documentation, "Configure Liveness, Readiness and Startup Probes" — probe mechanics, thresholds and timeouts.
- Kubernetes documentation, "Assign Memory Resources to Containers and Pods" — the memory request defaulting from a limit, and OOM behaviour.
- Kubernetes documentation, "Assign CPU Resources to Containers and Pods" — CPU shares versus CFS quota and period.
cgroups(7)man page — thecpu,cpuacctandmemorycgroup controllers the kubelet configures.