Kubernetes. Finding initContainers in pods in a particular namespace

9/6/2021

I've recently joined a new project in Kubernetes. Last team didn't seem to manage deployments well and some task are managed wit single pods running with init containers in'em.

So, for example we have namespace "test" and pods there - some of them were run manually, aside the deployment by other team members and contain initContainers. I need to find a particular pod with init container in it - get his name, manifest and so on.

The cheat sheet in Kubernetes docs suggests a solution with getting a container id:

kubectl get pods -n test -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3

It gives me an ID of InitContainer.

When I try to get name of it and respectively pod name I try to use this code snippet:

kubectl get pod -n test -o jsonpath='{range .items[?(@.status.containerStatuses[].containerID=="docker://686bc30be6e870023dcf611f7a7808516e041c892a236e565ba2bd3e0569ff7a")]}{.metadata.name}{end}'

and it gives me nothing so far.

Is there more elegant and easy way of getting pod names with initcontainers in a particular namespace?

I also tried this solution:

kubectl get pod -n test -o custom-columns='POD_NAME:metadata.name,INIT_NAME:spec.InitContainers[*].name'

but it returns nothing.

The solution I'am using now is parsing yaml output with "for" cycle in bash but it doesn't sound good to me.

Any suggestions?

-- sickb0y
kubernetes

1 Answer

9/7/2021

You need to improve your request with initContainerStatuses to find necessary information only for Init containers:

kubectl get pod -n <namespace> -o jsonpath='{range .items[?(@.status.initContainerStatuses[].containerID=="docker://<container_id>")]}{.metadata.name}{end}'

For example:

kubectl get pod -n kube-system -o jsonpath='{range .items[?(@.status.initContainerStatuses[].containerID=="docker://e235d512c3a5472c8f7de6e73c724317639c9132c07193
cb9")]}{.metadata.name}{end}'
weave-net-s26tf
-- Andrew Skorkin
Source: StackOverflow