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.
Create a
Deployment
named asic-deploy-nautilus
.Configure
spec
as replicas should be1
, labelsapp
should beic-nautilus
, template's metadata lablesapp
should be the sameic-nautilus
.The
initContainers
should be named asic-msg-nautilus
, use imagefedora
, preferably withlatest
tag and use command'/bin/bash'
,'-c'
and'echo Init Done - Welcome to xFusionCorp Industries > /ic/official'
. The volume mount should be named asic-volume-nautilus
and mount path should be/ic
.Main container should be named as
ic-main-nautilus
, use imagefedora
, preferably withlatest
tag and use command'/bin/bash'
,'-c'
and'while true; do cat /ic/official; sleep 5; done'
. The volume mount should be named asic-volume-nautilus
and mount path should be/ic
.Volume to be named as
ic-volume-nautilus
and it should be anemptyDir
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