【问题标题】:Trying to connect to mongodb service through Consul Connect Sidecar Proxy尝试通过 Consul Connect Sidecar Proxy 连接到 mongodb 服务
【发布时间】:2020-04-22 15:14:41
【问题描述】:

我设置了一个 Minikube,并在其中运行了一个 mongo 实例。我使用 Consul + Consul Connect 来网格化我的服务。只有我无法使用 sidecar 上游从另一个服务连接到 mongo,一些奇怪的事情正在发生......

我的 mongo 实例是使用 bitnami helm chart 安装的,我只是设置服务名称,设置用户名并更改存储类以匹配我的需要,并将服务网格的 consul 注释放在 pod 注释部分:

image:
  registry: docker.io
  repository: bitnami/mongodb
  tag: 4.2.5-debian-10-r3
  pullPolicy: IfNotPresent
  debug: false
serviceAccount:
  create: true
  name: "svc-identity-data"
usePassword: true
mongodbRootPassword: rootpassword
mongodbUsername: identity
mongodbPassword: identity
mongodbDatabase: company
service:
  name: svc-identity-data
  annotations: {}
  type: ClusterIP
  port: 27017
useStatefulSet: true
replicaSet:
  enabled: false
  useHostnames: true
  name: rs0
  replicas:
    secondary: 1
    arbiter: 1
  pdb:
    enabled: true
    minAvailable:
      primary: 1
      secondary: 1
      arbiter: 1
annotations: {}
labels: {}
podAnnotations:
    "consul.hashicorp.com/connect-inject": "true"
    "consul.hashicorp.com/connect-service": "svc-identity-data"
    "consul.hashicorp.com/connect-service-protocol": "tcp"
persistence:
  enabled: true
  mountPath: /bitnami/mongodb
  subPath: ""
  storageClass: "standard"
  accessModes:
    - ReadWriteOnce
  size: 8Gi
  annotations: {}
configmap:
  storage:
    dbPath: /bitnami/mongodb/data/db
    journal:
      enabled: true
    directoryPerDB: false
  systemLog:
    destination: file
    quiet: false
    logAppend: true
    logRotate: reopen
    path: /opt/bitnami/mongodb/logs/mongodb.log
    verbosity: 0
  net:
    port: 27017
    unixDomainSocket:
      enabled: true
      pathPrefix: /opt/bitnami/mongodb/tmp
    ipv6: false
    bindIp: 0.0.0.0
  processManagement:
     fork: false
     pidFilePath: /opt/bitnami/mongodb/tmp/mongodb.pid
  setParameter:
     enableLocalhostAuthBypass: true
  security:
    authorization: enabled

其次,我启动了一个独立的 mongodb pod 来使用 mongo 客户端,并使用注释与 consul connect 啮合

apiVersion: v1
kind: Pod
metadata:
  name: mongo-client
  labels:
    name: mongo-client
  annotations:
        "consul.hashicorp.com/connect-inject": "true"
        "consul.hashicorp.com/connect-service-upstreams": "svc-identity-data:28017"
        "consul.hashicorp.com/connect-service-protocol": "tcp"    
spec:
  containers:
  - name: mongo-client
    image: mongo:4.2.5
    imagePullPolicy: IfNotPresent
    resources:
      limits:
        memory: "128Mi"
        cpu: "500m"
    ports:
      - containerPort: 27017

我现在有一个 mongodb 服务和一个 mongo 客户端 pod,其上游到 mongodb 服务绑定在 127.0.0.1:28017

当我尝试使用上游连接到 mongodb 服务时,出现我不理解的行为

> kubectl exec -it mongo-client mongo --host 127.0.0.1 --port 28017 -u root -p rootpassword

MongoDB shell version v4.2.5
connecting to: mongodb://127.0.0.1:28017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8c46012d-8083-4029-8495-167bbe8bf063") }
MongoDB server version: 4.2.5
Server has startup warnings: 
2020-04-22T12:20:14.777+0000 I  STORAGE  [initandlisten] 
2020-04-22T12:20:14.777+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-04-22T12:20:14.777+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 
bye

这里没问题,对我来说一切都很好,但是如果我使用带有连接字符串而不是单独参数的 mongo,我会收到连接被拒绝

> kubectl exec -it mongo-client mongo mongodb://root:roopassword@127.0.0.1:28017/?authSource=admin

MongoDB shell version v4.2.5
connecting to: mongodb://127.0.0.1:28017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
2020-04-22T15:04:07.955+0000 I  NETWORK  [js] DBClientConnection failed to receive message from 127.0.0.1:28017 - HostUnreachable: Connection closed by peer
2020-04-22T15:04:07.968+0000 E  QUERY    [js] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:28017'  :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-04-22T15:04:07.973+0000 F  -        [main] exception: connect failed
2020-04-22T15:04:07.973+0000 E  -        [main] exiting with code 1

我完全不明白使用连接字符串和单独的参数有什么区别,如果您有任何线索或解决方案,请告诉我。

P.S : 我没有设置任何安全通信 (tls),我在 minikube 上(因为我是微服务架构和 Kubernetes n00b),它是为了试验服务网格(我们需要生活在当前时代),不使用边车连接到服务的解决方案不是重点,顺便说一下,使用连接字符串直接连接到服务可以完美地工作。

> kubectl exec -it mongo-client mongo -mongodb://root:roopassword@svc-identity-data:28017/?authSource=admin

MongoDB shell version v4.2.5
connecting to: mongodb://svc-identity-data:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("713febaf-2000-4ca6-8b1f-963c76986e72") }
MongoDB server version: 4.2.5
Server has startup warnings: 
2020-04-22T12:20:14.777+0000 I  STORAGE  [initandlisten] 
2020-04-22T12:20:14.777+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-04-22T12:20:14.777+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 
bye

编辑:重新启动 minikube 使所有事情都按预期工作。我将就此事进行更多调查以了解原因。也许其他人会遇到同样的问题。

编辑 2:我发现了一件事:通过边车连接到 mongo 时的连接错误是随机的,当我运行命令直到成功时,这就是我得到的

root@mongo-client:/# mongo mongodb://root:rootpassword@localhost:28017/?authSource=admin
MongoDB shell version v4.2.5
connecting to: mongodb://localhost:28017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
2020-04-24T12:51:15.641+0000 I  NETWORK  [js] DBClientConnection failed to receive message from localhost:28017 - HostUnreachable: Connection closed by peer
2020-04-24T12:51:15.702+0000 E  QUERY    [js] Error: network error while attempting to run command 'isMaster' on host 'localhost:28017'  :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-04-24T12:51:15.729+0000 F  -        [main] exception: connect failed
2020-04-24T12:51:15.729+0000 E  -        [main] exiting with code 1
root@mongo-client:/# mongo mongodb://root:rootpassword@localhost:28017/?authSource=admin
MongoDB shell version v4.2.5
connecting to: mongodb://localhost:28017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("628bfcf9-6d44-4168-ab74-19a717d746f6") }
MongoDB server version: 4.2.5
Server has startup warnings: 
2020-04-24T06:43:39.359+0000 I  STORAGE  [initandlisten] 
2020-04-24T06:43:39.359+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-04-24T06:43:39.359+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 
bye

在 mongo 一侧的日志:

2020-04-24T12:51:19.281+0000 I  NETWORK  [conn6647] end connection 127.0.0.1:54148 (6 connections now open)
2020-04-24T12:51:19.526+0000 I  COMMAND  [conn6646] command admin.$cmd appName: "MongoDB Shell" command: saslStart { saslStart: 1, mechanism: "SCRAM-SHA-256", payload: "xxx", $db: "admin" } numYields:0 reslen:196 locks:{} protocol:op_msg 231ms
2020-04-24T12:51:19.938+0000 I  ACCESS   [conn6646] Successfully authenticated as principal root on admin from client 127.0.0.1:54142
2020-04-24T12:51:20.024+0000 I  NETWORK  [listener] connection accepted from 127.0.0.1:54168 #6648 (7 connections now open)
2020-04-24T12:51:20.027+0000 I  NETWORK  [conn6648] received client metadata from 127.0.0.1:54168 conn6648: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.2.5" }, os: { type: "Linux", name: "PRETTY_NAME="Debian GNU/Linux 10 (buster)"", architecture: "x86_64", version: "Kernel 4.19.94" } }
2020-04-24T12:51:20.215+0000 I  NETWORK  [conn6648] end connection 127.0.0.1:54168 (6 connections now open)
2020-04-24T12:51:21.328+0000 I  NETWORK  [conn6646] end connection 127.0.0.1:54142 (5 connections now open)

我越来越困惑,我无法解释这种行为。

【问题讨论】:

  • svc-identity-data 解析为什么?
  • 啊,是的,这是我的 mongodb 服务的名称。我将编辑帖子以将我的值放入掌舵图
  • 那么它解决了什么问题?
  • @Oleg 解析为集群中的服务 ip svc-identity-data.default.svc.cluster.local has address 10.107.99.51

标签: mongodb kubernetes consul envoyproxy


【解决方案1】:

我找到了解决方案,结果证明这是最简单的问题:资源

我的 minikube 不足以让所有 pod 快速运行,它会在 sidecar 代理 pod 之间引入延迟,即使 kubenetes 在任何中断时都没有引发错误。

我是一名 kubernetes 学习者,所以我没有马上想到它。现在我知道发生了什么,我可以朝着正确的方向进行调查,以了解延迟可能是什么问题。

【讨论】:

    【解决方案2】:

    问题可能是证书的CN与MongoDB的配置文件中主机名的值不匹配。它是关于 MongoDB 规范和运行它的参数。

    证书的 CN(通用名称)或 SAN(主题备用名称)必须与您在运行 mongo 时提供的 --hostname 的值匹配。 您的 MongoDB URI 是:

    MONGODB_URI=mongodb://root:roopassword@127.0.0.1:28017/?authSource=admin
    

    MongoDB 不在本地主机上。此外,MongoDB 服务器需要允许任何主机连接到数据库。默认情况下,它只允许来自相同运行时的连接。您需要使用数据库容器获取分配给 pod 的服务 IP 地址 - svc-identity-data 的地址为 10.107.99.51

    看看:mongodb-ssl,mongodb-failed-to-connect

    【讨论】:

    • MongoDB绑定在所有接口上(configMap中的bindIp=0.0.0.0),实际情况是,重启minikube后一切正常,我可以通过sidecar代理连接到mongodb
    • 你的虚拟机驱动是什么?
    • minikube 的 VirtualBox
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多