<p>I have created a POD that contains a nodejs app from which I'm trying to connect to a remote database in another server. My POD configuration goes like this:</p>
<pre><code>apiVersion : &quot;v1&quot;
kind : &quot;Service&quot;
metadata :
  name : &quot;mysql&quot;
spec :
  ports :
    -
      name : &quot;mysql&quot;
      protocol : &quot;TCP&quot;
      port : 3306
      targetPort : 3306
      nodePort : 0
---
kind: &quot;Endpoints&quot;
apiVersion: &quot;v1&quot;
metadata:
  name: &quot;mysql&quot;
subsets:
  -
    addresses:
      -
        ip: &quot;xxx.xxx.xxx.xxx&quot; #The IP Address of the external web server
    ports:
      -
        port: 3306
        name: &quot;mysql&quot;
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: productos
  labels:
    app: productos
spec:
  replicas: 3
  selector:
    matchLabels:
      app: productos
      tier: backend
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: productos
        tier: backend
    spec:
      restartPolicy: Always
      containers:
      - image: sospinah/tutorial:lopido-ms-productos6
        name: productos
        env:
        - name: MYSQL_HOST
          value: &quot;mysql.default.svc.cluster.local&quot;
        - name: MYSQL_USER
          value: &quot;&lt;username&gt;&quot;
        - name: MYSQL_DATABASE
          value: &quot;&lt;dbname&gt;&quot;
        - name: MYSQL_PASSWORD
          value: &quot;&lt;password&gt;&quot;
        ports:
        - containerPort: 4000
</code></pre>
<p>In my nodejs application I have defined my database connection as follows:</p>
<pre><code>const connection = mysql.createConnection({
        host: process.env.MYSQL_HOST,
        user: process.env.MYSQL_USER,
        password: process.env.MYSQL_PASSWORD,
        database: process.env.MYSQL_DATABASE,
});
</code></pre>
<p>And I'm getting a timeout error on app startup</p>
<pre><code>&gt; nodejs-microservice@1.0.0 start /usr/src/app
&gt; node index.js

server listening at port 0.0.0.0:4000
error connecting mysql:  { Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout (/usr/src/app/node_modules/mysql/lib/Connection.js:419:13)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:422:8)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
    --------------------
    at Protocol._enqueue (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at Connection.connect (/usr/src/app/node_modules/mysql/lib/Connection.js:130:18)
    at Object.bdConfig.testConection (/usr/src/app/database/config.js:16:23)
    at Object.&lt;anonymous&gt; (/usr/src/app/index.js:18:14)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true }

</code></pre>
<p>I would like to know what am I missing in my POD configuration and/or application configuration.</p>

Sebastian Ospina
<p>You can use service name to connect pods internally. you can use 'mysql' service name if your webserver is running inside same kubernetes cluster.</p>

<p>If you want to run your mysql inside kubernetes cluster and want to make connection outside of kubenetes cluster then probably you can expose your mysql service to internet and you can to your mysql database from anywhere. If your all application running inside same cluster then use a service name to resolve the address.</p>
