Kubernetes Series – 4.3: Kubernetes Services

In this article, we will talk about Kubernetes services. Let’s get started!

Kubernetes Services

A Kubernetes Service is an abstraction layer that defines a logical set of Pods and enables external traffic exposure, load balancing, and service discovery for those Pods.

After the documentation definition, let’s try to understand why we need services in Kubernetes to get a grasp of it.

Kubernetes Pods are ephemeral. This means they will be destroyed and/or restarted according to the needs of our deployment. Each Pod has its unique IP but once they are gone and new Pods rescheduled, a new IP is attached to the new Pod. This gives us the question, in what address can we communicate to those Pods. Service comes into play here.

Basically, services allow your application to receive traffic. This traffic can be inside the cluster or outside the cluster. Therefore we need to use different types of services, as such:

ClusterIP – Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.

NodePort – Exposes the Service on the same port of each selected Node in the cluster. Makes a Service accessible from outside the cluster using <NodeIP>:<NodePort>.

LoadBalancer – Expose a service to the internet. Assigns a fixed, external IP to the Service. Mostly used on clouds.

ExternalName – Maps the Service to the contents of the externalName field.

Services may attach to the Pods both by using labels and selectors or by manually adding an Endpoint object.

You can create a service both in an imperative and declarative way.

Let’s start our beloved nginx app and try to access it this time.

kubectl run nginx --image=nginx --port=80 --labels app=myapp

kubernetes services

If we try to access for instance <PodIP>:<PodPort> or localhost:<PodPort>, we won’t be able to access our nginx service.

dashboard

It is because the traffic is not yet allowed from outside the cluster. We need to create a nodeport service to access it. But there is a different challenge here. Since I am using a local setup to build my Kubernetes cluster ( I use kind ), I need to configure my own cluster setup accordingly. Such as container port, host port, listen address, etc. Eventually, if your node has a public IP then after you create your nodeport service you should be accessing your application on <NodeIp>:<NodePort>

Please, refer to your cluster setup documentation for more information. For more information, please refer to the cluster creation article.

So after my configuration, my kind cluster setup is now allowing me to access the node from localhost on port 30000.

Now I need to create a nodeport service such as below:

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

This service is for an application that has the “app: myapp” label. And ports exposed on “80” can be accessed through the node port “30000”.

Let’s check the services first by:

kubectl get services

or shorter version,

kubect get svc

kubectl get services

And if I try to access now “localhost:30000”:

ngnix kubernetes service

Now we are able to access our nginx app!

Kubernetes Series

Thanks for reading,
Ege Aksoz

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.