{"id":1092,"date":"2023-09-14T03:17:00","date_gmt":"2023-09-14T03:17:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=1092"},"modified":"2023-09-14T03:17:00","modified_gmt":"2023-09-14T03:17:00","slug":"kubernetes-pod-lifecycle","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=1092","title":{"rendered":"Kubernetes Pod Lifecycle Explained With Examples"},"content":{"rendered":"<p>In this blog, we will look at the lifecycle of a Kubernetes pod with examples and illustrations.<\/p>\n<p>If you are new to the concepts of pods, please read the <a href=\"https:\/\/devopscube.com\/kubernetes-pod\/\">Kubernetes pod<\/a> blog to understand all the basics and do hands-on on creating and managing pods.<\/p>\n<p>To understand the lifecycle of a pod, we will look a the following<\/p>\n<ol>\n<li>Pod Phases<\/li>\n<li>Pod Conditions<\/li>\n<li>Container Status<\/li>\n<\/ol>\n<p>To understand the concepts better, let&#8217;s assume a multi-container pod with the following<\/p>\n<ol>\n<li>An init container to fetch an API secret in the runtime.<\/li>\n<li>container-01 (java-api) runs the <strong>java application<\/strong><\/li>\n<li>container-02 (log-reader) fetches the application logs and sends them to a log forwarder.<\/li>\n<\/ol>\n<p>We will understand the pod lifecycle by understanding what happens when we deploy the pod with the above requirements.<\/p>\n<h2 id=\"pod-phases\">Pod Phases<\/h2>\n<p>When you deploy a pod, it could typically fall under any one of the following phases.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table>\n<thead>\n<tr>\n<th>Po Phases<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Pending<\/strong><\/td>\n<td>The Pod is created but not yet running.<\/td>\n<\/tr>\n<tr>\n<td><strong>Running<\/strong><\/td>\n<td>At least one container is running, or is in the process of starting or restarting.<\/td>\n<\/tr>\n<tr>\n<td><strong>Succeeded<\/strong><\/td>\n<td>All containers have been completed successfully.<\/td>\n<\/tr>\n<tr>\n<td><strong>Failed<\/strong><\/td>\n<td>At least one container has failed.<\/td>\n<\/tr>\n<tr>\n<td><strong>Unknown<\/strong><\/td>\n<td>The Pod status couldn&#8217;t be obtained by the API server.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<p>Now let us understand these phases with a <strong>real-time example<\/strong> using our application pod. Let&#8217;s call our pod <strong>java-api-pod<\/strong><\/p>\n<p>Here is how the pod phases work when you deploy a pod multi-container pod.<\/p>\n<h3 id=\"1-pending-phase\">1. Pending Phase<\/h3>\n<p>When you deploy the api-pod, it will be in the pending phase. Before the Pod moves to the &#8220;<strong>Running<\/strong>&#8221; phase, the <strong>init container will run to completion<\/strong> before any other containers start.<\/p>\n<p>Following are some of the common scenarios where a Pod continues to be in the pending stage.<\/p>\n<ol>\n<li>Lack of and CPU &amp; Memory resources for the pod.<\/li>\n<li>If the pod has a volume definition and the volume is not available.<\/li>\n<li>If Kubernetes can&#8217;t pull the container image.<\/li>\n<li>If an init container fails to start<\/li>\n<\/ol>\n<p>If the init container fails with a non-zero exit code and the <strong>restartPolicy<\/strong> is set to <strong><code>Never<\/code><\/strong>, the pod directly goes to the failed phase.<\/p>\n<h3 id=\"2-running-phase\">2. Running Phase<\/h3>\n<p>After the init container finishes fetching the secret, the Pod moves to the &#8220;Running&#8221; phase. Both <strong>container-01<\/strong> and <strong>container-02<\/strong> will start. Because our Java application is an API that needs to be running, the Pod will stay in the &#8220;<strong>Running<\/strong>&#8221; phase as long as the Java API application is up. For some reason, if any of the containers fail to start, the pod goes to the failed phase.<\/p>\n<h3 id=\"3-succeeded-phase-\">3. Succeeded Phase:<\/h3>\n<p>This phase is not applicable for our Java application, because &#8220;<strong>Succeeded<\/strong>&#8221; is for containers that complete their tasks and then exit.<\/p>\n<p>Our <strong>java-api-pod<\/strong> is meant to keep running, so it won&#8217;t reach this phase unless you manually stop it.<\/p>\n<p>The succeeded phase is applicable for pods that are part of Kubernetes Job\/Cronjob Objects.<\/p>\n<h3 id=\"4-failed-phase\">4. <strong>Failed Phase<\/strong><\/h3>\n<p>If your init container, Java application container, or the log reader container crashes or exits for some reason, the Pod will move to the &#8220;Failed&#8221; phase.<\/p>\n<p>A pod moves to the failed phase in the following scenarios<\/p>\n<ol>\n<li>If the init container or main container exits with a non-zero exit code and has <strong>restartPolicy<\/strong> set to <strong>Never<\/strong>.<\/li>\n<li>If a node fails or a pod is evicted from a node and can&#8217;t be moved to another node, it moves to a failed state.<\/li>\n<li>If a pod has <strong>activeDeadlineSeconds<\/strong> (Typically on <a href=\"https:\/\/devopscube.com\/create-kubernetes-jobs-cron-jobs\/\">Jobs &amp; Cronjobs<\/a>) field enabled and it exceeds the time limits, the pod gets terminated and marked as Failed.<\/li>\n<li>If you manually delete the pod and it can&#8217;t gracefully terminate, the pod moves to a failed state.<\/li>\n<\/ol>\n<h3 id=\"5-unknown-phase\">5. Unknown Phase<\/h3>\n<p>This is rare, but if the api-server can&#8217;t get the status of your Pod for some reason, it will be marked as &#8220;Unknown.&#8221;<\/p>\n<h2 id=\"pod-conditions\">Pod Conditions<\/h2>\n<p>The pod&#8217;s phase gives a brief update on the pod&#8217;s current status as <strong>Pod conditions <\/strong>give you detailed information related to scheduling, readiness, and initialization.<\/p>\n<p>If you describe a pod, you will see the conditions section as shown below. The conditions are part of the <strong><code>PodStatus<\/code><\/strong> object.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-8-38.png\" class=\"kg-image\" alt=\"Pod Conditions\" loading=\"lazy\"><\/figure>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/kubernetes-pod-lifecycle\/\" target=\"_blank\" rel=\"noopener noreferrer\">Kubernetes Pod Lifecycle Explained With Examples \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/kubernetes-pod-lifecycle\/<\/p>\n","protected":false},"author":1,"featured_media":1093,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1092"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1092\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/1093"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}