<p>I created two namespaces and services in each namespace:</p>
<ul>
<li>Namespace: app-layer
<ul>
<li>rest-app</li>
<li>db-service-externalname</li>
</ul>
</li>
<li>Namespace: data-layer
<ul>
<li>db-service</li>
</ul>
</li>
</ul>
<p>When I try to connect to the MySQL database in DB Service from the <code>rest-app</code>, I get the error:</p>
<blockquote>
<p>MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
---&gt; System.AggregateException: One or more errors occurred. (Name or service not known)</p>
</blockquote>
<p>I printed out in logs, and it correctly has <code>db-service</code> as the service name, and has the right user/pass.</p>
<p>Here's what I defined:</p>
<p><strong>db-service</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: db-service
  namespace: data-layer
spec:
  selector:
    app: db-service
  ports:
   - port: 3306
  clusterIP: None
</code></pre>
<p><strong>db-service-externalname</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: db-service
  namespace: app-layer
spec:
  type: ExternalName
  externalName: db-service.data-layer.service.cluster.local
  ports:
    - port: 3306
</code></pre>
<p><strong>rest-app</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: rest-app
  namespace: app-layer
  labels:
    app: rest-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rest-app
  template:
    metadata:
      labels:
        app: rest-app
    spec:
      containers:
        - name: rest-app
          image: restapp:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 5000
          env:
            # These are from a secret I defined, and the logs show
            # the rest app gets them correctly
            - name: MYSQL_ROOT_USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: db-username
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: db-password
            # I hard-coded this to the externalName I created. 
            # Is that right?
            - name: MYSQL_URL
              value: db-service
</code></pre>
<p>Questions:</p>
<ul>
<li>Am I correct to make the externalName in the app-layer namespace?</li>
<li>Do I need to define it differently?</li>
<li>Can the C# app in the docker container not refer to <code>db-service</code> which is the name of the externalName service?</li>
</ul>

Don Rhummy
<p>The <a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer">ExternalName</a> type service should be as below. Notice usage of <code>svc</code> instead of <code>service</code>.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: db-service
  namespace: app-layer
spec:
  type: ExternalName
  externalName: db-service.data-layer.svc.cluster.local
  ports:
    - port: 3306
</code></pre>
