{"id":878,"date":"2022-01-27T05:12:00","date_gmt":"2022-01-27T05:12:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=878"},"modified":"2022-01-27T05:12:00","modified_gmt":"2022-01-27T05:12:00","slug":"setup-kube-state-metrics","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=878","title":{"rendered":"How To Setup Kube State Metrics on Kubernetes Cluster"},"content":{"rendered":"<p>In this blog, we will look at what is Kube State Metrics and its setup on Kubernetes. This blog is part of the <a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\" rel=\"noreferrer noopener\">Prometheus kubernetes setup tutorial<\/a> series.<\/p>\n<h2 id=\"what-is-kube-state-metrics\">What is Kube State Metrics?<\/h2>\n<p><a href=\"https:\/\/github.com\/kubernetes\/kube-state-metrics?ref=devopscube.com\" rel=\"noreferrer noopener\">Kube State metrics<\/a> is a service that talks to the Kubernetes API server to get all the details about all the API objects like deployments, pods, daemonsets, Statefulsets, etc.<\/p>\n<p>Primarily it produces metrics in Prometheus format with the stability as the Kubernetes API. Overall it provides kubernetes objects &amp; resources metrics that you cannot get directly from native Kubernetes monitoring components.<\/p>\n<p>Kube state metrics service exposes all the metrics on <code>\/metrics<\/code> URI. <a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\" rel=\"noreferrer noopener\">Prometheus<\/a> can scrape all the metrics exposed by Kube state metrics.<\/p>\n<p>Following are some of the important metrics you can get from Kube state metrics.<\/p>\n<ol>\n<li>Node status, node capacity (CPU and memory)<\/li>\n<li>Replica-set compliance (desired\/available\/unavailable\/updated status of replicas per deployment)<\/li>\n<li>Pod status (waiting, running, ready, etc)<\/li>\n<li>Ingress metrics<\/li>\n<li>PV, PVC metrics<\/li>\n<li>Daemonset &amp; Statefulset metrics.<\/li>\n<li>Resource requests and limits.<\/li>\n<li>Job &amp; Cronjob metrics<\/li>\n<\/ol>\n<p>You can check out the detailed supported metrics from the <a href=\"https:\/\/github.com\/kubernetes\/kube-state-metrics\/tree\/master\/docs?ref=devopscube.com\" rel=\"noreferrer noopener\">documentation here<\/a>.<\/p>\n<h2 id=\"kube-state-metrics-setup\">Kube State Metrics Setup<\/h2>\n<p>Kube state metrics is available as a public docker image. You will have to deploy the following Kubernetes objects for Kube state metrics to work.<\/p>\n<ol>\n<li>A <strong>Service Account<\/strong><\/li>\n<li><strong>Cluster Role<\/strong> &#8211; For kube state metrics to access all the Kubernetes API objects.<\/li>\n<li><strong>Cluster Role Binding<\/strong> &#8211; Binds the service account with the cluster role.<\/li>\n<li>Kube State Metrics <strong>Deployment<\/strong><\/li>\n<li><strong>Service<\/strong> &#8211; To expose the metrics<\/li>\n<\/ol>\n<p>All the above Kube state metrics objects will be deployed in the <code>kube-system<\/code> namespace<\/p>\n<p>Let&#8217;s deploy the components. All the deployment objects are available in Github. You can also find the same deployment object in the <a href=\"https:\/\/github.com\/kubernetes\/kube-state-metrics\/tree\/master\/examples\/standard?ref=devopscube.com\" rel=\"noreferrer noopener\">official repo<\/a> as well.<\/p>\n<p><strong>Step 1:<\/strong> Clone the Github repo<\/p>\n<pre><code>git clone https:\/\/github.com\/devopscube\/kube-state-metrics-configs.git<\/code><\/pre>\n<p><strong>Step 2: <\/strong>Create all the objects by pointing to the cloned directory.<\/p>\n<pre><code>kubectl apply -f kube-state-metrics-configs\/<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Check the deployment status using the following command.<\/p>\n<pre><code>kubectl get deployments kube-state-metrics -n kube-system<\/code><\/pre>\n<h2 id=\"kube-state-metrics-prometheus-config\">kube State Metrics Prometheus Config<\/h2>\n<p>All the Kube static metrics can be obtained from the Kube state service endpoint on <code>\/metrics<\/code> URI.<\/p>\n<p>This configuration can be added as part of the <strong>Prometheus job configuration<\/strong>. You need to add the following job configuration to your Prometheus config for Prometheus to scrape all the Kube state metrics.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note<\/strong><\/b>: If you have followed my prometheus guide, you dont have to add this scrape config. It is already part of the <a href=\"https:\/\/github.com\/bibinwilson\/kubernetes-prometheus\/blob\/master\/config-map.yaml?ref=devopscube.com\" rel=\"noreferrer noopener\">prometheus config<\/a>. You should see the target status &#8220;up&#8221; after deploying kube state metrics.<\/div>\n<\/div>\n<p>If you have follo<\/p>\n<pre><code>- job_name: 'kube-state-metrics'\n  static_configs:\n    - targets: ['kube-state-metrics.kube-system.svc.cluster.local:8080']\n<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-kube-state-metrics\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Setup Kube State Metrics on Kubernetes Cluster \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-kube-state-metrics\/<\/p>\n","protected":false},"author":1,"featured_media":879,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-878","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\/878","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=878"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/879"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}