2

i have a question on the kubernetes DNS lookup, if i am using in my services deployment "dns" instead of "env",

Can my microservice using another microservices in the cluster get the dns names of all the microservices?

I get this piece of code, if I use env then I get the the info of host from env. but if I am using dns what format and how do I get the dns names, is there a DNS object I can query on the client side?

if (isset($_GET['cmd']) === true) {
  $host = 'redis-master';
if (getenv('GET_HOSTS_FROM') == 'env') {
  $host = getenv('REDIS_MASTER_SERVICE_HOST');
}

Ref: https://github.com/GoogleCloudPlatform/container-engine-samples/blob/master/guestbook/php-redis/guestbook.php

If someone has examples (preferably nodejs), I can dig into that.

Rajesh Jain
  • 1,159
  • 3
  • 19
  • 37

1 Answers1

7

First off this is documented throughly. In any case if you want to query DNS to find out where things are running you can do so if you know the service name by pointing at:

my-svc.my-namespace.svc.cluster.local

Additionally, if you also want to abstract port numbers and are OK with knowing a port name you can query SRV records and get both port numbers as well as CNAME:

_my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster.local

For your specific example this would be something like (assuming default namespace):

_redis-client._tcp.redis-service.default.svc.cluster.local

Querying SRV records is more reliable than depending on environment variables because if during the lifetime of the pod, an external service changes location, environment variables can't be re-injected, but re-querying DNS records will yield updated results.

vascop
  • 4,972
  • 4
  • 37
  • 50
  • Hi. How do you perform a SRV look up to query for the ports? – Eugene R. Wang Nov 05 '18 at 21:38
  • @EugeneR.Wang "dig" is a good tool to test it out manually, but presumably this would be done through some sort of other system. OP was using php so presumably something like http://php.net/manual/en/function.dns-get-record.php – vascop Nov 07 '18 at 11:37