从 docker-compose 配置 MongoDB 副本集

Configuring MongoDB replica set from docker-compose

我正在使用 docker-compose 来启动 3 个 MongoDB 服务器,它们应该在一个副本集中。

我先启动3台MongoDB服务器,然后配置副本集。 这就是我在 bash script:

中进行副本集配置的方式
mongo --host 127.0.0.1:27017 <<EOF
var cfg = {
    "_id": "rs",
    "version": 1,
    "members": [
        {
            "_id": 0,
            "host": "127.0.0.1:27017",
            "priority": 1
        },

        // snip...

    ]
};
rs.initiate(cfg);
rs.reconfig(cfg)
EOF

我在这里尝试使用 docker-compose.

复制副本集的配置
# docker-compose.yml

mongosetup:
  image: mongo:3.0
  links:
    - mongo1:mongo1
  command: echo 'var cfg = { "_id": "rs", "version": 1, "members": [ { "_id": 0, "host": "127.0.0.1:27017", "priority": 1 }, { "_id": 1, "host": "mongo2:27017", "priority": 1 },{ "_id": 2, "host": "mongo2:27017", "priority": 1 } ] }; rs.initiate(cfg);' | mongo mongo1

不幸的是,这造成了这个错误:yaml.scanner.ScannerError: mapping values are not allowed here

推荐的方法是什么? 是否可以将 cfg 对象存储在 docker-compose 读取的单独文件中?

我通过将配置放在从入口点调用的 setup.sh 中解决了这个问题。

mongosetup:
  image: mongo:3.0
  links:
    - mongo1:mongo1
    - mongo2:mongo2
    - mongo3:mongo3
  volumes:
    - ./scripts:/scripts
  entrypoint: [ "/scripts/setup.sh" ]

setup.sh

#!/bin/bash

MONGODB1=`ping -c 1 mongo1 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`

mongo --host ${MONGODB1}:27017 <<EOF
   var cfg = {
        "_id": "rs",
        "version": 1,
        "members": [
            {
                "_id": 0,
                "host": "${MONGODB1}:27017",
[cut..]
EOF

我已经使用 docker-compose 创建了一个设置,它在副本集中启动了 3 个 MongoDB,并使用 mongodb-river 启动了 ElasticSearch。

可在 https://github.com/stabenfeldt/elastic-mongo

购买