MongoDB stopped being accessible when started using Kompose on Minikube

3/9/2019

I have a simple app that uses MongoDB docker image calling the url: mongodb://mongo:27017/. It was working fine using docker-compose:

docker-compose.yml

version: "3.3"
services:
  mongo:
    image: mongo
    restart: always
    container_name: mongo
    volumes:
      - /data/mongodb:/data/db
    ports:
     - "30001:27017"
  myapp:
    image: myapp:0.1.0
    restart: always
    links:
     - mongo # link this service to the database service
    depends_on:
     - mongo
networks:
  default:

Then, I started to migrate to Kubernetes using Minikube (on top of VMWare) generating the following manifest YAML:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    name: mongo-claim0
  name: mongo-claim0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    name: mongo
  name: mongo
spec:
  ports:
  - name: "30001"
    port: 30001
    targetPort: 27017
  selector:
    name: mongo
status:
  loadBalancer: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    name: mongo
  name: mongo
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: mongo
    spec:
      containers:
      - image: mongo
        name: mongo
        ports:
        - containerPort: 27017
        resources: {}
        volumeMounts:
        - mountPath: /data/db
          name: mongo-claim0
      restartPolicy: Always
      volumes:
      - name: mongo-claim0
        persistentVolumeClaim:
          claimName: mongo-claim0
status: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    name: myapp
  name: myapp
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: myapp
    spec:
      containers:
        image: myapp:0.1.0
        name: myapp
        resources: {}
      restartPolicy: Always
status: {}

My PODs/services are running fine (kubectl get pods,svc):

NAME                                 READY   STATUS             RESTARTS   AGE
pod/mongo-5d89cc6f7f-c2p4b           1/1     Running            0          43m
pod/myapp-6c9c4f89fb-gg68t           1/1     Running            0          43m

NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
service/kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP           110m
service/mongo           NodePort    10.101.179.59    <none>        27017:30001/TCP   43m

I also, can't connect via Robo3T (MongoDB client) thought port 30001 as I could using docker-compose (even using the vboxnet0 IP 192.168.99.1).

I don't know what could be the problem.

UPDATE 1: I found out Kompose created a ClusterIP and transfered the port for inside the cluster to 30001, but that needed to be the port to connect from outside the cluster. I changed the services to a NodePort as follows:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: mongo
  name: mongo
spec:
  type: NodePort 
  ports:
    - port: 27017
      nodePort: 30001
      targetPort: 27017
  selector:
    name: mongo
status:
  loadBalancer: {}

Now myapp pod is able to connect to mongo, but I still can't connect using Robo3t to the cluster via VMWare IP (192.168.99.1) and port 30001.

-- staticdev
kompose
kubernetes
minikube
mongodb

1 Answer

4/11/2019

To make it work, you have to call minikube service <servicename>, in my case the servicename is mongo.

-- staticdev
Source: StackOverflow