<p>I am trying to get the <em>first</em> pod from within a deployment (filtered by labels) with status <em>running</em> - 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:</p>

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

<p>How is it possible to</p>

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

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

<p>Regards</p>

<p>Update: I already tried the link posted in the comments earlier with the following:</p>

<pre><code>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'
</code></pre>

<p>the result is 4x "null" - there are 4 running pods.</p>

<p>Edit2: Resolved - see comments</p>

<p>Since kubectl 1.9 you have the option to pass a <code>--field-selector</code> argument (see <a href="https://github.com/kubernetes/kubernetes/pull/50140" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/pull/50140</a>). E.g.</p>
<pre><code>kubectl get pod -l app=yourapp --field-selector=status.phase==Running -o jsonpath=&quot;{.items[0].metadata.name}&quot;
</code></pre>
<p>Note however that for not too old <code>kubectl</code> versions, many reasons to find a running pod are moot, 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:</p>
<pre><code>$ 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 &quot;https&quot; in a pod selected by the service
  kubectl port-forward service/myservice 8443:https
</code></pre>
<p>(Note <code>logs</code> also accepts a service, even though an example is omitted in the help.)</p>
<p>The selection algorithm favors &quot;active pods&quot; for which a main criterion is having a status of &quot;Running&quot; (see <a href="https://github.com/kubernetes/kubectl/blob/2d67b5a3674c9c661bc03bb96cb2c0841ccee90b/pkg/polymorphichelpers/attachablepodforobject.go#L51" rel="nofollow noreferrer">source</a>).</p>
