使用docker-compose部署MongoDB shard-cluster

主要参考官网文档:Deploy a Sharded Cluster — MongoDB Manual

1. docker镜像

可以使用官方提供的镜像,也可以自己做。

我自己做了一个,只要将从官网下载的二进制文件打入镜像,再下载一个依赖libcurl4即可。

FROM ubuntu:22.04
add bin/ /usr/bin
add sources.list /etc/apt
run apt update && apt install -y libcurl4
VOLUME /data/db
ENTRYPOINT ["/usr/bin/mongod"]

2. 集群架构

如上图所示,为简化部署,mongos和config节点都使用单节点,部署了两个用作分片的replica-set,分别命名为RS0和RS1。各使用两个节点做高可用。

3. 部署容器

3.1 docker-compose文件

涉及到6个docker容器,所以决定使用docker-compose来编排部署,

由于构建rs时需要录入每个节点的地址,这里给每个容器都事先分配好了地址,这样配置的时候直接使用即可。

version: "3.8"
services:
  configserver:
    image: mongodb/mongodb-community-server:latest
    volumes:
    - type: volume
      source: configserver
      target: /data/configdb
      read_only: false
    command: ["mongod", "--configsvr", "--replSet", "configRS", "--dbpath", "/data/configdb", "--bind_ip_all"]
    networks:
      shard_cluster:
        ipv4_address: 192.168.31.201
  rs00:
    image: mongodb/mongodb-community-server:latest
    volumes:
    - type: volume
      source: data00
      target: /data/db
    networks:
      shard_cluster:
        ipv4_address: 192.168.31.211
    command: ["mongod", "--shardsvr", "--replSet", "RS0", "--dbpath", "/data/db", "--bind_ip_all"]
  rs01:
    image: mongodb/mongodb-community-server:latest
    volumes:
    - type: volume
      source: data01
      target: /data/db
    networks:
      shard_cluster:
        ipv4_address: 192.168.31.212
    command: ["mongod", "--shardsvr", "--replSet", "RS0", "--dbpath", "/data/db", "--bind_ip_all"]
  rs10:
    image: mongodb/mongodb-community-server:latest
    volumes:
    - type: volume
      source: data10
      target: /data/db
    networks:
      shard_cluster:
        ipv4_address: 192.168.31.213
    command: ["mongod", "--shardsvr", "--replSet", "RS1", "--dbpath", "/data/db", "--bind_ip_all"]
  rs11:
    image: mongodb/mongodb-community-server:latest
    volumes:
    - type: volume
      source: data11
      target: /data/db
    networks:
      shard_cluster:
        ipv4_address: 192.168.31.214
    command: ["mongod", "--shardsvr", "--replSet", "RS1", "--dbpath", "/data/db", "--bind_ip_all"]
  mongos:
    image: mongodb/mongodb-community-server:latest
    command: ["mongos", "--configdb", "configRS/192.168.31.201:27019", "--bind_ip_all"]
    ports:
      - target: 27017
        published: 27017
        protocol: tcp
    networks:
      shard_cluster:
        ipv4_address: 192.168.31.221
networks:
  shard_cluster:
    ipam:
      config:
        - subnet: 192.168.31.0/24
volumes:
  configserver:
  data00:
  data01:
  data10:
  data11:

3.2 部署容器

执行命令即可部署

docker-compose up -d

3.3 配置集群

配置configserver

$docker exec -it mongo_cluster_configserver_1 mongosh --port 27019
test> rs.initiate({_id: "configRS", configsvr: true, members:[{_id: 0, host: "192.168.31.201:27019"}]})

配置数据节点

$ docker-compose exec rs00   mongosh --port 27018
test> rs.initiate({_id: "RS0", members: [{_id:0, "host": "192.168.31.211:27018"},{_id:1, host: "192.168.31.212:27018"}]})

$ docker-compose exec rs10   mongosh --port 27018
test> rs.initiate({_id: "RS1", members: [{_id:0, "host": "192.168.31.213:27018"},{_id:1, host: "192.168.31.214:27018"}]})

配置mongos

$ docker-compose exec mongos   mongosh --port 27017
[direct: mongos] test> sh.addShard( "RS0/192.168.31.211:27018,192.168.31.212:27018")
{
  shardAdded: 'RS0',
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1696212363, i: 5 }),
    signature: {
      hash: Binary.createFromBase64("AAAAAAAAAAAAAAAAAAAAAAAAAAA=", 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1696212363, i: 5 })
}
[direct: mongos] test> sh.addShard( "RS1/192.168.31.213:27018,192.168.31.214:27018")
{
  shardAdded: 'RS1',
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1696212377, i: 13 }),
    signature: {
      hash: Binary.createFromBase64("AAAAAAAAAAAAAAAAAAAAAAAAAAA=", 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1696212377, i: 3 })
}

验证功能

1. 在集合上开启分片
sh.shardCollection("test.testc", { _id : "hashed" } )
2. 在集合插入数据
for(i=0; i<1000;i++) {db.testc.insertOne({"name": "User_" + i});}
3. 到单个rs上查看
$ docker-compose exec rs00   mongosh --port 27018
RS0 [direct: primary] test> db.testc.countDocuments()
531

可以看到单个rs上存放了约一半的数据,分片生效
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值