Pages

Monday, May 21, 2018

Kubernetes - Container Life Cycle

Kubernetes lets us to attach life cycle events to containers that are going to create. We can attach the events to containers so that they execute along with containers. 

There are 2 events called postStart and preStart. K8 sends the postStart event immediately after the container is created. The postStart handler runs asynchronously relative to the Container’s code, but Kubernetes’ management of the container blocks until the postStart handler completes. The Container’s status is not set to RUNNING until the postStart handler completes.

K8 sends the preStop event immediately before the container is terminated. the container termination will be blocked until the preStop event is completed.

Lets create a pod manifest with PreStop and postStart events as,
[root@manja17-I13330 kubenetes-config]# cat lifecycle-pod.yml
apiVersion: v1
kind: Pod
metadata:
 name: testing-service
spec:
 containers:
   - name: test-ser
     image: docker.io/jagadesh1982/testing-service
     ports:
     - containerPort: 9876
     lifecycle:
         postStart:
           exec:
             command: ["/bin/sh", "-c", "touch /tmp/hello"]
         preStop:
           exec:
             command: ["/bin/sh", "-c", "echo Hello from the preSop handler > /tmp/hello"]

In the above manifest file ,we have 2 lifecycle events defined in which postStart talks about creating a file hello in /tmp and preStop event is kicked when the container is going to terminate.

The life cycle events can have similar health check events that we discussed earlier including httpGet, tcpCheck and exec.

Now lets create the pod using "kubectl" command and when we login to the pod using,
[root@manja17-I13330 kubenetes-config]# kubectl exec  testing-service -it -- bash
root@testing-service:/usr/src/app# ls /tmp/
hello
root@testing-service:/usr/src/app# exit
exit

We can see that the file is created. This file is created as a part of the postStart event which runs before the container is created.

More to come , Happy learning :-)

2 comments :

  1. I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
    shipping container

    ReplyDelete
  2. I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site.
    shipping container

    ReplyDelete