简体   繁体   中英

What is the Userid and password for the mongo db

I am doing this exercise on my vagrant built bare metal cluster on a windows machine.

Was able to successfully run the app.

留言簿应用

But I am not able to connect to the database to see the data, say from mongo db compass.

使用指南针连接到 mongodb

What should be the user id or password for this?

After a bit of research, I used the following steps to get into the mongo container and verify the data. But I want to connect to the database using a client like compass.

Used the following command to find where the mongo db backend database pod is deployed.

vagrant@kmasterNew:~/GuestBookMonog$ kubectl get pods -o wide
NAME                       READY   STATUS    RESTARTS   AGE     IP              NODE          NOMINATED NODE   READINESS GATES
frontend-848d88c7c-95db6   1/1     Running   0          4m51s   192.168.55.11   kworkernew2   <none>           <none>
mongo-75f59d57f4-klmm6     1/1     Running   0          4m54s   192.168.55.10   kworkernew2   <none>           <none>

Then ssh into that node and did

docker container ls 

to find the mongo db container

It looks something like this. I removed irrelevant data.

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS     NAMES
1ba3d05168ca   dc77715107a9             "docker-entrypoint.s…"   53 minutes ago   Up 53 minutes             k8s_mongo_mongo-75f59d57f4-5tw5b_default_eeddf81b-8dde-4c3e-8505-e08229f97c8b_0      

A reference from SO

docker exec -it 1ba3d05168ca bash

Another reference from SO in this context

mongo
show dbs
use guestbook
show collections
db.messages.find()

Finally I was able to verify the data

> db.messages.find()
{ "_id" : ObjectId("6097f6c28088bc17f61bdc32"), "message" : ",message1" }
{ "_id" : ObjectId("6097f6c58088bc17f61bdc33"), "message" : ",message1,message2" }

But the question is how can I see this data from mongo db compass? I am exposing the both the frontend as well as the backend services using NodePort type. You can see them below.

The follow are the k8s manifest files for the deployment that I got from the above example .

Front end deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  labels:
    app.kubernetes.io/name: guestbook
    app.kubernetes.io/component: frontend
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: guestbook
      app.kubernetes.io/component: frontend
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: guestbook
        app.kubernetes.io/component: frontend
    spec:
      containers:
      - name: guestbook
        image: paulczar/gb-frontend:v5
        # image: gcr.io/google-samples/gb-frontend:v4
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 80

The front end service.

apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app.kubernetes.io/name: guestbook
    app.kubernetes.io/component: frontend
spec:
  # if your cluster supports it, uncomment the following to automatically create
  # an external load-balanced IP for the frontend service.
  # type: LoadBalancer
  type: NodePort
  ports:
  - port: 80
    nodePort: 30038
  # - targetPort: 80
  #   port: 80
  #   nodePort: 30008

  selector:
    app.kubernetes.io/name: guestbook
    app.kubernetes.io/component: frontend

Next the mongo db deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  labels:
    app.kubernetes.io/name: mongo
    app.kubernetes.io/component: backend
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: mongo
      app.kubernetes.io/component: backend
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mongo
        app.kubernetes.io/component: backend
    spec:
      containers:
      - name: mongo
        image: mongo:4.2
        args:
          - --bind_ip
          - 0.0.0.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 27017

Finally the mongo service

apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    app.kubernetes.io/name: mongo
    app.kubernetes.io/component: backend
spec:
  ports:
  - port: 27017
    targetPort: 27017
    nodePort: 30068
  type: NodePort
  selector:
    app.kubernetes.io/name: mongo
    app.kubernetes.io/component: backend

Short answer: there isn't one.

Long answer: you are using the mongo image, do you can pull up the readme for that on https://hub.docker.com/_/mongo . That shows that authentication is disabled by default and must be manually enabled via --auth as a command line argument. When doing that, you can specific the initial auth configuration via environment variables and then more complex stuff in the referenced.d/ folder.

Found it. Its pretty simple.

The connection string would be

mongodb://192.62.62.100:30068

And this is how it looks.

We need to select Authentication none. Here 30068 is the node port of the mongo db service.

Mongo db 指南针连接

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM