<p>I am trying to implement peer discovery logic through DNS in go using SRV records in cluster. I have headless service and statefulset pods ready and I am able to list all SRV records by using</p>

<pre><code>kubectl run -it srvlookup --image=tutum/dnsutils --rm --restart=Never -- dig SRV demoapp.default.svc.cluster.local
</code></pre>

<p>but the following code does not work in cluster:</p>

<pre><code>func pingdns() (url string) {
log.Println("start ping demoapp.default.svc.cluster.local.")
_, addrs, err := net.LookupSRV("dns-tcp", "tcp", "demoapp.default.svc.cluster.local")
if err != nil {
    log.Println(err.Error())
    return "dns wrong"
}
fmt.Println(addrs)
return "dns done."
</code></pre>

<p>}</p>

<p>error output:</p>

<pre><code>lookup _dns-tcp._tcp.demoapp.default.svc.cluster.local on 10.96.0.10:53: no such host
</code></pre>

<p>I found example in this k8s-in-action book but it is written in NodeJS. How to do it in Golang ?</p>

<pre><code>const dns = require('dns');
const dataFile = "/var/data/kubia.txt";
const serviceName = "kubia.default.svc.cluster.local";
const port = 8080;
...
var handler = function(request, response) {
  if (request.method == 'POST') {
...
} else {
    response.writeHead(200);
    if (request.url == '/data') {
      var data = fileExists(dataFile)
        ? fs.readFileSync(dataFile, 'utf8')
        : "No data posted yet";
      response.end(data);
    } else {
      response.write("You've hit " + os.hostname() + "\n");
      response.write("Data stored in the cluster:\n");
      dns.resolveSrv(serviceName, function (err, addresses) {
The app performs a DNS lookup to obtain SRV records.
   if (err) {
  response.end("Could not look up DNS SRV records: " + err);
  return;
}
var numResponses = 0;
if (addresses.length == 0) {
  response.end("No peers discovered.");
} else {
addresses.forEach(function (item) { ...
</code></pre>

cong
<p>Thanks Shubham ! I read the medium post and found using grpc to make connection to SRV should browse all IPs as a RR fashion. But I am still looking for get all IPs. </p>

<p>Medium article:
<a href="https://medium.com/google-cloud/loadbalancing-grpc-for-kubernetes-cluster-services-3ba9a8d8fc03" rel="nofollow noreferrer">https://medium.com/google-cloud/loadbalancing-grpc-for-kubernetes-cluster-services-3ba9a8d8fc03</a></p>

<p>Gitrepo:
<a href="https://github.com/jtattermusch/grpc-loadbalancing-kubernetes-examples#example-1-round-robin-loadbalancing-with-grpcs-built-in-loadbalancing-policy" rel="nofollow noreferrer">https://github.com/jtattermusch/grpc-loadbalancing-kubernetes-examples#example-1-round-robin-loadbalancing-with-grpcs-built-in-loadbalancing-policy</a></p>

<pre><code>import (
   "google.golang.org/grpc/balancer/roundrobin"
   "google.golang.org/grpc/credentials"
)

conn, err := grpc.Dial("dns:///be-srv-lb.default.svc.cluster.local", grpc.WithTransportCredentials(ce), grpc.WithBalancerName(roundrobin.Name))
c := echo.NewEchoServerClient(conn)
</code></pre>

<p>It makes calls on a list of IPs one by one. RR </p>

<pre><code>Creating channel with target greeter-server.default.svc.cluster.local:8000
Greeting: Hello you (Backend IP: 10.0.2.95)
Greeting: Hello you (Backend IP: 10.0.0.74)
Greeting: Hello you (Backend IP: 10.0.1.51)
</code></pre>

<p>I have found my main issue is related to this problem. </p>

<p><a href="https://stackoverflow.com/questions/30043248/why-golang-lookup-function-cant-provide-a-server-parameter">Why golang Lookup*** function can&#39;t provide a server parameter?</a></p>
