CKA & CKAD Series (Part 6): Services
Tags: kubernetes, services
Published: 2022-08-10 18:02:00

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyp9hdvojn3ogp4g1xl2.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fm83uorame7ztehcjoa1.png)

Checkout these carefully:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bu41rmbaxfa38xg73w74.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xwo3vmzestmsmllg0je.png)

First 3 part is easy with Apiversion, kind and meta data But the most interesting part is all about spec:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tinyk4avrbvgtbbpct3.png)

For the spec, we have 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41zneuiblz1g3es9gfua.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mh9o1f0ul2078js5iab.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xglt2oxntos3u6qpui08.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2w1dyw1h8djx7sceer8w.png)

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

Now, we have updated our selector like this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80fd6970fasex1frfovm.png)
 
 Now, we just need to using the create command to create the service.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a6kuc1czps61793zzrg.png)


We can check the services from here:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ip5cmrh21i6iacx2ht6s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybr9r7up254wrv284l9m.png)

Also for multiple nodes, the service can be created like this and the pods can be accessed by this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht8vg8r06v5mxsrs79m1.png)

Now, lets do some hands on....

From the series, we know that we created a deployment which created 6 pods.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzs4k4simsy3nyuvgb9w.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkeszut11vod4olqk5uq.png)

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
```
 Lets now run it in our terminal to create the service:

```
kubectl create -f service.yaml
```
Now check the services, using

```
kubectl get services
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue3pn6r7jjd1mfrfewi4.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/baz2z4qkbvgupj2o7n5a.png)
First check out the pods and using one of the pods, get the Node IP, which will look like this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ieux4jzd8qsmgt1m7qv9.png)

Now paste it in the browser and use the NodePort too.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojleeddi99gkqiwgeibs.png)



**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kuyekjbwxiepuhqz36s.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skita8xg1zfyd4k46qsc.png)
 
To create a clusterIP, follow this format
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85xoet01sr0ju1ngrqvs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6sgyprxs7irvl7todob.png)
"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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isau6dvu5disa0rafs2i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7naoke6wlserj3ooz3x.png)


**3) Load Balancer ** 
 Assume that you have 4 nodes
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf3qrfq1fc5kswsapdae.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj8ipem2w26wky985bcz.png)
Look carefully, as we have 4 nodes, now we will have 4 links for votes and thus 4 links for getting results:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z01qoezg1619qsg9mmwr.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8ol1raeptewnfjtcf3w.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvgbczvctswj7f5ir91p.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcdnvuxe7wzp5c1k6zx9.png)
 

So, that is it!

<img src="https://media0.giphy.com/media/Nfs5y1kzJKfu/giphy.gif?cid=ecf05e472d3w835ptrri5m1p6ifnwl9mi7ope9pifvhpcxf6&rid=giphy.gif&ct=g">