The Endpoints Object: The Real Connection Behind Services
We've established that a Service provides a stable IP (ClusterIP
) and uses a selector to identify target Pods. But how does traffic actually get from the Service IP to one of those potentially many, dynamically changing Pod IPs? This is where the Endpoints
object (API type v1.Endpoints
) comes into play.
For every Service in Kubernetes (unless it's type: ExternalName
or explicitly configured otherwise), the Kubernetes control plane automatically creates and manages a corresponding Endpoints
object. This Endpoints
object has the same name and resides in the same namespace as its parent Service.
The Core Function:
The primary role of the Endpoints
object is to maintain a dynamic list of IP address and port combinations that represent the actual, ready Pods matching the Service's selector at that moment in time.
Think of it like this:
Service: Defines the stable entry point (
ClusterIP
,Port
) and the desired group of backends (Selector
).Endpoints Controller: A part of the Kubernetes control plane constantly watches:
Services and their selectors.
Pods and their labels.
Pods and their readiness status (from Readiness Probes).
Endpoints
Object: The result of the Endpoints Controller's work. It lists theIP:TargetPort
pairs for only those Pods that match the Service's selector AND are currently Ready.
How kube-proxy
Uses Endpoints:
Components like kube-proxy
(which run on each Node) watch these Endpoints
objects. When traffic arrives destined for a Service's ClusterIP:Port
, kube-proxy
uses the list of IPs and ports from the corresponding Endpoints
object to select one actual backend Pod IP and forward the traffic there, performing the load balancing. (Note: The exact mechanism depends on the kube-proxy
mode - iptables, IPVS, etc. - but the reliance on the Endpoints
data is fundamental).
The v1.Endpoints
Structure:
Let's look at the key fields within an Endpoints
object when retrieved via the API:
metadata
: Contains thename
andnamespace
, which match the associated Service.subsets
([]v1.EndpointSubset): This is the most important field. It's a list of subsets, where each subset typically corresponds to a unique combination of ports defined in the parent Service. Often, there's just one subset if the Service defines only one port mapping. EachEndpointSubset
contains:addresses
([]v1.EndpointAddress): A list of IP addresses for Pods that are ready to serve traffic for the ports defined in this subset. EachEndpointAddress
has:ip
(string): The actualpodIP
of a ready backend Pod.nodeName
(string, optional): The name of the Node where this Pod is running.targetRef
(*v1.ObjectReference, optional): A reference back to the specific Pod object this IP belongs to.
notReadyAddresses
([]v1.EndpointAddress): A list of IP addresses for Pods that match the Service selector but are not ready (e.g., failing readiness probes, terminating). These IPs are generally not used for routing normal Service traffic but can be useful for debugging or specific scenarios. The structure is the same asaddresses
.ports
([]v1.EndpointPort): A list describing the ports exposed by the Pods in this subset. EachEndpointPort
has:port
(int32): ThetargetPort
(the port number on the Pod) that traffic should be sent to.protocol
(string):TCP
,UDP
,SCTP
.name
(string, optional): The name of the port, matching theServicePort
name if defined.
Inspecting Endpoints with client-go
:
You can retrieve and inspect Endpoints
objects just like any other resource:
Why Inspect Endpoints?
While Endpoints
are usually managed automatically, inspecting them programmatically is incredibly useful for:
Debugging Service Issues: If a Service isn't working, checking its corresponding
Endpoints
object is often the first step. Are there anyaddresses
listed? Are the IPs and ports correct? Are expected Pods perhaps in thenotReadyAddresses
list instead?Building Custom Logic: Sometimes you might need direct access to the list of ready backend IPs for specific load balancing strategies or service discovery mechanisms outside of
kube-proxy
.Understanding Service Behavior: Seeing the dynamic updates to the
Endpoints
object as Pods become ready or unready solidifies the understanding of how Services connect to Pods.
The automatic management of Endpoints
based on Service selectors and Pod readiness is a cornerstone of Kubernetes networking, providing the dynamic link between stable service abstractions and ephemeral Pod instances. Next, we'll briefly touch upon how kube-proxy
conceptually uses this information.
Last updated
Was this helpful?