Blog

    Prometheus Can't Reach Your App? Network Policies Might Be to Blame!

    Written By
    Nic Vermandé
    Published Date
    Oct 17 2024
    Read Time
    5 minutes

    Prometheus is a powerful tool for monitoring Kubernetes applications, but it can run into issues if it's unable to scrape metrics from your services. One common reason for this is a misconfigured network policy that’s blocking Prometheus from reaching the application endpoints.

    If Prometheus can’t access your application metrics, it’s likely that a network policy is restricting the communication path between Prometheus and the service. Let’s explore how you can identify and fix this issue.


    What Does the Error Look Like?

    If Prometheus is being blocked by a network policy, you might notice errors both in Kubernetes and Prometheus logs.

    In Prometheus, you might see messages like:

    level=warn ts=2023-10-10T12:34:56.789Z caller=scrape.go:1366 component="scrape manager" scrape_pool=app-metrics target=http://<app-service>:8080/metrics msg="Scrape failed" err="Get "http://<app-service>:8080/metrics": context deadline exceeded"


    In Kubernetes, you might also notice failed connections when describing the Prometheus pod:

    Warning  Failed  1m (x3 over 3m)  kubelet  Readiness probe failed: Get "http://<app-service>:8080/metrics": context deadline exceeded


    These errors suggest that Prometheus is unable to connect to the endpoint due to a blocked connection, which is often caused by a network policy not allowing the necessary traffic.


    Why is This Happening?

    Network policies in Kubernetes control the traffic that is allowed to and from pods. If your application has a restrictive network policy that doesn't allow Prometheus to access its metrics endpoint, Prometheus won't be able to scrape the data it needs. This can lead to incomplete metrics, broken dashboards, or missing alerts.


    How to Fix Prometheus Scraping Issues Due to Network Policies

    To resolve the issue, you need to ensure that network policies allow Prometheus to access the target metrics endpoint. Here are the steps you can take:

    1. Check Existing Network Policies: Make sure that Prometheus is allowed to connect to the services it needs. You need a network policy that explicitly allows ingress to the metrics endpoint.

    2. Test Connectivity using kubectl exec and netcat

    To see if a network policy is blocking communication, you can run a connectivity test from the Prometheus pod to the target application:

    kubectl exec -it prometheus-pod -- nc -zv app-service 8080

       

    • Replace prometheus-pod with the name of the Prometheus pod.

    • Replace app-service with the name of the target service.

    • Replace 8080 with the port where metrics are exposed.



      If no shell is available in the container (distroless image) or netcat is not installed, you can use kubectl debug to achieve the same:

      kubectl debug my_source_pod -it --image=ubuntu --target=my_source_container apt-get update && apt-get install -y netcat 


      And run the command:

      nc -zv my_svc 8080

    3. Review and Adjust Network Policies


    If Prometheus cannot reach the metrics endpoint, it's likely due to a restrictive network policy. Here's an example of a problematic policy that doesn’t allow Prometheus ingress:

       apiVersion: networking.k8s.io/v1
       kind: NetworkPolicy
       metadata:
         name: restrict-app
       spec:
         podSelector:
           matchLabels:
             app: my-app
         policyTypes:
         - Ingress
         ingress:
         - from:
             - podSelector: {}

      

       This policy is too restrictive and doesn't allow traffic from Prometheus.

     Corrected policy

    Here's a corrected policy that allows ingress from Prometheus:

       apiVersion: networking.k8s.io/v1
       kind: NetworkPolicy
       metadata:
         name: allow-prometheus-to-app
       spec:
         podSelector:
           matchLabels:
             app: my-app
         policyTypes:
         - Ingress
         ingress:
         - from:
             - podSelector:
                 matchLabels:
                   app: prometheus


    This policy allows Prometheus to scrape the metrics by permitting ingress traffic from the Prometheus pod to the application.


    Monitor Traffic with tcpdump

    If the issue still persists, you can use tcpdump to monitor the traffic and identify where it's being blocked:

    1. Run an ephemeral container that shares the host network:

         kubectl debug node/<node-name> -it --image=ubuntu


    2. Update and install tcpdump:

        apt-get update && apt-get install tcpdump -y


    3. Use tcpdump to monitor traffic from the Prometheus pod IP to the application service:

        tcpdump -i any src <prometheus-pod-ip> and dst <app-service-ip> and port 8080
    • Replace <prometheus-pod-ip> with the IP address of the Prometheus pod.

    • Replace <app-service-ip> with the IP address of the application service.

    • Replace 8080 with the relevant port.

    Automate Network Policies with Otterize

    Manually managing network policies can be challenging, especially in dynamic environments. 

    Otterize's Intent Operator and Network Mapper can help automate the creation and management of these policies, ensuring your pods have the connectivity they need without manual tweaking.


    Here’s a quick tutorial on how to deploy Otterize and start creating network policies, and a longer and detailed guide on mastering cloud-native packets flows in your cluster.


    Ready to make network policies a breeze?

    Stop stressing over network policy details. Let Otterize handle the heavy lifting with ClientIntents, and get back to focusing on your app’s real business.


    Stay connected and learn more

    If you found this article helpful, we'd love to hear from you! Here are some ways to stay connected and dive deeper into Otterize.

    🏠 Join our community

    Stay ahead of the curve by joining our growing Slack community. Get the latest Otterize updates and discuss real-world use cases with peers.

    🌐 Explore our resources

    Take a sneak peek at our datasheets:

    Or continue your exploration journey with our blogs and tutorials, or self-paced labs.

    Don't be a stranger – your insights and questions are valuable to our community!

    Like this article?

    Sign up for newsletter updates

    By subscribing you agree to with our Privacy Policy and to receive updates from us.
    Share article
    Resource Library

    Read blogs by Otis, run self-paced labs that teach you how to use Otterize in your browser, or read mentions of Otterize in the media.

    • Kubernetes
    • Network Policy
    • AWS
    • IAM
    Jan 27 2025
    New year, new features

    We have some exciting announcements for the new year! New features for both security and platform teams, usability improvements, performance improvements, and more! All of the features that have been introduced recently, in one digest.

    • Kubernetes
    • Zero-trust
    • IBAC
    • Automation
    • Startups
    • Podcasts
    • Network Policy
    • PCI
    Dec 11 2024
    First Person Platform E04 - Ian Evans on security as an enabler for financial institutions

    The fourth episode of First Person Platform, a podcast: platform engineers and security practitioners nerd out with Ori Shoshan on access controls, Kubernetes, and platform engineering.

      Oct 31 2024
      Kubernetes Liveness Probe Failed: Connection Refused