How to list containers running in a kubernetes pod

Unfortunately, there isnt a simple get command that can be run to retrieve containers running inside pods. However, the custom-columns command can be exposed via kubectl to get this information out.

For more info on custom-columns visit the kubectl docs – https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns

The following command will list pod name and container name for all pods in the default namespace

kubectl get pods -o=custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[*].name

The output is something like this:

kubectl_containers

You can also just get the container via the jsonpath option

kubectl get pods mongod-1 -o jsonpath='{.spec.containers[*].name}'

The reason I did a bit of research (5 minutes!) into this was because the container name is required when running the exec command in kubectl. eg:

kubectl exec -ti mongod-1 -c mongod-container bash
Advertisements

One Response to How to list containers running in a kubernetes pod

  1. manasamittal94 says:

    Nice blog with excellent information. Thank you, keep sharing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: