Sobes.tech
Junior — Middle

Can you explain what ClusterIP is in Kubernetes and how it is used for service interaction?

sobes.tech AI

Answer from AI

ClusterIP is a type of service in Kubernetes that provides an internal IP address for accessing a set of pods within the cluster. This IP is only accessible inside the cluster and is not visible from outside.

When a service of type ClusterIP is created, Kubernetes automatically assigns it a virtual IP address. All requests to this IP are forwarded to the pods that match the service's selector. Thus, other services and pods within the cluster can access the service via this IP or the service's DNS name.

Example usage:

  • A database service with type ClusterIP is only accessible to applications inside the cluster.
  • A web application accesses the database by the service name, without worrying about the specific pod IPs.

This simplifies interaction between application components and provides load balancing and fault tolerance.

Example of a ClusterIP service manifest:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP