Pages

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
metadata:
 name: testing-service
spec:
 containers:
   - name: test-ser
     image: docker.io/jagadesh1982/testing-service
     ports:
     - containerPort: 9876
 initContainers:
   - name:         counter
     image:        centos:7
     imagePullPolicy: IfNotPresent
     command:
         - "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/10.131.36.181
Start Time:   Mon, 21 May 2018 03:51:01 -0400
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.38.0.2
Init Containers:
 counter:
   Container ID:  docker://9b70db7b56681d380002666e69485b375ca707eca728675d27a3cf2bbc892226
   Image:         centos:7
   Image ID:      docker-pullable://docker.io/centos@sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
   Port:          <none>
   Host Port:     <none>
   Command:
     bin/bash
     -c
     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>
   Mounts:
     /var/run/secrets/kubernetes.io/serviceaccount from default-token-fx8mm (ro)
Containers:
 test-ser:
   Container ID:   docker://245fcadefece115124d225a2400f4d63da93ee2d59a6665c1aa3b03d55902775
   Image:          docker.io/jagadesh1982/testing-service
   Image ID:       docker-pullable://docker.io/jagadesh1982/testing-service@sha256:fa0894c592b7891c177a22bc61eb38ca23724aa9ff9b8ea3b713b32586a75c3d
   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>
   Mounts:
     /var/run/secrets/kubernetes.io/serviceaccount from default-token-fx8mm (ro)
Conditions:
 Type           Status
 Initialized    True
 Ready          True
 PodScheduled   True
Volumes:
 default-token-fx8mm:
   Type:        Secret (a volume populated by a Secret)
   SecretName:  default-token-fx8mm
   Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                node.kubernetes.io/unreachable:NoExecute for 300s
Events:
 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 "docker.io/jagadesh1982/testing-service"
 Normal  Pulled               54s kubelet, manja17-i14021  Successfully pulled image "docker.io/jagadesh1982/testing-service"
 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