Monday, 13 October 2025

Master Init Containers in Kubernetes: Pre-Deployment Configuration Made Easy

Our course you can check :-   Udemy course  


Ques:-   

There are some applications that need to be deployed on Kubernetes cluster and these apps have some pre-requisites where some configurations need to be changed before deploying the app container. Some of these changes cannot be made inside the images so the DevOps team has come up with a solution to use init containers to perform these tasks during deployment. Below is a sample scenario that the team is going to test first.

Create a Deployment named as ic-deploy-datacenter.

Configure spec as replicas should be 1, labels app should be ic-datacenter, template's metadata lables app should be the same ic-datacenter.

The initContainers should be named as ic-msg-datacenter, use image ubuntu with latest tag and use command '/bin/bash', '-c' and 'echo Init Done - Welcome to Devops Industries > /ic/news'. The volume mount should be named as ic-volume-datacenter and mount path should be /ic.

Main container should be named as ic-main-datacenter, use image ubuntu with latest tag and use command '/bin/bash', '-c' and 'while true; do cat /ic/news; sleep 5; done'. The volume mount should be named as ic-volume-datacenter and mount path should be /ic.

Volume to be named as ic-volume-datacenter and it should be an emptyDir type.


Ans:-

Here’s the complete Kubernetes YAML manifest for your scenario using init containers to perform pre-deployment configuration tasks:

raj@jumphost ~$ cat pod.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: ic-deploy-datacenter

  namespace: default

spec:

  replicas: 1

  selector:

    matchLabels:

      app: ic-datacenter

  template:

    metadata:

      labels:

        app: ic-datacenter

    spec:

      initContainers:

      - name: ic-msg-datacenter

        image: ubuntu:latest

        command: ["/bin/bash", "-c", "echo Init Done - Welcome to Devops Industries > /ic/news"]

        volumeMounts:

        - name: ic-volume-datacenter

          mountPath: /ic

      containers:

      - name: ic-main-datacenter

        image: ubuntu:latest

        command: ["/bin/bash", "-c", "while true; do cat /ic/news; sleep 5; done"]

        volumeMounts:

        - name: ic-volume-datacenter

          mountPath: /ic

      volumes:

      - name: ic-volume-datacenter

        emptyDir: {}


raj@jumphost ~$ kubectl apply -f pod.yaml 

deployment.apps/ic-deploy-datacenter created


raj@jumphost ~$ kubectl get all

NAME                                        READY   STATUS    RESTARTS   AGE

pod/ic-deploy-datacenter-7fb8794c8b-nbnlf   1/1     Running   0          16s


NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE

service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   21m


NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/ic-deploy-datacenter   1/1     1            1           17s


NAME                                              DESIRED   CURRENT   READY   AGE

replicaset.apps/ic-deploy-datacenter-7fb8794c8b   1         1         1       17s


raj@jumphost ~$ kubectl logs ic-deploy-datacenter-7fb8794c8b-nbnlf -c ic-main-datacenter

Init Done - Welcome to Devops Industries

Init Done - Welcome to Devops Industries

Init Done - Welcome to Devops Industries

Init Done - Welcome to Devops Industries

raj@jumphost ~$ 


repeating every 5 seconds.


Conclusion:-

In this hands-on tutorial, you'll learn how to use init containers in Kubernetes to handle pre-deployment tasks that can't be baked into application images. We'll walk through a real-world scenario where an init container sets up configuration files before the main application container starts.

You’ll learn how to:

  • Create a Kubernetes Deployment with init and main containers
  • Use emptyDir volumes to share data between containers
  • Configure init containers to write setup messages
  • Run a main container that reads and displays the setup output continuously

This course is ideal for DevOps engineers, SREs, and Kubernetes learners who want to understand how to use init containers for advanced deployment workflows.

No comments:

Post a Comment