Skip to main content

TASK 77: Init Containers in Kubernetes

Requirements

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-xfusion.

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

  • The initContainers should be named as ic-msg-xfusion, use image debian, preferably with latest tag and use command:

    • '/bin/bash',
    • '-c' and
    • 'echo Init Done - Welcome to xFusionCorp Industries > /ic/news'.
  • The volume mount should be named as ic-volume-xfusion and mount path should be /ic.

  • Main container should be named as ic-main-xfusion, use image debian, preferably 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-xfusion and mount path should be /ic.

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

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


Note

This document focuses on the implementation approach. The task statement in Requirements is preserved as the source requirement.

Prerequisites

  • Access to the configured Kubernetes cluster from jump_host.
  • kubectl is configured and points to the correct context.
  • Use the task-specified namespace, resource names, and images exactly.

Steps

Create the manifest for the resource definitions.

$ vi deploy.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: ic-deploy-xfusion
labels:
app: ic-xfusion
spec:
replicas: 1
selector:
matchLabels:
app: ic-xfusion
template:
metadata:
labels:
app: ic-xfusion
spec:
initContainers:
- name: ic-msg-xfusion
image: debian:latest
command:
[
"/bin/bash",
"-c",
"echo Init Done - Welcome to xFusionCorp Industries > /ic/news",
]
volumeMounts:
- name: ic-volume-xfusion
mountPath: /ic
containers:
- name: ic-main-xfusion
image: debian:latest
command:
[
"/bin/bash",
"-c",
"while true; do cat /ic/news; sleep 5; done",
]
volumeMounts:
- name: ic-volume-xfusion
mountPath: /ic
volumes:
- name: ic-volume-xfusion
emptyDir: {}

Apply.

kubectl apply -f deploy.yml

Check the deployment and pods. Ensure that they are running.

thor@jump_host ~$ k get all
NAME READY STATUS RESTARTS AGE
pod/ic-deploy-xfusion-c6755cb4f-4hr6t 1/1 Running 0 18s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8m2s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/ic-deploy-xfusion 1/1 1 1 18s

NAME DESIRED CURRENT READY AGE
replicaset.apps/ic-deploy-xfusion-c6755cb4f 1 1 1 18s

To verify the task, check the logs for the deployment. It should show the message:

thor@jump_host ~$ kubectl logs -f deployment/ic-deploy-xfusion

Defaulted container "ic-main-xfusion" out of: ic-main-xfusion, ic-msg-xfusion (init)
Init Done - Welcome to xFusionCorp Industries
Init Done - Welcome to xFusionCorp Industries
Init Done - Welcome to xFusionCorp Industries
Init Done - Welcome to xFusionCorp Industries

Verification

  • Confirm the required pods/services/deployments/jobs are in the expected state.
  • Run kubectl get commands for the affected resource types and namespace.

Resources