<p>I am using DNS based service discovery to discover services in K8s cluster.
From <a href="https://kubernetes.io/docs/concepts/services-networking/service/#dns" rel="nofollow noreferrer">this</a> link it is very clear that to discover a service named my-service we can do name lookup "my-service.my-ns" and the pod should be able to find the services.</p>

<p>However in case  of port discovery for the service the solution is to use 
is "_http._tcp.my-service.my-ns"
where</p>

<p>_http refers to the port named http in my-service. </p>

<p>But even after using _http._tcp.my-service it doesn't resolve port number. Below are the details.</p>

<p><strong>my-service which needs to be discovered</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-service
  ports:
    - name: http
      protocol: TCP
      port: 5000
      targetPort: 5000
</code></pre>

<p><strong>client-service yaml snippet trying to discover my-service and its port.</strong></p>

<pre><code>spec:
  containers:
  - name: client-service
    image: client-service
    imagePullPolicy: Always
    ports:
      - containerPort: 7799
    resources:
      limits:
        cpu: "100m"
        memory: "500Mi"
    env:
    - name: HOST
      value: my-service
    - name: PORT
      value: _http._tcp.my-service
</code></pre>

<p>Now when I make a request it fails and logs following request which is clearly incorrect as it doesn't discover port number.</p>

<pre><code> http://my-service:_http._tcp.my-service
</code></pre>

<p>I am not sure what wrong I am doing here, but I am following the same instructions mentioned in document.</p>

<p>Can somebody suggest what is wrong here and how we can discover port using DNS based service discovery? Is my understanding wrong here that it will return the literal value of port?</p>

<p><strong>Cluster details</strong></p>

<p>K8s cluster version is 1.11.5-gke.5
 and Kube-dns is running</p>

<p><strong>Additional details trying to discover service from busybox and its not able to discover port value 5000</strong></p>

<pre><code>kubectl exec  busybox -- nslookup my-service
Server:         10.51.240.10
Address:        10.51.240.10:53

Name:   my-service.default.svc.cluster.local
Address: 10.51.253.236

*** Can't find my-service.svc.cluster.local: No answer
*** Can't find my-service.cluster.local: No answer
*** Can't find my-service.us-east4-a.c.gdic-infinity-dev.internal: No answer
*** Can't find my-service.c.gdic-infinity-dev.internal: No answer
*** Can't find my-service.google.internal: No answer
*** Can't find my-service.default.svc.cluster.local: No answer
*** Can't find my-service.svc.cluster.local: No answer
*** Can't find my-service.cluster.local: No answer
*** Can't find my-service.us-east4-a.c.gdic-infinity-dev.internal: No answer
*** Can't find my-service.c.gdic-infinity-dev.internal: No answer
*** Can't find my-service.google.internal: No answer


kubectl exec  busybox -- nslookup _http._tcp.my-service
Server:         10.51.240.10
Address:        10.51.240.10:53

** server can't find _http._tcp.my-service: NXDOMAIN

*** Can't find _http._tcp.my-service: No answer
</code></pre>

<p>Since Services come with their own (Kubernetes-internal) IP addresses, the easy answer here is to not pick arbitrary ports for Services.  Change to <code>port: 80</code> in your Service definition, and clients will be able to reach it using the default HTTP port.  When you set the environment variable, set</p>

<pre><code>- name: PORT
  value: "80"
</code></pre>

<p>DNS supports several different record types; for example, an A record translates a host name to its IPv4 address, and AAAA to an IPv6 address.  The Kubernetes Service documentation you cite notes (emphasis mine)</p>

<blockquote>
  <p>you can do a <strong>DNS SRV query</strong> ... to discover the port number for <code>"http"</code>.</p>
</blockquote>

<p>While <a href="https://en.wikipedia.org/wiki/SRV_record" rel="nofollow noreferrer">SRV records</a> seems like they solve both halves of this problem (they provide a port and a host name for a service) in practice they seem to get fairly little use.  The linked Wikipedia page has a list of services that use it, but "connect to the thing this SRV record points at" isn't an option in mainstream TCP clients that I know of.</p>

<p>You should be able to verify this with a command like (running <a href="https://hub.docker.com/r/giantswarm/tiny-tools" rel="nofollow noreferrer">this debugging image</a>)</p>

<pre><code>kubectl run debug --rm -it --image giantswarm/tiny-tools sh
# dig -t srv _http._tcp.my-service
</code></pre>

<p>(But notice the <code>-t srv</code> argument; it is not the default record type.)</p>

<p>Most things that expect a <code>PORT</code> environment variable or similar expect a number, or if not, a name they can find in an <code>/etc/services</code> file.  The syntax you're trying to use here and trying to provide a DNS SRV name instead probably just won't work, unless you know the specific software supports it.</p>
