Cluster IP

ClusterIP can mean 2 things:

  • a type of service that is only accessible within a Kubernetes cluster,
  • the internal (virtual) IP of components within a Kubernetes cluster.

ClusterIP accesses the services through proxy. ClusterIP can access services only inside the cluster.

ClusterIP is the default Kubernetes service. This service is created inside a cluster and can only be accessed by other pods in that cluster. So basically we use this type of service when we want to expose a service to other pods within the same cluster.

This service is accessed using the Kubernetes proxy.

Default Kubernetes service type is clusterIP, When you create a headless service by setting clusterIP None, no load-balancing is done and no cluster IP is allocated for this service. Only DNS is automatically configured. When you run a DNS query for headless service, you will get the list of the Pods IPs and usually, client DNS chooses the first DNS record.

clusterIP service creates a single cluster IP and distributes the traffic between pods.

lets demonstration the above concept first we create a a pod with the help of below content

apiVersion: v1
kind: Pod
metadata:
  name: myfirstpod
  labels:
    podname: secondpodlb
spec:
  containers:
    - name: firstcontainer
      image: coolgourav147/nginx-custom

lets expose the pod using kubectl expose command

kubectl expose pod myfirstpod --port=8000 --target-port=80 --name myfirstservice

let's list the service using below command

C:\Users\Gaurav\Desktop\kubernetes-youtube>kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP    249d
myfirstservice   ClusterIP   10.104.138.205   <none>        8000/TCP   11s

now we can access the service using 10.104.138.205(IP) with in the cluster with 8000 port.

we can use below yaml file for cluster IP

apiVersion: v1
kind: Service
metadata:
  name: my-internal-service
spec:
  selector:
    app: secondpodlb
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP