kubernetes list all running pods name

Viewed 110561

I looking for the option to list all pods name

How to do without awk (or cut). Now i'm using this command

kubectl get --no-headers=true pods -o name | awk -F "/" '{print $2}'
12 Answers

Personally I prefer this method because it relies only on kubectl, is not very verbose and we don't get the pod/ prefix in the output:

kubectl get pods --no-headers -o custom-columns=":metadata.name"

Get Names of pods using -o=name Refer this cheatsheet for more.

kubectl get pods -o=name

Example output:

pod/kube-xyz-53kg5
pod/kube-xyz-jh7d2
pod/kube-xyz-subt9

To remove trailing pod/ you can use standard bash sed command

kubectl get pods -o=name | sed "s/^.\{4\}//"

Example output:

kube-xyz-53kg5
kube-pqr-jh7d2
kube-abc-s2bt9

To get podname with particular string, standard linux grep command

kubectl get pods -o=name | grep kube-pqr | sed "s/^.\{4\}//"

Example output:

kube-pqr-jh7d2

With this name, you can do things, like adding alias to get shell to running container:

alias bashkubepqr='kubectl exec -it $(kubectl get pods -o=name | grep kube-pqr | sed "s/^.\{4\}//") bash'

You can use custom-columns in output option to get the name and --no-headers option

kubectl get --no-headers=true pods -l app=external-dns -o custom-columns=:metadata.name

You can use -o=name to display only pod names. For example to list proxy pods you can use:

kubectl get pods -o=name --all-namespaces | grep kube-proxy

The result is:

pod/kube-proxy-95rlj
pod/kube-proxy-bm77b
pod/kube-proxy-clc25

There is also this solution:

kubectl get pods -o jsonpath={..metadata.name}

Get all running pods in the namespace

kubectl get pods --field-selector=status.phase=Running --no-headers -o custom-columns=":metadata.name" 

From viewing, finding resources.

You could also specify namespace with -n <namespace name>.

Here is another way to do it:

kubectl get pods -o=name --field-selector=status.phase=Running

The --field-selector=status.phase=Running is needed as the question mention all the running pod names. If the all in the question is for all the namespaces, just add the --all-namespaces option.

Note that this command is very convenient when one what a quick way to access something on the running pod(s), such as logs :

kubectl logs -f $(kubectl get pods -o=name --field-selector=status.phase=Running)

If you want to extract specific container's pod name then A simple command can do all the hard work

kubectl get pods --template '{{range .items}}{{.metadata.name}}{{end}}' --selector=app=<CONTAINER-NAME>

Just replace <CONTAINER-NAME> with your service container-name

Well, In our case we have kept pods inside different namespace, here to identify the specific pod or list of pods we ran following command-

Approach 1:

  1. To get the list of namespaces kubectl get ns -A

  2. To get all the pods inside one namespaces kubectl get pods -n <namespace>

Approach 2:

Use this command-

kubectl get pods --all-namespaces

kubectl get po --all-namespaces | awk '{if ($4 != "Running") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'

Related