Verified Solution

[StackOverflow/kubernetes] Is there a way to get the cluster name of Kubernetes in Grafana variables with prometheus data source?

Sponsored Content
### ROOT CAUSE The issue arises because the Prometheus data source in Grafana does not natively expose the Kubernetes cluster name in its metrics. The cluster name is not available through standard Prometheus metrics or labels unless explicitly added via configuration or custom scraping. ### CODE FIX To retrieve the Kubernetes cluster name in Grafana using the Prometheus data source, follow these steps: 1. **Add a Custom Metric to Prometheus**: - Deploy a sidecar container or use a script to scrape the Kubernetes API for the cluster name. For example, use the `cluster-info` endpoint: ```bash curl -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \ https://kubernetes.default.svc.cluster.local/api/v1/namespaces/kube-system/services/cluster-info:8443/clusterinfo/v1/clustername ``` - Expose this metric via a service monitor in Prometheus. 2. **Query the Metric in Grafana**: - Use the Prometheus query to fetch the cluster name: ```promql kube_cluster_name{cluster_name=~".*"} ``` - Create a new variable in Grafana by navigating to **Variables > Add variable**, set the **Data source** to Prometheus, and use the above query. 3. **Alternative: Use Grafana's Kubernetes Plugin**: - Install the [Grafana Kubernetes plugin](https://grafana.com/grafana/plugins/grafana-kubernetes-plugin/) for dedicated Kubernetes metadata queries. - Use the `kube` data source to fetch the cluster name directly. This approach ensures the cluster name is dynamically retrieved and available in Grafana variables.
Deploy on DigitalOcean ($200 Credit)

Related Fixes

[rust-lang/rust] [ICE]: `expected DefId to be a foreign function`
[StackOverflow/go] selenium/Edge driver fills up memory in golang application
[facebook/react] Bug: startTransition inside popstate shows Suspense fallback instead of previous UI