DEV Community 👩‍💻👨‍💻

Cover image for CKA & CKAD Series (Part 6): Services
Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

CKA & CKAD Series (Part 6): Services

Services are something that lets us contact our nodes or manage nodes , cluster etc.
For example, we have 3 type of services;
1) Nodeport
2) ClusterIP
3) Load balancer

1) Nodeport
basically , assume your laptop and a node in cluster has same IP . But the pods have different IP. Thus you can not access the containers within the pods through your laptop.

Image description

Here comes nodeport which is a port on nodes, helping you connect to your pods networks and get the container .

Image description

Checkout these carefully:
Image description
The web server on the container is connected to port 80 which is Target Port. It is also connected to port of the service. again the service has its own clusterIP address 10.106.11.1.2
The service is also then connected to NodePort which is the point for your laptop to get connected with the containers.

Lets try to create a service:
Image description

First 3 part is easy with Apiversion, kind and meta data But the most interesting part is all about spec:
Image description

For the spec, we have
Image description
We are creating a Nodeport and thus the type is NodePort. Again we need to share the ports.

So, why we gave these values , rights?
Check out this image

Image description
Surely you need to have the port portion here.

Now here is a thing. For a particular pod, we need to share the pod's labels . SO, that we can contact a particular pod.

Image description
So, we are now going to add selectors where we will share the labels of the pods.
This is the yaml file for the pod:
Image description

So, we will use the labels from here under the selectors.

Now, we have updated our selector like this:
Image description

Now, we just need to using the create command to create the service.
Image description

We can check the services from here:
Image description

SO, we handles an issue for a single pod. What about multiple pods??
In that case, we should have all the pods with same label and the selector for the NodePort also with the same label.
Image description

Also for multiple nodes, the service can be created like this and the pods can be accessed by this:
Image description

Now, lets do some hands on....

From the series, we know that we created a deployment which created 6 pods.
Image description

Now we would like to connect with those pods.Thus we will create a service . Basically a nodeport.
This was our deployment file used to create 6 replicas:
Image description

Now,and this is our service.yaml file to create a node port which has the same labels as the deployment file

apiVersion: v1 
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30004
  selector:
    app: myapp
Enter fullscreen mode Exit fullscreen mode

Lets now run it in our terminal to create the service:

kubectl create -f service.yaml
Enter fullscreen mode Exit fullscreen mode

Now check the services, using

kubectl get services
Enter fullscreen mode Exit fullscreen mode

Image description
Now, as our cluster has 1 node and within the node, we have 6 pods or replicas. we just need to know the node IP . also we have the NodePort 30004. So, in our browser we need to go and follow this format "node IP : NodePort"

Image description
First check out the pods and using one of the pods, get the Node IP, which will look like this:
Image description

Now paste it in the browser and use the NodePort too.
Image description

2)Cluster IP
To connect frontend, back end and database, it is best to use clusterIP so that, each portion can contact with each other.

Image description

Image description

To create a clusterIP, follow this format
Image description
Image description
"Target port" is where the "back end" is exposed and "port" is where the service is exposed.
To select the back end pods, we will those their labels under the selector:
Image description
Image description

*3) Load Balancer *
Assume that you have 4 nodes
Image description

It is basically part of a voting system. So, you will let voters vote using one port 30035 and get the result of the votes on port 31061
Image description
Look carefully, as we have 4 nodes, now we will have 4 links for votes and thus 4 links for getting results:
Image description
But a voter does not want to see these 8 links. He/she just wants to have a link to vote and get result.
Image description

Load Balancer will do all of these for you. Taking traffic from nodes to website etc.
Basically some cloud service providers like GCP, AWS,Azure etc do provide this service .

To create a load Balancer service, your yaml file should be like this:
Image description
Image description

So, that is it!

Top comments (0)

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.