Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a single pod name for kubernetes?

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?

like image 342
sjsc Avatar asked Jul 31 '18 11:07

sjsc


People also ask

How do I create a pod with a specific name?

If you want to create a pod using kubectl run use the below command "kubectl run times --generator=run-pod/v1 hello --image=busybox". It will create a pod with name hello. You are suppose to replace hello and image name. Otherwise you could use "kubectl create pod hello --image=busybox".

What is POD name in Kubernetes?

What is a Pod? Pods are the smallest, most basic deployable objects in Kubernetes. A Pod represents a single instance of a running process in your cluster. Pods contain one or more containers, such as Docker containers.

How do I get pod name in Helm chart?

Probably the best path is to change your Deployment into a StatefulSet. Each pod launched from a StatefulSet has an identity, and each pod's hostname gets set to the name of the StatefulSet plus an index.


2 Answers

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/

like image 109
Diego Mendes Avatar answered Nov 16 '22 00:11

Diego Mendes


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
like image 30
Markus Dresch Avatar answered Nov 15 '22 22:11

Markus Dresch