如何在Kubernetes中配置Mongodb连接以与我的Node.js应用连接

时间:2019-05-24 11:58:54

标签: node.js mongodb kubernetes database-connection

我创建了一个nodejs应用程序,并尝试将其与kubernetes集群上的mongodb连接。 nodejs和mongodb应用程序是集群中单独的容器。

当我显示状态时,mongodb和应用程序正在运行,我可以将我连接到mongodb pod并添加数据

NAME                                      READY   STATUS    RESTARTS   AGE
my-backend-core-test-5d7b78c9dc-dt4bg     1/1     Running   0          31m
my-frontend-test-6868f7c7dd-b2qtm         1/1     Running   0          40h
my-mongodb-test-7d55dbff74-2m6cm          1/1     Running   0          34m

但是当我尝试使用此脚本建立连接时:

const urlDB = "my-mongodb-service-test.my-test.svc.cluster.local:27017";
console.log("urlDB :: ", urlDB);

mongoose.connect('mongodb://'+urlDB+'/test', { useNewUrlParser: true }).then(() => {
    console.log("DB connected")
}).catch((err)=> {
    console.log("ERROR")
})

我的nodejs应用程序出现以下错误:

> my-core@1.0.0 start /usr/src/app
> node ./src/app.js

urlDB ::  my-mongodb-service-test.my-test.svc.cluster.local:27017
ERROR

正如在kubernetes上所解释的,我想使用service-name.namespace.svc.cluster.local(my-mongodb-service-test.my-test.svc.cluster.local:27017)在不同Pod之间进行通信

mongo日志向我显示了一个不同的主机,对应于我的pod而不是服务。如何在yaml文件上配置主机?

mongodb日志:


2019-05-24T10:57:02.367+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=my-mongodb-test-7d55dbff74-2m6cm
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] db version v4.0.9
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765
2019-05-24T10:57:02.374+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] modules: none
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] build environment:
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-05-24T10:57:02.375+0000 I CONTROL  [initandlisten] options: { net: { bindIp: "0.0.0.0" } }
2019-05-24T10:57:02.376+0000 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten]
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-05-24T10:57:02.377+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=485M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-05-24T10:57:03.521+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:521941][1:0x7f2d2eeb0a80], txn-recover: Main recovery loop: starting at 2/140416 to 3/256
2019-05-24T10:57:03.719+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:719280][1:0x7f2d2eeb0a80], txn-recover: Recovering log 2 through 3
2019-05-24T10:57:03.836+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:836203][1:0x7f2d2eeb0a80], txn-recover: Recovering log 3 through 3
2019-05-24T10:57:03.896+0000 I STORAGE  [initandlisten] WiredTiger message [1558695423:896185][1:0x7f2d2eeb0a80], txn-recover: Set global recovery timestamp: 0
2019-05-24T10:57:03.924+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-05-24T10:57:03.947+0000 I CONTROL  [initandlisten]
2019-05-24T10:57:03.984+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-05-24T10:57:03.986+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

mongodb yaml

apiVersion: v1
kind: Service
metadata:
  name: my-mongodb-service-test
  namespace: my-test 
spec:
  selector:
    app: my-mongodb
    env: test
  ports:
  - port: 27017
    targetPort: 27017 
    protocol: TCP


--- 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-mongodb-test
  namespace: my-test    
  labels:
    app: my-mongodb
    env: test
spec: 
  selector:
    matchLabels:
      app: my-mongodb-test
  replicas: 1
  template:  
    metadata:
      labels:
        app: my-mongodb-test
    spec:
      containers: 
        - name: mongo
          image: mongo:4.0.9
          command:
            - mongod
            - "--bind_ip"
            - "0.0.0.0"
          imagePullPolicy: Always
          ports:
            - containerPort: 27017
              name: mongo
              hostPort: 27017
              protocol: TCP
          volumeMounts:
              - mountPath: /data/db
                name: mongodb-volume
      volumes:
        - name: mongodb-volume
          hostPath:
            path: /home/debian/mongodb 

1 个答案:

答案 0 :(得分:1)

您的service selectorpod labels不匹配,服务端点为空(您可以通过kubectl describe svc/my-mongodb-service-test -n my-test进行检查),因此kubernetes无法按服务访问pod。

正确的服务选择器是:

apiVersion: v1
kind: Service
metadata:
  name: my-mongodb-service-test
  namespace: my-test 
spec:
  selector:
    app: my-mongodb
...

这应该与Deployment yaml中spec.template.metadata.labels指定的吊舱标签匹配。