Day #20 - Service Discovery - Connecting services within the cluster
Service discovery in Kubernetes is a mechanism that allows applications to find and communicate with each other without needing to hard code IP addresses or endpoint configuration.
An application deployment in Kubernetes consists of a pod or set of pods. These pods are ephemeral, which means their IP addresses and ports are continually changing. In the Kubernetes world, this continual change makes service discovery a huge difficulty.
What is Service Discovery?
Service discovery is a mechanism by which services discover each other dynamically without the need for hard coding IP addresses or endpoint configuration.
In modern cloud-native infrastructure such as Kubernetes, applications are designed using microservices. The different components need to communicate within a microservices architecture for applications to function, but individual IP addresses and endpoints change dynamically.
As a result, there is a need for service discovery so services can automatically discover each other.
Key Components of Service Discovery
Kubernetes provides built-in support for service discovery through the use of Services and DNS.
When a service is created, Kubernetes assigns it a stable DNS name and a virtual IP address (ClusterIP). This allows workloads to connect to the service using either its DNS name or IP address, even if the underlying pods' IPs change.
How service discovery works in Kubernetes:
There are two different ways of Service discovery in Kubernetes:
? A.) for API-aware clients
An application deployment consists of set of pods. These pods are ephemeral, which means their IP addresses and ports are continually changing. In the Kubernetes, this change makes service discovery a huge difficulty.
Kubernetes’ endpoints API is one method it supports service discovery. Client applications can use the endpoints API to discover the IP addresses and ports of pods in an application.
领英推荐
The Kubernetes control plane ETCD serves as a service registry, where all endpoints are registered and kept up to date by Kubernetes.
? B.) Client having no API support:
Not all clients support APIs, Kubernetes supports service discovery in other methods also.
A Kubernetes service object is a persistent endpoint that points to a collection of pods depending on label selectors. It uses labels and selectors to route requests to the backend pods.
Because pods can come and leave dynamically in Kubernetes, a service object ensures that the endpoint or IP address that points to the list of operating pods never changes. If numerous pods are operating in the same application, the requests are also load-balanced across a group of pods.
Clients can utilize the Kubernetes service’s DNS name. Kubernetes’ internal DNS manages service mapping.
The usage of DNS for name-to-IP mapping is optional, and Kubernetes can do so with environment variables. The fundamental implementation of Kubernetes Service is handled by a kube-proxy instance running on each worker node.
Things to Consider
Service discovery in Kubernetes makes it easier to manage, scale, and maintain applications.
Is it Possible to Connect Services Within the Cluster?
Yes, Kubernetes makes it straightforward to connect services within the cluster. Here’s how: