Home > front end >  How to get a pod name and namespace on a node running that pod?
How to get a pod name and namespace on a node running that pod?

Time:10-22

A node have a plenty of info for metrics collection, under cgroups kubepods.slice for example. but to complete the metric you have to relate a pod metric to a pod name. a name and namespace itself are kind of static metric of a pod, so they are first things alongside with pod uuid that it should have to describe a pod. How can i get this info from a node not using kubectl and not accessing remote kubernetes database of pods?

i can find only container ids/ pod uuids as the parts of cgroup structure.. where is name and namespace? (ideally a whole yaml manifest, its not that hard to store it on a node that is running pod, right?)

If its not having this info - why? that violates collection practices, you cannot do instrumentation as a single program that will collect pod metrics - you will require external process that will post-process such metrics, corresponding uuids to pods names and namespaces, and that is definetely not right approach, when you can have 1 small program running on the node itself

CodePudding user response:

You may use docker inspect:

docker inspect <container-id> --format='{{index .Config.Labels "io.kubernetes.pod.name"}

docker inspect <container-id> --format='{{index .Config.Labels "io.kubernetes.pod.namespace"}}'

Following command will list all the pod and their namespace running on the node. you can use the docker's data directory where it maintains the pod info.

find /var/lib/docker -name config.v2.json -exec perl -lnpe 's/.*pod\.name"*\s*:\s*"*([^"] ).*pod\.namespace"*\s*:\s*"*([^"] ).*/pod-name=$1 pod-namespace=$2/g' {}   |awk '!a[$0]  '|column -t

Exmaple:

find /var/lib/docker -name config.v2.json -exec perl -lnpe 's/.*pod\.name"*\s*:\s*"*([^"] ).*pod\.namespace"*\s*:\s*"*([^"] ).*/pod-name=$1 pod-namespace=$2/g' {}   |awk '!a[$0]  '|column -t
pod-name=huha              pod-namespace=foo
pod-name=zoobar1           pod-namespace=default
pod-name=kube-proxy-l2hsb  pod-namespace=kube-system
pod-name=weave-net-rbbwf   pod-namespace=kube-system
pod-name=zoobar2           pod-namespace=default

IMO, the parsing could be done with jq, I have used regex here to show the possibility of getting these values using docker data dir.

  • Related