Skip to main content

ClusterIP

ClusterIP exposes the service on a cluster-internal IP.

This is the default service type if nothing is specified in the definition for the type field.

The ClusterIP service assigns an IP address from the pool of IP addresses that the cluster has reserved.

ClusterIP definition

Below is a sample ClusterIP service definition.

apiVersion: v1
kind: Service
metadata:
name: nginx-cip-svc
spec:
selector:
component: frontend
ports:
- protocol: tcp
port: 8080
targetPort: 80
kubectl apply -f cip-svc.yaml

Manifest fields to know:

  • .spec.selector - the label on the Pods. Without this the Service cannot know where to forward the requests
  • .spec.ports
    • protocol - the IP protocol that the application listens on
    • port - port that the Service should listen on
    • targetPort - port on which the application is running on the Pods

So, the above Service manifest will listen on the port 8080 and forwards the requests to the port 80 on the backend nodes. And the backend nodes will be identified using the selector component: frontend which is the label on the Pods.

vagrant@vagrant:~/kubernetes-tutorial$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 78d nginx-cip-svc ClusterIP 10.105.57.78 <none> 8080/TCP 7s

Our ClusterIP is created.

Access via ClusterIP

The Service of type ClusterIP is only reachable from within the cluster. So, to access this we have to SSH to the master node and hit the IP.

minikube ssh
curl 10.105.57.78:8080

Minikube SSH cURL Nginx CIP

What is the advantage if we have to SSH into the master node to access application just like directly accessing the Pods? In the previous tutorial, we realized that it isn't feasible to SSH everytime and access the application and with ClusterIP we are doing the same again. We need to remember the Pod IPs when accessing directly, and those Pods are not permanent.

With ClusterIP, we get one fixed IP. New Pods may come up and existing Pods may go, but the ClusterIP remains same. It constantly watches for the Pods whose labels match with the selector and takes care of forwarding our requests to those Pods.

That's all for ClusterIP.