Kubernetes Deep Dive Part 1 : Init Containers and Lifecycle Hooks

Atharva Chauthaiwale
2 min readMay 28, 2018

I was introduced to Kubernetes about an year ago. It is amazing to see this project evolving so rapidly and becoming one of the top choices of building modern containerized distributed applications. In this deep dive series, I am planning to write about some advanced concepts in Kubernetes which I learned as part of my job. In the first part, I will compare and contrast Init Containers and Lifecycle Hooks. Readers should be familiar with basic concepts of Kubernetes such Pod, ReplicaSet, Deployment etc. Let’s begin !

What are Init Containers

  • Init Containers support most of the features of regular containers. However, they serve a special purpose. Init Containers always run before App Container in a Pod.
  • Init Containers have separate images from app Containers.
  • Unlike regular containers they run to completion ( Don’t run forever )
  • There can be series of init-containers in a pod. They execute one after the other before app container runs.
  • If one of the init containers fails, Pod is restarted.

Example:

apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['sh', '-c', 'echo The app is running! && sleep 3600'] initContainers: - name: init-mydb image: busybox command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

( Reference : https://kubernetes.io/docs/concepts/workloads/pods/init-containers/ )

What are Lifecycle hooks

Example:

  • Kubernetes supports container lifecycle hooks which allows a handler code/script to be executed when Container lifecycle event occurs.
  • There are two lifecycle hooks. PostStart hook executes immediately after container is created. PreStop hook is called just before the container is terminated. Both the hooks take no parameters.
  • K8s support two types of handlers for the lifecycle hooks. Exec handler is used for calling shell command/script while HTTP handler is used for calling HTTP endpoint
apiVersion: v1 kind: Pod metadata: name: lifecycle-demo spec: containers: - name: lifecycle-demo-container image: nginx lifecycle: preStop: exec: command: ["/usr/sbin/nginx","-s","quit"]

Init Containers vs Lifecycle Hooks

Usage scenarios

Init Containers

Lifecycle Hooks

  • Delay or Block the startup of app Containers until some set of preconditions are met. E.g . sleep 60
  • In some cases if one Pod has startup dependency on another ( Avoid as much as possible)
  • For multi-container Pod, perform common task required at startup
  • Perform cleanup before container is terminated with preStop hook
  • Save state of container before termination with preStop hook
  • Perform post configuration tasks on application startup inside container.

Conclusion

Both Init Containers and Lifecycle hooks are powerful constructs provided by Kubernetes and can help us solve many common problems in container deployments.

References

  1. https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/
  2. https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
  3. https://dzone.com/articles/kubernetes-lifecycle-of-a-pod

Looking for more of the latest headlines on LinkedIn?

Discover more stories

Originally published at https://www.linkedin.com on May 28, 2018.

--

--

Atharva Chauthaiwale

Cloud and Container enthusiast, blogger, open source contributor