Kubernetes: Service vs. Endpoints vs. EndpointSlice

Service

When you create a resource of type Service using kubectl create command, Kubernetes will auto create Endpoints and EndpointSlice.

If you are creating the Service using YAML file, then you need to make sure to add spec.selector part. Or else, it will not auto create Endpoint and EndpointSlice.

Even though you forget to add spec.selector part to the YAML file, you can edit your Service using kubectl edit command and add the spec.selector after.

NOTE!

If you specify spec.ports[].name in the Service definition, make sure to specify subsets[].ports[].name in Endpoints definition, or specify ports[].name in EndpointSlice definition.

Endpoints

When you create a resource of type Endpoints using YAML file, Neither Service, nor EndpointSlice will be auto created.

Endpoints is an “old” way to define the network targets in Kubernetes.

To link an Endpoints to a Service, you have to make sure that both names are the same. Because of this, the constraint is 1:1.

apiVersion: v1
kind: Service
metadata:
  name: some-service
  ...
---
apiVersion: v1
kind: Endpoints
metadata:
  name: some-service
  ...

EndpointSlice

When you create a resource of type EndpointSlice using YAML file, Neither Service, nor Endpoint will be auto created.

EndpointSlice is the “new” way to define the network targets in Kubernetes.

To link between an EndpointSlice to a Service, use the metadata.labels.kubernetes.io/service-name entry. This will allow multiple EndpointSlices linked to a Service (1:M).

apiVersion: discovery.k8s.io/v1
kind: EndpointSlice
metadata:
  name: example-abc
  labels:
    kubernetes.io/service-name: some-service

References:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *