In this blog we will try to understand what is service discovery in K8s and it is achieved.
Namespaces are one of the most important parts of the whole DevOps ecosystem including Kubernetes. They play a very important role behind various container implementations in the way that they provide an isolation for independent processes from global system’s interference directly.
Namespace Definition : A namespace wraps a global system resource in an abstraction that makes it appear to the processes within the namespace that they have their own isolated instance of the global resource. Changes to the global resource are visible to other processes that are members of the namespace, but are invisible to other processes.
Let’s go more Simpler
It seems quite obvious and simple how resources within the same namespace can communicate with each other.Consider it in this way, inside your house you can communicate with your family members easily i.e you don’t need house address for this. But what if you want to communicate with your friend residing at some xyz building. You will have to follow a fixed route to go there.
Service discovery is all about finding that fixed route in the world of Kubernetes. Service discovery does that with help of dns.
DNS
Domain Name Server aka DNS is basically a standard protocol that helps internet users to discover websites using human readable addresses i.e domain name.Users interact with the web browser using the domain name and the web browser communicates to the server using the IP address. DNS acts as a support which translates the domain name to IP address for overall workflow.
Understanding dns name format for your pod.
IP.podname.namespace.service.cluster.local
Let’s see some use cases for Service Discovery
Scenario-0
Check the ENVIRONMENT variable to discover running services.
Configure a deployment resource.
Exec inside the pod and run printenv to check the Environment variable.
Scale down and up to check if the environment changed or not.
Run get pods again.
Exec to pod and check the environment again.
Scenario-1
Get dns for another pod running in same namespace
Since we already have my-cluster-service running in the namespace “discovery”. Let’s run one more pod imperatively and then do “nslookup” with the service name.
As you can notice, we have got dns for my-cluster-service.
Scenario-2
Get dns for another pod running in different namespace
Get IP address for a pod running at default namespace. Copy that IP address.
Since we already have “welcome-pod” running in the default namespace. Let’s run one more pod imperatively in a different namespace and then do “nslookup” with the IP address we’ve copied above.
Notice the dns name for that particular IP address.
Cleaning Up :
Make sure to delete all the resources to keep your space neat and clean. Follow below commands for that :
kubectl delete po curl -n discovery
kubectl delete po welcome-pod
kubectl delete deploy nginx-deploy -n discovery
kubectl delete svc my-cluster-service -n discovery
kubectl delete namespace discovery
References:
I hope it makes you comfortable with service discovery in Kubernetes. If you have any doubt, feel free to contact me nitin.mishra@knoldus.com
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and if you feel, give me suggestions on scope of improvements.