0

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
  • 3,474
  • 7
  • 35
  • 89

1 Answers1

2

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

Matt
  • 7,419
  • 1
  • 11
  • 22
  • Thank you for your answer. The error is gone but now I'm getting `MongoDB :: connection error: Error: getaddrinfo ENOTFOUND panel-admin-mongo-cluster-ip-service` instead – Murakami Dec 27 '19 at 14:14
  • try adding `/somedbname` to URL like [here](https://stackoverflow.com/questions/39108992/mongoerror-getaddrinfo-enotfound-undefined-undefined27017/39109112) e.g. `mongodb://panel-admin-mongo-cluster-ip-service/somedbname` – Matt Dec 27 '19 at 14:45
  • Great! I've just connected to db, thank you for you time! – Murakami Dec 27 '19 at 15:04