42

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

I know for Minikube, it is 10.0.0.1/24.

For GKE, you can find out via

gcloud container clusters describe XXXXXXX --zone=XXXXXX |
  grep -e clusterIpv4Cidr -e servicesIpv4Cidr

But how do you find out on a generic Kubernetes cluster, particularly via kubectl?

richizy
  • 2,002
  • 3
  • 21
  • 26

9 Answers9

64

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.

Steven Dake
  • 850
  • 6
  • 4
  • 4
    Clean solution, and the only one that worked for me. – Anirudh May 18 '20 at 03:54
  • Thank you so much for the positive feedback! My first one on stack overflow :) If there is any way to improve the answer, please let me know. – Steven Dake May 23 '20 at 16:05
  • 1
    I lost access to "dump" and it was off hours, this helped me figure things out :) – Dejan Marjanović Jul 15 '20 at 23:52
  • 2
    You are the MVP @StevenDake. I tried this on 1.14.9 and it works there as well. – Kiran Aug 25 '20 at 14:49
  • 1
    Thnx. First answer didn't work for me, but this one did! Is there a similar way for finding cluster CIDR? (pod ip ranges) – Mohammad Yosefpor Oct 01 '20 at 19:58
  • Mohammad, I have looked around the various APIs for any form of pod IP input. I can't seem to find any. That doesn't mean one doesn't exist, but there are approximately 50 different APIs in K8s. If you find an API that accepts POD ips, I'll be happy to prototype. – Steven Dake Dec 31 '20 at 10:59
60

Get Services IPs range

kubectl cluster-info dump | grep -m 1 service-cluster-ip-range

You will see something like e.g. --service-cluster-ip-range=xxx.yy.0.0/13

Get Pods IPs range

kubectl cluster-info dump | grep -m 1 cluster-cidr

You will see something like e.g. --cluster-cidr=xxx.yy.0.0/11

Vasilis Vasilatos
  • 750
  • 10
  • 11
7

Did you check if the following command contains the info you need?

kubectl cluster-info dump

Oswin Noetzelmann
  • 9,166
  • 1
  • 33
  • 46
4

kubectl describe cm kubeadm-config -n kube-system |grep Subnet

2

With kubeadm

kubeadm config view | grep Subnet

Below output is self explanatory

podSubnet: 10.10.0.0/16

serviceSubnet: 10.96.0.0/12

2

If you did not specify --pod-network-cidr or --service-cidr, the defaults are used. Get defaults:

kubeadm config print init-defaults

Or get the configmap:

kubectl  --namespace kube-system get configmap kubeadm-config -o yaml
Moazzem Hossen
  • 2,276
  • 1
  • 19
  • 30
  • Did not work for me. I did not specify a `--pod-network-cidr` during init and it was not listed in `kubeadm config print init-defaults` or `kubectl --namespace kube-system get configmap kubeadm-config -o yaml` – ComradeJoecool Nov 24 '21 at 16:11
1

I can't leave a comment yet cause of rep so I'm just gonna answer with what I've found.

For minikube it looks like the CIDR can change, specifically for me it has. I found the CIDR under the .minikube directory at

.minikube\profiles\minikube

For windows you can find that at:

C:\Users\YourUserName\.minikube\profiles\minikube

For linux and mac I'd assume it would be under the ~/.minikube dir.

In the dir there should be a file called config.json which has a json object "KubernetesConfig": {} with the following field: ServiceCIDR. The value of that should be the CIDR for your services.

Unfortunately I have no other tips for kubectl to find the CIDR, maybe it's more of a provider thing and dependent on where/how you're running kubernetes.

saernz
  • 156
  • 1
  • 3
0

Similarly as proposed before, but with only the CIDR returned:

POD_CIDR=$(kubectl cluster-info dump | grep -m 1 -Po '(?<=--cluster-cidr=)[0-9.\/]+')
SERVICE_RANGE=$(kubectl cluster-info dump | grep -m 1 -Po '(?<=--service-cluster-ip-range=)[0-9.\/]+')
palexster
  • 9
  • 1
0

All of this answers/solution didn't work in my case.

And this is what worked for me and it's yet another way to see the All POD CIDRs used.

kubectl describe node | egrep 'PodCIDR|Name:'

Name: is so you identify the Node name too.

ashokrajar
  • 148
  • 1
  • 5