<p>The problem is that after deploying new deployment like</p>
<pre><code>kubectl apply -f deployment.yml
</code></pre>
<p>(let's say deployment with one replica)</p>
<p>Kubernetes will create second pod and shutdown the previous one - ok so far.</p>
<p>But immediately after kubectl apply I would like to detect in CI/CD if deployment was successful and in any case (no matter if rollout succeeded or failed) fetch log from one of newly deployed pod in order to provide as much as possible information in CI/CD log to determine what went wrong.</p>
<p>So I'm using</p>
<pre><code>kubectl rollout status deployment deployment-name
</code></pre>
<p>which is waiting for deployment to rollout. Immediately after though you will end up with two pods, one in status &quot;Running&quot; and another &quot;Terminating&quot;.</p>
<p>Now the problematic part:
Normally I would use method like</p>
<pre><code>kubectl get pods --selector=app=deployment-name --output=jsonpath='{.items[*].metadata.name}' --field-selector=status.phase=Running
</code></pre>
<p>but unfortunately it will return names of both pods (&quot;Running&quot; and &quot;Terminating&quot;) separated with space.</p>
<p>Now I've tried also</p>
<pre><code>kubectl get pods --selector=app=deployment-name --output=jsonpath='{.items[*].metadata.name}' --field-selector=status.phase=Running,status.phase!=Terminating
</code></pre>
<p>according to documentation:</p>
<p>but for some reason this will return exactly the same result, both pods running and terminating.</p>
<p>The quesiton is:</p>
<p>How to properly exclude TERMINATING pods from the result?</p>

<p>Known issue since 2018. Havent resolved yet.</p>
<p>Please refer to <a href="https://github.com/kubernetes/kubectl/issues/450" rel="nofollow noreferrer">Kubectl returns pods in various states when only those in Running state are selected via --field-selector, or -o jsonpath</a> for more details.</p>
<p>In short: There is NO normal, SHORT, adequate one-line command that would do what you want.</p>
<p><a href="https://github.com/kubernetes/kubectl/issues/450#issuecomment-448153487" rel="nofollow noreferrer">Reasons behind that</a>:</p>
<blockquote>
<p>This looks like not a bug. kubectl get pods output STATUS column not
shows status.phase. kubectl get pods displays table format of PodList
object and uses status.containerStatuses states to display STATUS
column data.</p>
<p>Pod phase valid states are pending, running, succeeded, failed and
unknown. From this document 'Running' state depends on
'restartPolicy'.</p>
<p>Jsonpath applies on 'PodList' object, not on kubectl output.
status.containerStatuses gives whether pod containers running or not.
Field selectors vary by Kubernetes resource types.
status.containerStatuses not supported in pods field selectors.</p>
</blockquote>
<p>Workarounds:</p>
<p><strong>1.</strong> <code>kubectl get pods | grep Running</code></p>
<p><strong>2.</strong> <code>kubectl get pods -o jsonpath='{.items[*].status.containerStatuses[*].state.running},{.items[*].metadata.name}' --field-selector=status.phase==Running | sed 's/ /\n/' | grep startedAt | awk -F',' '{print $2}'</code></p>
<p><strong>3.</strong> (<a href="https://github.com/kubernetes/kubectl/issues/450#issuecomment-706677565" rel="nofollow noreferrer">source</a>)  <code>kubectl get pods --field-selector=status.phase=Running --template {{range .items}}{{ if not .metadata.deletionTimestamp }}{{.metadata.name}}{{&quot;\n&quot;}}{{end}}{{end}}</code></p>
