<p>How to access headless services from another namespace?  If I try to access it by <code>&lt;service&gt;.&lt;namespace&gt;</code> then I sometimes connect to replica db <code>dbhost001-1</code>. I want to specifically connect to <code>dbhost001-0</code> master DB.</p>
<pre><code>kubectl get pods -n test-db-dev
NAME     READY   STATUS    RESTARTS   AGE
dbhost001-0   1/1     Running   0          38m
dbhost001-1   1/1     Running   0          17m
</code></pre>
<p><code>headless-service.yml</code>:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: dbhost001
  labels:
    app: dbhost001-service
  namespace: test-db-dev
spec:
  selector:
    app: dbhost001   # Metadata label of the deployment pod template or pod metadata label
  clusterIP: None
  ports:
    - name: mysql-port    # Optional when its just only one port
      protocol: TCP
      port: 3306
      targetPort: 3306
</code></pre>

user630702
<p>Considering that you're using StatefulSet for the DB.</p>
<p>If both the StatefulSet and it's governing service name is <code>dbhost001</code>, running on <code>default</code> namespace, you can connect Pod <code>dbhost001-0</code> on address: <code>dbhost001-0.dbhost001.default.svc</code>.</p>
<p>Format: <code>&lt;pod name&gt;.&lt;service name&gt;.&lt;namespace&gt;.svc</code></p>
