【问题标题】:run mongodb inside kubernetes - azure aks fails在 kubernetes 中运行 mongodb - azure aks 失败
【发布时间】:2018-08-13 12:09:06
【问题描述】:

我尝试在托管在 azure aks 上的 kubernetes 集群中运行 mongodb

按照本教程,我无法让它运行:https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/

这是我使用的 yaml:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: default-view
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: view
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default
---
apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 2
  template:
    metadata:
      labels:
        role: mongo
        environment: test
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo
          command:
            - mongod
            - "--replSet"
            - rs0
            - "--bind_ip"
            - 0.0.0.0            
            - "--smallfiles"
            - "--noprealloc"
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: MONGO_SIDECAR_POD_LABELS
              value: "role=mongo,environment=test"
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
      annotations:
        volume.beta.kubernetes.io/storage-class: "managed-premium"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 32Gi

我使用的连接字符串是:

"mongodb://mongo-0.mongo,mongo-1.mongo:27017/databasename\_?"

从我的 JS 应用程序中我得到:

数据库名称不能包含字符'\'

如何从 JS 应用程序连接到 mongodb?

【问题讨论】:

  • 也许我不清楚;错误消息有助于指示您不要包含反斜杠。为什么会在那里?

标签: mongodb azure kubernetes azure-aks


【解决方案1】:

mongodb://mongo-0.mongo,mongo-1.mongo:27017

您表示您有一个包含 2 个成员的副本集,并且都使用端口 27017。您的 mongodb 库将处理该 url 以连接到集群。

为了在本地连接,您必须进行端口转发:

kubectl port-forward mongo-0 27017 # 或 mongo-1

然后您可以使用本地主机 (127.0.0.1) 和端口 27017 通过 Robo 3T 连接到所选的 mongodb。

【讨论】:

  • 感谢您的回答!在所有示例中,我为连接字符串找到了这个:“_?”这是什么意思?
  • 不客气。根据您的示例,您正在指定一个名为 chronas-api_ 的数据库和字符 ?用于添加一些选项。检查文档here
【解决方案2】:

我能够使用此连接字符串解决问题:

mongodb://mongo-0.mongo,mongo-1.mongo:27017/chronas-api_?

但是谁能解释一下这个连接字符串的魔法以及如何使用“Robo 3T”连接到这个集群?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2021-05-08
    • 2021-09-20
    • 2022-10-07
    • 2021-07-28
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多