Q: How do you configure resource requests and limits for init containers?
To configure resource requests and limits for init containers, set resources on each init container just like an app container, but remember the scheduler computes the pod's effective request as the larger of the biggest single.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>configure resource requests and limits for init containers</strong>, set <code>resources</code> on each init container just like an app container, but remember the scheduler computes the pod's effective request as the larger of the biggest single init container and the sum of all app containers. Init containers run one at a time, so their needs do not add up. Interviewers ask this because a heavy migration init container can quietly inflate a pod's footprint and block scheduling.
Mental model: effective request is a max, not a sum
Init containers run sequentially to completion before app containers start, which changes how Kubernetes sums resources.
- Init side: the effective init request for a resource is the highest request among all init containers, since only one runs at a time.
- App side: app containers run together, so their requests are added.
- Pod effective request: the larger of the two values, per resource, plus any configured Pod Overhead. That value is what the scheduler reserves on the node.
- Limits: the same logic applies to the effective limit. Each container is still throttled or OOM-killed against its own limit while it runs.
- Sidecars: init containers with
restartPolicy: Always(native sidecars, beta and on by default from 1.29) keep running, so their requests are added to the app total instead of being compared as a max. - Why this surprises people: the sum-of-containers rule from app containers is widely known, so many teams forget init containers also reserve capacity at scheduling time, even though they exit before the app starts.
Hands-on: configure resource requests and limits for init containers
Give every init container explicit values; unset values default to nothing unless a LimitRange injects them.
apiVersion: v1
kind: Pod
metadata:
name: my-app
namespace: prod
spec:
initContainers:
- name: db-migrate
image: registry.example.com/migrate:1.4.0
resources:
requests: {cpu: 500m, memory: 128Mi}
limits: {cpu: "1", memory: 256Mi}
containers:
- name: app
image: registry.example.com/my-app:1.0.0
resources:
requests: {cpu: 200m, memory: 256Mi}
limits: {cpu: 500m, memory: 512Mi}
- name: proxy
image: registry.example.com/proxy:2.0.0
resources:
requests: {cpu: 100m, memory: 64Mi}
limits: {cpu: 200m, memory: 128Mi}
- Set both: requests for scheduling, limits for runtime enforcement. A migration tool that spikes memory should have a limit so it cannot starve neighbours.
- Worked example: init request 500m CPU and 128Mi memory; app containers requesting 200m plus 100m CPU and 256Mi plus 64Mi memory. Effective CPU is max(500m, 300m) = 500m, effective memory is max(128Mi, 320Mi) = 320Mi.
- QoS class: init containers participate; for Guaranteed QoS every container, including init, needs requests equal to limits for CPU and memory.
- Ephemeral storage: init containers that download large artifacts should also set
ephemeral-storagerequests and limits. - Do the arithmetic per resource: CPU, memory and ephemeral storage are each compared separately, so the winning container for CPU can differ from the winner for memory.
Verify what the scheduler actually reserved
Do not compute by hand in production; read it from the cluster.
kubectl -n prod get pod my-app -o jsonpath='{.status.qosClass}{"\n"}'
kubectl -n prod describe pod my-app | sed -n '/Init Containers/,/Conditions/p'
kubectl describe node worker-1 | sed -n '/Allocated resources/,/Events/p'
- Pod view:
kubectl describe pod my-app -n prodlists init and app container resources and the QoS class. - Node view:
kubectl describe node <node>under Allocated resources and the per-pod table shows the effective request counted against the node. - Scheduling failures: a Pending pod with Insufficient cpu or Insufficient memory in events may be caused purely by the init container request.
- OOM during init:
kubectl get podshowingInit:OOMKilledorInit:CrashLoopBackOffmeans the init limit is too low. - Spot-check with a script: list each pod's init and app requests with jsonpath or jq to find pods whose init requests dominate, then right-size them.
Edge cases: LimitRange, quotas and in-place changes
Namespace policy applies to init containers too.
- LimitRange: type
Containerdefaults, minimums and maximums are applied to init containers, so a default of 1Gi memory can silently raise your effective request. - ResourceQuota: the quota counts the pod's effective request, so oversized init containers consume quota too.
- Immutability: container resources are immutable on a running pod in most versions; in-place pod resize is a newer alpha/beta feature and is version dependent, so check your cluster before relying on it.
- Native sidecars: if an init container needs to keep running, mark it as a sidecar and size it as a long-running container.
- Limits versus requests: an init limit higher than the namespace LimitRange max is rejected, and a limit lower than the request is invalid; both errors appear at pod creation.
Production practice: keep init containers lean
Treat init containers as a cost on every pod start.
- Right-size from data: measure peak usage of migration or download jobs and set limits slightly above it.
- Move heavy work out: run database migrations as a separate Job rather than in every replica's init container.
- Policy: enforce that init containers declare resources using the same admission policy as app containers.
- Observability: watch init container duration and restarts, since slow init delays rollouts.
- Reuse the same image where possible: smaller, purpose-built init images pull faster and need less ephemeral storage, which shortens pod start-up.
- Init containers declare resources like any container, but the pod's effective request is the max of the largest init container and the sum of app containers.
- I verify with kubectl describe pod and the node's allocated resources, and read Pending events for Insufficient cpu or memory.
- I enforce resources on init containers through LimitRange and admission policy and move heavy migrations into Jobs.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Adding init container requests to the app total They run sequentially, so the scheduler uses the max, and over-adding leads to wrong capacity planning.
- Leaving init containers without limits A runaway download or migration can exhaust node memory and trigger evictions of neighbours.
- Ignoring LimitRange defaults The namespace default can inflate the init request without any change to the manifest.
- Putting long-running helpers in a normal init container They would block the app forever; use a native sidecar (restartPolicy: Always) or a regular container.