0
0
KubernetesConceptBeginner · 3 min read

ClusterIP Service in Kubernetes: What It Is and How It Works

A ClusterIP service in Kubernetes exposes a set of pods inside the cluster on a stable internal IP address. It allows communication between pods within the cluster but is not accessible from outside the cluster.
⚙️

How It Works

Imagine your Kubernetes cluster as a small city. Each pod is like a house where people live. A ClusterIP service acts like a local post office that gives a single address to a group of houses. Instead of remembering each house's address, you just send mail to the post office address, and it delivers it to the right house.

Technically, Kubernetes assigns a virtual IP address to the ClusterIP service. This IP is only reachable inside the cluster. When a pod sends a request to this IP, Kubernetes routes the request to one of the pods behind the service, balancing the load automatically.

This setup helps pods talk to each other easily without worrying about pod IPs changing when pods restart or scale.

💻

Example

This example shows a simple ClusterIP service exposing a set of pods running an Nginx web server inside the cluster.

yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.23
    ports:
    - containerPort: 80
Output
service/nginx-service created pod/nginx-pod created
🎯

When to Use

Use a ClusterIP service when you want to enable communication between pods inside the cluster only. It is perfect for internal microservices that do not need to be accessed from outside.

For example, a backend database service can be exposed with ClusterIP so that only application pods inside the cluster can connect to it securely.

It is also useful for service discovery within the cluster, letting pods find each other by a stable IP or DNS name.

Key Points

  • ClusterIP exposes services only inside the cluster.
  • It provides a stable internal IP address for pods to communicate.
  • Pods outside the cluster cannot access a ClusterIP service.
  • It is the default service type in Kubernetes.
  • Useful for internal microservices and databases.

Key Takeaways

ClusterIP service exposes pods on a stable internal IP accessible only inside the cluster.
It enables easy and reliable communication between pods without exposing them externally.
ClusterIP is the default and most common service type for internal microservices.
Use ClusterIP for services like databases or internal APIs that do not need outside access.
Pods use the ClusterIP address or DNS name to find and connect to the service.