DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 61

Using Init Containers for Application Pre-requisites in Kubernetes

I successfully utilized Kubernetes Init Containers to manage application pre-deployment prerequisites that couldn't be configured directly within the main application image. This approach ensures that critical setup tasks are completed before the application container starts, guaranteeing a ready and correctly configured environment.

This article details the scenario, the Kubernetes YAML manifest used, and the successful output validating the solution.

The Requirement

The team needed to deploy a simple application where a specific configuration file had to be created before the main application could start reading from it.

Key Specifications:

  • Deployment Name: ic-deploy-devops
  • Init Container (ic-msg-devops): Uses the debian:latest image to write the string "Init Done - Welcome to xFusionCorp Industries" into a file named /ic/ecommerce.
  • Main Container (ic-main-devops): Uses the debian:latest image to continuously read and print the contents of the /ic/ecommerce file.
  • Shared Volume (ic-volume-devops): An emptyDir volume used to pass the configuration file from the Init Container to the Main Container.

Kubernetes Deployment Manifest (ic-deployment.yaml)

The following YAML defines the Deployment, specifying the initContainers section to run first, followed by the main containers section. Both containers are configured to use the shared ic-volume-devops volume at the /ic mount path.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ic-deploy-devops
  labels:
    app: ic-devops
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ic-devops
  template:
    metadata:
      labels:
        app: ic-devops
    spec:
      # Init Container: Runs first to create the configuration file
      initContainers:
      - name: ic-msg-devops
        image: debian:latest
        command: ['/bin/bash', '-c']
        args: ['echo Init Done - Welcome to xFusionCorp Industries > /ic/ecommerce']
        volumeMounts:
        - name: ic-volume-devops
          mountPath: /ic

      # Main Container: Starts only after the Init Container successfully completes
      containers:
      - name: ic-main-devops
        image: debian:latest
        command: ['/bin/bash', '-c']
        args: ['while true; do cat /ic/ecommerce; sleep 5; done']
        volumeMounts:
        - name: ic-volume-devops
          mountPath: /ic

      # Volume: Provides shared storage for the setup file
      volumes:
      - name: ic-volume-devops
        emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

Execution and Validation

The team executed the deployment commands from the jump_host to apply the manifest, verify the status, and check the logs.

Terminal Output

The following logs confirm the successful execution of the deployment:

thor@jumphost ~$ vi ic-deployment.yaml
thor@jumphost ~$ kubectl apply -f ic-deployment.yaml
deployment.apps/ic-deploy-devops created

# Verification of Deployment Status
thor@jumphost ~$ kubectl get deployment ic-deploy-devops
NAME                READY   UP-TO-DATE  AVAILABLE   AGE
ic-deploy-devops    1/1     1           1           40s

# Verification of Pod Status
thor@jumphost ~$ kubectl get pods -l app=ic-devops
NAME                                     READY   STATUS    RESTARTS    AGE
ic-deploy-devops-754bd6c45c-ckzp7        1/1     Running   0           57s

# Check Logs of the Main Container
thor@jumphost ~$ kubectl logs ic-deploy-devops-754bd6c45c-ckzp7 -c ic-main-devops
Init Done - Welcome to xFusionCorp Industries
Init Done - Welcome to xFusionCorp Industries
Init Done - Welcome to xFusionCorp Industries
Init Done - Welcome to xFusionCorp Industries
... (continues to repeat)
thor@jumphost ~$
Enter fullscreen mode Exit fullscreen mode

Conclusion

The log output from the ic-main-devops container repeatedly printing "Init Done - Welcome to xFusionCorp Industries" is the final confirmation of success. This proves that the ic-msg-devops Init Container ran to completion, created the required configuration file in the shared emptyDir volume, and then the main application container successfully started and was able to access the pre-requisite file.

Init containers provide a reliable, clean, and decoupled way to handle setup logic and pre-deployment configurations in a Kubernetes environment.

Top comments (0)