<p>I am getting the IP address assigned to the pod using <code>kubectl get pods -o custom-columns=&quot;POD_IP&quot;:.status.podIPs</code> command.</p>
<p>And based on same approach, I am using <code>kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers.ports.containerPort</code> command to get the port number but it is coming as blank.</p>
<pre><code>cloudshell:~$ kubectl get pods -o custom-columns=&quot;POD_IP&quot;:.status.podIPs
POD_IP
[map[ip:10.32.0.194]]

cloudshell:~$ kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers.ports.containerPort
POD_PORT
&lt;none&gt;

cloudshell:~$ kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers
POD_PORT
[map[image:nginx:1.10.1 imagePullPolicy:IfNotPresent name:servic1 ports:[map[containerPort:8080 protocol:TCP]] resources:map[limits:map[cpu:500m ephemeral-storage:1Gi memory:2Gi] requests:map[cpu:500m ephemeral-storage:1Gi memory:2Gi]] securityContext:map[capabilities:map[drop:[NET_RAW]]] terminationMessagePath:/dev/termination-log terminationMessagePolicy:File volumeMounts:[map[mountPath:/var/run/secrets/kubernetes.io/serviceaccount name:kube-api-access-mgk8k readOnly:true]]]]

cloudshell:~$ 
</code></pre>
<p>I have tried to use <code>kubectl get pods -o custom-columns=&quot;Port Number of Pod&quot;:.spec.containers</code> command and I can see that my mapping (<code>.spec.containers.ports.containerPort</code>) is correct but somehow it is still not working.</p>
<p>I am totally sure that <code>.spec.containers.ports.containerPort</code> mapping correct, and same command format is giving IP address, so not able to catch what is wrong.</p>
<p>Is anyone able to catch what is wrong here?</p>

<p>Try:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl get pods \
--output=custom-columns=\
&quot;POD_PORT&quot;:.spec.containers[*].ports[*].containerPort
</code></pre>
<p>You can include <code>.metadata.name</code> too to aid clarity:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl get pods \
--output=custom-columns=\
&quot;NAME&quot;:.metadata.name,\
&quot;POD_PORT&quot;:.spec.containers[*].ports[*].containerPort
</code></pre>
<p>It's not clearly (!?) <a href="https://kubernetes.io/docs/reference/kubectl/#custom-columns" rel="noreferrer">documented</a> but I suspect the format is kubectl's <a href="https://kubernetes.io/docs/reference/kubectl/jsonpath/" rel="noreferrer">JSONPath</a> and there (appears to be) a subtle distinction between e.g. <code>.spec.containers[]</code> and <code>.spec.containers[*]</code> where the former stops when the property is not found and the latter includes everything.</p>
<p>Because <code>.spec</code> will always include one or more <code>.containers</code>, but each <code>container</code> may not have <code>.ports</code>, you can also:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl get pods \
--output=custom-columns=\
&quot;POD_PORT&quot;:.spec.containers[].ports[*].containerPort
</code></pre>
<p>Which <code>containers[]</code> but <code>ports[*]</code> to the same effect.</p>
<p><strong>NOTE</strong> as explained in <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#container-v1-core" rel="noreferrer">Container v1 core</a> see &quot;ports&quot;, ports that are exposed by the container need not be specified through <code>ports</code> i.e. this command will return documented ports but this list may exclude ports that are exposed by the containers (and not documented).</p>
