docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > Docker部署MongoDB Cluster

使用Docker部署MongoDB Cluster实践

作者:WalleZhang

本文详细介绍了如何使用Docker和mongoDB官方镜像搭建一个四节点的MongoDB分片集群,包括环境准备、配置文件设置、启动节点、初始化Replica Set和Shard,以及增加Shard节点和配置Sharding,最后,总结了整个搭建过程

环境准备

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

启动前准备工作

openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile

初始化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参数

启动节点

初始化

利用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节点

启动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" } )

参考资料:

官方资料:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文