在Kubernetes中运行复制的MongoDB 4.2:未收到replset配置 [英] Running replicated MongoDB 4.2 in Kubernetes: replset config not being received

查看:83
本文介绍了在Kubernetes中运行复制的MongoDB 4.2:未收到replset配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在AWS上运行Kubernetes集群,需要配置复制的MongoDB 4.2数据库. 我正在使用StatefulSets,以便其他Pod(例如REST API NodeJS Pod)轻松连接到mongo实例(例如dsn:"mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2. mongo:27017/app).

I'm running a Kubernetes cluster on AWS and need to configure a replicated MongoDB 4.2 Database. I'm using StatefulSets in order for other Pods (e.g., REST API NodeJS Pod) to easily connect to the mongo instances (example dsn: "mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo:27017/app").

mongo-configmap.yaml(提供了一个shell脚本,可在创建mongo容器时执行复制初始化):

mongo-configmap.yaml (provides a shell script to perform the replication initialization upon mongo container creation):

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongo-init
data:
  init.sh: |
    #!/bin/bash
    # wait for the readiness health check to pass 
    until ping -c 1 ${HOSTNAME}.mongo; do
      echo "waiting for DNS (${HOSTNAME}.mongo)..."
      sleep 2
    done
    until /usr/bin/mongo --eval 'printjson(db.serverStatus())'; do
      echo "connecting to local mongo..."
      sleep 2
    done
    echo "connected to local."
    HOST=mongo-0.mongo:27017
    until /usr/bin/mongo --host=${HOST} --eval 'printjson(db.serverStatus())'; do
      echo "connecting to remote mongo..."
      sleep 2
    done
    echo "connected to remote."
    if [[ "${HOSTNAME}" != 'mongo-0' ]]; then
      until /usr/bin/mongo --host=${HOST} --eval="printjson(rs.status())" \
            | grep -v "no replset config has been received"; do
        echo "waiting for replication set initialization"
        sleep 2
      done
      echo "adding self to mongo-0"
      /usr/bin/mongo --host=${HOST} --eval="printjson(rs.add('${HOSTNAME}.mongo'))"
    fi
    if [[ "${HOSTNAME}" == 'mongo-0' ]]; then
      echo "initializing replica set"
      /usr/bin/mongo --eval="printjson(rs.initiate(\
          {'_id': 'rs0', 'members': [{'_id': 0, \
           'host': 'mongo-0.mongo:27017'}]}))"
    fi
    echo "initialized"
    while true; do
      sleep 3600
    done

mongo-service.yaml:

mongo-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    app: mongo
spec:
  clusterIP: None
  ports:
  - port: 27017
  selector:
    app: mongo

mongo-statefulset.yaml(一个Pod内有2个容器,一个用于实际DB,另一个用于初始化复制):

mongo-statefulset.yaml (2 containers inside one Pod, 1 for the actual DB, the other for initialization of the replication):

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
  labels:
    app: mongo
spec:
  selector:
    matchLabels:
      app: mongo
  serviceName: "mongo"
  replicas: 3
  template:
    metadata:
      labels:
        app: mongo
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mongodb
        image: mongo:4.2
        command:
        - mongod
        args:
        - --replSet
        - rs0
        - "--bind_ip_all"
        ports:
        - containerPort: 27017
          name: web
        volumeMounts:
        - name: database
          mountPath: /data/db
        livenessProbe:
          exec:
            command:
            - /usr/bin/mongo
            - --eval
            - db.serverStatus()
          initialDelaySeconds: 10
          timeoutSeconds: 10
      - name: init-mongo
        image: mongo:4.2
        command:
        - bash
        - /config/init.sh
        volumeMounts:
        - name: config
          mountPath: /config
      volumes:
      - name: config
        configMap:
          name: "mongo-init"
  volumeClaimTemplates:
  - metadata:
      name: database
      annotations:
        volume.beta.kubernetes.io/storage-class: mongodb-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 5Gi

应用这些配置后,三个mongo pod会开始运行(mongo-0,mongo-1,mongo-2). 但是,其他吊舱无法连接到这些mongo吊舱. 进一步查看mongo-0 Pod(应该是主实例)后,发现复制无效.

After applying these configurations, the 3 mongo pods start running (mongo-0, mongo-1, mongo-2). However, other pods can't connect to these mongo pods. Further look into the mongo-0 pods (should be primary instance) reveals that the replication did not work.

kubectl exec -it mongo-0-/bin/bash

kubectl exec -it mongo-0 -- /bin/bash

然后运行"mongo"以启动mongo shell,并在mongo shell中输入"rs.status()"将得到以下输出:

Then running 'mongo' to start the mongo shell, and entering 'rs.status()' into the mongo shell results in the following output:

{
    "info" : "run rs.initiate(...) if not yet done for the set",
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94,
    "codeName" : "NotYetInitialized"
}

推荐答案

显然,mongo-4.x映像未安装"ping",因此,脚本的其余部分未执行. 将这两行添加到mongo-configmap.yaml中的脚本可解决此问题:

Apparently, mongo-4.x images do not come with 'ping' installed, therefore, the rest of the script was not executed. Adding these two lines to the script in mongo-configmap.yaml fixes the problem:

apt-get update
apt-get install iputils-ping --yes

这篇关于在Kubernetes中运行复制的MongoDB 4.2:未收到replset配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆