{"id":434,"date":"2024-04-24T04:27:30","date_gmt":"2024-04-24T04:27:30","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=434"},"modified":"2024-04-24T04:27:30","modified_gmt":"2024-04-24T04:27:30","slug":"setup-prometheus-pushgateway-on-kubernetes","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=434","title":{"rendered":"How To Setup Prometheus Pushgateway On Kubernetes"},"content":{"rendered":"<p>In this article, we will look at an easy way to set up a Prometheus pushgateway on Kubernetes with a practical example of pushing metrics from an app.<\/p>\n<p>There are scenarios where Prometheus can&#8217;t pull the metrics from the endpoint, for example, batch jobs or short-lived jobs.<\/p>\n<p>In that kind of situation, we can use the service or application itself <strong>to push the metrics <\/strong>to Prometheus.<\/p>\n<p>As we know, Prometheus can only pull the metrics; however, there is an option called <strong>Pushgateway<\/strong> where applications can push the metrics. <strong>Prometheus<\/strong> will then pull the retained metrics from the Pushgateway.<\/p>\n<p>If you want to know more about the push gateway, I suggest you read the push gateways section in the detailed <a href=\"https:\/\/devopscube.com\/prometheus-architecture\/\">Prometheus Architecture<\/a>.<\/p>\n<h2 id=\"setting-up-the-prometheus-pushgateway-on-kubernetes\"><strong>Setting Up the Prometheus Pushgateway on Kubernetes<\/strong><\/h2>\n<p>I am implementing this into an existing monitoring setup so I will use the Namespace <strong>monitoring<\/strong> in each configuration file.<\/p>\n<p>If you want to set up the Prometheus on your cluster, click <a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\">here.<\/a><\/p>\n<h3 id=\"step-1-create-a-pushgateway-deployment-manifest\">Step 1: Create a Pushgateway Deployment Manifest<\/h3>\n<p>We don&#8217;t need the ConfigMap for the Pushgateway, because most of our configuration will be on the target side and in the Prometheus configuration.<\/p>\n<p>Create a Deployment file <strong>pushgateway-deployment.yaml <\/strong>and add the following contents.<\/p>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: pushgateway\n  namespace: monitoring\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: pushgateway\n  template:\n    metadata:\n      labels:\n        app: pushgateway\n    spec:\n      containers:\n      - name: pushgateway\n        image: prom\/pushgateway:latest\n        ports:\n        - containerPort: 9091<\/code><\/pre>\n<p>Here, I am using the latest Pushgateway image, you can specify the particular version if you want and the default port number of the Pushgateway is <strong>9091.<\/strong><\/p>\n<p>To apply this configuration on a cluster, use the following command.<\/p>\n<pre><code>kubectl apply -f pushgateway-deployment.yaml<\/code><\/pre>\n<p>To view the list of deployments in the monitoring Namespace.<\/p>\n<pre><code>kubectl get deployments -n monitoring -o wide<\/code><\/pre>\n<p>From the output, you can ensure the Deployment is properly placed.<\/p>\n<h3 id=\"step-2-create-a-pushgateway-service\">Step 2: Create a Pushgateway Service<\/h3>\n<p>Create a service configuration file <strong>push gateway-service.yaml<\/strong> and add the contents to it.<\/p>\n<pre><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: pushgateway\n  namespace: monitoring\nspec:\n  selector:\n    app: pushgateway\n  type: NodePort\n  ports:\n    - port: 9091\n      targetPort: 9091\n      nodePort: 31500<\/code><\/pre>\n<p>I want to access the Pushgateway dashboard over the Internet, for that, I am using the <code>spec.type: NodePort<\/code><strong>,<\/strong> and also I have specified the port number, which is <strong>31500<\/strong>.<\/p>\n<p>To apply this configuration on Kubernetes, use the following command.<\/p>\n<pre><code>kubectl apply -f pushgateway-service.yaml<\/code><\/pre>\n<p>To view the list of available Services in the monitoring Namespace.<\/p>\n<pre><code>kubectl get svc -n monitoring -o wide<\/code><\/pre>\n<p>Once the configuration is completed, we can able to access the <strong>Pushgateway dashboard<\/strong> over the internet, for that, we need one of the instance public IPs and the node port number.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-78-8.png\" class=\"kg-image\" alt=\"prometheus pushgateway dashboard\" loading=\"lazy\" width=\"1032\" height=\"1125\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-78-8.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-78-8.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-78-8.png 1032w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If the configuration is properly done, you will get this dashboard, now in the <strong>Metrics<\/strong> section, no data will be available, also Prometheus will not scrape the metrics.<\/p>\n<p>First, we need to add a job to the existing Prometheus configuration.<\/p>\n<h3 id=\"step-3-add-pushgateway-job-in-prometheus\">Step 3: Add Pushgateway Job in Prometheus<\/h3>\n<p>Find the Prometheus <strong>ConfigMap<\/strong> Object or the YAML manifest and open it with a text editor.<\/p>\n<pre><code>vim config-map.yaml <\/code><\/pre>\n<p>Your Prometheus <strong>ConfigMap<\/strong> file name would be different, so find and provide the correct name.<\/p>\n<p>Add the following job configuration under the <strong>scrape configs<\/strong> section.<\/p>\n<pre><code>- job_name: \"pushgateway\"\n        honor_labels: true\n        static_configs:\n        - targets: [pushgateway.monitoring.svc:9091]<\/code><\/pre>\n<p>Ensure the target value, here, <strong>pushgateway<\/strong> is my service name and <strong>monitoring<\/strong> is the Namespace name, if you have given a different name, replace it with your values or provide the IP of the the Pushgateway.<\/p>\n<p>To apply the configuration and reflect the changes in the Prometheus, use the following command.<\/p>\n<pre><code>kubectl apply -f config-map.yaml<\/code><\/pre>\n<p>Check your Prometheus dashboard target section to verify that the configuration is properly done.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-79-6.png\" class=\"kg-image\" alt=\"prometheus dashboard\" loading=\"lazy\" width=\"759\" height=\"444\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-79-6.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-79-6.png 759w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If you can&#8217;t see the updates in the target, then perform the rollout restart to reflect the changes in the dashboard.<\/p>\n<pre><code>kubectl rollout restart deployment prometheus-deployment -n monitoring<\/code><\/pre>\n<p>Your Prometheus deployment and the Namespace name would be different, so ensure before executing the command.<\/p>\n<p>This process might interrupt the scrapping for a few seconds, be aware of that otherwise your certain metrics would be skipped from scrapping.<\/p>\n<h2 id=\"how-to-test-the-prometheus-pushgateway\">How to Test the Prometheus Pushgateway?<\/h2>\n<p>For testing, I am using a containerized Python script.<\/p>\n<p>Create a deployment file <code>app-deployment.yaml<\/code> and add the contents.<\/p>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: python-app\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: pushgateway-python-app\n  template:\n    metadata:\n      labels:\n        app: pushgateway-python-app\n    spec:\n      containers:\n      - name: pushgateway-python-app\n        image: techiescamp\/pushgateway-python-app:latest\n        ports:\n        - containerPort: 8085<\/code><\/pre>\n<p>This testing image is available in the <strong>Dockerhub<\/strong> public repository so that you can use the same image for testing purposes.<\/p>\n<p>If you use the same image, the container will generate some random values and push those as metrics in the Pushgateway.<\/p>\n<p>To deploy this in the cluster, use the following command.<\/p>\n<pre><code>kubectl apply -f app-deployment.yaml -n monitoring<\/code><\/pre>\n<p>To list the deployments, use the following command.<\/p>\n<pre><code>kubectl get deployments -n monitoring<\/code><\/pre>\n<p>If the deployment is successfully done, we can see metrics in the <strong>Pushgateway<\/strong> dashboard.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-81-8.png\" class=\"kg-image\" alt=\"prometheus pushgateway metrics\" loading=\"lazy\" width=\"762\" height=\"582\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-81-8.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-81-8.png 762w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Let&#8217;s try to make a query with this same metric <strong>random_metrics<\/strong> in the Prometheus, if we are getting values there, we can ensure the Prometheus is scraping the metrics from the <strong>Pushgateway<\/strong><\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-82-11.png\" class=\"kg-image\" alt=\"prometheus query\" loading=\"lazy\" width=\"745\" height=\"572\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-82-11.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-82-11.png 745w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Now, we know <strong>Prometheus <\/strong>is successfully scraping so<strong> <\/strong>visualization is also possible in <strong>Grafana.<\/strong><\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-84-12.png\" class=\"kg-image\" alt=\"prometheus pushgateway grafana dashboard\" loading=\"lazy\" width=\"876\" height=\"474\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-84-12.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-84-12.png 876w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If you want to install Grafana on your Kubernetes cluster, please refer to this <a href=\"https:\/\/devopscube.com\/setup-grafana-kubernetes\/\">blog<\/a>.<\/p>\n<h2 id=\"why-is-pushgateway-important\"><strong>Why is Pushgateway important?<\/strong><\/h2>\n<p>We often set cronjobs in Kubernetes, these jobs will start in the defined time and exit when the job is completed. we can&#8217;t be sure that the Prometheus will scrape the metrics before it exits.<\/p>\n<p>With the help of Pushgateway, when the job executes, the metrics will be sent to the Pushgateway.<\/p>\n<p>For some jobs we can&#8217;t even know when it executes and when it ends, for example, we can say services, are activated when there is necessary, in that situation if we need metrics then the Pushgateway would be a good option.<\/p>\n<h2 id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n<p>Pushgateway is always a custom setup because you will decide what metrics you have to push from the target, so it is very useful for that kind of specific situation.<\/p>\n<p>I believe this setup gives you an overall idea of how to set the Pushgateway on your cluster.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-prometheus-pushgateway-on-kubernetes\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Setup Prometheus Pushgateway On Kubernetes \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-prometheus-pushgateway-on-kubernetes\/<\/p>\n","protected":false},"author":1,"featured_media":435,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-434","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\/434","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=434"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/434\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/435"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}