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.
Here comes nodeport which is a port on nodes, helping you connect to your pods networks and get the container .
Checkout these carefully:
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.
First 3 part is easy with Apiversion, kind and meta data But the most interesting part is all about spec:
For the spec, we have
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
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.
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:
So, we will use the labels from here under the selectors.
Now, we have updated our selector like this:
Now, we just need to using the create command to create the service.
We can check the services from here:
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.
Also for multiple nodes, the service can be created like this and the pods can be accessed by this:
Now, lets do some hands on....
From the series, we know that we created a deployment which created 6 pods.
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:
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
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"
First check out the pods and using one of the pods, get the Node IP, which will look like this:
Now paste it in the browser and use the NodePort too.
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.
To create a clusterIP, follow this format
"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:
*3) Load Balancer *
Assume that you have 4 nodes
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
Look carefully, as we have 4 nodes, now we will have 4 links for votes and thus 4 links for getting results:
But a voter does not want to see these 8 links. He/she just wants to have a link to vote and get result.
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:
So, that is it!
Top comments (0)