Sub topic 6
How init containers affect resource requests
This chapter assumes you can write Pod manifests with resources.requests and resources.limits. You should also know that the scheduler places Pods on nodes based on requests. You should have read the earlier chapters on init container ordering and native sidecars.
Why init containers need special accounting
The scheduler needs one request number per resource for each Pod. A Pod's containers do not all run at the same time. Regular init containers run one after another and exit before the app containers start. Native sidecars (init containers with restartPolicy: Always) keep running after they start. The accounting rules follow from these two facts.
The rules
Kubernetes computes the Pod's effective request for CPU and memory separately. It takes the larger of two values:
- Steady state: the sum of all app container requests, plus the sum of all native sidecar requests.
- Init peak: for each regular init container, its own request plus the requests of every native sidecar listed before it. The highest of these values is the init peak.
The scheduler reserves this effective request on the node for the whole life of the Pod. It does not shrink the reservation once the init containers finish.
Regular init containers are never added together, because only one runs at a time. A native sidecar is added to every later phase, because it is still running during them.
The same formula applies to limits. The Pod-level effective limit is what the kubelet uses to size the Pod's cgroup.
Worked example: regular init containers only
apiVersion: v1
kind: Pod
metadata:
name: web-resources
spec:
initContainers:
- name: migrate
image: busybox:1.36
command: ['sh', '-c', 'echo migrating; sleep 5']
resources:
requests:
cpu: 500m
memory: 256Mi
- name: warm-cache
image: busybox:1.36
command: ['sh', '-c', 'echo warming; sleep 5']
resources:
requests:
cpu: 100m
memory: 64Mi
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 200m
memory: 128Mi
- name: proxy
image: nginx:1.27
resources:
requests:
cpu: 100m
memory: 64MiWork out each resource:
| Phase | CPU | Memory |
|---|---|---|
| Init container `migrate` | 500m | 256Mi |
| Init container `warm-cache` | 100m | 64Mi |
| App containers (`app` + `proxy`) | 300m | 192Mi |
| **Effective Pod request** | **500m** | **256Mi** |
The migrate container has the highest CPU and memory values, so it sets the Pod's request. The app containers need only 300m and 192Mi, but the scheduler still reserves 500m and 256Mi for the Pod's entire life.
Worked example: with a native sidecar
Now add a sidecar and an init container that runs after it:
apiVersion: v1
kind: Pod
metadata:
name: web-sidecar-resources
spec:
initContainers:
- name: log-shipper
image: busybox:1.36
restartPolicy: Always
command: ['sh', '-c', 'tail -F /dev/null']
resources:
requests:
cpu: 100m
memory: 64Mi
- name: migrate
image: busybox:1.36
command: ['sh', '-c', 'echo migrating; sleep 5']
resources:
requests:
cpu: 400m
memory: 128Mi
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 200m
memory: 256Milog-shipper is a sidecar listed before migrate, so it is already running when migrate runs.
| Phase | CPU | Memory |
|---|---|---|
| `migrate` + running sidecar | 400m + 100m = 500m | 128Mi + 64Mi = 192Mi |
| `app` + sidecar | 200m + 100m = 300m | 256Mi + 64Mi = 320Mi |
| **Effective Pod request** | **500m** | **320Mi** |
CPU is set by the init phase, and memory is set by the steady state. Each resource takes its own maximum. The Pod's effective request is therefore 500m CPU and 320Mi memory, even though no single phase uses both of those values.
If you moved log-shipper after migrate in the list, migrate would no longer count the sidecar. Its phase would drop to 400m and 128Mi. The sidecar would then only add to the steady-state sum, so the effective CPU request would be 400m. List order matters for sidecars.
Checking the result
Run kubectl describe node <node-name>. The "Non-terminated Pods" table lists the CPU and memory requests the scheduler counted for each Pod on that node. Compare the numbers with your own calculation.
Common errors
- Adding all init containers together. Regular init containers run one at a time, so only the largest counts.
- Forgetting that init requests are not released. A heavy init container makes the whole Pod harder to schedule, even if it runs for two seconds. Give init containers modest requests where you can.
- Ignoring sidecars in the init peak. A sidecar that starts earlier adds to every later init container's phase.
- Setting an init container's limit below what it needs. An init container that is killed for exceeding its memory limit fails and is retried, and the Pod stays in an Init state.
- Expecting separate accounting per resource. CPU and memory are computed independently, so the Pod's effective values can come from different phases.
Exercise
A Pod has a native sidecar listed first (request 50m CPU), then a regular init container (request 300m CPU), then two app containers (150m CPU each). What is the effective CPU request?
Show answer
The init phase is 300m + 50m = 350m. The steady state is 150m + 150m + 50m = 350m. The larger of the two is 350m, so the effective CPU request is 350m.