Init Containers in Kubernetes

Ticker

6/recent/ticker-posts

Init Containers in Kubernetes

 Question : 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-devops.

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

The initContainers should be named as ic-msg-devops, use image debian, preferably with latest tag and use command '/bin/bash', '-c' and 'echo Init Done - Welcome to xFusionCorp Industries > /ic/official'. The volume mount should be named as ic-volume-devops and mount path should be /ic.

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

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

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

Please Note:-  Perform the below commands based on your question server,  user name & other details that might differ. So please read the task carefully before executing it. 
All the Best 👍
Solution:  

1. Check existing running Pods  & Services 

thor@jump_host ~$ kubectl get pods

No resources found in default namespace.

thor@jump_host ~$

thor@jump_host ~$ kubectl get services

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

kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   13m

thor@jump_host ~$ kubectl get deploy

No resources found in default namespace.

thor@jump_host ~$ 


2.  Create a YAML  file with all the parameters,  Kindly do the changes as per task

you can copy from GitLab    https://gitlab.com/nb-tech-support/devops.git

    Refer Below Video for more clarity )

thor@jump_host ~$ vi /tmp/init.yaml

thor@jump_host ~$ cat /tmp/init.yaml

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:

      volumes:

        - name: ic-volume-devops

          emptyDir: {}

      initContainers:

        - name: ic-msg-devops

          image: debian:latest

          command:

            [

              "/bin/bash",

              "-c",

              "echo Init Done - Welcome to xFusionCorp Industries > /ic/official",

            ]

          volumeMounts:

            - name: ic-volume-devops

              mountPath: /ic

 

      containers:

        - name: ic-main-devops

          image: debian:latest

          command:

            [

              "/bin/bash",

              "-c",

              "while true; do cat /ic/official; sleep 5; done",

            ]

          volumeMounts:

            - name: ic-volume-devops

              mountPath: /ic

thor@jump_host ~$

3. Run the below command to create a pod 

thor@jump_host ~$ kubectl create -f /tmp/init.yaml

deployment.apps/ic-deploy-devops created

thor@jump_host ~$ 


4.  Wait for the pod to get completed status

thor@jump_host ~$ kubectl get deploy

NAME               READY   UP-TO-DATE   AVAILABLE   AGE

ic-deploy-devops   0/1     1            0           23s

thor@jump_host ~$

thor@jump_host ~$ kubectl get pods

NAME                               READY   STATUS    RESTARTS   AGE

ic-deploy-devops-59554f444-5dbmx   1/1     Running   0          29s

thor@jump_host ~$


5.  Validate the task by logs & cat the file

thor@jump_host ~$ kubectl logs -f ic-deploy-devops-59554f444-5dbmx

Init Done - Welcome to xFusionCorp Industries

Init Done - Welcome to xFusionCorp Industries

Init Done - Welcome to xFusionCorp Industries

Init Done - Welcome to xFusionCorp Industries

Init Done - Welcome to xFusionCorp Industries

^C

thor@jump_host ~$

thor@jump_host ~$ kubectl exec ic-deploy-devops-59554f444-5dbmx -- cat /ic/official

Init Done - Welcome to xFusionCorp Industries

thor@jump_host ~$


6.  Click on Finish & Confirm to complete the task successfully

Happy Learning!!!!

Apart from this if you need more clarity,  I have made a  tutorial video on this, please go through and share your comments. Like and share the knowledge 




Post a Comment

1 Comments

  1. Dear User, for New update in task regards to ENV . Please use updated Yaml file give in link below

    https://gitlab.com/nb-tech-support/devops/-/blob/master/Kubernetes%20Time%20Check%20Pod%20with%20ENV](https://gitlab.com/nb-tech-support/devops/-/blob/master/Kubernetes%20Time%20Check%20Pod%20with%20ENV)

    ReplyDelete

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user