Kubernetes Services ☸☸ ️️: Part-1

What are Services in Kubernetes ?

Introduction 🚩🚩

Hi fellow Readers 👋 :))
The Subject "Kubernetes Services" is a big and lengthy one. So I have decided to write two parts for it. This will not only help me to make the articles short and crisp but also I feel help you peeps to understand the concept better and in greater depth.

In this part, we will try to understand the following things :

  • What is a Kubernetes Service ?
  • Types of Services in Kubernetes .
  • How does a NodePort Service work ?
  • How to make a NodePort Service using a Yaml file ?
So without any further delay, let us get started with our article :))

Definition 🤓🤓

Kubernetes Services

Services in Kubernetes is an abst ract way to expose an application running on a set of Pods as a network service.

With Kubernetes you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them.

Ok Ok enough of technical definition. 😏
Let us understand "What are Services ??" using very simple terms. Lets GO !!🏇

Lets Dig In !!⛏️⛏️

Imagine there are three different group of PODs present on a Kubernetes node.

  • Frontend PODs [Pods which are running the frontend part of the web application on them which will be used by the end user]
  • Backend PODs [Pods which are running the backend part of the web application on them which will be used by the end user]
  • Database PODs [Pods which run the database application insta nces, which are responsible for storing the data of the web applications]

All the above nodes are running properly and the applications running on them are also in good state and shape.

But there are some questions which might arise 👀👀. Let us discuss them :

  1. How does the end user access the frontend application running on frontend PODs in their respective computer ??
  2. How does the frontend PODs communicate with the backend PODs to make the whole application actually run ??
  3. How does the frontend PODs communicate with the database PODs to get the data required for them ??
Working of Kubernetes Services

As the above diagram depicts, all the above questions / problems which we discussed. Can be easily solved by using "Kubernetes Service s".

Kubernetes Services in very simple terms, are Kubernetes objects which help us to

  1. Expose the applications running on the PODs to the end user.
  2. Help the PODs to communicate with other PODs that are present on the Kubernetes cluster i.e helps in inter-pod connectivity.
  3. Helps to collect the user requests and equally divide those requests among the applications running on the PODs i.e helps to load balance the user requests for the applications running on the PODs.
Hurray !!🥳🥳 Now we understand what are Services and what they can do.
Lets now discuss about the types of Services :))

Types of Kubernetes Services 🌐🌐

Types of Kubernetes Services
< ul>
  • NodePort : Exposes a service via a static port on each node's IP. In simple terms, this service helps to expose the services/applications running on the PODs to the end user.
  • ClusterIP : Exposes a service which is only accessible from within the cluster. In simple terms, this service help the PODs to access the services running on the another PODs. (Inter-PODs communication)
  • LoadBalancer : Exposes the service via the cloud provider's load balancer. In simple terms, this service helps to load balance the user requests for the applications running on the PODs via the cloud provider's load balancer.
  • ExternalName : Maps a service to a predefined externalName field by returning a value for the CNAME record. In simple terms, these services work as other regular services only, but when you want to access to that service name, instead of returning cluster-ip of this service, it retu rns CNAME record with value that mentioned in externalName: parameter of service.
  • How does a NodePort Service work ?? 🤔🤔

    NodePort Service

    Let us try to understand the scenario shown in the above diagram.

    1. There is a worker node which contains a POD in it.
    2. The POD runs a web application on it.
    3. There is a NodePort service which we have created and that service, maps the port on the node to a port present on the node.

    Now whenever an end user wants to access the web application running on the POD, following things happen behind the scenes :

    1. The user just send request on the Node port number which is mapped to the POD (Here port : 30008)
    2. N ext the NodePort service takes that user request and maps it to its own port i.e service port (Here port : 80)
    3. Next from the service port, the request is being mapped to the port of the web application POD (Here TargetPort : 80)
    In these simple steps, By using the NodePort service. Users are able to access the applications which are running on the PODs from their own devices.

    How can we create a NodePort service ?? 🤯🤯

    We can create a NodePort service using YAML files. To create a Kubernetes NodePort service with YAML, you first create an empty file, assign it the necessary access permissions, and then define the necessary key-value pairs.

    Below is an example of a NodePort service definition file (nodePortService.yaml)

    apiVersion: v1 
    kind: Service
    metadata:
    name: my-service
    spec:
    type: NodePort
    selector:
    app : echo-hostname
    ports:
    - nodePort: 30008
    port: 80
    targetPort: 80

    There are many aspects and components in this file. Lets break down each one of them 😀

    • Let's start with the apiVersion (key-value pair). This is used to clarify what API server and version you will be running in the background when creating the NodePort service.
    • Next is kind which signifies the kind of definition file this is. In our case, it is a "Service".
    • Next is metadata, which is a dictionary including the item name. The metadata stores values that are assigned to the NodePort Service which is being created.
    • Finally there is spec which is actually an array/list. Following are the values which are present in it and what they mean.
      type : The value of this key, defines what is the type of service is being created. In our case, it is a "NodePort".
      selector : It basically has the labels of the PODs thus helping the service to identify the PODs it has to work with.
      ports : It is the section which contains the ports the service has to work with.
      nodePort has the value of the port present on the Node where the user request will come. It's value can be in the following range 30000–32767.
      port has the value of the port present on the service object where the user request will be mapped.
      targetPort has the value of the port present on the POD where the endpoint of the application is exposed and where the user request will be finally mapped to in the end.

    We are done with the NodePort service definition file. Now we can save and exit the file.

    Use this command to create the NodePort service based on the above YAML file :

    kubectl create -f nodePortService.yaml

    Use this command to view all the services running on the cluster :

    kubectl get services

    What next ? 👀 👀

    Thanks a lot for reaching till here! It is the end of this article.
    I will soon come back with the next part of this blog. Where I will try to explain other types of services in depth.
    Much more to go, it will be a fun journey where we will learn a lot of cool stuff together.
    Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))

    In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles

    Twitter — https://twitter.com/ChindaVibhor

    LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/

    Previous articles written by Me :

    References

    Service

    I will still keep on coming with new articles covering a bunch of topics I am exploring.

    That's All folks !! Doodles :))


    Kubernetes Services ☸☸ ️️: Part-1 was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.

    Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

    Comments

    Did you find the article or blog useful? Please share this among your dev friends or network.

    An android app or website on your mind?

    We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

    Contact Us

    Trending DevOps Articles

    Working with System.Random and threads safely in .NET Core and .NET Framework

    Popular DevOps Categories

    Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
    Show more