0
0
Kubernetesdevops~5 mins

Init containers in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your main app needs some setup before it starts. Init containers run first to prepare things like files or settings, so your app can run smoothly.
When you need to download configuration files before your app starts
When you want to wait for a database to be ready before launching your app
When you need to set up permissions or create directories before the main container runs
When you want to run a script that checks or fixes something before the app starts
When you want to separate setup tasks from the main app for better organization
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  initContainers:
  - name: init-setup
    image: busybox
    command: ['sh', '-c', 'echo Setting up environment; sleep 5']
  containers:
  - name: main-app
    image: nginx
    ports:
    - containerPort: 80

This YAML defines a Pod with two containers.

initContainers: Runs first to do setup tasks. Here it just prints a message and waits 5 seconds.

containers: The main app container runs after the init container finishes successfully.

Commands
This command creates the Pod with the init container and main container defined in pod.yaml. It starts the setup process first.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/example-pod created
Check the status of the Pod to see if the init container has finished and the main container is running.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-pod 1/1 Running 0 10s
See detailed information about the Pod, including the init container status and logs if needed.
Terminal
kubectl describe pod example-pod
Expected OutputExpected
Name: example-pod Namespace: default Status: Running Init Containers: init-setup: Container ID: docker://abc123 Image: busybox State: Terminated Reason: Completed Last State: Terminated Reason: Completed Containers: main-app: Container ID: docker://def456 Image: nginx State: Running Started: Thu, 01 Jun 2024 12:00:00 +0000
Key Concept

Init containers run and finish before the main containers start, ensuring setup tasks complete first.

Common Mistakes
Not defining init containers under the correct spec key 'initContainers'
Kubernetes will not recognize the setup container as an init container and may run it alongside the main container.
Always place setup containers under 'initContainers' in the Pod spec to run them first.
Init container command fails or never completes
The main container will never start because init containers must finish successfully first.
Ensure init container commands complete successfully and handle errors properly.
Expecting init containers to keep running after main container starts
Init containers always stop after finishing; they do not run alongside main containers.
Use regular containers for long-running processes, init containers only for setup.
Summary
Define init containers in the Pod spec under 'initContainers' to run setup tasks before the main app.
Use 'kubectl apply -f pod.yaml' to create the Pod with init containers.
Check Pod status with 'kubectl get pods' and details with 'kubectl describe pod example-pod' to verify init container completion.