<p>How do you find the cluster/service CIDR for a Kubernetes cluster, once it is already running?</p>

<p>I know for Minikube, it is 10.0.0.1/24.</p>

<p>For GKE, you can find out via</p>

<pre><code>gcloud container clusters describe XXXXXXX --zone=XXXXXX |
  grep -e clusterIpv4Cidr -e servicesIpv4Cidr
</code></pre>

<p>But how do you find out on a generic Kubernetes cluster, particularly via <code>kubectl</code>?</p>

<p>I spent hours searching for a generic way to do this. I gave up searching and wrote my own. As of Kubernetes 1.18, this method works across cloud providers, beyond just GKE.</p>

<pre><code>SVCRANGE=$(echo '{"apiVersion":"v1","kind":"Service","metadata":{"name":"tst"},"spec":{"clusterIP":"1.1.1.1","ports":[{"port":443}]}}' | kubectl apply -f - 2&gt;&amp;1 | sed 's/.*valid IPs is //')
echo $SVCRANGE
172.21.0.0/16
</code></pre>

<p>This one liner works by feeding an invalid service cluster IP into kubectl apply and parsing the error output, which provides the service CIDR information.</p>
