Types of services in Kubernetes

Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2024-01-30 10:16:56 Viewed : 162



In Kubernetes, services are used to expose applications and services to other components within the cluster or external systems. There are several types of services in Kubernetes, each serving a different purpose. Here are the common service types:

  1. ClusterIP:

    • Exposes the service on a cluster-internal IP address.
    • The service is accessible only within the cluster.
    yaml
    kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: ClusterIP
  2. NodePort:

    • Exposes the service on a static port on each nodes IP.
    • Makes the service accessible from outside the cluster using the nodes IP and the static port.
    yaml
    kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: NodePort
  3. LoadBalancer:

    • Exposes the service externally using a cloud providers load balancer.
    • Automatically assigns an external IP to the service.
    yaml
    kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer
  4. ExternalName:

    • Maps the service to the contents of the externalName field (e.g., a DNS name).
    • It does not provide load balancing or other features typical of other service types.
    yaml
    kind: Service apiVersion: v1 metadata: name: my-service spec: type: ExternalName externalName: my.external.service
  5. Headless:

    • Creates a service without a cluster-internal IP.
    • Useful for services that do not require load balancing, and you want to interact directly with the individual pods.
    yaml
    kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: my-app clusterIP: None

These service types provide flexibility in exposing and accessing applications within a Kubernetes cluster. The appropriate type depends on the specific requirements and use case of your application.

Search
Related Articles

Leave a Comment: