<p>I've created a service which wraps an nfs, and I need to get the cluster IP for it so that I can set it to persistent volumne using it.</p>

<p>I know I can use the following to get this:</p>

<pre><code>$ kubectl get svc nfs-server
NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
nfs-server   ClusterIP   10.59.243.58   &lt;none&gt;        2049/TCP,20048/TCP,111/TCP   2m
</code></pre>

<p>What I want to know is, how do I extract that cluster IP in a bash script?  It's generated as part of a deployment process, so I can't just type it in to my persistent volume manifest.</p>

Andy
<p>You can parse the of <code>kubectl get svc</code> command something like below to get the inner details of the deployment.</p>

<pre><code>export CLUSTER_IP=$(kubectl get services/nfs-server -o go-template='{{(index.spec.clusterIP)}}');echo CLUSTER_IP=$CLUSTER_IP
</code></pre>

<p>Alternatively, you can try any combination of shell hacks involving <code>cut and awk</code>. One such example is;</p>

<pre><code>kubectl describe svc/nfs-server | grep IP: | awk '{print $2;}'
</code></pre>
