{"id":368357,"date":"2021-11-17T13:00:38","date_gmt":"2021-11-17T10:00:38","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/"},"modified":"2021-11-17T13:00:38","modified_gmt":"2021-11-17T10:00:38","slug":"how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/","title":{"rendered":"#How to Restart Kubernetes Pods With Kubectl \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a239b821083d\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a239b821083d\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/#Scaling_the_Replica_Count\" >Scaling the Replica Count<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/#Downtimeless_Restarts_With_Rollouts\" >Downtimeless Restarts With Rollouts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/#Abusing_ReplicaSet_Monitoring\" >(Ab)using ReplicaSet Monitoring<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/#Changing_Pod_Annotations\" >Changing Pod Annotations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-restart-kubernetes-pods-with-kubectl-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Restart Kubernetes Pods With Kubectl \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 1200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-14588\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/c0e5b7df.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/c0e5b7df.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/c0e5b7df.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration of the Kubernetes logo on a smartphone\" width=\"1200\" height=\"675\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-illustration\/kubernetes-symbol-on-smartphone-screen-white-1974756836\">o_m\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Kubernetes Pods should operate without intervention but sometimes you might hit a problem where a container\u2019s not working the way it should. Restarting the Pod can help restore operations to normal.<\/p>\n<p>Kubectl doesn\u2019t have a direct way of restarting individual Pods. Pods are meant to stay running until they\u2019re replaced as part of your deployment routine. This is usually when you release a new version of your container image.<\/p>\n<p>Here are a few techniques you can use when you want to restart Pods without building a new image or running your CI pipeline. They can help when you think a fresh set of containers will get your workload running again.<\/p>\n<h2 id=\"scaling-the-replica-count\"><span class=\"ez-toc-section\" id=\"Scaling_the_Replica_Count\"><\/span>Scaling the Replica Count<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although there\u2019s no <code>kubectl restart<\/code>, you can achieve something similar by scaling the number of container replicas you\u2019re running. This works when your Pod is part of a Deployment, StatefulSet, ReplicaSet, or Replication Controller.<\/p>\n<pre>kubectl scale deployment my-deployment --replicas=0&#13;\nkubectl scale deployment my-deployment --replicas=3<\/pre>\n<p>Scaling your Deployment down to <code>0<\/code> will remove all your existing Pods. Wait until the Pods have been terminated, using <code>kubectl get pods<\/code> to check their status, then rescale the Deployment back to your intended replica count. Kubernetes will create new Pods with fresh container instances.<\/p>\n<h2 id=\"downtimeless-restarts-with-rollouts\"><span class=\"ez-toc-section\" id=\"Downtimeless_Restarts_With_Rollouts\"><\/span>Downtimeless Restarts With Rollouts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Manual replica count adjustment comes with a limitation: scaling down to <code>0<\/code> will create a period of downtime where there\u2019s no Pods available to serve your users. An alternative option is to initiate a rolling restart which lets you replace a set of Pods without downtime. It\u2019s available with Kubernetes v1.15 and later.<\/p>\n<pre>kubectl rollout restart deployment my-deployment<\/pre>\n<p>When you run this command, Kubernetes will gradually terminate and replace your Pods while ensuring some containers stay operational throughout. The rollout\u2019s phased nature lets you keep serving customers while effectively \u201crestarting\u201d your Pods behind the scenes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14591\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/56e27762.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"806\" height=\"93\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>After the rollout completes, you\u2019ll have the same number of replicas as before but each container will be a fresh instance. You can check the status of the rollout by using <code>kubectl get pods<\/code> to list Pods and watch as they get replaced. There\u2019s also <code>kubectl rollout status deployment\/my-deployment<\/code> which shows the current progress too.<\/p>\n<p><code>kubectl rollout<\/code> works with Deployments, DaemonSets, and StatefulSets. Most of the time this should be your go-to option when you want to terminate your containers and im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely start new ones.<\/p>\n<h2 id=\"abusing-replicaset-monitoring\"><span class=\"ez-toc-section\" id=\"Abusing_ReplicaSet_Monitoring\"><\/span>(Ab)using ReplicaSet Monitoring<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When your Pod\u2019s part of a ReplicaSet or Deployment, you can initiate a replacement by simply deleting it. The ReplicaSet will notice the Pod has vanished as the number of container instances will drop below the target replica count.<\/p>\n<pre>kubectl delete pod my-pod<\/pre>\n<p>The ReplicaSet will intervene to restore the minimum availability level. It\u2019ll automatically create a new Pod, starting a fresh container to replace the old one.<\/p>\n<p>This is technically a side-effect \u2013 it\u2019s better to use the <code>scale<\/code> or <code>rollout<\/code> commands which are more explicit and designed for this use case. Nonetheless manual deletions can be a useful technique if you know the identity of a single misbehaving Pod inside a ReplicaSet or Deployment. A rollout would replace all the managed Pods, not just the one presenting a fault.<\/p>\n<p>You can expand upon the technique to replace all failed Pods using a single command:<\/p>\n<pre>kubectl delete pods --field-selector=status.phase=Failed<\/pre>\n<p>Any Pods in the <code>Failed<\/code> state will be terminated and removed. The replication controller will notice the discrepancy and add new Pods to move the state back to the configured replica count. If you\u2019re confident the old Pods failed due to a transient error, the new ones should stay running in a healthy state.<\/p>\n<h2 id=\"changing-pod-annotations\"><span class=\"ez-toc-section\" id=\"Changing_Pod_Annotations\"><\/span>Changing Pod Annotations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another way of forcing a Pod to be replaced is to add or modify an annotation. Kubernetes will replace the Pod to <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ly the change.<\/p>\n<p>You can use the <code>kubectl annotate<\/code> command to apply an annotation:<\/p>\n<pre>kubectl annotate pods my-pod app-version=\"2\" --overwrite<\/pre>\n<p>This command updates the <code>app-version<\/code> annotation on <code>my-pod<\/code>. The <code>--overwrite<\/code> flag instructs Kubectl to apply the change even if the annotation already exists. Without it you can only add new annotations as a safety measure to prevent unintentional changes.<\/p>\n<p>Updating a deployment\u2019s environment variables has a similar effect to changing annotations. This is ideal when you\u2019re already exposing an app version number, build ID, or deploy date in your environment.<\/p>\n<pre>kubectl set env deployment my-deployment APP_VERSION=\"2\"<\/pre>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes Pods should usually run until they\u2019re replaced by a new deployment. As a result, there\u2019s no direct way to \u201crestart\u201d a single Pod. If one of your containers experiences an issue, aim to <em>replace<\/em> it instead of restarting. The subtle change in terminology better matches the stateless operating model of Kubernetes Pods.<\/p>\n<p>Scale your replica count, initiate a rollout, or manually delete Pods from a ReplicaSet to terminate old containers and start fresh new instances. Rollouts are the preferred solution for modern Kubernetes releases but the other approaches work too and can be more suited to specific scenarios.<\/p>\n<p>Foremost in your mind should be these two questions: do you want all the Pods in your Deployment or ReplicaSet to be replaced, and is any downtime acceptable? Manual Pod deletions can be ideal if you want to \u201crestart\u201d an individual Pod without downtime, provided you\u2019re running more than one replica, whereas <code>scale<\/code> is an option when the <code>rollout<\/code> command can\u2019t be used and you\u2019re not concerned about a brief period of unavailability.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14587\/how-to-restart-kubernetes-pods-with-kubectl\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Restart Kubernetes Pods With Kubectl \u2013 CloudSavvy IT&#8221; o_m\/Shutterstock.com Kubernetes Pods should operate without intervention but sometimes you might hit a problem where a container\u2019s not working the way it should. Restarting the Pod can help restore operations to normal. Kubectl doesn\u2019t have a direct way of restarting individual Pods. Pods are meant&#8230;<\/p>\n","protected":false},"author":1,"featured_media":368358,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/c0e5b7df.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-368357","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/368357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=368357"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/368357\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/368358"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=368357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=368357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=368357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}