What are init containers and why do you need them?

When we talk about Kubernetes, we talk about pods and containers. There is a difference between the pod and a container. When I was first introduced with pods I mistook them as containers. In this article we will see the difference between them and what are init containers and why do we need them.

POD vs Container

A pod can run multiple containers sharing the same network namespace. It means that each container can see the other container on the localhost. This is great functionality when you want to run two parts of the app that need to connect very frequently. When they connect on the localhost it will be very fast and no packet will leave the pod.

Its like VM with different types of processes and those process are like containers running in them.

What are init Containers?

These are the normal containers except they do some tasks before the main containers and always exits, this means they are not long-running containers but the containers have some specialized tasks. They come to do the task and complete after that the main containers start to come up.

A single pod can have multiple init containers. They can run one by one and complete their tasks.

Below is the workflow of how they look like.

What are they used for?

These containers are used to prepare the pod to run the main application containers. These tasks can be changing permissions of some files to changing some specific environments. They can also be used as a precheck to verify if the application can be run on this pod.

There may be some tasks that only the root user can do. And since you never want to run your application as a root user as this is a security threat. Init containers can do the task for you that you wanted to run as a root user.

This was what are init containers and why do you need them? Subscribe for more articles on kubernetes, SRE, DevOps, and sysadmin.

Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.