将mongodb从docker迁移到kubernetes

时间:2019-05-21 17:52:37

标签: mongodb docker kubernetes

目前,我有一个mongo集群,其中有两个从属服务器,一个主服务器在docker host1:27017host2:27018host3:27018上运行,当前版本为3.4.1

我的想法是在我的k8s中添加一个副本集,等待同步,添加第二个副本集,等待同步,添加第三个副本集,然后杀死旧的副本集。

似乎并不简单,首先我可以从3.4.1迁移到4.0.9还是复制有问题?

我在本地做了一些测试

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

但是我有以下错误

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

如果您有任何想法,或者让我知道我是否朝着正确的方向发展:)

1 个答案:

答案 0 :(得分:0)

如果您不熟悉mongodb-replicaset,我建议直接使用mongodb-replicaset helm charts

对我来说,mongodb-replicaset helm charts的配置已经完成。因此,我只创建了statefulset,然后手动创建了副本集。

首先,创建statefuleset和服务。

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: {}

然后,附加到mongo-rs-pod之一,登录mongo shell,执行:

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" }
]})