{"id":6297,"date":"2019-07-26T18:57:43","date_gmt":"2019-07-26T18:57:43","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6297"},"modified":"2021-11-16T11:11:38","modified_gmt":"2021-11-16T11:11:38","slug":"working-with-kubernetes-cluster-using-kubectl-part-10-pods-life-cycle","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/working-with-kubernetes-cluster-using-kubectl-part-10-pods-life-cycle\/","title":{"rendered":"Working with Kubernetes Cluster using Kubectl Part &#8211; 10 &#8211; Pods Life Cycle"},"content":{"rendered":"\n<p><strong>pod-restart-policy.yaml<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: v1\nkind: Pod\nmetadata:\n  name: hello-world-onfailure-pod\nspec:\n  containers:\n  - name: hello-world\n    image: gcr.io\/google-samples\/hello-app:1.0\n  restartPolicy: OnFailure\n---\napiVersion: v1\nkind: Pod\nmetadata:\n  name: hello-world-never-pod\nspec:\n  containers:\n  - name: hello-world\n    image: gcr.io\/google-samples\/hello-app:1.0\n  restartPolicy: Never\n<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\n\n<span class=\"hljs-comment\">#Start up kubectl get events --watch and background it.<\/span>\nkubectl get events --watch &amp;\nclear\n\n<span class=\"hljs-comment\">#Create a pod...we can see the scheduling, container pulling and container starting.<\/span>\nkubectl apply -f pod.yaml\n\n<span class=\"hljs-comment\">#We've used exec to launch a shell before, but we can use it to launch ANY program inside a container.<\/span>\n<span class=\"hljs-comment\">#Let's use killall to kill the hello-app process inside our container<\/span>\nkubectl exec -it hello-world-pod -- \/bin\/sh \nps\n<span class=\"hljs-keyword\">exit<\/span>\n\n<span class=\"hljs-comment\">#We still have our kubectl get events running in the background, so we see if re-create the container automatically.<\/span>\nkubectl exec -it hello-world-pod -- \/usr\/bin\/killall hello-app\n\n<span class=\"hljs-comment\">#Our restart count increased by 1 after the container needed to be restarted.<\/span>\nkubectl get pods\n\n<span class=\"hljs-comment\">#Look at Containers-&gt;State, Last State, Reason, Exit Code, Restart Count and Events<\/span>\n<span class=\"hljs-comment\">#This is because the container restart policy is Always by default<\/span>\nkubectl describe pod hello-world-pod\n\n<span class=\"hljs-comment\">#Cleanup time<\/span>\nkubectl delete pod hello-world-pod\n\n<span class=\"hljs-comment\">#Kill our watch<\/span>\nfg\nctrl+c\n\n<span class=\"hljs-comment\">#Remember...we can ask the API server what it knows about an object, in this case our restartPolicy<\/span>\nkubectl explain pods.spec.restartPolicy\n\n<span class=\"hljs-comment\">#Create our pods with the restart policy<\/span>\nmore pod-restart-policy.yaml\nkubectl apply -f pod-restart-policy.yaml\n\n<span class=\"hljs-comment\">#Check to ensure both pods are up and running, we can see the restarts is 0<\/span>\nkubectl get pods \n\n<span class=\"hljs-comment\">#Let's kill our apps in both our pods and see how the container restart policy reacts<\/span>\nkubectl exec -it hello-world-never-pod -- \/usr\/bin\/killall hello-app\nkubectl get pods\n\n<span class=\"hljs-comment\">#Review container state, reason, exit code, ready and contitions Ready, ContainerReady<\/span>\nkubectl describe pod hello-world-never-pod\n\n<span class=\"hljs-comment\">#let's use killall to terminate the process inside our container. <\/span>\nkubectl exec -it hello-world-onfailure-pod -- \/usr\/bin\/killall hello-app\n\n<span class=\"hljs-comment\">#We'll see 1 restart on the pod with the OnFailure restart policy.<\/span>\nkubectl get pods \n\n<span class=\"hljs-comment\">#Let's kill our app again, with the same signal.<\/span>\nkubectl exec -it hello-world-onfailure-pod -- \/usr\/bin\/killall hello-app\n\n<span class=\"hljs-comment\">#Check its status, which is now Error too...why? The backoff.<\/span>\nkubectl get pods \n\n<span class=\"hljs-comment\">#Let's check the events, we hit the backoff loop. 10 second wait. Then it will restart.<\/span>\n<span class=\"hljs-comment\">#Also check out State and Last State.<\/span>\nkubectl describe pod hello-world-onfailure-pod \n\n<span class=\"hljs-comment\">#Check its status, should be Running...after the Backoff timer expires.<\/span>\nkubectl get pods \n\n<span class=\"hljs-comment\">#Now let's look at our Pod statuses<\/span>\nkubectl delete pod hello-world-never-pod\nkubectl delete pod hello-world-onfailure-pod\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_74859\"><iframe loading=\"lazy\"  id=\"_ytid_89480\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_74859\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>pod-restart-policy.yaml<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-6297","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=6297"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6297\/revisions"}],"predecessor-version":[{"id":25474,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6297\/revisions\/25474"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}