环境准备
两台服务器,建立 Docker Swarm 集群,一个 Manager,一个 Worker。
docker 版本:18-06
mongo 版本:3.6
172.16.2.30 server01
172.16.2.31 server02
MongoDB 集群架构设计
搭建集群
Manager节点创建集群网络
1 2
| docker network create -d overlay --attachable mongo
|
所有节点创建相关文件夹
1 2
| mkdir -p /root/mongo/config /root/mongo/shard1 /root/mongo/shard2 /root/mongo/shard3
|
Manager节点创建 stack.yml
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| version: '3.3' services: mongors1n1: # docker 中国的镜像加速地址 image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard1 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard1:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: # 指定在服务器 server01 上启动 constraints: - node.hostname==server01 mongors2n1: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard2 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard2:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 mongors3n1: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard3 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard3:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 mongors1n2: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard1 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard1:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 mongors2n2: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard2 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard2:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 mongors3n2: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard3 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard3:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 mongors1n3: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard1 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard1:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server02 mongors2n3: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard2 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard2:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server02 mongors3n3: image: registry.docker-cn.com/library/mongo command: mongod --shardsvr --replSet shard3 --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/shard3:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server02 cfg1: image: registry.docker-cn.com/library/mongo command: mongod --configsvr --replSet cfgrs --smallfiles --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/config:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 cfg2: image: registry.docker-cn.com/library/mongo command: mongod --configsvr --replSet cfgrs --smallfiles --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/config:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 cfg3: image: registry.docker-cn.com/library/mongo command: mongod --configsvr --replSet cfgrs --smallfiles --dbpath /data/db --port 27017 networks: - mongo volumes: - /etc/localtime:/etc/localtime - /root/mongo/config:/data/db deploy: restart_policy: condition: on-failure replicas: 1 placement: constraints: - node.hostname==server01 mongos: image: registry.docker-cn.com/library/mongo # mongo 3.6 版默认绑定IP为 127.0.0.1,此处绑定 0.0.0.0 是允许其他容器或主机可以访问 command: mongos --configdb cfgrs/cfg1:27017,cfg2:27017,cfg3:27017 --bind_ip 0.0.0.0 --port 27017 networks: - mongo # 映射宿主机的 27017 端口 ports: - 27017:27017 volumes: - /etc/localtime:/etc/localtime depends_on: - cfg1 - cfg2 - cfg3 deploy: restart_policy: condition: on-failure # 在集群内的每一台服务器上都启动一个容器 mode: global networks: mongo: external: true
|
启动服务,在Manager节点上执行
1
| docker stack deploy -c stack.yml mongo
|
Manager节点查看服务的启动情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| docker service ls
正常情况下,会出现如下结果:
[root@server01 mongo]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS itmtld6o6ldv mongo_cfg1 replicated 0/1 registry.docker-cn.com/library/mongo:latest brlsgen2xluh mongo_cfg2 replicated 0/1 registry.docker-cn.com/library/mongo:latest yx4rg2p21pyz mongo_cfg3 replicated 1/1 registry.docker-cn.com/library/mongo:latest kn4zlkff55ly mongo_mongors1n1 replicated 1/1 registry.docker-cn.com/library/mongo:latest 8siujq2xe4mt mongo_mongors1n2 replicated 0/1 registry.docker-cn.com/library/mongo:latest tv6epypyk65n mongo_mongors1n3 replicated 0/1 registry.docker-cn.com/library/mongo:latest ri7zzhp8v5c6 mongo_mongors2n1 replicated 1/1 registry.docker-cn.com/library/mongo:latest g9v90ifpupns mongo_mongors2n2 replicated 0/1 registry.docker-cn.com/library/mongo:latest t5ircpm49ojl mongo_mongors2n3 replicated 0/1 registry.docker-cn.com/library/mongo:latest 59o5883orn01 mongo_mongors3n1 replicated 1/1 registry.docker-cn.com/library/mongo:latest wvemmmjv7mx6 mongo_mongors3n2 replicated 0/1 registry.docker-cn.com/library/mongo:latest 5307by34ep0j mongo_mongors3n3 replicated 0/1 registry.docker-cn.com/library/mongo:latest bs0ols6tbijc mongo_mongos global 3/3 registry.docker-cn.com/library/mongo:latest *:27017->27017/tcp
|
初始化集群
Manager节点初始化 Mongo 配置集群
1
| docker exec -it $(docker ps | grep "cfg1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id: \"cfgrs\",configsvr: true, members: [{ _id : 0, host : \"cfg1\" },{ _id : 1, host : \"cfg2\" }, { _id : 2, host : \"cfg3\" }]})' | mongo"
|
Manager节点初始化三个 Mongo 数据集群
1 2 3 4 5 6
| docker exec -it $(docker ps | grep "mongors1n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard1\", members: [{ _id : 0, host : \"mongors1n1\" },{ _id : 1, host : \"mongors1n2\" },{ _id : 2, host : \"mongors1n3\", arbiterOnly: true }]})' | mongo"
docker exec -it $(docker ps | grep "mongors2n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard2\", members: [{ _id : 0, host : \"mongors2n1\" },{ _id : 1, host : \"mongors2n2\" },{ _id : 2, host : \"mongors2n3\", arbiterOnly: true }]})' | mongo"
docker exec -it $(docker ps | grep "mongors3n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard3\", members: [{ _id : 0, host : \"mongors3n1\" },{ _id : 1, host : \"mongors3n2\" },{ _id : 2, host : \"mongors3n3\", arbiterOnly: true }]})' | mongo"
|
Manager节点将三个数据集群当做分片加入 mongos
1 2 3 4 5
| docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard1/mongors1n1:27017,mongors1n2:27017,mongors1n3:27017\")' | mongo "
docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard2/mongors2n1:27017,mongors2n3:27017,mongors2n3:27017\")' | mongo "
docker exec -it $(docker ps | grep "mongos" | awk '{ print $1 }') bash -c "echo 'sh.addShard(\"shard3/mongors3n1:27017,mongors3n2:27017,mongors3n3:27017\")' | mongo "
|
连接集群
内部:在 mongo 网络下的容器,通过 mongos:27017 连接
外部:通过 IP:27017 连接,IP 可以为两台服务的中的一个的 IP
后期可以添加mongdodb可视化工具:mongodb-express