What is the cluster IP in Kubernetes?
Asked Answered
C

5

43

I have created a cluster of three nodes: one master, two minions. How to check the cluster IP in Kubernetes? Is it the IP of the master node?

Carpic answered 29/10, 2015 at 6:49 Comment(2)
What do you mean by the "cluster IP"? What is your use case?Saccharase
Was trying to implement service type load-balancer from following link: kubernetes.io/v1.0/docs/user-guide/services.html ,there it asked for cluster-ip .Carpic
S
49

ClusterIP can mean 2 things: a type of service which is only accessible within a Kubernetes cluster, or the internal ("virtual") IP of components within a Kubernetes cluster. Assuming you're asking about finding the internal IP of a cluster, it can be accessed in 3 ways (using the simple-nginx example):

  1. Via command line kubectl utility:

    $ kubectl describe service my-nginx
    Name:           my-nginx
    Namespace:      default
    Labels:         run=my-nginx
    Selector:       run=my-nginx
    Type:           LoadBalancer
    IP:         10.123.253.27
    LoadBalancer Ingress:   104.197.129.240
    Port:           <unnamed>   80/TCP
    NodePort:       <unnamed>   30723/TCP
    Endpoints:      10.120.0.6:80
    Session Affinity:   None
    No events.
    
  2. Via the kubernetes API (here I've used kubectl proxy to route through localhost to my cluster):

    $ kubectl proxy &
    $ curl -G http://localhost:8001/api/v1/namespaces/default/services/my-nginx
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": <omitted>,
      "spec": {
        "ports": [
          {
            "protocol": "TCP",
            "port": 80,
            "targetPort": 80,
            "nodePort": 30723
          }
        ],
        "selector": {
          "run": "my-nginx"
        },
        "clusterIP": "10.123.253.27",
        "type": "LoadBalancer",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {
          "ingress": [
            {
              "ip": "104.197.129.240"
            }
          ]
        }
      }
    }
    
  3. Via the $<NAME>_SERVICE_HOST environment variable within a Kubernetes container (in this example my-nginx-yczg9 is the name of a pod in the cluster):

    $ kubectl exec my-nginx-yczg9 -- sh -c 'echo $MY_NGINX_SERVICE_HOST'
    10.123.253.27
    

More details on service IPs can be found in the Services in Kubernetes documentation, and the previously mentioned simple-nginx example is a good example of exposing a service outside your cluster with the LoadBalancer service type.

Saccharase answered 2/11, 2015 at 18:31 Comment(3)
What are the service endpoints than? We get them by kubectl get endpoints?Tayler
@IvanAracki kubectl get endpoints gives you the pod IP not the service endpoints,Adactylous
presumably the second link (now broken) should be updated to kubernetes.io/docs/concepts/services-networking/serviceCabriole
T
38

Run this

$ kubectl cluster-info

It shows result like this where you can see the Kubernetes master IP

Kubernetes Cluster IP

Triumphant answered 16/6, 2017 at 18:34 Comment(1)
This answer is one of my expectation. Thanks!Mussman
T
12

Cluster IP is a virtual IP that is allocated by the K8s to a service. It is K8s internal IP.

A Cluster IP makes it accessible from any of the Kubernetes cluster’s nodes. The use of virtual IP addresses for this purpose makes it possible to have several pods expose the same port on the same node – All of these pods will be accessible via a unique IP address.

This IP is stable and never changes in the service lifecycle(unless deleted explicitly).

2 different pods can communicate using this IP, though I recommend using cluster DNS service.

Tara answered 16/6, 2019 at 15:59 Comment(2)
Can we assign name to cluster IP without purchasing external dns name ? Could suggest you can we achieve this ?Aleda
I tried to ping & traceroute to cluster-ip from one of my pod in minikube, but neither of the commands are successful. I also tried doing "minikube ssh" and then ran ping & traceroute (after installing iputils-ping & traceroute packages), but no success either. Am I missing anything here?Pallaten
H
8

cluster IP only allocated to service, it is Kubernetes's internal ip。

Hirz answered 29/10, 2015 at 10:4 Comment(0)
E
0

The ClusterIP provides a load-balanced IP address. One or more pods that match a label selector can forward traffic to the IP address. The ClusterIP service must define one or more ports to listen on with target ports to forward TCP/UDP traffic to containers.

Epilate answered 10/11, 2022 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.