1 00:00:00,401 --> 00:00:03,867 Kubernetes provides container lifecycle hooks to 2 00:00:03,867 --> 00:00:07,267 trigger commands on Container lifecycle events. 3 00:00:07,267 --> 00:00:11,301 If we recall, container lifecycle had 5 stages, 4 00:00:11,301 --> 00:00:17,034 Created, Running, Paused, Stopped and Deleted. 5 00:00:17,034 --> 00:00:20,767 Out of these 5, kubectl provides lifecycle hooks 6 00:00:20,767 --> 00:00:25,234 for two of the states, which are Created and Stopped. 7 00:00:26,301 --> 00:00:28,234 Let's explore both of these using 8 00:00:28,234 --> 00:00:32,433 lifecycle-pod.yaml file. This is a standard 9 00:00:32,433 --> 00:00:36,067 nginx Pod named lifecyc-pod. And under the 10 00:00:36,067 --> 00:00:39,567 containers spec, we have two lifecycle hooks called 11 00:00:39,567 --> 00:00:43,201 postStart and preStop. These hooks functionality 12 00:00:43,201 --> 00:00:46,467 is pretty much as their name suggests. Both of 13 00:00:46,467 --> 00:00:48,867 them have handlers attached to them, which are 14 00:00:48,867 --> 00:00:52,334 executable commands, postStart hooks handler will 15 00:00:52,334 --> 00:00:57,167 'echo Welcome' to a file called poststart-msg and it 16 00:00:57,167 --> 00:00:59,734 will trigger after the container will enter the 17 00:00:59,734 --> 00:01:02,834 Created state. This is the state where resources 18 00:01:02,834 --> 00:01:05,667 for read-write layer are reserved, but the 19 00:01:05,667 --> 00:01:09,267 container is not running yet. In other words, the 20 00:01:09,267 --> 00:01:13,301 latest cmd or entrypoint instruction is yet to be 21 00:01:13,301 --> 00:01:16,001 executed. The hook works synchronously with the 22 00:01:16,001 --> 00:01:19,701 Pod's container creation process. Which means if 23 00:01:19,701 --> 00:01:22,934 for some reason the handler of the hook hangs or 24 00:01:22,934 --> 00:01:26,301 fails to execute, the pod will remain in container 25 00:01:26,301 --> 00:01:29,467 Created state and won't go into the Running state. 26 00:01:29,867 --> 00:01:32,567 To brief things up, first of all the container will 27 00:01:32,567 --> 00:01:35,001 be created, then the postStart hook will be 28 00:01:35,001 --> 00:01:38,501 handled, and the message will be printed. And then 29 00:01:38,501 --> 00:01:41,401 the container will start running by executing cmd 30 00:01:41,401 --> 00:01:44,667 or entrypoint command. A general use of this hook 31 00:01:44,667 --> 00:01:47,567 is for better debugging, just like try and catch 32 00:01:47,567 --> 00:01:51,101 clause in the programming. But it also brings the 33 00:01:51,101 --> 00:01:53,967 burden of stalling the container if the hook 34 00:01:53,967 --> 00:01:57,634 doesn't get handled properly. So if Pod events and 35 00:01:57,634 --> 00:02:00,534 logs are sufficient for your debugging, you might 36 00:02:00,534 --> 00:02:04,801 want to skip using this hook. Lastly, we have preStop hook, 37 00:02:04,801 --> 00:02:07,234 which triggers before termination of the 38 00:02:07,234 --> 00:02:10,001 container. We are simply quitting the nginx 39 00:02:10,001 --> 00:02:12,866 process before terminating the container. But if 40 00:02:12,866 --> 00:02:15,301 you want to strongly verify this hook, you can 41 00:02:15,301 --> 00:02:17,734 apply SIGKILL to one of the container's crucial 42 00:02:17,734 --> 00:02:20,667 processes and you would find the container exited 43 00:02:20,667 --> 00:02:23,867 with a respective error code. Let's exit the file 44 00:02:23,867 --> 00:02:25,234 and create the Pod. 45 00:02:25,234 --> 00:02:29,034 [No Audio] 46 00:02:29,034 --> 00:02:31,601 30 seconds down and the Pod is ready. 47 00:02:31,934 --> 00:02:33,801 I know we have sold the benefits of 48 00:02:33,801 --> 00:02:37,167 containers a lot, but it is always amusing to see 49 00:02:37,167 --> 00:02:39,934 such a level of managed isolation being created 50 00:02:39,934 --> 00:02:43,167 with such less efforts and within such a short time. 51 00:02:43,267 --> 00:02:46,134 Now let's execute the Pod with kubectl exec 52 00:02:46,134 --> 00:02:51,401 command and run bash on it. cat the file poststart-msg 53 00:02:51,401 --> 00:02:55,234 and bingo the hook was executed successfully. 54 00:02:55,434 --> 00:02:59,367 The message is loud and clear. Well not that loud, 55 00:02:59,467 --> 00:03:01,901 but it is quite clear. In the next lecture, 56 00:03:01,901 --> 00:03:04,767 we will see how to replace container cmd command