How to parse kubectl describe output and get the required field value

12/7/2018

I am trying to fetch Nodeport from a specific pod using kubectl describe command. I came to know from this question that -o option doesn't work with describe, hence I am trying with following way but I am not getting required value, can someone correct me.

kubectl -n core describe svc/pg-debug
Name:                     pg-debug
Namespace:                core
Labels:                   <none>
Annotations:              <none>
Selector:                 app=postgresql-default
Type:                     NodePort
IP:                       172.17.17.19
Port:                     <unset>  5432/TCP
TargetPort:               5432/TCP
NodePort:                 <unset>  24918/TCP
Endpoints:                172.16.90.10:5432
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Following is the command that i am trying to get the value "24918"

kubectl -n core describe svc/pg-debug | grep NodePort |awk -F:  '/nodePort/{gsub(/ /,"",$2)}'
-- Auto-learner
kubectl
kubernetes

1 Answer

12/7/2018

You can use following command to get nodePort from service

kubectl get svc pg-debug -n core -o jsonpath='{.spec.ports[].nodePort}'

Refs:

-- Prafull Ladha
Source: StackOverflow