List all containers in a single pod in Kubernetes

0
15044
List all containers in a single pod in Kubernetes

Typically, you will run only one container in a single pod. However, if there is any situation that you need to deal with several containers in a pod, you have to list all containers in a single pod in Kubernetes. I will show you how to do that in this article.

List all containers in a single pod in Kubernetes

The tip here is to use kubectl get pods in combination with the output option -o. The full command to list all containers in a single pod is:

$ kubectl get pods POD_NAME -n NAMESPACE -o jsonpath='{.spec.containers[*].name}*'

For example:

$ kubectl get pods my-pod -n app-ns -o jsonpath='{.spec.containers[*].name}*'
webapi data

In my result, there are two containers webapi and data running in same pod my-pod inside namespace app-ns.

That is a short way to get all containers’ names in a single command.

However, if you have interest in containers’ details, you can issue this command:

$ kubectl describe pod my-service
Name:         client-pod
Namespace:    default
Priority:     0
Node:         docker-desktop/192.168.65.3
Start Time:   Sun, 23 Feb 2020 21:02:51 -0600
Labels:       component=web
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"component":"web"},"name":"client-pod","namespace":"default"},"spec...
Status:       Running
IP:           10.1.0.15
IPs:          <none>
Containers:
  webapi:
  ...
  data:
  ...

You will see right under the Containers key, there are list of containers’ specs starting with their names. In facts, they are spec of containers when applying Kubernetes template.

Conclusion

As I mentioned above, it is pretty unsual to have multiple containers running inside one pod in Kubernetes; however, in case you face that situation, you should know how to list all containers in single pod in Kubernetes in order to proceed to job, such as: transfer files between Kubernetes pod and local system to analyze for problems…etc.