使用Docker部署MongoDB Cluster

使用Docker部署MongoDB Cluster

环境准备

  • 四台服务器,分别命名为ServerA、ServerB、ServerC、ServerD
  • 2 Shard(1 Primary 1 Secondary 1 Arbiter) Nodes
  • 3 Config Nodes
  • 4 Router Nodes

Docker镜像

MongoDB官方镜像

docker-compose.yml

ServerA配置文件

version: '2'
services:
  configsrv:
    image: mongo
    command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
    volumes:
      - /data/configsrv_db:/data/configdb
    ports:
      - "27018:27017"
    restart:
      always
    container_name:
      configsrv
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  rs1_node:
    image: mongo
    command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
    volumes:
      - /data/rs1_node_db:/data/db
    ports:
      - "27019:27017"
    restart:
      always
    container_name:
      rs1_node
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  router:
    image: mongo
    command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
    ports:
      - "27017:27017"
    volumes:
      - /data/router_db:/data/db
    restart:
      always
    container_name:
      router
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

ServerB配置文件

version: '2'
services:
  configsrv:
    image: mongo
    command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
    volumes:
      - /data/configsrv_db:/data/configdb
    ports:
      - "27018:27017"
    restart:
      always
    container_name:
      configsrv
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  rs1_node:
    image: mongo
    command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
    volumes:
      - /data/rs1_node_db:/data/db
    ports:
      - "27019:27017"
    restart:
      always
    container_name:
      rs1_node
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  rs2_arbiter:
    image: mongo
    command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs2 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
    volumes:
      - /data/rs2_arbiter_db:/data/db
    ports:
      - "27020:27017"
    restart:
      always
    container_name:
      rs2_arbiter
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  router:
    image: mongo
    command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
    ports:
      - "27017:27017"
    volumes:
      - /data/router_db:/data/db
    restart:
      always
    container_name:
      router
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

ServerC配置文件

version: '2'
services:
  configsrv:
    image: mongo
    command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
    volumes:
      - /data/configsrv_db:/data/configdb
    ports:
      - "27018:27017"
    restart:
      always
    container_name:
      configsrv
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  rs2_node:
    image: mongo
    command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
    volumes:
      - /data/rs2_node_db:/data/db
    ports:
      - "27019:27017"
    restart:
      always
    container_name:
      rs2_node
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  rs1_arbiter:
    image: mongo
    command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs1 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
    volumes:
      - /data/rs1_arbiter_db:/data/db
    ports:
      - "27020:27017"
    restart:
      always
    container_name:
      rs1_arbiter
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  router:
    image: mongo
    command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
    ports:
      - "27017:27017"
    volumes:
      - /data/router_db:/data/db
    restart:
      always
    container_name:
      router
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

ServerD配置文件

version: '2'
services:
  rs2_node:
    image: mongo
    command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
    volumes:
      - /data/rs2_node_db:/data/db
    ports:
      - "27019:27017"
    restart:
      always
    container_name:
      rs2_node
    ulimits:
      nofile:
        soft: 300000
        hard: 300000
  router:
    image: mongo
    command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
    ports:
      - "27017:27017"
    volumes:
      - /data/router_db:/data/db
    restart:
      always
    container_name:
      router
    ulimits:
      nofile:
        soft: 300000
        hard: 300000

启动前准备工作

  • 创建mongodb-keyfile文件
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
  • 创建宿主机的volume文件夹

初始化Config节点

重要:在初始化启动前需要去掉docker-compose.yml配置文件中的--keyFile参数

启动节点

在ServerA、ServerB和ServerC三台服务器上运行命令:
docker-compose up -d configsrv

初始化

利用mongo连接到ServerA节点,输入以下命令创建管理用户:

use admin

db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]  })

db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ]  })

初始化ReplicaSet信息

rs.initiate(
  {
    _id: "configrs",
    configsvr: true,
    members: [
      { _id : 0, host : "ServerA:27018" },
      { _id : 1, host : "ServerB:27018" },
      { _id : 2, host : "ServerC:27018" }
    ]
  }
)

初始化Shard1节点

重要:在初始化启动前需要去掉docker-compose.yml配置文件中的--keyFile参数

启动节点

在ServerA和ServerB两台服务器上运行命令:docker-compose up -d rs1_node

在ServerC服务器上运行命令:docker-compose up -d rs1_arbiter

初始化

利用mongo连接到ServerA节点,创建管理用户

use admin

db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]  })

db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ]  })

初始化ReplicaSet信息

rs.initiate(
  {
    _id : "rs1",
    members: [
      { _id : 0, host : "ServerA:27019" },
      { _id : 1, host : "ServerB:27019" }
    ]
  }
)

增加Arbiter节点

rs.addArb("ServerC:27020")

查看rs状态:rs.status()

初始化Shard2节点

与Shard1节点雷同,只需要修改对应的服务器IP

重启Config和Shard节点

取消--keyFile参数的注释,删掉上述创建的所有container

利用docker-compose再次启动上面所有节点

启动Router节点

使用命令docker-compose up -d router在四台服务器上启动路由节点

配置Cluster

增加Shard节点

使用mongo连接到任意一台服务器的router节点,然后执行以下命令将Shard节点加入到当前Cluster中

use admin
db.auth("<username>","<password>")
sh.addShard("rs1/ServerA:27019")
sh.addShard("rs2/ServerD:27019")

启动Sharding

在对collection进行sharding之前一定要先对数据库启动sharding

sh.enableSharding("<database>")
sh.shardCollection( "<database>.<collection>", { _id : "hashed" } )

参考资料

官方资料:
Deploy a Sharded Cluster
Hashed Sharding

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值