Bartosz Bierkowski - Low dose cloud

OpenShift Morsels: execute actions before your pod starts using init containers

On day #19 I will introduce you to init containers.

Pod may consist of multiple containers, that is clear. Since a while, also init containers are part of the pod. They run before your main containers start, are executed in order one after another and do run until completion. They can be very useful for scenarios that require some kind of initialization of your application. Although it is additional container, their usage actually may simplify runtime behaviour of your application.

Some scenarios I can imagine:

  • initialization of data or database
  • recovery of data before starting of your application
  • caching artifacts on persistent volume
  • setup of configuration specific to environment to make the app environment agnostic

Below you can see an init container for an application based on nodejs image. It is just a fragment of the full deployment config. I used the same node image that is used in main container, but I print out only the environment variables.

The fields are basically the same fields you can find in the pod. You can attach the volume that later will be used in the main application. The init container will be stopped before your application starts, so there is no issue with two containers using the same volume.

There is another reason I write about init containers. The early version of init containers is good to picture the differences between labels and annotations. In Kubernetes prior to 1.6 and OpenShift < 3.6, the init containers used annotations to store their configuration. At the moment as you can see the description above, normal fields in spec are used to store configuration (which is better in the long term).

To get the output of the environment variables printed out in the container above, you need to use the -c  parameter and specify container name.

This will print out the log of the init container. You can also use it to debug more or much more complex information.

Documentation

OpenShift Morsels: labels vs annotations – my post about difference between labels and annotations

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/ – kubernetes documentation about init containers

https://docs.openshift.com/container-platform/3.6/architecture/core_concepts/pods_and_services.html#pods-services-init-containers – OpenShift documentation in the same topic

Environment

The commands were executed using minishift and the following client/server versions of OpenShift.

Client:
oc v3.7.0+7ed6862
client kubernetes v1.7.6+a08f5eeb62
Server:
openshift v3.6.0+c4dd4cf
server kubernetes v1.6.1+5115d708d7

Newsletter

Thanks for reading the OpenShift morsels. To get updates about new articles, you can sign up to the newsletter below.

As a thank you message, you will also get access to OpenShift CLI CheatSheet listing most commonly used commands together with a short explanation.

Did you like the article?
Join the newsletter to receive notifications about new articles.
I respect your privacy.