Kubernetes Services and Service Discovery -4
Service is a method for exposing a network application that is running as one or more Pods in your cluster.
Services provide a stable IP address and DNS name that can be used to access the application, regardless of the underlying pod IP addresses or which node the pods are running on.
Components of a Kubernetes services
Kubernetes services connect a set of pods to an abstracted service name and IP address. Services provide discovery and routing between pods. For example, services connect an application front-end to its backend, each of which running in separate deployments in a cluster. Services use labels and selectors to match pods with other applications. The core attributes of a Kubernetes service are:
- A label selector that locates pods
- The clusterIP IP address and assigned port number
- Port definitions
- Optional mapping of incoming ports to a targetPort
Types of Kubernetes services?
- ClusterIP. Exposes a service which is only accessible from within the cluster.
- NodePort. Exposes a service via a static port on each node’s IP.
- LoadBalancer. Exposes the service via the cloud provider’s load balancer.
- ExternalName. Maps a service to a predefined externalName field by returning a value for the CNAME record.
apiVersion: v1
kind: Service
metadata:
name: service-backend
spec:
ports:
- port: 4000
protocol: TCP
targetPort: 6565
selector:
run: deployment-backend
type: ClusterIP
Service Discovery
Service discovery is a built-in feature that allows different components of an application to discover and communicate with each other using the DNS name of the service.
When a service is created in Kubernetes, it is assigned a unique IP address and DNS name, which can be used by other components of the application to discover and communicate with the service instances.
Services allow Pods that work uniformly in clusters to connect across the network. Service discovery is the process of connecting pods and services.
There are two options for discovering services internal to Kubernetes:
- DNS discovery: Kubernetes provides a CoreDNS server and kube-DNS as an add-on resource, and every service registers with the DNS server so they can interact and communicate.
- Environment variables: Kubernetes can import environment variables from older services when it creates new pods, enabling pod communication.
There are two options for Kubernetes external service discovery:
- Load balancer discovery: Kubernetes and the cloud provider together serve as load balancer, redirecting pod traffic.
- NodePort discovery: Kubernetes uses special ports of node IP addresses to expose NodePort services.