使用docker-compose搭建mongdb分片3主机

Posted gsls200808

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用docker-compose搭建mongdb分片3主机相关的知识,希望对你有一定的参考价值。

host1ip:192.168.3.55

host2ip:192.168.3.56

host3ip:192.168.3.57

初始化docker-compose目录

创建docker-compose主文件夹

mkdir -p /root/iatc/commpwr
cd /root/iatc/commpwr
mkdir data mongos shard1 shard2 shard3

生成key.file,在一台机器生成,其他机器拷贝

openssl rand -base64 756 > key.file
chmod chmod 400 key.file

将key.file拷贝到其他机器后都需要重新修改权限为400

chmod chmod 400 key.file

编辑docker-compose.yml文件,内容如下

 version: '3'
 services:
     rs_config_server:
          container_name: 'rs_config_server'
          image: 'mongo:latest'
          restart: always
          volumes:
          - $PWD/data/db:/data/db 
          - $PWD/data/configdb:/data/configdb 
          - $PWD/key.file:/data/mongodb/key.file
          ports:
          - 27019:27019
          command: --configsvr --replSet "rs_config_server" --bind_ip_all
     rs_shard_server_01:
          container_name: 'rs_shard_server_01'
          image: 'mongo:latest'
          restart: always
          volumes:
          - $PWD/shard1/db:/data/db  
          - $PWD/shard1/configdb:/data/configdb
          - $PWD/key.file:/data/mongodb/key.file
          ports:
          - 27118:27018
          command: --shardsvr --replSet "rs_shard_server_01" --bind_ip_all 
     rs_shard_server_02:
          container_name: 'rs_shard_server_02'
          image: 'mongo:latest'
          restart: always
          volumes:
          - $PWD/shard2/db:/data/db  
          - $PWD/shard2/configdb:/data/configdb
          - $PWD/key.file:/data/mongodb/key.file
          ports:
          - 27218:27018
          command: --shardsvr --replSet "rs_shard_server_02" --bind_ip_all 
     rs_shard_server_03:
          container_name: 'rs_shard_server_03'
          image: 'mongo:latest'
          restart: always
          volumes:
          - $PWD/shard3/db:/data/db  
          - $PWD/shard3/configdb:/data/configdb
          - $PWD/key.file:/data/mongodb/key.file
          ports:
          - 27318:27018
          command: --shardsvr --replSet "rs_shard_server_03" --bind_ip_all 
     rs_mongos_server:
          container_name: 'rs_mongos_server'
          image: 'mongo:latest'
          restart: always
          volumes:
          - $PWD/mongos/db:/data/db 
          - $PWD/mongos/configdb:/data/configdb
          - $PWD/key.file:/data/mongodb/key.file
          ports:
          - 27017:27017
          command: mongos --configdb rs-config-server/192.168.3.55:27019,192.168.3.56:27019,192.168.3.57:27019 --bind_ip_all 

docker-compose每台机器拷贝,然后执行下面命令启动

docker-compose up -d

放行防火墙端口

firewall-cmd --permanent --add-port=27019/tcp
firewall-cmd --permanent --add-port=27118/tcp
firewall-cmd --permanent --add-port=27218/tcp
firewall-cmd --permanent --add-port=27318/tcp
firewall-cmd --permanent --add-port=27017/tcp
firewall-cmd --reload

在一台机器上配置集群和分片参数

初始化配置服务器

执行命令

docker exec -it rs_config_server bash
mongo --host 192.168.3.55 --port 27019
rs.initiate(
	_id: "rs_config_server",
	configsvr: true,
	members: [
		_id: 0,
		host: "192.168.3.55:27019"
	, 
		_id: 1,
		host: "192.168.3.56:27019"
	, 
		_id: 2,
		host: "192.168.3.57:27019"
	, ]
);
# 查看状态
rs.status()

开始关联分片集群或设置仲裁节点,这里不设置仲裁

shard1分片执行如下命令

docker exec -it rs_shard_server_01 bash
mongo --host 192.168.3.55 --port 27118
rs.initiate(
	_id: "rs_shard_server_01",
	members: [
		_id: 0,
		host: "192.168.3.55:27118"
	, 
		_id: 1,
		host: "192.168.3.56:27118"
	, 
		_id: 2,
		host: "192.168.3.57:27118"
	, ]
);
#查看状态
rs.status()

shard2分片执行如下命令

docker exec -it rs_shard_server_02 bash
mongo --host 192.168.3.55 --port 27218
rs.initiate(
	_id: "rs_shard_server_02",
	members: [
		_id: 0,
		host: "192.168.3.55:27218"
	, 
		_id: 1,
		host: "192.168.3.56:27218"
	, 
		_id: 2,
		host: "192.168.3.57:27218"
	, ]
);
# 查看状态
rs.status()

shard3分片执行如下命令

docker exec -it rs_shard_server_03 bash
mongo --host 192.168.3.55 --port 27318
rs.initiate(
	_id: "rs_shard_server_03",
	members: [
		_id: 0,
		host: "192.168.3.55:27318"
	, 
		_id: 1,
		host: "192.168.3.56:27318"
	, 
		_id: 2,
		host: "192.168.3.57:27318"
	, ]
);
#查看状态
rs.status()

配置路由mongos

执行命令


 docker exec -it rs_mongos_server bash 

 mongo --host 192.168.3.55 --port 27017
 # 配置 mongo router 路由信息
 sh.addShard("rs_shard_server_01/192.168.3.55:27118,192.168.3.56:27118,192.168.3.57:27118");
 sh.addShard("rs_shard_server_02/192.168.3.55:27218,192.168.3.56:27218,192.168.3.57:27218");
 sh.addShard("rs_shard_server_03/192.168.3.55:27318,192.168.3.56:27318,192.168.3.57:27318");
 
 #查看状态
 sh.status()

测试分片


docker exec -it rs_mongos_server bash
mongo --host 192.168.3.55 --port 27017
use admin
#  testdb1开启分片功能
db.runCommand(  enablesharding  : "testdb1");
db.runCommand(  shardcollection : "testdb1.tab1",key : id: 1  )

# 添加数据 
use testdb1;
for(var i=1;i<=10000;i++) 
db.tab1.save(id:i,"test1":"testval1");

#查看分片状态
db.tab1.stats();
#查看数据分布
db.tab1.getShardDistribution();

以上是关于使用docker-compose搭建mongdb分片3主机的主要内容,如果未能解决你的问题,请参考以下文章

mongdb分片原理以及分片副本集群搭建

mongdb 副本集的原理搭建应用

docker-compose 搭建 Kafka (1)

使用 docker-compose 搭建私服 Gitlab

使用Docker-Compose搭建consul集群环境!!!

使用Docker-Compose搭建consul集群环境!!!