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');
}
If someone has examples (preferably nodejs), I can dig into that.
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.