{"id":757,"date":"2024-04-04T15:32:45","date_gmt":"2024-04-04T15:32:45","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=757"},"modified":"2024-04-04T15:32:45","modified_gmt":"2024-04-04T15:32:45","slug":"blackbox-exporter-on-kubernetes","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=757","title":{"rendered":"Setup Blackbox Exporter on Kubernetes: A Step-by-Step Guide"},"content":{"rendered":"<p>In this blog, let&#8217;s explore how to set up Blackbox Exporter on Kubernetes. By the end, you&#8217;ll have a clear understanding of the process.<\/p>\n<p>If a requirement comes to monitor a server&#8217;s or application&#8217;s endpoint, then <strong>Blackbox Exporter<\/strong> will fulfill your requirements.<\/p>\n<p>This is also one of the Exporter that <a href=\"https:\/\/devopscube.com\/prometheus-architecture\/\" rel=\"noreferrer noopener\"><strong>Prometheus<\/strong><\/a> has, and this will collect the probe metrics using the endpoint type such as HTTP, TCP, ICMP, etc.<\/p>\n<h2 id=\"why-blackbox-exporter-is-important\"><strong>Why Blackbox Exporter is Important?<\/strong><\/h2>\n<p>Sometimes Kubernetes needs to access external resources such as databases and APIs, If those resources have any issue then <a href=\"https:\/\/devopscube.com\/setup-kubernetes-cluster-kubeadm\/\" rel=\"noreferrer noopener\">Kubernetes<\/a> can&#8217;t able to access them, so we have to monitor their endpoint so that we can identify the issue earlier and fix it.<\/p>\n<p><a href=\"https:\/\/devopscube.com\/set-up-blackbox-exporter-on-vm\/\" rel=\"noreferrer noopener\">Blackbox Exporter<\/a> not only helps to troubleshoot, but it is also useful to improve the performance of the Kubernetes with external resources.<\/p>\n<p>It can track the request and response time between the resources, based on the data, we can identify any kind of latency issue present and we can make adjustments to improve the performance.<\/p>\n<h2 id=\"how-to-setup-blackbox-exporter-on-kubernetes\"><strong>How to Setup Blackbox Exporter On Kubernetes?<\/strong><\/h2>\n<p>I have created a repo to store the configuration YAML files, if you are interested, use them for this setup.<\/p>\n<pre><code>https:\/\/github.com\/arunlalp\/kubernetes-blackbox-exporter.git<\/code><\/pre>\n<p>This setup is a part of the <strong>Prometheus<\/strong> monitoring setup on our EKS cluster so I am using the existing namespace, which is <strong>monitoring<\/strong>.<\/p>\n<p>If you don&#8217;t have a dedicated namespace for your monitoring resources, create one before starting the setup.<\/p>\n<h3 id=\"step-1-create-a-configmap\">Step 1: Create a ConfigMap<\/h3>\n<p>Create a file <strong>blackbox-configmap.yaml<\/strong> and add the following contents.<\/p>\n<pre><code>apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: blackbox-exporter-config\n  namespace: monitoring\ndata:\n  config.yml: |\n    modules:\n      http_endpoint:\n        prober: http\n        timeout: 5s\n        http:\n          valid_http_versions: [\"HTTP\/1.1\", \"HTTP\/2.0\"]\n          valid_status_codes: [200, 204]\n          no_follow_redirects: false\n          preferred_ip_protocol: \"ip4\"<\/code><\/pre>\n<p>Here, you can see the <strong>metadata<\/strong> section, I have mentioned the namespace <strong>monitoring<\/strong>, you can modify it with your namespace and replace the name if you want.<\/p>\n<p>Now, focus on the <code>data<\/code> section, where you will add the modules. The Blackbox Exporter works based on modules.<\/p>\n<p>For this, I have mentioned only one <strong>HTTP<\/strong> endpoint-based module, but you can add multiple modules with different endpoint types.<\/p>\n<p>To apply this <strong>configmap<\/strong> to your cluster, use the following command.<\/p>\n<pre><code>kubectl apply -f blackbox-configmap.yaml<\/code><\/pre>\n<p>If you want to see the list <strong>configmaps<\/strong> in the <strong>monitoring<\/strong> namespace.<\/p>\n<pre><code>kubectl get configmaps -n monitoring<\/code><\/pre>\n<h3 id=\"step-2-create-the-blackbox-deployment\">Step 2: Create the Blackbox Deployment<\/h3>\n<p>Create the configuration file <strong>blackbox-deployment.yaml<\/strong> and add the contents.<\/p>\n<pre><code>---\napiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: blackbox-exporter\n  namespace: monitoring\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: blackbox-exporter\n  template:\n    metadata:\n      labels:\n        app: blackbox-exporter\n    spec:\n      containers:\n      - name: blackbox-exporter\n        image: prom\/blackbox-exporter:latest\n        args:\n          - \"--config.file=\/etc\/blackbox-exporter\/config.yml\"\n        ports:\n        - containerPort: 9115\n        volumeMounts:\n        - name: config-volume\n          mountPath: \/etc\/blackbox-exporter\n      volumes:\n      - name: config-volume\n        configMap:\n          name: blackbox-exporter-config<\/code><\/pre>\n<p>Don&#8217;t forget to add the namespace name in the <strong>metadata<\/strong> section, in the spec section, you can see that I am using the latest Blackbox image <strong>prom\/blackbox-exporter:latest<\/strong>.<\/p>\n<p>you can give a specific version if you want, and the default port of the Blackbox is 9115, which I have mentioned as the value of <strong>containerPort.<\/strong><\/p>\n<p>To apply this configuration, use the following command.<\/p>\n<pre><code>kubectl apply -f blackbox-deployment.yaml<\/code><\/pre>\n<p>To view the list of <strong>deployments<\/strong> in the <strong>monitoring<\/strong> namespace<\/p>\n<pre><code>kubectl get deployments -n monitoring -o wide<\/code><\/pre>\n<h3 id=\"step-3-create-a-service-for-blackbox-exporter\">Step 3: Create a Service for Blackbox Exporter<\/h3>\n<p>Create a configuration file <strong>blackbox-service.yaml<\/strong> and add the following contents.<\/p>\n<pre><code>---\napiVersion: v1\nkind: Service\nmetadata:\n  name: blackbox-exporter\n  namespace: monitoring\nspec:\n  selector:\n    app: blackbox-exporter\n  type: NodePort\n  ports:\n    - port: 9115\n      targetPort: 9115\n      nodePort: 30500\n\n<\/code><\/pre>\n<p>In this configuration also you have to provide the namespace name and the main thing you have to consider is the <strong>spec<\/strong> section <code>type<\/code> parameter.<\/p>\n<p>I am using <strong>NodePort <\/strong>type so that I can access the <strong>Blackbox Exporter<\/strong> over the Internet, you can use other types based on your requirements.<\/p>\n<p>The <strong>nodePort<\/strong> I have mentioned here is <strong>30500<\/strong>, otherwise, it will choose some random port from between<strong> <\/strong>the<strong> 30000-32767<\/strong> range.<\/p>\n<p>To apply this configuration, use the following command.<\/p>\n<pre><code>kubectl apply -f blackbox-service.yaml<\/code><\/pre>\n<p>To view the list of services in the <strong>monitoring<\/strong> namespace.<\/p>\n<pre><code>kubectl get svc -n monitoring -o wide<\/code><\/pre>\n<p>Now, you can able to access the <strong>Blackbox Exporter<\/strong> dashboard over the internet, for that you need the public IP of one of your nodes and the port number <strong>30500<\/strong>.<\/p>\n<p>The output you will get is<\/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-60-5.png\" class=\"kg-image\" alt=\"blackbox exporter on kubernetes: This is the dashboard of blackbox exporter \" loading=\"lazy\" width=\"1196\" height=\"997\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-60-5.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-60-5.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-60-5.png 1196w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>This time you can&#8217;t see any results in this dashboard because we haven&#8217;t mentioned the target anywhere.<\/p>\n<p>We can add the targets, only in the <strong>Prometheus<\/strong> configuration file, so for that, we have to do some modifications in the <a href=\"https:\/\/devopscube.com\/prometheus-alert-manager\/\"><strong>Prometheus<\/strong><\/a> configuration file.<\/p>\n<h3 id=\"step-4-add-blackbox-job-in-prometheus\">Step 4: Add Blackbox Job in Prometheus<\/h3>\n<p>You can directly make modifications to the <strong>Prometheus configmap<\/strong> file, for that first, you need to identify the <strong>configmap<\/strong> name.<\/p>\n<p>To list them <strong>configmaps<\/strong> in the <strong>monitoring<\/strong> namespace, use the following command.<\/p>\n<pre><code>kubectl get configmaps -n monitoring<\/code><\/pre>\n<p>Once find the configmap name, use the below command to edit it.<\/p>\n<pre><code>kubectl edit configmap prometheus-server-conf -n monitoring<\/code><\/pre>\n<p>There is an alternative method to edit the ConfigMap, for that, you need to find the ConfigMap YAML manifest.<\/p>\n<p>If you find the file name, open it with any text editor utility. In my case, the Prometheus configmap file name is <strong>config-map.yaml<\/strong>.<\/p>\n<pre><code>vim config-map.yaml<\/code><\/pre>\n<p>Add this content inside this file, under the <strong>scrape_configs<\/strong> section.<\/p>\n<pre><code>- job_name: 'blackbox-exporter-metrics'\n        metrics_path: \/probe\n        params:\n          module: [http_endpoint]\n        static_configs:\n          - targets:\n            - https:\/\/www.google.com\n            - devopscube.com\n            - https:\/\/prometheus.io\n        relabel_configs:\n         - source_labels: [__address__]\n           target_label: __param_target\n         - source_labels: [__param_target]\n           target_label: instance\n         - target_label: __address__\n           replacement: \"blackbox-exporter.monitoring.svc:9115\"<\/code><\/pre>\n<p>In the <strong>params<\/strong> section, <strong>http_endpoint<\/strong> is the module name, if you don&#8217;t remember, verify your Blackbox configmap yaml file.<\/p>\n<p>Under the <strong>static_configs<\/strong>, targets should be given, for testing purposes.<\/p>\n<p>I have given three websites so that the Blackbox Exporter will monitor these website endpoints and collect the metrics based on their health and availability.<\/p>\n<p>The replacement of the value indicates that from where the Prometheus has to scrape the metrics.<\/p>\n<p>Once the modifications are completed, you can deploy this configuration in <strong>Prometheus<\/strong>.<\/p>\n<pre><code>kubectl apply -f config-map.yaml<\/code><\/pre>\n<p>Sometimes it would take a few minutes to reflect the changes in the <strong>Prometheus<\/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-61-7.png\" class=\"kg-image\" alt=\"prometheus dashboard target\" loading=\"lazy\" width=\"887\" height=\"784\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-61-7.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-61-7.png 887w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If you are not able to find the changes in the <strong>Prometheus<\/strong> dashboard, then you can perform the rollout restart deployment.<\/p>\n<p>To restart the rollout, use the following command.<\/p>\n<pre><code>kubectl rollout restart deployment prometheus-deployment -n monitoring<\/code><\/pre>\n<p>Replace the <strong>prometheus-deployment<\/strong> with your deployment name.<\/p>\n<blockquote><p>You have to really aware when you perform rollout restart, because until the restart completed, the Prometheus won&#8217;t scrape the metrics.<\/p><\/blockquote>\n<h3 id=\"step-5-make-queries-from-prometheus\">Step 5: Make Queries from Prometheus<\/h3>\n<p>Once the configuration is completed, <a href=\"https:\/\/devopscube.com\/setup-prometheus-using-docker\/\">Prometheus<\/a> will scrape the metrics from the Blackbox Exporter based on the scrape interval.<\/p>\n<p>I am using the <strong>probe_http_status_code<\/strong> metric to see the results.<\/p>\n<p><strong>Prometheus output<\/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-62-5.png\" class=\"kg-image\" alt=\"prometheus query\" loading=\"lazy\" width=\"841\" height=\"448\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-62-5.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-62-5.png 841w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h3 id=\"step-6-visualize-the-metrics-using-the-grafana\">Step 6: Visualize the Metrics using the Grafana<\/h3>\n<p>This time I am not going to make a query using the <strong>Grafana<\/strong>. Instead of that, I am using a dashboard template to visualize the important probe metrics of all the endpoints.<\/p>\n<p><strong>Grafana output<\/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-65-6.png\" class=\"kg-image\" alt=\"grafana dashborad\" loading=\"lazy\" width=\"1039\" height=\"857\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-65-6.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-65-6.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-65-6.png 1039w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n<p>I have already done this test setup using Linux virtual machines, this time I implemented the setup in Kubernetes and I didn&#8217;t want to make this test setup more complicated so I just used only one module to probe the metrics from the endpoint.<\/p>\n<p>When you test or deploy this setup, adjust the values for your needs and try to use other modules and endpoints to know the difference and use cases.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/blackbox-exporter-on-kubernetes\/\" target=\"_blank\" rel=\"noopener noreferrer\">Setup Blackbox Exporter on Kubernetes: A Step-by-Step Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/blackbox-exporter-on-kubernetes\/<\/p>\n","protected":false},"author":1,"featured_media":758,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-757","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\/757","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=757"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/757\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/758"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}