<p>I want to get specific output for a command like getting the nodeports and loadbalancer of a service. How do I do that?</p>

Anvay
<p>The question is pretty lacking on what exactly wants to be retrieved from Kubernetes but I think I can provide a good baseline.</p>
<p>When you use Kubernetes, you are most probably using <code>kubectl</code> to interact with <code>kubeapi-server</code>.</p>
<p>Some of the commands you can use to retrieve the information from the cluster:</p>
<ul>
<li><code>$ kubectl get RESOURCE --namespace NAMESPACE RESOURCE_NAME</code></li>
<li><code>$ kubectl describe RESOURCE --namespace NAMESPACE RESOURCE_NAME</code></li>
</ul>
<hr />
<h3>Example:</h3>
<p>Let's assume that you have a <code>Service</code> of type <code>LoadBalancer</code> (I've redacted some output to be more readable):</p>
<ul>
<li><code>$ kubectl get service nginx -o yaml</code></li>
</ul>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
spec:
  clusterIP: 10.2.151.123
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30531
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - ip: A.B.C.D
</code></pre>
<p>Getting a <code>nodePort</code> from this output could be done like this:</p>
<ul>
<li><code>kubectl get svc nginx -o jsonpath='{.spec.ports[].nodePort}'</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>30531
</code></pre>
<p>Getting a <code>loadBalancer IP</code> from this output could be done like this:</p>
<ul>
<li><code>kubectl get svc nginx -o jsonpath=&quot;{.status.loadBalancer.ingress[0].ip}&quot;</code></li>
</ul>
<pre><code>A.B.C.D
</code></pre>
<p>You can also use <code>kubectl</code> with <code>custom-columns</code>:</p>
<ul>
<li><code>kubectl get service -o=custom-columns=NAME:metadata.name,IP:.spec.clusterIP</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>NAME         IP
kubernetes   10.2.0.1
nginx        10.2.151.123
</code></pre>
<hr />
<p>There are a lot of possible ways to retrieve data with <code>kubectl</code> which you can read more by following the:</p>
<ul>
<li><code>kubectl get --help</code>:</li>
</ul>
<blockquote>
<p>-o, --output='': Output format. One of:
json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...
See <a href="http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns" rel="nofollow noreferrer">custom columns</a>, <a href="http://golang.org/pkg/text/template/#pkg-overview" rel="nofollow noreferrer">golang template</a> and <a href="http://kubernetes.io/docs/user-guide/jsonpath" rel="nofollow noreferrer">jsonpath template</a>.</p>
</blockquote>
<ul>
<li><em><a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/#formatting-output" rel="nofollow noreferrer">Kubernetes.io: Docs: Reference: Kubectl: Cheatsheet: Formatting output</a></em></li>
</ul>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/reference/kubectl/overview/" rel="nofollow noreferrer">Kubernetes.io: Docs: Reference: Kubectl: Overview</a></em></li>
<li><em><a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">Github.com: Kubernetes client: Python</a></em> - if you would like to retrieve this information with Python</li>
<li><em><a href="https://stackoverflow.com/a/53669973/12257134">Stackoverflow.com: Answer: How to parse kubectl describe output and get the required field value</a></em></li>
</ul>
