Kubernetes(k8s) - What are Services?

Kubernetes Services are resources that map network traffic to the Pods in your cluster. You need to create a Service each time you expose a set of Pods over the network, whether within your cluster or externally.


Kubernetes Services are API objects that enable network exposure for one or more cluster Pods. Services are integral to the Kubernetes networking model and provide important abstractions of lower-level components, which could behave differently between different clouds.


kubernetes-k8s-what-is-service

Why Services are needed in Kubernetes?

Services are necessary because of the distributed architecture of Kubernetes clusters. Apps are routinely deployed as Pods that could have thousands of replicas, spanning hundreds of physical compute Nodes. When a user interacts with your app, their request needs to be routed to any one of the available replicas, regardless of where it’s placed.


Services sit in front of your Pods to achieve this behavior. All network traffic flows into the Service before being redirected to one of the available Pods. Your other apps can then communicate with the service’s IP address or DNS name to reliably access the Pods you’ve exposed.


DNS for Services is enabled automatically through the Kubernetes service discovery system. Each Service is assigned a DNS A or AAAA record in the format ..svc.cluster-domain e.g. myservice.default.svc.cluster.local. This enables reliable in-cluster networking without having to lookup service IP addresses.



How does a Kubernetes Service work?

Services make use of the labels that are assigned to the pod or deployment to select the correct pod. A service object can even be configured to target a deployment, so all the pods created by a deployment will be exposed by the service object. A single service can also be used to target a group of different pods. You just need to ensure that the correct labels are being selected.


kubernetes-k8s-what-is-service

Kubernetes Service types?

ClusterIP Services - ClusterIP Services assign an IP address that can be used to reach the Service from within your cluster. This type doesn’t expose the Service externally.


NodePort Services - NodePort Services are exposed externally through a specified static port binding on each of your Nodes. Hence, you can access the Service by connecting to the port on any of your cluster’s Nodes.


LoadBalancer Services - A LoadBalancer is also a type of service that exposes the pod to external traffic. As the name implies, a LoadBalancer service distributes the traffic between the pods that are targeted by the service.


ExternalName Services - ExternalName is unique as it does not use labels and selectors like the other types of services. Instead, the service maps to a DNS name using a CNAME record.



Creating Kubernetes Service

Make sure you have running Pod/ReplicaSet/Deployment running before planing for Service. You can refer Kubernetes Deployments for details. Let’s take a look at an example YAML definition for a Kubernetes Service with Pods having Label app=nginx and Port No 80:

              
                ---
                           apiVersion: v1
                          kind: Service
                      metadata:
                                    name: nginx-service
                        labels:
                               app: nginx
                  spec:
                               type: NodePort
                          selector:
                                app: nginx
                       ports:
                           - port: 80
                                    nodePort: 30000
              
              

Let’s take a look at following fields:


selector — Used for selecting a single pod or a group of pods to route traffic to.


port — The port where their service is listening for external traffic.


type — Defining the type of service.


apiVersion — Specifies the Kubernetes API version, such as "v1" for Services.


kind — Specifies the type of Kubernetes resource, in this case, "Service."


spec — Defines the desired state of the Service, including the number of type, selector and ports.


metadata — Provides metadata for the Service, including the name, labels.



kubectl - deploy and interact with Services

kubectl, (kube-control, or as some people call it, kube-cuddle) is the Kubernetes command-line tool. It allows us to run commands against Kubernetes clusters.


With kubectl, we can create a Service using our YAML definition file like so:


kubectl apply -f myyamlfile.yaml

We can list all of our Services like so:


kubectl get svc



Summary and Conclusions


Managing services in Kubernetes is a crucial part of deploying and running applications in a cluster. By default, the pod is not exposed to any traffic. It has to be exposed by using a Kubernetes service. When you want to enable inter-pod connectivity, you would want to expose both pods using a ClusterIP service.


When it comes to exposing the pod to external traffic, you can make use of the NodePort or Loadbalancer services. The NodePort service exposes the traffic on a port between 30000 and 32767. For LoadBalancer services, this number is automatically assigned to the service by Kubernetes.



Author


sagar-mehta

Sagar Mehta is Atgen Software Solutions Founder and a recognised expert in the field of Intelligent Automation, including Robotic Process Automation, Workload Automation, DevOps, SRE and Advanced Analytics. Sagar advocates a pragmatic approach to Automation, encouraging a policy of using ‘the best tool for the job’.


Prior to co-founding Atgen Software Solutions, Sagar worked in Senior Automation roles, architecting and delivering robust, scalable solutions for many of the world’s biggest banks and working with leading Automation vendors. He developed his first automated solution in 2006 and has continued to deliver robust, scalable and sophisticated Automation ever since.


Sagar is a regular guest speaker and panellist at Automation seminars, conferences and user group events.

Contact

Have a similar problem to solve, let's work together.

Our Address

#107, Tower B, Escon Arena, Zirakpur, Punjab, India - 140603

Email Us

info@atgensoft.com

Call Us

+91-8806666141