Configuring Init Containers

1. Understanding Init Container Purpose

PropertyBehavior
OrderRun sequentially before app containers
Must succeedEach must complete (exit 0); else Pod restarts
No probesLiveness/readiness not applicable
Use casesWait for dependencies, fetch config, run migrations, set up filesystem

2. Defining Init Containers

Example: Wait for database

spec:
  initContainers:
  - name: wait-db
    image: busybox:1.36
    command: ["sh","-c","until nc -z db 5432; do sleep 1; done"]
  containers:
  - name: app
    image: myapp:1.0

3. Setting Init Container Order

RuleDetail
SequentialDefined list order = execution order
FailureRestart per Pod restartPolicy; back-off doubles
ResourcesEffective request = max(initContainers, sum(appContainers))

4. Sharing Volumes with Main Containers

Example: Init writes, app reads

volumes:
- name: shared
  emptyDir: {}
initContainers:
- name: fetch
  image: curlimages/curl
  command: ["sh","-c","curl -o /data/config.json https://cfg/svc"]
  volumeMounts: [{ name: shared, mountPath: /data }]
containers:
- name: app
  image: myapp
  volumeMounts: [{ name: shared, mountPath: /etc/app }]
Volume TypeUse
emptyDirMost common for handoff
PVCPersist across pod restarts

5. Setting Init Container Resources

FieldNote
requests/limitsSame fields as app containers
Effective Pod requestmax(highest init, sum(app))

6. Handling Init Container Failures

restartPolicyFailure Behavior
Always / OnFailureRestart failed init container (back-off)
NeverPod marked Failed
kubectl describe pod <pod>   # Init Containers section shows reason
kubectl logs <pod> -c <init-name>

7. Debugging Init Containers

CommandPurpose
kubectl logs POD -c INITView init logs
kubectl logs POD -c INIT --previousLogs from prior attempt
kubectl describe pod PODStatus, reason, last state

8. Using Init Containers for Dependencies

initContainers:
- name: wait-redis
  image: busybox
  command: ["sh","-c","until nslookup redis; do sleep 2; done"]
- name: wait-api
  image: curlimages/curl
  command: ["sh","-c","until curl -sf http://api/health; do sleep 2; done"]
PatternProbe
DNS checknslookup service
TCP checknc -z host port
HTTP checkcurl -sf URL/health

9. Running Database Migrations

Example: Flyway migration init

initContainers:
- name: migrate
  image: flyway/flyway:10
  args: ["-url=jdbc:postgresql://db/app","-user=app","migrate"]
  env:
  - name: FLYWAY_PASSWORD
    valueFrom: { secretKeyRef: { name: db, key: password } }
Note: Per-pod migrations run on every replica restart. For one-shot migrations prefer a Job gated by a Helm hook or Argo sync wave.

10. Downloading Configuration Files

SourceInit Image
HTTP/Scurlimages/curl
Gitalpine/git
Vaultvault + vault kv get
AWS S3amazon/aws-cli