Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you cleanly list all the containers in a kubernetes pod?

Tags:

kubernetes

People also ask

How do you clean up Kubernetes pods?

First, confirm the name of the node you want to remove using kubectl get nodes , and make sure that all of the pods on the node can be safely terminated without any special procedures. Next, use the kubectl drain command to evict all user pods from the node.

How do I list all containers in Docker?

In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command. This command provides a variety of ways to list and filter all containers on a particular Docker engine.

Which command can be used to list pods in Kubernetes?

The most common operations can be done with the following kubectl commands: kubectl get - list resources. kubectl describe - show detailed information about a resource. kubectl logs - print the logs from a container in a pod.


Answer

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

Explanation

This gets the JSON object representing the pod. It then uses kubectl's JSONpath to extract the name of each container from the pod.


You can use get and choose one of the supported output template with the --output (-o) flag.

Take jsonpath for example, kubectl get pods -l k8s-app=kube-dns -o jsonpath={.items[*].spec.containers[*].name} gives you etcd kube2sky skydns.

Other supported output output templates are go-template, go-template-file, jsonpath-file. See http://kubernetes.io/docs/user-guide/jsonpath/ for how to use jsonpath template. See https://golang.org/pkg/text/template/#pkg-overview for how to use go template.

Update: Check this doc for other example commands to list container images: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/


Quick hack to avoid constructing the JSONpath query for a single pod:

$ kubectl logs mypod-123
a container name must be specified for pod mypod-123, choose one of: [etcd kubesky skydns]

I put some ideas together into the following:

Simple line:

kubectl get po -o jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{range .spec.containers[*]}{"\tname: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}'

Split (for readability):

kubectl get po -o jsonpath='
    {range .items[*]}
    {"pod: "}
    {.metadata.name}
    {"\n"}{range .spec.containers[*]}
    {"\tname: "}
    {.name}
    {"\n\timage: "}
    {.image}
    {"\n"}
    {end}'

if you want a clear output of which containers are from each Pod

kubectl get po -l k8s-app=kube-dns \
   -o=custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[*].name

If you use json as output format of kubectl get you get plenty details of a pod. With json processors like jq it is easy to select or filter for certain parts you are interested in.

To list the containers of a pod the jq query looks like this:

kubectl get --all-namespaces --selector k8s-app=kube-dns --output json pods \
  | jq --raw-output '.items[].spec.containers[].name'

If you want to see all details regarding one specific container try something like this:

kubectl get --all-namespaces --selector k8s-app=kube-dns --output json pods \
  | jq '.items[].spec.containers[] | select(.name=="etcd")'

Use below command:

kubectl get pods -o=custom-columns=PodName:.metadata.name,Containers:.spec.containers[*].name,Image:.spec.containers[*].image