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.
I know I can use the following to get this:
$ kubectl get svc nfs-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nfs-server ClusterIP 10.59.243.58 <none> 2049/TCP,20048/TCP,111/TCP 2m
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.
You can parse the of kubectl get svc
command something like below to get the inner details of the deployment.
export CLUSTER_IP=$(kubectl get services/nfs-server -o go-template='{{(index.spec.clusterIP)}}');echo CLUSTER_IP=$CLUSTER_IP
Alternatively, you can try any combination of shell hacks involving cut and awk
. One such example is;
kubectl describe svc/nfs-server | grep IP: | awk '{print $2;}'
The previous answer using go-template
output was failing. Using jsonpath
worked for me:
#!/bin/bash
cluster_ip=$(kubectl get svc nfs-server -ojsonpath='{.spec.clusterIP}')
echo $cluster_ip