[英]Configuring MongoDB replica set from docker-compose
I'm using docker-compose to start 3 MongoDB servers which should be in a replica set. 我正在使用docker-compose来启动3个应该在副本集中的MongoDB服务器。
I first start 3 MongoDB servers, then I configure the replica set. 我首先启动3个MongoDB服务器,然后配置副本集。 This is how I would do the replica set config in a bash script : 这就是我在bash脚本中执行副本集配置的方法:
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
Here I'm trying to replicate the configuring of the replica set using docker-compose . 在这里,我试图使用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
Unfortunately that creates this error: yaml.scanner.ScannerError: mapping values are not allowed here
. 不幸的是,这会产生此错误: yaml.scanner.ScannerError: mapping values are not allowed here
。
What's the recommended approach? 推荐的方法是什么? Is it possible to store the cfg object in a separate file that docker-compose reads? 是否可以将cfg对象存储在docker-compose读取的单独文件中?
I fixed the problem by putting the config in setup.sh which I called from entrypoint. 我通过将配置放在我从入口点调用的setup.sh来解决问题。
mongosetup:
image: mongo:3.0
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
volumes:
- ./scripts:/scripts
entrypoint: [ "/scripts/setup.sh" ]
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
I have created a setup with docker-compose that starts 3 MongoDBs in a replica set and ElasticSearch with mongodb-river. 我用docker-compose创建了一个设置,它在一个副本集中启动了3个MongoDB,在mongodb-river中启动了ElasticSearch。
It's available at https://github.com/stabenfeldt/elastic-mongo 它可以在https://github.com/stabenfeldt/elastic-mongo上找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.