Kubernetes Services simply explained
Tags: kubernetes, devops, docker, tutorial
Published: 2020-10-28 16:17:25

In [this tutorial](https://youtu.be/T4Z7visMM4E) 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](https://dev-to-uploads.s3.amazonaws.com/i/l5be9pu9vvdqyyc73fc4.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/i/cgqrdftldxm6td2ijwxe.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/qeo1x5jlfvdy7k5sw5xe.png)

####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](https://dev-to-uploads.s3.amazonaws.com/i/4eja8x98bxgd8h2kjcc7.png)

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](https://youtu.be/80Ew_fsV4rM)

#### 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:**

{% youtube T4Z7visMM4E %}

-----------------
► Get **30% off** - with this code: UDEMY_NANA_NOV2020: [Udemy course here](https://bit.ly/35qF26i)
► **Kubernetes 101**: [Compact and easy-to-read ebook bundle 🚀](https://www.techworld-with-nana.com/e-books) 
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:
- [Private FB group](https://www.facebook.com/groups/techworldwithnana)
- [Twitter](https://twitter.com/Njuchi_)
- [YouTube](https://www.youtube.com/channel/UCdngmbVKX1Tgre699-XLlUA?sub_confirmation=1)
- [Instagram](https://www.instagram.com/techworld_with_nana/)
- [LinkedIn](linkedin.com/in/nana-janashia)