How do you secure Prometheus scraping across namespaces in a cluster?
IntermediateScenarioPrometheus and PromQL at Scale
Answer
Situation read → Metrics endpoints can reveal internals. Scraping must be authenticated and limited by network and RBAC.
What I'd do →
- Give Prometheus a ServiceAccount with a ClusterRole limited to
get/list/watchon pods, services, endpoints, andnonResourceURLs: ["/metrics"]. - Use TLS and bearer tokens for sensitive targets (kubelet, API server, kube-rbac-proxy in front of app metrics).
- Use NetworkPolicies: allow ingress to metrics ports only from the monitoring namespace.
- Restrict which namespaces the Operator watches (
serviceMonitorNamespaceSelector). - Do not expose the Prometheus UI publicly. Put it behind an authenticated proxy (OAuth2 Proxy).
Why → Metrics can leak topology and business numbers, and Prometheus holds powerful discovery credentials.
Interview tip → Mention that Prometheus itself has no built-in user authentication on its UI, so you front it.
More on Prometheus and PromQL at Scale
Prometheus memory has grown to 60 GB and keeps climbing. How do you find the cause?IntermediateA developer added `user_id` as a metric label. What is the problem and what do you advise?BeginnerYour dashboard shows the average of per-pod p99 latencies. Why is it wrong, and how do you compute a correct fleet-wide p99?IntermediateChoose histogram bucket boundaries for a service with a 300 ms latency SLO.AdvancedYou have 500 short-lived batch jobs that finish before Prometheus can scrape them. How do you capture their metrics?IntermediateYour EC2 fleet autoscales constantly. How do you keep Prometheus targets up to date?Intermediate