{"id":478627,"date":"2022-07-28T09:02:59","date_gmt":"2022-07-28T06:02:59","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-run-commands-inside-kubernetes-pod-containers\/"},"modified":"2022-07-28T09:02:59","modified_gmt":"2022-07-28T06:02:59","slug":"how-to-run-commands-inside-kubernetes-pod-containers","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-run-commands-inside-kubernetes-pod-containers\/","title":{"rendered":"#How to Run Commands Inside Kubernetes Pod Containers"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_88 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-6ac86c241ebc8\" 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-6ac86c241ebc8\" 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-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-commands-inside-kubernetes-pod-containers\/#%E2%80%9CHow_to_Run_Commands_Inside_Kubernetes_Pod_Containers%E2%80%9D\" >&#8220;How to Run Commands Inside Kubernetes Pod Containers&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-commands-inside-kubernetes-pod-containers\/#Using_Kubectl_Exec\" >Using Kubectl Exec<\/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-run-commands-inside-kubernetes-pod-containers\/#Choosing_a_Different_Container\" >Choosing a Different Container<\/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-run-commands-inside-kubernetes-pod-containers\/#Waiting_for_Pods_to_Be_Running\" >Waiting for Pods to Be Running<\/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-run-commands-inside-kubernetes-pod-containers\/#When_Should_I_Use_It\" >When Should I Use It?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-commands-inside-kubernetes-pod-containers\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Run_Commands_Inside_Kubernetes_Pod_Containers%E2%80%9D\"><\/span>&#8220;How to Run Commands Inside Kubernetes Pod Containers&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9632\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2021\/02\/748108a6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\"\/><\/p>\n<p><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>lications running in Kubernetes are normally long-lived services that you don\u2019t need to interact with. Sometimes you might want to manually run a command inside a container though, perhaps for a one-off maintenance operation or to assist a debugging procedure.<\/p>\n<p>In this article we\u2019ll show how to run commands and get interactive shell sessions inside Kubernetes containers. This will let you start processes in the container using your local terminal. It\u2019s fully integrated with Kubernetes and its Kubectl CLI, so you don\u2019t have to pollute your container images with an SSH daemon to enables remote access.<\/p>\n<h2 id=\"using-kubectl-exec\"><span class=\"ez-toc-section\" id=\"Using_Kubectl_Exec\"><\/span>Using Kubectl Exec<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/generated\/kubectl\/kubectl-commands#exec\"><code>kubectl exec<\/code><\/a> executes a command inside a running container. It has the following basic syntax:<\/p>\n<pre>$ kubectl exec demo-pod -- demo-command<\/pre>\n<p>This will run <code>demo-command<\/code> inside the first container of the <code>demo-pod<\/code> Pod. The command is executed with root privileges.<\/p>\n<p>Additional flags are needed to set up an interactive terminal session:<\/p>\n<ul>\n<li><strong><code>--stdin<\/code> (<code>-i<\/code>)<\/strong> \u2013 Pass your terminal\u2019s standard input stream into the container.<\/li>\n<li><strong><code>--tty<\/code> (<code>-t<\/code>)<\/strong> \u2013 Mark the standard input stream as a TTY, making it interactive.<\/li>\n<\/ul>\n<p>Here\u2019s an example of getting a shell to the first container in a Pod:<\/p>\n<pre>$ kubectl exec -it demo-pod -- \/bin\/sh<\/pre>\n<p>Everything after the <code>--<\/code> becomes part of the command that\u2019s executed inside the container. <code>kubectl exec<\/code> ignores the container\u2019s default entrypoint, instead launching a new process with the command you specify. You shouldn\u2019t wrap the command with quotation marks (<code>\"\/bin\/sh\"<\/code>) unless you\u2019d use them normally when running the command locally.<\/p>\n<h2 id=\"choosing-a-different-container\"><span class=\"ez-toc-section\" id=\"Choosing_a_Different_Container\"><\/span>Choosing a Different Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl exec<\/code> connects to the Pod\u2019s default container when no other arguments are given. The default container is the one with the <code>kubectl.kubernetes.io\/default-container<\/code> annotation. This will be the first container in the Pod if you\u2019ve not manually added the annotation to any other.<\/p>\n<p>Here\u2019s a Pod that runs two containers:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>demo-pod<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>app-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>nginx:latest<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>sidecar-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>busybox:latest<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You can run a command inside the <code>sidecar-container<\/code> by adding the <code>-c<\/code> flag to <code>kubectl exec<\/code>:<\/p>\n<pre>$ kubectl exec --it demo-pod -c sidecar-container -- \/bin\/sh<\/pre>\n<h2 id=\"waiting-for-pods-to-be-running\"><span class=\"ez-toc-section\" id=\"Waiting_for_Pods_to_Be_Running\"><\/span>Waiting for Pods to Be Running<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubectl will wait a minute for the Pod to be <code>Running<\/code> if its containers haven\u2019t already started when you run the <code>exec<\/code> command. This timeout value can be changed with the <code>--pod-running-timeout<\/code> flag. It\u2019s useful when you want to run a command straight after creating a new Pod, when it may not have been scheduled to a Node.<\/p>\n<pre>$ kubectl exec --pod-running-timeout=5m demo-pod -- demo-command<\/pre>\n<h2 id=\"when-should-i-use-it\"><span class=\"ez-toc-section\" id=\"When_Should_I_Use_It\"><\/span>When Should I Use It?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl exec<\/code> is best reserved for specialist situations where you need to directly interact with a container\u2019s filesystem. You could use it to access logs or caches after a problem or to run an infrequently used housekeeping script that\u2019s provided in a container image.<\/p>\n<p>Although <code>kubectl exec<\/code> lets you run any command, you should avoid dramatically modifying the container\u2019s environment. This could cause the container to drift away from the expected state. Containers shouldn\u2019t normally require intervention so use of <code>kubectl exec<\/code> inside production environments usually signals that something\u2019s gone wrong.<\/p>\n<p>Don\u2019t use this command to install packages or apply updates inside the container. This kind of operation should be handled by building a new container image that includes the changes, then replacing your Kubernetes Pods with ones that run the revised build. Following this principle ensures your changes won\u2019t be lost when the Pod restarts and reverts to its container image\u2019s filesystem. Keeping your containers immutable also guarantees they can be reliably reproduced from their images, which enhances reliability and ease of debugging.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Running a one-off command inside a Kubernetes container lets you debug issues, perform maintenance tasks, and inspect the container\u2019s filesystem. In each of these situations, you can use <code>kubectl exec<\/code> to get a connection to a container.<\/p>\n<p>Adding the <code>-it<\/code> flag combination will forward your terminal\u2019s standard input stream as a TTY, providing an interactive shell session. This won\u2019t always be necessary depending on the command you want to use: if it only produces output, without requiring any input, you can safely omit the flags.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\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.howtogeek.com\/devops\/how-to-run-commands-inside-kubernetes-pod-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Run Commands Inside Kubernetes Pod Containers&#8221; Applications running in Kubernetes are normally long-lived services that you don\u2019t need to interact with. Sometimes you might want to manually run a command inside a container though, perhaps for a one-off maintenance operation or to assist a debugging procedure. In this article we\u2019ll show how to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":478628,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2021\/02\/748108a6.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-478627","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\/478627","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=478627"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/478627\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/478628"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=478627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=478627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=478627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}