DEV Community

Cover image for Kubernetes Services simply explained
TechWorld with Nana
TechWorld with Nana

Posted on • Edited on

Kubernetes Services simply explained

In this tutorial I will give you a complete overview of Kubernetes Services.

What is a Service in Kubernetes? And why we need it? 🤔

In a Kubernetes cluster, each Pod gets its own internal IP address, but the Pods in K8s are ephemeral, meaning that they are destroyed frequently.
And when the Pod restarts or when old one dies and the 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.

Stable IP address

With the Service component however you have a solution of a stable or static IP address that stays even when the Pod is destroyed.

So basically in front of each Pod we set a Service, which represents a stable IP address.

Load Balancer

A Service also provides load balancing, because when you have Pod replicas, e.g. 3 mysql replicas, the Service will get each request targeted to that mysql application and then forward it to one of those Pods.
So clients can call a single stable IP address instead of calling each Pod individually. 👍

So, 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

Kubernetes Services

Selectors, Labels and Ports

Once a request gets handed over to the Service, then Service will know to forward this request to one of the Pods, that are registered as the Service Endpoints.

The questions you may be asking now:
1. How does Service know, which Pods to forward the request to?
2. How does Service know, which port to forward the request to?

The first one is defined by "selectors". A Service defines its member Pods or its endpoint Pods using selector attribute in the Service YAML configuration file. Selector is a key value pair, which must match the label in the Pod configuration file defined in the metadata section:
Selector and Label in Kubernetes

I explain this and the second question in much more detail in the below video.

Different Service Types ⭐️

There are several types of Services you can configure:

  • ClusterIP Service
  • NodePort Service
  • LoadBalancer Service

K8s Service types

ClusterIP

The most common one is ClusterIP, which is the default type of a Service. It's an internal Service, which means the Service is only reachable within the cluster.

NodePort

Exposes the Service on each Node's IP at a static port. So, external traffic has access to fixed port on each Worker Node!
That type of exposure is not very secure. A better alternative would be LoadBalancer type.

LoadBalancer

With LoadBalancer type, the Service becomes accessible through a cloud provider's load balancer. Each cloud provider (AWS, Azure, Google Cloud, Linode etc) has its own native load balancer implementation.

LoadBalancer Services

You can also use Ingress to make your Service accessible from outside. It will act as the entry point for your cluster, but Ingress is not a Service type. K8s Ingress explained here

Multi-Port and Headless Service

You can configure multiple ports on a Service. This would be a Multi-Port Service.

A Headless Service is used, when a Pod or a client wants to communicate directly with another specific Pod.

They are not configured via the type field directly.


I will explain the differences between them in more detail and when to use which, here:


► Get 30% off - with this code: UDEMY_NANA_NOV2020: Udemy course here
Kubernetes 101: Compact and easy-to-read ebook bundle 🚀
It's a handy way to quickly look something up or refresh your knowledge at work and use it as your cheatsheet 😎

Like, share and follow me 😍 for more content:

Top comments (2)

Collapse
 
ryands17 profile image
Ryan Dsouza

Awesome post! I also watched your video on Deployments and StatefulSets that explains the difference. Could you share any resource that demos StatefulSet as I wanted to try creating a database with that instead of a Deployment.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Thought out and detailed good post.