{"id":40574,"date":"2023-09-29T02:50:17","date_gmt":"2023-09-29T02:50:17","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=40574"},"modified":"2024-05-29T03:10:49","modified_gmt":"2024-05-29T03:10:49","slug":"how-to-deploy-prometheus-in-kubernetes","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-deploy-prometheus-in-kubernetes\/","title":{"rendered":"How to deploy Prometheus &#038; Grafana in Kubernetes?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Prometheus Stack<\/h2>\n\n\n\n<p>The Prometheus stack typically includes several components that work together to provide comprehensive monitoring and alerting capabilities for your infrastructure and applications. Here&#8217;s a table outlining the core components of the Prometheus stack:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Component<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>Prometheus<\/td><td>Time-series database and monitoring tool that collects and stores metrics from various sources. It serves as the core of the stack.<\/td><\/tr><tr><td>Alertmanager<\/td><td>Handles alerts generated by Prometheus and manages their routing, deduplication, and notification to various alerting channels (e.g., email, Slack).<\/td><\/tr><tr><td>Node Exporter<\/td><td>A Prometheus exporter for collecting hardware and OS-level metrics from individual nodes in your cluster (e.g., CPU, memory, disk usage).<\/td><\/tr><tr><td>Exporters (e.g., Prometheus Blackbox Exporter, SNMP Exporter)<\/td><td>Exporters are small applications that expose metrics in a format Prometheus can scrape. They are used to collect specialized metrics from various services and systems.<\/td><\/tr><tr><td>Grafana<\/td><td>A popular open-source dashboard and visualization platform that integrates with Prometheus to create and display custom dashboards and graphs.<\/td><\/tr><tr><td>Pushgateway<\/td><td>Allows for the collection of metrics from batch jobs or other short-lived processes that cannot be scraped directly by Prometheus. It acts as an intermediary between these jobs and Prometheus.<\/td><\/tr><tr><td>Prometheus Operator<\/td><td>An operator that simplifies the management and operation of Prometheus, Alertmanager, and related resources in Kubernetes clusters.<\/td><\/tr><tr><td>Thanos (Optional)<\/td><td>An extension to Prometheus that provides long-term storage, global query view, and horizontal scalability for Prometheus. It&#8217;s often used for large-scale or federated deployments.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of Prometheus Deployment in Kubernetes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Prometheus Operator<\/h3>\n\n\n\n<p>The Prometheus Operator uses Kubernetes custom resources to simplify the deployment and configuration of Prometheus, Alertmanager, and related monitoring components.<br>URL &#8211; <a href=\"https:\/\/github.com\/prometheus-operator\/prometheus-operator\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/prometheus-operator\/prometheus-operator<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. kube-prometheus<\/h3>\n\n\n\n<p>kube-prometheus provides example configurations for a complete cluster monitoring stack based on Prometheus and the Prometheus Operator. This includes deployment of multiple Prometheus and Alertmanager instances, metrics exporters such as the node_exporter for gathering node metrics, scrape target configuration linking Prometheus to various metrics endpoints, and example alerting rules for notification of potential issues in the cluster.<br>URL &#8211; <a href=\"https:\/\/github.com\/prometheus-operator\/kube-prometheus\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/prometheus-operator\/kube-prometheus<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. helm chart<\/h3>\n\n\n\n<p>The prometheus-community\/kube-prometheus-stack helm chart provides a similar feature set to kube-prometheus. This chart is maintained by the Prometheus community. For more information, please see the chart&#8217;s readme<br>URL &#8211; <a href=\"https:\/\/github.com\/prometheus-community\/helm-charts\/tree\/main\/charts\/kube-prometheus-stack\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/prometheus-community\/helm-charts\/tree\/main\/charts\/kube-prometheus-stack<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. kube-prometheus<\/h3>\n\n\n\n<p>If you want the whole stack, you must try kube-prometheus. This Components included in this package:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Prometheus Operator<\/li>\n\n\n\n<li>Highly available Prometheus<\/li>\n\n\n\n<li>Highly available Alertmanager<\/li>\n\n\n\n<li>Prometheus node-exporter<\/li>\n\n\n\n<li>Prometheus Adapter for Kubernetes Metrics APIs<\/li>\n\n\n\n<li>kube-state-metrics<\/li>\n\n\n\n<li>Grafana<\/li>\n<\/ul>\n\n\n\n<p>URL &#8211; <a href=\"https:\/\/github.com\/prometheus-operator\/kube-prometheus\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/prometheus-operator\/kube-prometheus<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ git clone https:\/\/github.com\/prometheus-operator\/kube-prometheus\r\n$ cd kube-prometheus\r\n$ ls manifests\/setup\/\r\n$ kubectl apply --server-side -f manifests\/setup\r\n$ kubectl wait --for condition=Established --all CustomResourceDefinition --namespace=monitoring\r\n$ kubectl apply -f manifests\/\r\n$ kubectl get ns\r\n$ kubectl get all -n monitoring\r\n$ kubectl patch svc grafana --type='json' -p '&#91;{\"op\":\"replace\",\"path\":\"\/spec\/type\",\"value\":\"NodePort\"}]' -nmonitoring\r\n$ kubectl get all -n monitoring\r\n$ kubectl --namespace monitoring port-forward  --address 0.0.0.0 svc\/grafana 3000\r\n\r\nhttp:&#47;&#47;34.207.57.210:3000\/\r\nadmin\r\nadmin<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Prometheus STACK Deployment Using Kube Prometheus <\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"514\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-750-1024x514.png\" alt=\"\" class=\"wp-image-40575\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-750-1024x514.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-750-300x151.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-750-768x386.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/09\/image-750.png 1045w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/prometheus-operator.dev\/docs\/kube\/kube-prometheus-on-kubeadm\/\" target=\"_blank\" rel=\"noopener\">Installation and Configuration GUIDE<\/a> <\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/prometheus-operator\/kube-prometheus\" target=\"_blank\" rel=\"noopener\">Installtion and Configguration Instructions<\/a> <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prometheus &amp; Grafana Deployment Using Helm Chart<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/da0111b4760531ae789a099c174522d9.js\"><\/script>\n\n\n\n<p><strong>Configure Data Sources and Dashboards in Grafana<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Log in to Grafana using the default credentials (admin\/admin).<\/li>\n\n\n\n<li>Add Prometheus as a data source:\n<ul class=\"wp-block-list\">\n<li>Click on the gear icon (\u2699\ufe0f) in the left sidebar.<\/li>\n\n\n\n<li>Select &#8220;Data Sources.&#8221;<\/li>\n\n\n\n<li>Click on &#8220;Add data source.&#8221;<\/li>\n\n\n\n<li>Choose &#8220;Prometheus.&#8221;<\/li>\n\n\n\n<li>Configure the Prometheus URL (<a href=\"http:\/\/prometheus-server.monitoring.svc.cluster.local\/\" target=\"_blank\" rel=\"noopener\">http:\/\/prometheus-server.monitoring.svc.cluster.local<\/a>) and save.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Import or create dashboards in Grafana to visualize your Kubernetes cluster&#8217;s metrics. You can find pre-built dashboards for Kubernetes and Prometheus in the Grafana dashboard marketplace.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Reference<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>https:\/\/prometheus-operator.dev\/<\/li>\n\n\n\n<li>https:\/\/grafana.com\/docs\/grafana-cloud\/monitor-infrastructure\/kubernetes-monitoring\/configuration\/configure-infrastructure-manually\/prometheus\/prometheus-operator\/<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Prometheus Stack The Prometheus stack typically includes several components that work together to provide comprehensive monitoring and alerting capabilities for your infrastructure and applications. Here&#8217;s a table outlining the core&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5012],"tags":[],"class_list":["post-40574","post","type-post","status-publish","format-standard","hentry","category-grafana"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=40574"}],"version-history":[{"count":10,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40574\/revisions"}],"predecessor-version":[{"id":46348,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40574\/revisions\/46348"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=40574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=40574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=40574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}