Kubernetes services:

  • Enable communication between various components within and outside of the application.
  • It helps us connect applications together with other applications or users.
  • It enable us to easily and effectively deploy a micro-service based application on the Kubernetes cluster

Let’s say A full-stack web application have different kinds of pods hosting a different kinds of applications:

- We may have a number of pods running a front-end web-server.

- Another set of pods running a back-end server.

- A set of pods running a Key-value store like Redis and another set of pods running a persistent database like MySQL.

Full-stack web application

The web front-end server needs to communicate to the back-end server and the back-end server needs to communicate to the database as well as the Redis etc.

So what is the right way to establish the connectivity between these services or tiers of our application?

As shown in above Figure, all pods have an IP address assigned to them, but these IPs are not static, as these pods can go down anytime and new pods are created all the time with any of the random IP from the pool, hence we can’t rely on for internal communication between these applications.

Also what if any of the front-end PODs need to connect to a back-end service or PODs? Which of the three would the request go to? and who makes that decision.

Here the Kubernetes Service (ClusterIP) comes into play. It helps us to group these pods together and provide a single interface to access the PODs in a group.

Kubernetes Service ClusterIP

For example, A service created for the back-end pod will help to group all the back-end PODs together and provide a single interface to for other PODs to access this service, similarly a service created for the Redis allow the back-end PODs to access the Redis through the service.

The requests from front-end to back-end and back-end to Redis are forwarded to any of the PODs under the service randomly.

Service enables us to scale or move any layer of the application as required without impacting communication between various services.

Each service gets an IP and name assigned to it inside the cluster and that is the name is used by other PODs to access the service.

  • service-definition.yaml
apiVersion: v1
kind: service
metadata:
name: back-end
spec:
type: CluserIP
ports:
- targetPort: 80
port: 80
selector:
- name: web-app
type: back-end

ClusterIP is the default service in Kubernetes, if you didn’t specify the service type, it will automatically assume the type to be clusterIP.

In service-definition file under ports, the targetPort is the port where the back-end is exposed, which is port 80 in our case and the only port is where the service is exposed which is port 80 as well.

To link the service to a set of Pods, we used selectors. Refer to your pod-definition file and copy the labels from it.

  • To create the service run the below command:
# kubectl create -f service-definition.yaml
service back-end created
  • To see the created service, run:
# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.32.10.1 <none> 80/TCP 3m

The service can be accessed by other PODs using the ClusterIP or the service name.

That’s it for Service ClusterIP, we will discuss other services in the next Article.

Thanks for reading :)

Refer Other useful Kubernetes Articles:

Kubernetes Services for Absolute Beginners — NodePort

labels-and-selectors-in-kubernetes

Kubernetes workflow for Absolute Beginners

--

--

Shubham Agarwal
Shubham Agarwal

Written by Shubham Agarwal

Site Reliability Engineer, have 5 years of experience in IT support and Operations

Responses (1)