MongoDB in Kubernetes within GCP

12/27/2019

I'm trying to deploy mongodb on my k8s cluster as mongodb is my db of choice. To do that I've config files (very similar to what I did with postgress few weeks ago).

Here's mongo's deployment k8s object:

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

In order to get into the pod I made a service:

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

And of cource I need a PVC as well:

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

In order to get to the db from my server I used server deployment object:

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

But for some reason when I'm booting up all containers with kubectl apply command my server says: MongoDB :: connection error: MongoParseError: Invalid connection string

Can I deploy it like that (as it was possible with postgress)? Or what am I missing here?

-- Murakami
docker
kubernetes
mongodb

1 Answer

12/27/2019

Use mongodb:// in front of your panel-admin-mongo-cluster-ip-service

So it should look like this: mongodb://panel-admin-mongo-cluster-ip-service

-- HelloWorld
Source: StackOverflow