Migrate mongodb from docker to kubernetes

5/21/2019

At the moment I have a mongo cluster with two slaves and a master runing on docker host1:27017, host2:27018, host3:27018 the current version is 3.4.1

My idea was to add one replicaset into my k8s infra, wait for sync, add a second one, wait for sync, add a third one and then kill the old ones.

Not seems that simple, first of all can I do this migrating from 3.4.1 to 4.0.9 or will I have problems with the replication ?

I made some tests in local doing

docker run -p 27017:27017 mongo:4.0 mongod --replSet rs0  --bind_ip localhost,host1

But I have the folowing error

Failed to set up listener: SocketException: Cannot assign requested address

If you have any idea how to do that, or just let me know if I am going into the good direction :)

-- Ajouve
docker
kubernetes
mongodb

1 Answer

5/22/2019

If you are not familar with mongodb-replicaset, I recommend to use mongodb-replicaset helm charts directly.

For me, the configuration of mongodb-replicaset helm charts is too completed. So I just create statefulset, then init replicaset manually.

First, create statefuleset and service.

apiVersion: v1
kind: Service
metadata:
  name: mongo-rs-headless
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app: mongo-rs-pod
  ports:
  - name: http
    port: 27017
    protocol: TCP
    targetPort: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mongo-rs-sts
spec:
  serviceName: mongo-rs-headless
  replicas: 3
  updateStrategy:
    type: OnDelete
  template:
    metadata:
      labels:
        app: mongo-rs-pod
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: compute
        image: mongo:3-stretch
        imagePullPolicy: IfNotPresent
        command:
        - mongod
        - --dbpath=/data/db
        - --port=27017
        - --bind_ip=0.0.0.0
        - --replSet=rs1
        ports:
        - name: mongo
          containerPort: 27017
        volumeMounts:
        - name: mongo-data
          mountPath: /data/db
        livenessProbe:
          exec:
            command:
            - mongo
            - --eval
            - "db.adminCommand('ping')"
          initialDelaySeconds: 30
          timeoutSeconds: 5
          failureThreshold: 3
          periodSeconds: 30
          successThreshold: 1
        readinessProbe:
          exec:
            command:
            - mongo
            - --eval
            - "db.adminCommand('ping')"
          initialDelaySeconds: 5
          timeoutSeconds: 1
          failureThreshold: 3
          periodSeconds: 20
          successThreshold: 1
  volumes:
  - name: mongo-data
    emptyDir: {}

Then, attach into one of the mongo-rs-pod, login into mongo shell, execute:

rs.initiate({_id: "rs1", members: [
  { _id : 0, host : "mongo-rs-pod-0.mongo-rs-headless.default.svc.cluster.local" },
  { _id : 1, host : "mongo-rs-pod-1.mongo-rs-headless.default.svc.cluster.local" },
  { _id : 2, host : "mongo-rs-pod-2.mongo-rs-headless.default.svc.cluster.local" }
]})
-- menya
Source: StackOverflow