Monday, May 21, 2018

Kubernetes - Init Containers

In kubernetes, a Pod can have multiple containers running apps. Beside these application containers we can also have a init Containers which run before the app containers are started.

So lets take a use case, we want to have a application Container running in a Pop which has a Volume attached to the Pod. our requirement is that before the application container starts in the pod we need to have some files in our volumes so that once the application containers are up and running we can have the data ready. This is the use case where we can use init Containers.

Create a pod manifest file using,
[root@manja17-I13330 kubenetes-config]# cat initContainer.yml
apiVersion: v1
kind: Pod
 name: testing-service
   - name: test-ser
     - containerPort: 9876
   - name:         counter
     image:        centos:7
     imagePullPolicy: IfNotPresent
         - "bin/bash"
         - "-c"
         - "for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done"

Once we create the pod using the kubectl command, we can describe the pod using, [root@manja17-I13330 kubenetes-config]# kubectl describe pod testing-service
Name:         testing-service
Namespace:    default
Node:         manja17-i14021/
Start Time:   Mon, 21 May 2018 03:51:01 -0400
Labels:       <none>
Annotations:  <none>
Status:       Running
Init Containers:
   Container ID:  docker://9b70db7b56681d380002666e69485b375ca707eca728675d27a3cf2bbc892226
   Image:         centos:7
   Image ID:      docker-pullable://
   Port:          <none>
   Host Port:     <none>
     for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done
   State:          Terminated
     Reason:       Completed
     Exit Code:    0
     Started:      Mon, 21 May 2018 03:51:03 -0400
     Finished:     Mon, 21 May 2018 03:51:03 -0400

   Ready:          True
   Restart Count:  0
   Environment:    <none>
     /var/run/secrets/ from default-token-fx8mm (ro)
   Container ID:   docker://245fcadefece115124d225a2400f4d63da93ee2d59a6665c1aa3b03d55902775
   Image ID:       docker-pullable://
   Port:           9876/TCP
   Host Port:      0/TCP
   State:          Running
     Started:      Mon, 21 May 2018 03:51:07 -0400
   Ready:          True
   Restart Count:  0
   Environment:    <none>
     /var/run/secrets/ from default-token-fx8mm (ro)
 Type           Status
 Initialized    True
 Ready          True
 PodScheduled   True
   Type:        Secret (a volume populated by a Secret)
   SecretName:  default-token-fx8mm
   Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations: for 300s
       for 300s
 Type    Reason               Age From        Message
 ----    ------               ---- ----        -------
 Normal  SuccessfulMountVolume  59s kubelet, manja17-i14021  MountVolume.SetUp succeeded for volume "default-token-fx8mm"
 Normal  Pulled               58s kubelet, manja17-i14021  Container image "centos:7" already present on machine
 Normal  Created               58s kubelet, manja17-i14021  Created container
 Normal  Started               57s kubelet, manja17-i14021  Started container
 Normal  Pulling               57s kubelet, manja17-i14021  pulling image ""
 Normal  Pulled               54s kubelet, manja17-i14021  Successfully pulled image ""
 Normal  Created               53s kubelet, manja17-i14021  Created container
 Normal  Started               53s kubelet, manja17-i14021  Started container
 Normal  Scheduled              30s default-scheduler        Successfully assigned testing-service to manja17-i14021

This is how we will be using a init Container.

No comments :

Post a Comment