10

I'm looking for a command like "gcloud config get-value project" that retrieves a project's name, but for a pod (it can retrieve any pod name that is running). I know you can get multiple pods with "kubectl get pods", but I would just like one pod name as the result.

I'm having to do this all the time:

kubectl get pods       # add one of the pod names in next line
kubectl logs -f some-pod-frontend-3931629792-g589c some-app 

I'm thinking along the lines of "gcloud config get-value pod". Is there a command to do that correctly?

Daniel Marques
  • 1,249
  • 8
  • 17
sjsc
  • 4,552
  • 10
  • 39
  • 55

3 Answers3

14

There are many ways, here are some examples of solutions:

kubectl get pods -o name --no-headers=true

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

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

For additional reading, please take a look to these links:

kubernetes list all running pods name

Kubernetes list all container id

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

Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27
Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
10

You can use the grep command to filter any output on stdout. So to get pods matching a specified pattern you can use a command like this:

> kubectl get pods --all-namespaces|grep grafana

Output:

monitoring      kube-prometheus-grafana-57d5b4d79f-smkz6               2/2       Running   0          1h

To only output the pod name, you can use the awk command with a parameter of '{print $2}', which displays the second column of the previous output:

kubectl get pods --all-namespaces|grep grafana|awk '{print $2}'

To only display one line you can use the head command like so:

kubectl get pods --all-namespaces|grep grafana|awk '{print $2}'|head -n 1
Markus Dresch
  • 5,290
  • 3
  • 20
  • 40
  • Thanks Markus. Is there a way to just output the "kube-prometheus-grafana-57d5b4d79f-smkz6" value? – sjsc Jul 31 '18 at 11:29
  • Okay great! Just one more issue. I have 3 pods running and it's listing all 3 pods. Can I output just one of the pod's name? (Sorry, I'm a complete novice at grep) – sjsc Jul 31 '18 at 11:34
  • sure :) another command to the rescue: head -n 1. updated answer. or you could provide the full pod name in grep. and there are many more ways to do it, like Diegos answer. – Markus Dresch Jul 31 '18 at 11:35
  • On Windows, use `findstr` in place of `grep`. – Venryx Aug 28 '21 at 23:23
0
  • This will output the last pod name : kubectl get pods -o go-template --template ' {{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | awk '{print $1}' | tail -n 1

  • if you want to fetch logs for a pod: kubectl logs -f kubectl get pods -o go-template --template ' {{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | awk '{print $1}' | tail -n 1

just run the command with a single quotes.

Links:

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

Hope this helps!!

PRAVEEN KUMAR
  • 39
  • 1
  • 1
  • 10