{"id":355672,"date":"2021-10-21T01:42:30","date_gmt":"2021-10-20T22:42:30","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/"},"modified":"2021-10-21T01:42:30","modified_gmt":"2021-10-20T22:42:30","slug":"how-to-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/","title":{"rendered":"#How to View Kubernetes Pod Logs With Kubectl \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 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-6af2b2683c6be\" 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-6af2b2683c6be\" 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-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Getting_Started\" >Getting Started<\/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-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Accessing_Pod_Logs\" >Accessing Pod Logs<\/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-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Continually_Streaming_Logs\" >Continually Streaming Logs<\/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-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Viewing_Older_Logs\" >Viewing Older Logs<\/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-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Getting_Recent_Logs\" >Getting Recent Logs<\/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-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Accessing_Logs_From_Other_Resource_Types\" >Accessing Logs From Other Resource Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#More_Advanced_Log_Management\" >More Advanced Log Management<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-view-kubernetes-pod-logs-with-kubectl-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to View Kubernetes Pod Logs With Kubectl \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9632\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?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\/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\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Viewing Pod logs is often the first step in diagnosing a problem with your cluster\u2019s workloads. Here\u2019s how to use Kubectl to live stream logs to your terminal, letting you inspect the output from your <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>lication.<\/p>\n<h2 id=\"getting-started\"><span class=\"ez-toc-section\" id=\"Getting_Started\"><\/span>Getting Started<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Make sure you\u2019ve got Kubectl installed and connected to your cluster. You can specify a Kubeconfig file by setting the <code>KUBECONFIG<\/code> environment variable in your shell:<\/p>\n<pre><code>export KUBECONFIG=~\/.kube\/my-cluster.yaml<\/code><\/pre>\n<p>Then use Kubectl to list your Pods:<\/p>\n<pre><code>kubectl get pods<\/code><\/pre>\n<p>Remember to add the <code>--namespace<\/code> flag when your Pods live outside the default namespace:<\/p>\n<pre><code>kubectl --namespace my-namespace get pods<\/code><\/pre>\n<p>Adding a temporary alias to your shell is a good way to shorten this step, helping you run several commands against the same namespace:<\/p>\n<pre><code>alias k=\"kubectl --namespace my-namespace\"&#13;\n&#13;\nk get pods<\/code><\/pre>\n<h2 id=\"accessing-pod-logs\"><span class=\"ez-toc-section\" id=\"Accessing_Pod_Logs\"><\/span>Accessing Pod Logs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>kubectl logs<\/code> command lets you inspect the logs produced by a named Pod:<\/p>\n<pre><code>kubectl logs pod-name<\/code><\/pre>\n<p>The Pod\u2019s existing logs will be emitted to your terminal. When the Pod\u2019s formed from more than one container, you must also specify the name of the contaienr you want to inspect:<\/p>\n<pre><code>kubectl logs pod-name container-name<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14514\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/8907eacf.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1100\" height=\"322\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Alternatively, set the <code>--all-containers<\/code> flag to include log lines produced by any of the containers in the Pod. Beware that you could see verbose and repetitive output when this flag is used against a busy Pod:<\/p>\n<pre><code>kubectl logs pod-name --all-containers<\/code><\/pre>\n<p>You can also get the logs from a set of Pods with a given label. This lets you aggregate logs from different Pods, provided they all share the same label:<\/p>\n<pre><code>kubectl logs -l my-label=my-value --all-containers<\/code><\/pre>\n<h2 id=\"continually-streaming-logs\"><span class=\"ez-toc-section\" id=\"Continually_Streaming_Logs\"><\/span>Continually Streaming Logs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The plain <code>logs<\/code> command emits the currently stored Pod logs and then exits. Add the <code>-f<\/code> (<code>--follow<\/code>) flag to the command to follow the logs and live stream them to your terminal.<\/p>\n<p>Kubectl will emit each new log line into your terminal until you stop the command with Ctrl+C. This is equivalent to using <code>tail -f<\/code> with a local log file in a non-containerized environment.<\/p>\n<h2 id=\"viewing-older-logs\"><span class=\"ez-toc-section\" id=\"Viewing_Older_Logs\"><\/span>Viewing Older Logs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl logs<\/code> won\u2019t include log lines produced by old containers that were once Pod members but have since been replaced. These logs can be accessed by adding the <code>-p<\/code> (<code>--previous<\/code>) flag.<\/p>\n<p>Kubectl will then surface the entirety of the stored log for the Pod, including lines that were emitted by containers that have since been terminated.<\/p>\n<h2 id=\"getting-recent-logs\"><span class=\"ez-toc-section\" id=\"Getting_Recent_Logs\"><\/span>Getting Recent Logs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sometimes you don\u2019t need to see the entire log stream. Kubectl supports a <code>--since<\/code> flag which surfaces log lines emitted after a given time:<\/p>\n<pre><code>kubectl logs pod-name --since=2h<\/code><\/pre>\n<p>This command will show the log output from <code>pod-name<\/code> that was produced within the past two hours. Another variant, <code>--since-time<\/code>, supports an <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc3339\">RFC3339-compliant<\/a> timestamp string instead of the relative time expression shown above.<\/p>\n<p>The <code>--tail<\/code> flag is another option for condensing logs. This limits the number of lines that are displayed, avoiding a full terminal when you only need to see very recent output:<\/p>\n<pre><code># Shows the last 10 lines of the log&#13;\nkubectl logs pod-name --tail=10<\/code><\/pre>\n<p>Kubectl doesn\u2019t show line timestamps by default as many applications already include them in their log output. Add the <code>--timestamps<\/code> flag to have Kubectl add timestamps to the start of lines when your workload doesn\u2019t provide them.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14516\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/10\/110dbcb3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1162\" height=\"509\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can prepend the inspected pod and container names to log lines too. This functionality is activated with the <code>--prefix<\/code> flag. It can be combined with <code>--timestamps<\/code> to display the time each line was created and the source it originated from.<\/p>\n<h2 id=\"accessing-logs-from-other-resource-types\"><span class=\"ez-toc-section\" id=\"Accessing_Logs_From_Other_Resource_Types\"><\/span>Accessing Logs From Other Resource Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>kubectl logs<\/code> works with deployment and job resources in addition to pods:<\/p>\n<pre><code>kubectl logs job\/my-job&#13;\nkubectl logs deployment\/my-deployment<\/code><\/pre>\n<p>You\u2019ll get the logs from the first container within the job or deployment. Use the <code>--all-containers<\/code> flag to surface logs created by any of the matching containers. You can use all the flags described above whether you\u2019re viewing a pod, deployment, or job.<\/p>\n<h2 id=\"more-advanced-log-management\"><span class=\"ez-toc-section\" id=\"More_Advanced_Log_Management\"><\/span>More Advanced Log Management<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubectl doesn\u2019t include a way to filter, search, or transform your logs. It\u2019s best to pipe the <code>kubectl logs<\/code> output into established terminal tools like <code>awk<\/code>, <code>grep<\/code> or <code>sed<\/code> for this purpose.<\/p>\n<pre><code>kubectl logs my-pod | grep search-expression<\/code><\/pre>\n<p>Similarly, use the existing redirection features in your shell to save logs to a file:<\/p>\n<pre><code>kubectl logs my-pod &gt; my-pod-logs.txt<\/code><\/pre>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubectl lets you access logs from your resources either on a per-container basis or in aggregate. You can view a snapshot of currently collected logs, continually stream new lines to your terminal, and access historical lines emitted by terminated containers.<\/p>\n<p>The command comes with some limited customization options including a line count limiter and simplistic date filtering. When more demanding parsing is needed, pipe the output into Unix terminal commands to rapidly analyze your logs and find the causes of errors in your applications.<\/p>\n<p>Kubectl collects logs from the standard output and error streams of your containers. It\u2019s important to ensure you write output to these streams correctly as a misconfigured container will result in empty output when you run <code>kubectl logs<\/code>.\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\/14512\/how-to-view-kubernetes-pod-logs-with-kubectl\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to View Kubernetes Pod Logs With Kubectl \u2013 CloudSavvy IT&#8221; Viewing Pod logs is often the first step in diagnosing a problem with your cluster\u2019s workloads. Here\u2019s how to use Kubectl to live stream logs to your terminal, letting you inspect the output from your application. Getting Started Make sure you\u2019ve got Kubectl installed&#8230;<\/p>\n","protected":false},"author":1,"featured_media":355673,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-355672","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\/355672","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=355672"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/355672\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/355673"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=355672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=355672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=355672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}