150

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
  • 6,404
  • 8
  • 39
  • 56
  • 1
    Actually your command is quite good (comparing to suggested solutions below). You can replace the `awk` with **grep -Po '\/\K.+'** to [keep selection after \K](https://www.regular-expressions.info/keep.html), and also remove `--no-headers=true` – Noam Manos Feb 26 '20 at 14:41
  • 3
    I find `cut -d/ -f2` is easier to type than either the `awk` or the `grep`. – Robert Steward Apr 15 '21 at 15:31

15 Answers15

232

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
  • 2,569
  • 1
  • 9
  • 6
121

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}}'
Community
  • 1
  • 1
Robert Bailey
  • 17,866
  • 3
  • 50
  • 58
  • 1
    Thanks, Can't find any reference about go-template. – Ali SAID OMAR Mar 07 '16 at 08:26
  • 2
    http://kubernetes.io/v1.1/docs/user-guide/kubectl/kubectl_get.html describes the different output types and gives an example of using the template. The templating language itself is described at https://golang.org/pkg/text/template/ and you can run `kubectl describe -o yaml pod ` to see the fields that are available to operate on. – Robert Bailey Mar 07 '16 at 19:30
  • You have a whitespace too much in the first template declaration: {{range .items}} – HermanTheGermanHesse Jun 09 '20 at 13:00
72

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
  • 1,481
  • 15
  • 22
  • 4
    Actually, you can just do: `kubectl get pods -o name` then use the output `pod/mypod-xxxxx` like this : `kubectl describe pod/mypod-xxxxx` – Brou Nov 21 '18 at 16:14
  • 3
    Using awk instead of sed would make it working with anything (you are ignoring the first 4 characters, but what about another resource than pods, with a longer or shorter name?). With awk : `kubectl get pods -o=name | awk -F "/" '{print $2}'` – ZedTuX Jan 16 '19 at 05:30
  • As @Brou pointed out, `resource/name` is a valid format to kubectl to specific objects, so you don't need to trim the `pod/` prefix. – Devy Mar 08 '19 at 18:51
48

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
  • 581
  • 4
  • 4
18

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
  • 421
  • 1
  • 4
  • 9
17

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 want a quick way to access something from the running pod(s), such as logs :

kubectl logs -f $(kubectl get pods -o=name --field-selector=status.phase=Running)
ponsfrilus
  • 1,010
  • 1
  • 17
  • 25
13

There is also this solution:

kubectl get pods -o jsonpath={..metadata.name}
Radu Gabriel
  • 2,841
  • 23
  • 15
  • 1
    This is the most concise way for sure, and meet's the OP's requirement of not piping through another tool, but could be problematic if there's more than one pod name returned. Appending a newline, and wrapping the name + newline in a range clause ensures readability: `kubectl get pods -o jsonpath='{range.items[*]}{..metadata.name}{"\n"}{end}'` – Dale C. Anderson Mar 17 '22 at 22:51
  • @DaleC.Anderson - running `kubectl -n pool-agents get pods -o jsonpath='{range.items[*]}{..metadata.name}{"\n"}{end}'` returns this error: `error: error parsing jsonpath {range.items[*]}{..metadata.name}{\n}{end}, unrecognized character in action: U+005C '\'` any idea why? – Tar Nov 10 '22 at 19:32
  • @Tar no idea. If you quote it as written, you shouldn't get that error, but if you skip the single quotes surrounding the json expression, it produces that error. Maybe its your shell, or maybe you missed some chars? If the error persists, ask a new Q. Thats what this site is for . – Dale C. Anderson Nov 12 '22 at 00:15
  • @DaleC.Anderson ok found it: need to swap single quotes with double quotes in all places in Windows' console (CMD and PowerShell). This works: `kubectl -n pool-agents get pods -o jsonpath="{range.items[*]}{..metadata.name}{'\n'}{end}"` – Tar Nov 12 '22 at 16:38
8

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>.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Zstack
  • 4,046
  • 1
  • 19
  • 22
4

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
  • 4,859
  • 2
  • 34
  • 41
3

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

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
0

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
Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
0

I am using like below :

kubectl get pods -l service=usercontent

It brings all pods that related this service.

nzrytmn
  • 6,193
  • 1
  • 41
  • 38
0
kubectl get pods --no-headers -o name
DimiDak
  • 4,820
  • 2
  • 26
  • 32
-1
kubectl exec -it $(kubectl get pods | grep mess | awk '{print $1}') /bin/bash


hostname -i

https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/

kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |tr -s '[[:space:]]' '\n' |sort |uniq  | grep mess

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\

sort

feca4d9f-02db-4edf-88a2-6e9d169a92a9

You can get all the values of a ConfigMap in one line by using the kubectl get command with the -o go-template option to iterate over the values and print them on separate lines. Here's an example:

kubectl get configmap MY_CONFIG_MAP -o go-template='{{range $k,$v := .data}}{{$k}}={{$v}}{{"\n"}}{{end}}'

kubectl get configmap MY_CONFIG_MAP -o jsonpath='{.data}'
-10

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

lanni654321
  • 1,019
  • 11
  • 7