Wednesday, September 2, 2026

Init Containers

 


What is an Init Container?


An Init Container is a special container that performs startup tasks before the main application container starts. It is used for initialization tasks such as checking dependencies, preparing configurations, or waiting for services to become available. The application container starts only after the Init Container completes successfully.



Why Use Init Containers?


Init Containers are commonly used for:


Checking if a database is available

Waiting for another service to start

Downloading configuration files

Initializing data or directories



How It Works


Step 1: Init Container Runs

           ↓

Step 2: Init Container Completes

           ↓

Step 3: Main Application Container Starts




Example:



apiVersion: v1

kind: Pod

metadata:

  name: init-container-demo


spec:

  initContainers:

  - name: init-myservice

    image: busybox

    command: ['sh', '-c', 'echo Initializing...; sleep 10']


  containers:

  - name: nginx

    image: nginx



Create the Pod - kubectl apply -f pod.yaml




What Happens?


Init Container starts.

Waits for 10 seconds.

Exits successfully.

Nginx container starts.





Real-Life Example


Suppose your application needs MySQL to be available before starting.



Init Container

      |

      |--> Check MySQL Connection

      |

Success?

      |

     Yes

      |

Application Starts




Without Init Container?


Application Starts

       |

MySQL Not Ready

       |

Application Fails



With Init Container?


Wait for MySQL

       |

MySQL Ready

       |

Application Starts Successfully




Verify Init Container


Check Pod details - kubectl describe pod init-container-demo

View logs - kubectl logs init-container-demo -c init-myservice




No comments:

Post a Comment

testing