Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes (kubectl) get running pods

I am trying to get the first pod from within a deployment (filtered by labels) with status running - currently I could only achieve the following, which will just give me the first pod within a deployment (filtered by labels) - and not for sure a running pod, e.g. it might also be a terminating one:

kubectl get pod -l "app=myapp" -l "tier=webserver" -l "namespace=test" 
                -o jsonpath="{.items[0].metadata.name}"

How is it possible to

a) get only a pod list of "RUNNING" pods and (couldn't find anything here or on google)

b) select the first one from that list. (thats what I'm currently doing)

Regards

Update: I already tried the link posted in the comments earlier with the following:

kubectl get pod -l "app=ms-bp" -l "tier=webserver" -l "namespace=test"  
-o json | jq -r '.items[] | select(.status.phase = "Running") | .items[0].metadata.name'

the result is 4x "null" - there are 4 running pods.

Edit2: Resolved - see comments

like image 754
user3746259 Avatar asked Jan 29 '23 12:01

user3746259


1 Answers

Since kubectl 1.9 you have the option to pass a --field-selector argument (see https://github.com/kubernetes/kubernetes/pull/50140). E.g.

kubectl get pod -l app=yourapp --field-selector=status.phase==Running -o jsonpath="{.items[0].metadata.name}"

Note however that for not too old kubectl versions many reasons to find a running pod are mute, because a lot of commands which expect a pod also accept a deployment or service and automatically select a corresponding pod. To quote from the documentation:

$ echo exec logs port-forward | xargs -n1 kubectl help | grep -C1 'service\|deploy\|job'

  # Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default
  kubectl exec deploy/mydeployment -- date

  # Get output from running 'date' command from the first pod of the service myservice, using the first container by default
  kubectl exec svc/myservice -- date

--

  # Return snapshot logs from first container of a job named hello
  kubectl logs job/hello

  # Return snapshot logs from container nginx-1 of a deployment named nginx
  kubectl logs deployment/nginx -c nginx-1

--

 Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.

--

  # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
  kubectl port-forward deployment/mydeployment 5000 6000

  # Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by the service
  kubectl port-forward service/myservice 8443:https

(Note logs also accepts a service, even though an example is omitted in the help.)

The selection algorithm favors "active pods" for which a main criterion is having a status of "Running" (see https://github.com/kubernetes/kubectl/blob/2d67b5a3674c9c661bc03bb96cb2c0841ccee90b/pkg/polymorphichelpers/attachablepodforobject.go#L51).

like image 54
David Ongaro Avatar answered Feb 02 '23 10:02

David Ongaro