kubernetes list all running pods name

3/4/2016

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}'
-- Ali SAID OMAR
kubernetes

7 Answers

3/4/2016

You can use the go templating option built into kubectl to format the output to just show the names for each pod:

kubectl get pods --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
-- Robert Bailey
Source: StackOverflow

10/7/2018

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"
-- juancho85
Source: StackOverflow

11/17/2017

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
-- ADavid
Source: StackOverflow

9/22/2018

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'

-- krozaine
Source: StackOverflow

5/21/2018

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
-- Amadey
Source: StackOverflow

4/4/2018

jsonpath alternative

kubectl get po -o jsonpath="{range .items[*]}{@.metadata.name}{end}" -l app=nginx-ingress,component=controller

see also: more examples of kubectl output options

-- Vincent De Smet
Source: StackOverflow

1/14/2019

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

-- lanni654321
Source: StackOverflow