List Namespace name, Namespace Age, and Status on Grafana using PromQL (prometheus) from Kubernetes Metric server

6/22/2020

We are having a Kubernetes cluster and using Prometheus + Grafana for monitoring and alerting. We need to show a panel on Grafana that shows us the view (same as kubectl get namespaces) . Currently we are able to get name and status column using the below PROMQL along with Hide options in Visualization section of Grafana.

count(kube_namespace_status_phase) by (phase, namespace)

But we also want to find the AGE from when a namespace was active/created. We are not able to find AGE in any of the 4 kube metrics of namespace available -

  1. kube_namespace_created
  2. kube_namespace_status_phase
  3. kube_namespace_lables
  4. kube_namespace_annotations

Any suggestions would be helpful.

-- GauravP
grafana
kubernetes
prometheus

3 Answers

6/23/2020

Unfortunately as you already noticed there is no specific metric that could be used to calculate the age of an object. The closest thing that you could use to achieve your goal would be to use kube_namespace_created which shows at what time namespace in Kubernetes was created.

I was also not able to find a proper Prometheus operator/function in order to make some kind of workaround PROMQL.

I am posting this answer as a community wiki. Feel free to expand on it as you wish.

I hope it helps.

-- WytrzymaƂy Wiktor
Source: StackOverflow

8/5/2020

I have seen on other metrics regarding age that you need to multiply by 1000

So if you do;

kube_namespace_created * 1000

You would need to check/test but I've seen that used on other queries about finding the age of worker nodes.

-- Syko80
Source: StackOverflow

3/28/2022

The following query returns the age of every Kubernetes namespace in seconds:

time() - kube_namespace_created

It uses time() function, which returns the current time in seconds.

-- valyala
Source: StackOverflow