'Prometheus : unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT

I'm trying to make use of Prometheus autodiscovery inside my Kubernetes cluster for monitoring, I use a custom image based on alpine + the version 2.33.4 of Prometheus from the official release ([link][1] ).

All is just fine, but the autodiscovery isn't working and I get this error in logs :

discovery manager scrape" msg="Cannot create service discovery" err="unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined" type=kubernetes

I check inside the container and the variables exist, also I tried to force prometheus to use a host and a port by adding api_server in prometheus.yaml but nothing change

kubernetes_sd_configs:
- api_server: "https://kubernetes.default.svc"
  role: service

the image [1]: https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gz



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source