docker搭建mongo分片集群(docker-compose)

本文是对上一篇文章(docker搭建mongo分片集群)的进一步整理,简化mongdb分片集群的配置
docker-compose.yml文件内容如下:

version: '3'
services:
    # 分片集群,两个节点
    shard_server11:
        container_name: shard_server11
        image: mongo
        networks:
            mongo:
                ipv4_address: 172.20.0.11
        ports:
            - 27018:27018
        volumes:
            - /opt/mongo/shard_server11/data/db:/data/db
            - /opt/mongo/shard_server11/data/configdb:/data/configdb
            - /opt/mongo/shard_server11/data/backup:/data/backup
        command: --shardsvr --bind_ip_all
        restart: always
        depends_on:
            - rs_config_server1
            - rs_config_server2

    shard_server21:
        container_name: shard_server21
        networks:
            mongo:
                ipv4_address: 172.20.0.12
        image: mongo
        ports:
            - 27028:27018
        volumes:
            - /opt/mongo/shard_server21/data/db:/data/db
            - /opt/mongo/shard_server21/data/configdb:/data/configdb
            - /opt/mongo/shard_server21/data/backup:/data/backup
        command: --shardsvr --bind_ip_all
        restart: always
        depends_on:
            - rs_config_server1
            - rs_config_server2


    # 配置服务器集群两个节点(mongodb3.4之后的版本需要两个config_server)
    rs_config_server1:
        container_name: rs_config_server1
        networks:
            mongo:
                ipv4_address: 172.20.0.13
        image: mongo
        volumes:
            - /opt/mongo/config_server1/data/db:/data/db
            - /opt/mongo/config_server1/data/configdb:/data/configdb
        command: --configsvr --replSet "rs_config_server" --bind_ip_all
        restart: always

    rs_config_server2:
        container_name: rs_config_server2
        networks:
            mongo:
                ipv4_address: 172.20.0.14
        image: mongo
        volumes:
            - /opt/mongo/config_server2/data/db:/data/db
            - /opt/mongo/config_server2/data/configdb:/data/configdb
        command: --configsvr --replSet "rs_config_server" --bind_ip_all
        restart: always

    

    # 路由节点mongos
    mongos:
        container_name: mongos
        networks:
            mongo:
                ipv4_address: 172.20.0.15
        image: mongo
        ports:
            - 27017:27017
        volumes:
            - /opt/mongo/mongos/data/db:/data/db
            - /opt/mongo/mongos/data/configdb:/data/configdb
        entrypoint: mongos
        command: --configdb rs_config_server/172.20.0.13:27019,172.20.0.14:27019 --bind_ip_all
        depends_on:
            - shard_server11
            - shard_server21

networks:
    mongo:
        driver: bridge
        ipam:
            config:
                - subnet: 172.20.0.10/24

通过compose启动集群

docker-compose up -d

配置服务器设置(config_server)

连接到任意一台配置服务器,执行以下命令进行设置

docker exec -it rs_config_server1 /bin/bash

mongo --host localhost --port 27019

rs.initiate({
    _id: "rs_config_server",
    configsvr: true,
    members: [
        { _id : 0, host : "172.20.0.13:27019" },
        { _id : 1, host : "172.20.0.14:27019" }
    ]
});

配置mongos

docker exec -it mongos /bin/bash

mongo --host localhost --port 27017

将分片集群添加到mongos中

sh.addShard("172.20.0.11:27018")
sh.addShard("172.20.0.12:27018")

到此整个集群就已经配置完成了
剩下的是事情就是对具体的collection进行分片设置了

你可能感兴趣的:(docker搭建mongo分片集群(docker-compose))