<p>I want to get the name of the oldest pod as part of a script.  It seems like I should be able to run <code>kubectl get po --no-headers=true</code>, sort-by AGE, and then just pipe to <code>head -n1|awk '{print $1}'</code>, but I can't seem to get sort-by working.  I'm running kubectl 1.7.9. </p>

jars99
<p>The AGE times are in an irregular format (<code>23m</code>, <code>2d</code>) that’s hard to sort on, but you can ask <code>kubectl</code> to write out the time a pod started instead.  The times will come out in the very sortable ISO 8601 format.  This recipe to get the single oldest pod might work for you:</p>

<pre class="lang-sh prettyprint-override"><code>kubectl get pods \
    --no-headers \
    --output=custom-columns=START:.status.startTime,NAME:.metadata.name \
| sort \
| head -1 \
| awk '{print $2}'
</code></pre>

<p>The <code>kubectl</code> command asks to only print out the start time and name, in that order, for each pod.</p>

<p>Also consider <code>kubectl get pods -o json</code>, which will give you a very large very detailed JSON record.  If you have a preferred full-featured scripting language you can pick that apart there, or use a command-line tool like <code>jq</code> to try digesting it further.  Any field path can also be inserted into the <code>custom-columns</code> output spec.</p>
