GCP中Kubernetes中的MongoDB

时间:2019-12-27 12:19:29

标签: mongodb docker kubernetes

我正在尝试在k8s集群上部署mongodb,因为mongodb是我的首选数据库。为此,我需要配置文件(与几周前的postgress非常相似)。

这是mongo的部署k8s对象:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: panel-admin-mongo-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: panel-admin-mongo
  template:
    metadata:
      labels:
        component: panel-admin-mongo
    spec:
      volumes:
        - name: panel-admin-mongo-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
        - name: panel-admin-mongo
          image: mongo
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: panel-admin-mongo-storage
              mountPath: /data/db

为了进入吊舱,我提供了服务:

apiVersion: v1
kind: Service
metadata:
  name: panel-admin-mongo-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: panel-admin-mongo
  ports:
    - port: 27017
      targetPort: 27017

当然,我也需要PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

为了从我的服务器访问数据库,我使用了服务器部署对象:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: panel-admin-api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: panel-admin-api
  template:
    metadata:
      labels:
        component: panel-admin-api
    spec:
      containers:
        - name: panel-admin-api
          image: my-image
          ports:
            - containerPort: 3001
          env:
            - name: MONGO_URL 
              value: panel-admin-mongo-cluster-ip-service // This is important
      imagePullSecrets:
        - name: gcr-json-key

但是由于某种原因,当我使用kubectl apply命令启动所有容器时,我的服务器会说: MongoDB :: connection error: MongoParseError: Invalid connection string

我可以像部署postgress那样部署它吗?还是我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

panel-admin-mongo-cluster-ip-service

前面使用 mongodb://

因此它应该像这样: mongodb://panel-admin-mongo-cluster-ip-service