部署
创建docker网桥
1
| docker network create mongo
|
创建认证文件(用于密码认证)
1 2 3
| openssl rand -base64 741 > /data/mongo-shard/key.file chmod 600 /data/mongo-shard/key.file chown 999 /data/mongo-shard/key.file
|
创建mongo-shard.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| version: '2' services: shard1: image: mongo:4.0.9 container_name: mongo_shard1 command: mongod --shardsvr --directoryperdb --replSet shard1 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/shard1:/data/db - /data/mongo-shard/key.file:/etc/key.file privileged: true mem_limit: 16000000000 networks: - mongo
shard2: image: mongo:4.0.9 container_name: mongo_shard2 command: mongod --shardsvr --directoryperdb --replSet shard2 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/shard2:/data/db - /data/mongo-shard/key.file:/etc/key.file privileged: true mem_limit: 16000000000 networks: - mongo
shard3: image: mongo:4.0.9 container_name: mongo_shard3 command: mongod --shardsvr --directoryperdb --replSet shard3 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/shard3:/data/db - /data/mongo-shard/key.file:/etc/key.file privileged: true mem_limit: 16000000000 networks: - mongo
config1: image: mongo:4.0.9 container_name: mongo_config1 command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/config1:/data/configdb - /data/mongo-shard/key.file:/etc/key.file networks: - mongo
config2: image: mongo:4.0.9 container_name: mongo_config2 command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/config2:/data/configdb - /data/mongo-shard/key.file:/etc/key.file networks: - mongo
config3: image: mongo:4.0.9 container_name: mongo_config3 command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/config3:/data/configdb - /data/mongo-shard/key.file:/etc/key.file networks: - mongo
mongos: image: mongo:4.0.9 container_name: mongo_mongos command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017 ports: - 27017:27017 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/key.file:/etc/key.file depends_on: - config1 - config2 - config3 networks: - mongo networks: mongo: external: true
|
初始化脚本deploy-mongo.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #!/bin/sh
docker-compose -f mongo-shard.yaml up -d
sleep 30s
docker-compose -f mongo-shard.yaml exec config1 bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"config1:27019\" },{ _id : 1, host : \"config2:27019\" }, { _id : 2, host : \"config3:27019\" }]})' | mongo --port 27019" docker-compose -f mongo-shard.yaml exec shard1 bash -c "echo 'rs.initiate({_id: \"shard1\",members: [{ _id : 0, host : \"shard1:27018\" }]})' | mongo --port 27018" docker-compose -f mongo-shard.yaml exec shard2 bash -c "echo 'rs.initiate({_id: \"shard2\",members: [{ _id : 0, host : \"shard2:27018\" }]})' | mongo --port 27018" docker-compose -f mongo-shard.yaml exec shard3 bash -c "echo 'rs.initiate({_id: \"shard3\",members: [{ _id : 0, host : \"shard3:27018\" }]})' | mongo --port 27018" docker-compose -f mongo-shard.yaml exec mongos bash -c "echo 'sh.addShard(\"shard1/shard1:27018\")' | mongo" docker-compose -f mongo-shard.yaml exec mongos bash -c "echo 'sh.addShard(\"shard2/shard2:27018\")' | mongo" docker-compose -f mongo-shard.yaml exec mongos bash -c "echo 'sh.addShard(\"shard3/shard3:27018\")' | mongo"
|
执行初始化脚本后,添加root用户
1 2 3 4 5 6 7 8 9
| use admin db.createUser( { user:"root", pwd:"123456", roles:[{role:"root",db:"admin"}] } )
|
修改mongo-shard.yaml,所有容器添加认证参数–keyFile “/etc/key.file”
重新up即可
最终mongo-shard.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| version: '2' services: shard1: image: mongo:4.0.9 container_name: mongo_shard1 command: mongod --shardsvr --keyFile "/etc/key.file" --directoryperdb --replSet shard1 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/shard1:/data/db - /data/mongo-shard/key.file:/etc/key.file privileged: true mem_limit: 16000000000 networks: - mongo
shard2: image: mongo:4.0.9 container_name: mongo_shard2 command: mongod --shardsvr --keyFile "/etc/key.file" --directoryperdb --replSet shard2 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/shard2:/data/db - /data/mongo-shard/key.file:/etc/key.file privileged: true mem_limit: 16000000000 networks: - mongo
shard3: image: mongo:4.0.9 container_name: mongo_shard3 command: mongod --shardsvr --keyFile "/etc/key.file" --directoryperdb --replSet shard3 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/shard3:/data/db - /data/mongo-shard/key.file:/etc/key.file privileged: true mem_limit: 16000000000 networks: - mongo
config1: image: mongo:4.0.9 container_name: mongo_config1 command: mongod --configsvr --keyFile "/etc/key.file" --directoryperdb --replSet fates-mongo-config --smallfiles volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/config1:/data/configdb - /data/mongo-shard/key.file:/etc/key.file networks: - mongo
config2: image: mongo:4.0.9 container_name: mongo_config2 command: mongod --configsvr --keyFile "/etc/key.file" --directoryperdb --replSet fates-mongo-config --smallfiles volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/config2:/data/configdb - /data/mongo-shard/key.file:/etc/key.file networks: - mongo
config3: image: mongo:4.0.9 container_name: mongo_config3 command: mongod --configsvr --keyFile "/etc/key.file" --directoryperdb --replSet fates-mongo-config --smallfiles volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/config3:/data/configdb - /data/mongo-shard/key.file:/etc/key.file networks: - mongo
mongos: image: mongo:4.0.9 container_name: mongo_mongos command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --keyFile "/etc/key.file" --bind_ip 0.0.0.0 --port 27017 ports: - 27017:27017 volumes: - /etc/localtime:/etc/localtime - /data/mongo-shard/key.file:/etc/key.file depends_on: - config1 - config2 - config3 networks: - mongo networks: mongo: external: true
|