How do you find the cluster & service CIDR of a Kubernetes cluster?

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.

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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)