what is the use of cluster IP in kubernetes

10/3/2019

Can someone help me understand about the IP address I see for cluster IP when I list services.

  1. what is cluster IP (not the service type, but the real IP)?
  2. how it is used?
  3. where does it come from?
  4. can I define the range for cluster IP (like we do for pod network)?
-- Sandeep kumar singh
kubernetes
service

2 Answers

10/3/2019

The cluster IP is the address where your service can be reached from inside the cluster. You won't be able to ping from the external network the cluster IP unless you do some kind of SSH tunneling. This IP is auto assigned by k8s and it might be possible to define a range (I'm not sure and I don't see why you need to do so).

-- Ko2r
Source: StackOverflow

10/4/2019

Good question to start learning something new (also for me):

Your concerns are related to kube-proxy by default in K8s cluster it's working in iptables mode.

Every node in a Kubernetes cluster runs a kube-proxy. Kube-proxy is responsible for implementing a form of virtual IP for Services.

In this mode, kube-proxy watches the Kubernetes control plane for the addition and removal of Service and Endpoint objects. For each Service, it installs iptables rules, which capture traffic to the Service’s clusterIP and port, and redirect that traffic to one of the Service’s backend sets. For each Endpoint object, it installs iptables rules which select a backend Pod.

  1. Node components kube-proxy:

    • kube-proxy is a network proxy that runs on each node in your cluster, implementing part of the Kubernetes Service concept.
    • kube-proxy maintains network rules on nodes. These network rules allow network communication to your Pods from network sessions inside or outside of your cluster.
    • kube-proxy uses the operating system packet filtering layer if there is one and it’s available. Otherwise, kube-proxy forwards the traffic itself.

As described here:

Due to these iptables rules, whenever a packet is destined for a service IP, it’s DNATed (DNAT=Destination Network Address Translation), meaning the destination IP is changed from service IP to one of the endpoints pod IP chosen at random by iptables. This makes sure the load is evenly distributed among the backend pods.

When this DNAT happens, this info is stored in conntrack — the Linux connection tracking table (stores 5-tuple translations iptables has done: protocol, srcIP, srcPort, dstIP, dstPort). This is so that when a reply comes back, it can un-DNAT, meaning change the source IP from the Pod IP to the Service IP. This way, the client is unaware of how the packet flow is handled behind the scenes.

There are also different modes, you can find more information here

  1. During cluster initialization you can use --service-cidr string parameter Default: "10.96.0.0/12"

    • ClusterIP: The IP address assigned to a Service

Kubernetes assigns a stable, reliable IP address to each newly-created Service (the ClusterIP) from the cluster's pool of available Service IP addresses. Kubernetes also assigns a hostname to the ClusterIP, by adding a DNS entry. The ClusterIP and hostname are unique within the cluster and do not change throughout the lifecycle of the Service. Kubernetes only releases the ClusterIP and hostname if the Service is deleted from the cluster's configuration. You can reach a healthy Pod running your application using either the ClusterIP or the hostname of the Service.

  • Pod IP: The IP address assigned to a given Pod.

    Kubernetes assigns an IP address (the Pod IP) to the virtual network interface in the Pod's network namespace from a range of addresses reserved for Pods on the node. This address range is a subset of the IP address range assigned to the cluster for Pods, which you can configure when you create a cluster.

Resources:

Hope this helped

-- Hanx
Source: StackOverflow