Home > Enterprise >  Does kubernetes have service discovery solution or I need to implement this myself?
Does kubernetes have service discovery solution or I need to implement this myself?

Time:02-06

I develop microservices (using nodejs) for each microservice I serve with a random port, for that, I need a discovery service.

The question is does kubernetes have a service discovery solution or should I implement this myself?

CodePudding user response:

The short answer is yes, Kubernetes does have a service discovery solution. Kubernetes has a built-in DNS server that can be used to discover services. For example, if you have a service named my-service in the my-namespace namespace, you can access it from any pod using http://my-service.my-namespace.svc.cluster.local.

The long answer is that Kubernetes provides two different types of service discovery:

  1. DNS-based service discovery
  2. Environment variable-based service discovery

DNS-based service discovery

DNS-based service discovery is the most common service discovery mechanism in Kubernetes. It is based on the built-in DNS server that is part of the Kubernetes cluster. Kubernetes automatically creates DNS records for each service. The DNS records are named after the service name and the service namespace. The DNS records are stored in the cluster DNS server, which is a special type of DNS server that is part of the Kubernetes cluster. The cluster DNS server is automatically configured as the default DNS server for all pods in the cluster. If you have a service named my-service in the my-namespace namespace, you can access it from any pod using http://my-service.my-namespace.svc.cluster.local.

Environment variable-based service discovery

Environment variable-based service discovery is a more advanced service discovery mechanism. It is based on the fact that Kubernetes automatically creates environment variables for each service. The environment variables are named after the service name and the service namespace. The environment variables are stored in the container's environment. If you have a service named my-service in the my-namespace namespace, you can access it from any pod using the MY_SERVICE_MY_NAMESPACE_SERVICE_HOST and MY_SERVICE_MY_NAMESPACE_SERVICE_PORT environment variables.

DNS-based service discovery vs. environment variable-based service discovery

DNS-based service discovery is the most common service discovery mechanism in Kubernetes. It is very easy to use and it is supported by all Kubernetes client libraries. Environment variable-based service discovery is a more advanced service discovery mechanism. It is more flexible and it is supported by all Kubernetes client libraries. The main drawback of environment variable-based service discovery is that it requires the client application to be aware of the Kubernetes API.

  • Related