1

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

xakepp35
  • 2,878
  • 7
  • 26
  • 54
  • 1
    try this `find /var/lib/kubelet/pods -name etc-hosts -exec grep -ohP '^\d+\.\d+\.\d+\.\d+\s+\K.*' {} + |grep -vE "localhost|$(hostname)"` this is only a partial or partial answer. – P.... Oct 19 '21 at 19:54
  • Does this answer your question? [Programmatically get the name of the pod that a container belongs to in Kubernetes?](https://stackoverflow.com/questions/37253068/programmatically-get-the-name-of-the-pod-that-a-container-belongs-to-in-kubernet) – Chin Huang Oct 19 '21 at 19:54
  • @ChinHuang no. i want to get all pods names on a node that is running kubelet – xakepp35 Oct 19 '21 at 20:08
  • @P.... yes, it got a pod name + containers folder contains all containers nams, which is very good. Only question - is it possible to get a namespace? – xakepp35 Oct 19 '21 at 20:56
  • Which container runtime are you using? – Mikolaj S. Oct 20 '21 at 12:24
  • @Mikolaj i want to do monitoring for different container envs – xakepp35 Oct 21 '21 at 06:09

1 Answers1

1

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.

Note: for crio a similar json is placed under overlay directory. see OP comment below.

P....
  • 17,421
  • 2
  • 32
  • 52
  • nice catch! if i use crio is it the same? – xakepp35 Oct 20 '21 at 08:52
  • 1
    you should be able to find similar info at `/var/lib/containers`, I am not sure of the structure of the data as I do not have env to test `crio`. – P.... Oct 20 '21 at 13:55
  • you were right. in crio its even much easier - you have just 1 json file in overlay folder, with all info that is needed, much less syscalls to collect. thanks your idea saved our lives) – xakepp35 Nov 05 '21 at 13:21