Kubernetes

What is service discovery in Kubernetes?

Microservices are used in modern systems to keep them scalable and functional. With its tools and functionalities, Kubernetes creates the perfect setting for microservices to collaborate. The solution as a whole becomes much more flexible as each component of the application is deployed in a container. Service discovery is the process of locating apps and microservices on a network.

The technique of automatically finding devices and services on a network is known as service discovery. The Service Discovery Protocol (SDP) is a networking standard for detecting networks and locating resources when users are provided with suitable resources, such as a Bluetooth-enabled printer or laptop, service discovery aids in reducing configuration hassles.

Service discovery is accomplished in Kubernetes using automatically created service names corresponding to the Service’s IP address. In Kubernetes, the services model offers the most basic yet crucial component of microservices discovery. Recognizing how an application operates on Kubernetes requires an understanding of service discovery. Kubernetes Service Syncs standardizes and automates connectivity between services, addressing container issues in a microservices design. Services link Pods to the network in a consistent manner across clusters.

The process of determining how to join service is known as service discovery. Server-side & client-side service discovery are the main types of service discovery. Client applications can use server-side service discovery approaches to support via a router or load balancer. Client-side service discovery lets client applications locate services by searching or asking a service registry containing service instances and endpoints.

Service instances can be registered with and temporarily suspended from the service registry in two different methods. The self-registration pattern allows service entities to register independently with the service registry. The third-party registration pattern involves having another system component perform the registration and deregistration on account of the service. One of the components that make this container platform capable and adaptable is Kubernetes service discovery. Through standardization, advanced technologies like service mesh render Kubernetes service discovery more effective. We’ll look at some of the basic processes of service discovery in Kubernetes in this topic.

Let’s start by putting together some necessary services to examine how Kubernetes’ service concepts perform in practical terms. Let’s access the terminal of the Ubuntu 20.04 LTS OS to talk about service discovery in Kubernetes. For this, you might use the Application area or a shortcut key that is “Ctrl+Alt+T”.

To initialize minikube, run the command listed below.

$ minikube start

The current edition of minikube will be displayed in the output of this instruction. Now we will make a namespace named “develop” by using the cat command in the shell.

$ cat << ENDL > develop-namespace.yml

The content will be added directly to the terminal without opening it from the file.

To generate a namespace in the terminal, utilize the command as:

$ kubectl apply -f develop-namespace.yml

The namespace has been created effectively. Now again, we will create another namespace named “production” by using the cat command in the shell.

To generate a namespace in the terminal, execute the command as:

$ kubectl apply -f production-namespace.yml

Again, the namespace has been created effectively. Now we are going to deploy our sample applications on the namespaces that we have created earlier. For this purpose, we will create a deployment named “hello” for the develop namespace by using the cat command in the shell.

$ cat << ENDL > app-deployment-develop.yml

The content will be added directly to the terminal without opening it from the file.

To create a deployment for the develop namespace, run the command as:

$ kubectl apply -f app-deployment-develop.yml

The deployment has been created effectively. Again, we will create another deployment for a namespace named “production” by using the cat command in the shell.

$ cat << ENDL > app-deployment-production.yml

To create a deployment for the production namespace in the terminal, utilize the command as:

$ kubectl apply -f app-deployment-production.yml

The deployment has been created efficiently, as can be seen from the displayed output. Now to define the pods and to find their IP address, we are going to execute the below-listed command as:

$ kubectl describe pods --all-namespaces

The IP addresses for each Pod in the output above are internal and unique to each occurrence. Each time we redeployed the application, it would be given a new IP address. We can now test whether or not we can ping a Pod inside the cluster. Create a file by using the cat command as.

$ cat << ENDL >> jumpod.yml

To create a pod for the default namespace in the shell, run the command as:

kubectl apply -f jumpod.yml

The pod has been created, as can be seen from the above-attached image. Ping the IP address of a temporary Pod inside the default namespace by using the cat command in the shell by using the kubectl exec command.

$ kubectl exec -it jumpod ping 10.244.0.149

The domain name system is among the essential aspects of effective internet service (DNS). All internet content and web services depend on DNS, so you must be able to solve any issues quickly. A tool called nslookup is one method to do this. nslookup stands for “name server lookup,” It allows you to check up the information about a domain name and discover problems. We can acquire the FQDN of the Pod using the nslookup tool:

$ kubectl exec -it jumpod nslookup 10.244.0.149

Now to check the external deployed IP address, we will execute the below command:

$ kubectl get services --all-namespaces

Conclusion

When migrating to microservices, service discovery is likely the most important piece of infrastructure to implement. In this tutorial, we have seen how simple it is to expose an app to the public utilizing Kubernetes Services.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content