简体   繁体   English

如何从kubernetes容器内的nodejs应用访问远程mysql数据库?

[英]how to access to a remote mysql database from a nodejs app inside a kubernetes pod?

I have created a POD that contains a nodejs app from wich im trying to connect to a remote database in another server. 我创建了一个POD,其中包含一个来自node im的应用程序,它们试图连接到另一台服务器中的远程数据库。 My POD configuration goes like this: 我的POD配置如下所示:

apiVersion : "v1"
kind : "Service"
metadata :
  name : "mysql"
spec :
  ports :
    -
      name : "mysql"
      protocol : "TCP"
      port : 3306
      targetPort : 3306
      nodePort : 0
---
kind: "Endpoints"
apiVersion: "v1"
metadata:
  name: "mysql"
subsets:
  -
    addresses:
      -
        ip: "xxx.xxx.xxx.xxx" #The IP Address of the external web server
    ports:
      -
        port: 3306
        name: "mysql"
---
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: "mysql.default.svc.cluster.local"
        - name: MYSQL_USER
          value: "<username>"
        - name: MYSQL_DATABASE
          value: "<dbname>"
        - name: MYSQL_PASSWORD
          value: "<password>"
        ports:
        - containerPort: 4000

In my nodejs application i have defined my database connection as follows: 在我的nodejs应用程序中,我定义了数据库连接,如下所示:

const connection = mysql.createConnection({
        host: process.env.MYSQL_HOST,
        user: process.env.MYSQL_USER,
        password: process.env.MYSQL_PASSWORD,
        database: process.env.MYSQL_DATABASE,
});

And im getting a timeout error on app startup 我在应用程序启动时收到超时错误

> nodejs-microservice@1.0.0 start /usr/src/app
> 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.<anonymous> (/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 }

I would like to know what am i missing in my POD configuration and/or application configuration. 我想知道我的POD配置和/或应用程序配置中缺少什么。

you have defined mysql host as mysql.default.svc.cluster.local 您已将mysql主机定义为mysql.default.svc.cluster.local

This would work if both nodejs and mysql run in same kubernetes cluster. 如果nodejs和mysql都在同一个kubernetes集群中运行,这将起作用。

You can use service name to connect pods internally. 您可以使用服务名称在内部连接Pod。 you can use 'mysql' service name if your webserver is running inside same kubernetes cluster. 如果您的网络服务器在相同的kubernetes集群中运行,则可以使用“ mysql”服务名称。

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. 如果您想在kubernetes集群中运行mysql并想在kubenetes集群之外建立连接,那么您可以将mysql服务公开到Internet,并且可以从任何地方访问mysql数据库。 If your all application running inside same cluster then use a service name to resolve the address. 如果您的所有应用程序都在同一群集中运行,则使用服务名称来解析地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM