2

I want to create MongoDB replica of the three machines, it needs to specify the IP-addresses of these machines? But they run into a pod's and have a dynamic IP. If you try to specify the DNS-name service MongoDB he says

No host described in new configuration XXXXX for replica set app_replica maps to this node

How to configure MongoDB replica for c k8s?

I use DNS-addons for k8s. And I try to initialize the cluster as follows:

var config = {
    "_id" : "app_replica",
    "members" : [
        {
            "_id" : 0,
            "host" : "mongodb-node-01"
        },
        {
            "_id" : 1,
            "host" : "mongodb-node-02"
        },
        {
            "_id" : 2,
            "host" : "mongodb-node-03",
            "arbiterOnly" : true
        }
    ]
}

rs.initiate(config)

Config Service:

apiVersion: v1
kind: Service
metadata:
  name: "mongodb-node-01"
  labels:
    app: "mongodb-node-01"
spec:
  ports:
  - port: 27017
    targetPort: 27001
  selector:
    app: "mongodb-node-01"

Config Replication Controller:

apiVersion: v1
kind: ReplicationController
metadata:
  name: "mongodb-node-01"
  labels:
    app: "mongodb-node-01"
spec:
  replicas: 1
  selector:
    app: "mongodb-node-01"
  template:
    metadata:
      labels:
        app: "mongodb-node-01"
    spec:
      containers:
      - name: "mongodb-node-01"
        image: 192.168.0.139:5000/db/mongo
        command:
          - mongod
          - "--replSet"
          - "app_replica"
          - "--smallfiles"
          - "--noprealloc"
        env:
        - name: ENV
          value: "prod"
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
          readOnly: false
      volumes:
      - name: mongo-persistent-storage
        hostPath:
          path: /data/mongo/mongodb-node-01
      nodeSelector:
        database: "true"
        mongodb01: "true"

2 Answers 2

1

You need to setup multiple deployments and services. Take a look at this zookeeper example - https://gist.github.com/bprashanth/8160d0cf1469b4b125af95f697433934

You do not rely on node/machine IPs. Instead you rely on stable DNS names of multiple services.

Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at https://www.mongodb.com/blog/post/running-mongodb-as-a-microservice-with-docker-and-kubernetes, there is a great pdf tutorial.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.