Init Containers

This is part three of a series of lab tasks from KodeKloud for Kubernetes. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.

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.

  1. Create a Deployment named as ic-deploy-nautilus.

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

  3. The initContainers should be named as ic-msg-nautilus, use image fedora, 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-nautilus and mount path should be /ic.

  4. Main container should be named as ic-main-nautilus, use image fedora, 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-nautilus and mount path should be /ic.

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

The solution to this task is straight forward and needs fewer steps.

$ kubectl get pods
No resources found in default namespace.
$

$ kubectl get deploy
No resources found in default namespace.
$

$ kubectl get services 
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   67m
$

Create deployment YAML specification using dry-run options of kubectl:

$ kubectl create deployment ic-deploy-nautilus --replicas=1 \
--image=fedora:latest --dry-run=client -o yaml > ic-deployment.yaml

Edit the file, add section for initContainers and other changes as per problem statement. Generated and updated YAML can be found on GitHub link.

And create the deployment:

$ kubectl create -f ic-deployment.yaml
# Verify
$ kubectl get pods
$ kubectl logs -f <REPLACE_WITH_REAL_POD>

Clean up

Delete deployment:

$ kubectl delete deployment ic-deployment.yaml