Kubernetes Services provide a stable networking interface for ephemeral pods, facilitating discovery, load balancing, and communication both within and outside the cluster.
Abstract
Kubernetes Services are a fundamental component that abstract the complexity of pod networking by offering a consistent and reliable endpoint for communication. Services expose a single IP address and DNS name for a group of pods, which is crucial since pods are dynamic and can be created or terminated frequently. This abstraction ensures that network requests are efficiently routed to available pods, even as the underlying pods change. Services use label selectors to dynamically discover and route traffic to the appropriate pods, supporting both internal and external access. They also provide load balancing to manage traffic across multiple pod replicas, ensuring high availability and fault tolerance. Kubernetes supports various service types, including ClusterIP, NodePort, LoadBalancer, and ExternalName, each serving different use cases for exposing services within and outside the cluster. Service discovery can be achieved through DNS or environment variables, with DNS being the recommended approach for its simplicity and reliability. Services are designed to only send traffic to healthy pods, offering features like session affinity and support for TCP and UDP protocols.
Opinions
Services are deemed essential for providing a stable networking endpoint in the face of pod ephemerality.
The use of Services for abstracting the networking complexities is highly recommended for reliable communication within Kubernetes clusters.
DNS-based service discovery is preferred over environment variables due to its ease of use and scalability.
Services are praised for their ability to load balance traffic and ensure that only healthy pods receive network requests, contributing to the overall resilience of the application.
The dynamic updating of Services as pods are added or removed is seen as a key feature for maintaining consistent network access without manual intervention.
Kubernetes — Services Overview
What are Kubernetes Services? — Introduction to K8s Services.
Kubernetes — Services
TL;DR
Kubernetes Service is used to expose an application deployed on a set of pods using a single endpoint. Services are introduced to provide reliable networking by bringing stable IP addresses and DNS names to ephemeral pods. Service enables network access to a set of Pods in Kubernetes.
Why Services in Kubernetes?
In Kubernetes, each Pod gets its own internal IP address, but Pods are ephemeral (not constant). Pods are frequently created and destroyed, causing their IP addresses to change constantly.
Non-functioning pods get replaced by new ones automatically. Meaning that when old Pod dies and new one gets started in its place it gets a new IP address. So it doesn’t make sense to use Pod IP addresses directly, because then you would have to adjust that every time the Pod gets recreated. It will create discoverability issues for the deployed application in pods, and making it difficult to identify which pods to connect.
With the Service component you have a solution of a stable or static IP address that stays even when the Pod is destroyed. Basically we set a Service in front of each Pod, which represents a stable IP address. So clients can call a single stable IP address instead of calling each Pod individually.
What is a Service in Kubernetes?
Kubernetes service is a Kubernetes object that provides stable networking for pods and provide a way to Kubernetes to configuring a proxy to forward traffic to a set of pods.
It enables communication between nodes, pods, and users of app, both internal and external, to the cluster. Service also provides load balancing when you have Pod replicas.
Services are a good abstraction for loose coupling for communication within the cluster, but also from external services like a browser request coming to the cluster.
Example:
How Kubernetes Services Works?
Kubernetes assigns IP address to service on creation, just like a node or pod. These addresses get assigned from a service cluster’s IP range. Service is also assigned a DNS name based on the service name, and an IP port.
Instead of static IP address-based assignments, Services use selectors (or labels) to define which pods uses which service. Services select Pods based on their labels. When a network request is made to the service, it selects all Pods in the cluster matching the service’s selector, chooses one of them, and forwards the network request to it.
Service automatically discovers a new pod with labels that match the selector. This process seamlessly adds new pods to the service, and at the same time, removes terminated pods from the cluster.
In Kubernetes, there are two ways to discover a service:
1. DNS Based
In this discovery method, DNS server is added to the cluster in order to watch the Kubernetes API create DNS record sets for each new service. When DNS is enabled throughout the cluster, all pods should be able to automatically perform name resolution of services. This is recommended method.
# DNS record for a Kubernetes service:
service.namespace.svc.cluster.local
# Pod would have a DNS record such as:10.32.0.125.namespace.pod.cluster.local
2. Environment variables
In this discovery method, a pod runs on a node, so the kubelet adds environment variables for each active service.
Kubernetes Service Key Points
Service gives stable networking endpoints for a set of Pods.
Service gets single IP, DNS and Port that never change.
Service enables how pods talk to each other inside the cluster.
Service enables how pods expose to the world/outside of cluster.
Service offers load balancing, naming and discovery isolation.
Service only send traffic to healthy Pods and hide unreliable Pods.
Service can do session affinity.
Service supports TCP and UDP.
Service is Load Balancer in Cloud Kubernetes services.
Summary
Kubernetes service provides a stable networking endpoint — a fixed IP, DNS, and port. Any pod can be added or removed without the fear that basic network information would change in any way.