Skip to content

Latest commit

 

History

History
58 lines (38 loc) · 3.46 KB

File metadata and controls

58 lines (38 loc) · 3.46 KB

Types of services in Kubernetes

There are several types of services in Kubernetes, each with its own use case and set of features. Some of the most commonly used service types include: ClusterIP: This is the default service type. It creates a virtual IP that is only reachable within the cluster. It is used for internal communication between pods.

  1. NodePort: This service type opens a specific port on each node in the cluster and maps it to a service's port. It allows external traffic to reach the pods by accessing a specific node IP and port.

  2. LoadBalancer: This service type creates an external load balancer in the underlying cloud provider, and maps it to the service's port. It allows external traffic to reach the pods by accessing the load balancer's IP address.

  3. ExternalName: This service type creates a CNAME record that maps the service to an external DNS name. It allows external traffic to reach the pods by accessing the external DNS name.

  4. ExternalIPs: This service type allows to expose a service externally using a set of arbitrary IP addresses.

  5. Headless: This service type does not provide a virtual IP, and is used for services that do not require load balancing.

  6. ExternalTrafficPolicy: This service type allows to control how external traffic is handled by the service. By default, external traffic is distributed to all pods, but it can be set to Local to ensure that external traffic is only sent to pods running on the same node.

  7. Session Affinity: This service type allows to configure how requests are routed to pods. There are two types of session affinity, ClientIP and None, which allow you to configure how requests are sent to pods in a service.

NodePort Service

A NodePort service in Kubernetes is a type of service that maps a specific port on each node in the cluster to a service's port. It allows external traffic to reach the pods associated with the service by accessing a specific node IP and port.

Here are some key points about NodePort service:

  • It is a type of service that opens a specific port on each node in the cluster and maps it to a service's port.

  • It allows external traffic to reach the pods by accessing a specific node IP and port.

  • The NodePort service allows you to access the service from outside of the cluster by using the IP address of any node in the cluster and the NodePort port.

  • The NodePort port is an ephemeral port, it is different from the port exposed by the pod, and it is allocated from a range of ports specified by the cluster administrator (30000-32767 by default)

  • The NodePort service is useful when you want to expose a service to external clients without creating a load balancer.

  • NodePort services can be useful for development and testing, but they are not recommended for production use because of the lack of high availability and load balancing features.

  • It is not suitable for exposed services that require high availability and traffic management, such as LoadBalancer or ExternalIPs service types are better options.

Here is an example of how to create a NodePort service in Kubernetes:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: NodePort

The service is created with a type of NodePort and specifies the targetPort for the pods. The service will be available at the nodeIP:nodePort. You can use the kubectl get services command to see the IP and ports of your services.