{"id":483956,"date":"2022-08-13T01:00:35","date_gmt":"2022-08-12T22:00:35","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/"},"modified":"2022-08-13T01:00:35","modified_gmt":"2022-08-12T22:00:35","slug":"how-to-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/","title":{"rendered":"#How to Monitor Kubernetes Resource Usage With Metrics Server and Kubectl Top"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a3d87a14cb35\" 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-6a3d87a14cb35\" 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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#%E2%80%9CHow_to_Monitor_Kubernetes_Resource_Usage_With_Metrics_Server_and_Kubectl_Top%E2%80%9D\" >&#8220;How to Monitor Kubernetes Resource Usage With Metrics Server and Kubectl Top&#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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#Adding_Metrics_Server_to_Kubernetes\" >Adding Metrics Server to Kubernetes<\/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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#Retrieving_Metrics_With_Kubectl_Top\" >Retrieving Metrics With Kubectl Top<\/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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#Changing_the_Object_Sort_Order\" >Changing the Object Sort Order<\/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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#Filtering_the_Object_List\" >Filtering the Object List<\/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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#Getting_the_Utilization_of_a_Specific_Resource\" >Getting the Utilization of a Specific Resource<\/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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Monitor_Kubernetes_Resource_Usage_With_Metrics_Server_and_Kubectl_Top%E2%80%9D\"><\/span>&#8220;How to Monitor Kubernetes Resource Usage With Metrics Server and Kubectl Top&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-803403\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Kubernetes.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Kubernetes logo\" width=\"1602\" height=\"902\"\/><\/p>\n<p>Monitoring the resource usage of your Kubernetes cluster is essential so you can track performance and understand whether your workloads are operating efficiently. The <code>kubectl top<\/code> command streams metrics directly from your cluster, letting you access the basics in your terminal.<\/p>\n<p>This command won\u2019t usually work straightaway in a fresh Kubernetes environment. It depends on the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/kubernetes-sigs\/metrics-server\">Metrics Server addon<\/a> being installed in your cluster. This component collects metrics from your Nodes and Pods and provides an API to retrieve the data.<\/p>\n<p>In this article we\u2019ll show how to install Metrics Server and access its measurements using <code>kubectl top<\/code>. You\u2019ll be able to view the CPU and memory consumption of each of your Nodes and Pods.<\/p>\n<h2 id=\"adding-metrics-server-to-kubernetes\"><span class=\"ez-toc-section\" id=\"Adding_Metrics_Server_to_Kubernetes\"><\/span>Adding Metrics Server to Kubernetes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes distributions don\u2019t normally come with Metrics Server built-in. You can easily check whether your cluster already has support by trying to run <code>kubectl top<\/code>:<\/p>\n<pre>$ kubectl top node&#13;\nerror: Metrics API not available<\/pre>\n<p>The error message confirms that the metrics server API is not present in the cluster.<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/kubernetes-sigs\/metrics-server\">Metrics Server<\/a> is maintained within the Kubernetes Special Interest Group (SIG) community. It can be added to your cluster using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/kubernetes-sigs\/metrics-server\/releases\/latest\/download\/components.yaml\">its plain YAML manifest<\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/artifacthub.io\/packages\/helm\/metrics-server\/metrics-server\">the project\u2019s Helm chart<\/a>.<\/p>\n<p>We\u2019ll use the manifest file for this tutorial. Run the following Kubectl command to install the Metrics Server:<\/p>\n<pre>$ kubectl apply -f https:\/\/github.com\/kubernetes-sigs\/metrics-server\/releases\/latest\/<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\">download<\/a>\/components.yaml&#13;\nserviceaccount\/metrics-server created&#13;\nclusterrole.rbac.authorization.k8s.io\/system:aggregated-metrics-reader created&#13;\nclusterrole.rbac.authorization.k8s.io\/system:metrics-server created&#13;\nrolebinding.rbac.authorization.k8s.io\/metrics-server-auth-reader created&#13;\nclusterrolebinding.rbac.authorization.k8s.io\/metrics-server:system:auth-delegator created&#13;\nclusterrolebinding.rbac.authorization.k8s.io\/system:metrics-server created&#13;\nservice\/metrics-server created&#13;\ndeployment.apps\/metrics-server created&#13;\napiservice.apiregistration.k8s.io\/v1beta1.metrics.k8s.io created<\/pre>\n<p>Metrics Server will now start collecting and exposing Kubernetes resource consumption data. If the installation fails with an error, you should check your cluster meets <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/kubernetes-sigs\/metrics-server#requirements\">the project\u2019s requirements<\/a>. Metrics Server has specific dependencies which may not be supported in some environments.<\/p>\n<p>Many Kubernetes distributions bundle Metrics Server support using their own addons system. You can use this command to easily add Metrics Server to a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/minikube.sigs.k8s.io\/docs\">Minikube<\/a> cluster, for example:<\/p>\n<pre>$ minikube addons enable metrics-server&#13;\nUsing image k8s.gcr.io\/metrics-server\/metrics-server:v0.4.2&#13;\nThe 'metrics-server' addon is enabled<\/pre>\n<h2 id=\"retrieving-metrics-with-kubectl-top\"><span class=\"ez-toc-section\" id=\"Retrieving_Metrics_With_Kubectl_Top\"><\/span>Retrieving Metrics With Kubectl Top<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With Metrics Server installed, you can now <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/generated\/kubectl\/kubectl-commands#top\">run <code>kubectl top<\/code><\/a> to access the information it collects.<\/p>\n<p>Use the <code>node<\/code> sub-command to get the current resource utilization of each of the Nodes in your cluster:<\/p>\n<pre>$ kubectl top node&#13;\nNAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   &#13;\nminikube   249m         3%     847Mi           2%<\/pre>\n<p>The <code>pod<\/code> sub-command provides individual metrics for each of your Pods:<\/p>\n<pre>$ kubectl top pod&#13;\nNAME    CPU(cores)   MEMORY(bytes)   &#13;\nnginx   120m         8Mi<\/pre>\n<p>This will surface Pods in the <code>default<\/code> namespace. Add the <code>--namespace<\/code> flag if you\u2019re interested in Pods in a specific namespace:<\/p>\n<pre>$ kubectl top pod --namespace demo-app&#13;\nNAME    CPU(cores)   MEMORY(bytes)   &#13;\nnginx   0m           2Mi<\/pre>\n<p>The <code>--all-namespaces<\/code> flag is also supported to list every Pod in your cluster.<\/p>\n<p>Metrics may take a few minutes to become available after new Pods are created. There\u2019s a delay in the metrics server\u2019s pipeline so it doesn\u2019t become a performance issue itself.<\/p>\n<p>The <code>kubectl top<\/code> command doesn\u2019t overwhelm you with dozens of metrics. It focuses on covering the bare essentials of CPU and memory usage. This basic start can be adequate for scenarios where you simply need data fast, such as identifying the Pod that\u2019s caused a spike in overall utilization.<\/p>\n<p>One source of confusion can be the <code>100m<\/code> values reported in the <code>CPU(cores)<\/code> field. The command displays CPU usage <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/discuss.kubernetes.io\/t\/metric-server-cpu-and-memory-units\/7497\/2\">in millicores<\/a>. A measurement of <code>1000m<\/code> always means 100% consumption of a single CPU core. <code>500m<\/code> indicates 50% consumption of one core, while <code>2000m<\/code> means two cores are being occupied.<\/p>\n<h2 id=\"changing-the-object-sort-order\"><span class=\"ez-toc-section\" id=\"Changing_the_Object_Sort_Order\"><\/span>Changing the Object Sort Order<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>kubectl top<\/code> command can optionally sort the emitted object list by CPU or memory consumption. This makes it easier to quickly spot the Nodes or Pods that are exerting the highest pressure on cluster resources.<\/p>\n<p>Add the <code>--sort-by<\/code> flag with either <code>cpu<\/code> or <code>memory<\/code> as its value to activate this behavior:<\/p>\n<pre>$ kubectl top pod --sort-by=memory&#13;\nNAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   &#13;\nnginx-1    249m         3%     1790Mi          5%&#13;\nnginx-2    150m         1%     847Mi           2%<\/pre>\n<h2 id=\"filtering-the-object-list\"><span class=\"ez-toc-section\" id=\"Filtering_the_Object_List\"><\/span>Filtering the Object List<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In common with other Kubectl commands, the <code>--selector<\/code> flag lets you filter the object list to items with specific labels:<\/p>\n<pre>$ kubectl top pod --selector application=demo-app&#13;\nNAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   &#13;\nnginx-1    249m         3%     1790Mi          5%&#13;\nnginx-2    150m         1%     847Mi           2%<\/pre>\n<p>In this example, only Pods that have the <code>application: demo-app<\/code> label will be included in the output. <code>=<\/code>, <code>==<\/code>, and <code>!=<\/code> are supported <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubectl\/cheatsheet\/#viewing-finding-resources\">as operators<\/a>. Multiple constraints can be applied by stringing them together as a comma-separated string, such as <code>application=demo-app,version!=1<\/code>. Objects will only show up if they match all of the label filters in your query.<\/p>\n<h2 id=\"getting-the-utilization-of-a-specific-resource\"><span class=\"ez-toc-section\" id=\"Getting_the_Utilization_of_a_Specific_Resource\"><\/span>Getting the Utilization of a Specific Resource<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>top node<\/code> and <code>top pod<\/code> sub-commands can both be passed the name of a specific Node or Pod to fetch. The current metrics associated with that item will be displayed in isolation.<\/p>\n<p>Supply the object\u2019s name as a plain argument to the command, straight after <code>node<\/code> or <code>pod<\/code>:<\/p>\n<pre>$ kubectl top node minikube&#13;\nNAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   &#13;\nminikube   245m         3%     714Mi           2%<\/pre>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>kubectl top<\/code> command surfaces essential resource consumption metrics for Nodes and Pods in your Kubernetes cluster. You can use it to quickly check the CPU and memory usage associated with each of your workloads. This information can be helpful to diagnose performance issues and identify when it\u2019s time to add another Node.<\/p>\n<p>Before using the command, you need to install the Kubernetes <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/kubernetes-sigs\/metrics-server\">Metrics Server<\/a> in your cluster. This provides the API that exposes resource utilization data. Enabling Metrics Server incurs a performance overhead but this is usually negligible in most deployments. It <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/kubernetes-sigs\/metrics-server#scaling\">typically requires<\/a> 1m core of CPU and 2MiB of memory per monitored Node, although this may vary with the workloads running in your specific environment.<\/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-monitor-kubernetes-resource-usage-with-metrics-server-and-kubectl-top\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Monitor Kubernetes Resource Usage With Metrics Server and Kubectl Top&#8221; Monitoring the resource usage of your Kubernetes cluster is essential so you can track performance and understand whether your workloads are operating efficiently. The kubectl top command streams metrics directly from your cluster, letting you access the basics in your terminal. This command&#8230;<\/p>\n","protected":false},"author":1,"featured_media":483957,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Kubernetes.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-483956","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\/483956","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=483956"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/483956\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/483957"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=483956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=483956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=483956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}