172

I have 3 nodes, running all kinds of pods. I would like to have a list of nodes and pods, for an example:

NODE1 POD1
NODE1 POD2
NODE2 POD3
NODE3 POD4

How can this please be achieved?

Thanks.

Kenneth Ito
  • 5,201
  • 2
  • 25
  • 44
testTester
  • 2,371
  • 3
  • 18
  • 22
  • 3
    `kubectl get pod --all-namespaces -o json | jq '.items[] | .spec.nodeName + " " + .metadata.name'` – Adiii Oct 20 '20 at 23:30

7 Answers7

274

You can do that with custom columns:

kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces

or just:

kubectl get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name --all-namespaces
nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • 1
    Thanks. This is what I was looking for. It worked, but needed to add --all-namespaces to the command so pods would appear. E.g.: `kubectl get pod --all-namespaces -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName` – testTester Feb 26 '18 at 08:57
  • 5
    I found it helpful to add `| sort` at the end of the second command so that the results are grouped by node. – jwadsack Apr 10 '19 at 18:25
  • This is excellent answer to find and group pods by nodes . Helpful in finding which nodes contain which pods – Joy Apr 21 '20 at 09:37
  • 5
    the -o=wide option will default show you the node as well. ```kubectl get pods -o=wide``` – Michael Jun 30 '20 at 11:26
  • `| sort -r ` keeps headers at the top of sorting – Cirelli94 May 27 '21 at 11:22
  • @Cirelli94 `| sort -r` only keeps the header at the top if your node names come before NAME alphabetically! (sort -r just reverses the sort order). You would have to use something like `| (read -r; printf "%s\n" "$REPLY"; sort)` to always keep the header at the top (or others from [this post](https://unix.stackexchange.com/questions/11856/sort-but-keep-header-line-at-the-top)). – ngreatorex May 01 '23 at 12:39
191

kubectl has a simple yet useful extended output format that you can use like

kubectl get pod -o wide

so while custom formats provided in other answers are good, this might be a handy shortcut.

Radek 'Goblin' Pieczonka
  • 21,554
  • 7
  • 52
  • 48
15

You can use kubectl get pods --all-namespaces to list all the pods from all namespaces and kubectl get nodes for listing all nodes.

Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
  • OK, thanks. While your commands work, and it was kind of what I was using so far, the goal is to have a single command that outputs the pods/nodes relation. Hope you can please help. Again, thanks! – testTester Feb 26 '18 at 08:01
  • 1
    Maybe this one helps you: kubectl get pods -o wide, but it will print a lot of infos. – Dina Bogdan Feb 26 '18 at 08:05
10

The following command does more or less what you wanted. However, it's more of a jq trick than kubectl trick:

kubectl get pod --all-namespaces -o json | jq '.items[] | .spec.nodeName + " " + .status.podIP'

Community
  • 1
  • 1
lang2
  • 11,433
  • 18
  • 83
  • 133
  • Thanks! After installing "jq", unfortunately I get a "parse error: Invalid numeric literal at line 1, column 10" with your command above. – testTester Feb 26 '18 at 08:00
  • This only gives nodes' names & pods' IPs – Amine Zaine Oct 18 '19 at 15:06
  • @AmineZaine replace the .status.podip with `kubectl get pod --all-namespaces -o json | jq '.items[] | .spec.nodeName + " " + .metadata.name'` – Adiii Oct 20 '20 at 23:31
8

This gets you: "nodeName namespace pod" across the cluster:

kubectl get pods --all-namespaces --output 'jsonpath={range .items[*]}{.spec.nodeName}{" "}{.metadata.namespace}{" "}{.metadata.name}{"\n"}{end}'
Michael Sinz
  • 81
  • 1
  • 1
7

Maybe the answers are a little bit old, now you can simply launch this:

kubectl get pods -o wide
6

Not exactly as you wanted cause it describe much more, but you can use

kubectl describe nodes

it will expose each pod per node in the cluster with the following info

Namespace | Name | CPU Requests | CPU Limits | Memory Requests | Memory Limits

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47
  • Thanks very much! Do you know please if it is possible to remove all the extra information and maybe present the details in a simplified table, as per the question above? Again, thanks! – testTester Feb 26 '18 at 07:34