In the full-stack web application, you may have a number of pods running a front-end, back-end, and database. The front-end pods need to communicate with the back-end pods, the back-end pods need to communicate to the database. So, what is the right way to establish connectivity between these services? The pods all have an IP address, but these IP addresses are not static. These pods can go down anytime and new pods are created all the time. Thus, you can not rely on these IP addresses for internal communication between the application. Also, what if the front-end pods need to connect to a back-end service which of the pods would it go to, and who makes that decision?
Kubernetes ClusterIP service can help us group the pod together and provide a single interface to access the pods in a group. For example, a service created for the back-end pod will help group all the back-end pods together and provide a single interface for other pods to access to service. The requests are forwarded to one of the pods under the service randomly. This enables us to easily and effectively deploy a microservices-based application on Kubernetes Cluster. Each layer can now scale or move as required without impacting communication between the various services. Each service gets an IP name assigned to it inside the cluster, and that is the name that should be used by other pods to access the service. This type of service is known as cluster IP to create such a service.
Cluster IP Service
apiVersion: v1
kind: Service
metadata:
name: nginx-clusterip-service
spec:
type: ClusterIP
ports:
- targetPort: 80
port: 80
selector:
app: nginx
$ kubectl create -f nginx-clusterip-service.yaml
service/nginx-clusterip-service created
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 80m
nginx-clusterip-service ClusterIP 10.110.52.209 <none> 80/TCP 28s
$ kubectl get ep nginx-clusterip-service
NAME ENDPOINTS AGE
nginx-clusterip-service 172.18.0.3:80,172.18.0.4:80,172.18.0.5:80 + 1 more... 8m12s
# If you are using minikube
minikube ip
127.0.0.1
minikube ssh
docker@minikube:~$ curl http://10.110.52.209:80
docker@minikube:~$ curl http://172.18.0.3:80
docker@minikube:~$ curl http://172.18.0.4:80
docker@minikube:~$ curl http://172.18.0.5:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>