Basically Services means the Network load balancer.
There will be a problem like if some one has to call the rest call where inside the POD its running in that case he needs the ipaddress and POrt.
But these ip address keeps on changing when the pod fone k8s will create one more pod with different ip.
In this case we have the use the Services Basically this will works based on the labels
" POd label should match with the services selector label " then those Services provides one of the ip with that ip we can access that "
like --type=clusterIp : kubectl expose rs nagendrareplica --port=1234 --target-port=80
There are three types:
Clusterip : We can use this cluster ip to access tha application inside the cluster with the port not outside of it
--type=nodeport : This one will do the cluster ip step and also opens the node port which can be accessible by outside
root@ip-172-31-46-170:/home/ubuntu/nagendra# kubectl expose rs nagendrareplica --port=1234 --target-port=80 --type=NodePort -n=nagendra
From Clusterip with port we can access inside the cluster
From nodeip where services is running with the port given by node port we can access outside whcih will redirect that traffic into the correspoding application
- type=--loadbalancer : This will create the loadbalancer ip and the node port. Here k8s not able to create the loadbalancer thats why its coming as pending --type=LoadBalancer : ubectl expose rs nagendrareplica --port=1234 --target-port=80 --type=LoadBalancer -n=nagendra servicel1 LoadBalancer 10.111.196.233 1234:30649/TCP 5s
Workflow like : f user do the curl of www.app.xxx. ( which was added in DNS mapping there we have to mention the loadbalancerip and node port)
Then the request goes to the loadbalancer ip to the one of the node and that corresponding nodeport and then it redirected that info to the 80 port and gives the result
Note : There is a problem like if there are too many services resources like if its 100 then it has to create 100 loadbalancer which is costlier.
root@ip-172-31-46-170:/home/ubuntu/nagendra# kubectl get services -n=nagendra
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nag-cs ClusterIP 10.100.6.37 5678/TCP 66m
nagendrareplica NodePort 10.100.64.125 1234:30710/TCP 25s
nagendrareplicav2 ClusterIP 10.111.113.155 80/TCP 56m