DOCKER 布署 MONGODB 分片集群
服务器IP分配情况
本人在一台服务器上进行的布署
KEY | 配置信息 |
---|---|
user | root |
password | root |
URL | mongodb://root:rootpwd@宿主机IP:28017 |
路径 | /data/datastation/mongodb |
路由服务 | 宿主机IP:28017 |
配置服务 | 宿主机IP:28101 |
配置服务 | 宿主机IP:28102 |
配置服务 | 宿主机IP:28103 |
分片服务1 | 宿主机IP:28201 |
分片服务1 | 宿主机IP:28202 |
分片服务1 | 宿主机IP:28203 |
分片服务2 | 宿主机IP:28221 |
分片服务2 | 宿主机IP:28222 |
分片服务2 | 宿主机IP:28223 |
分片服务3 | 宿主机IP:28231 |
分片服务3 | 宿主机IP:28232 |
分片服务3 | 宿主机IP:28233 |
创建DOCKER-MONGODB网络
docker network create --subnet 172.100.100.0/24 mongodb
下载DOCKER镜像
docker pull docker:4.0
CONFIGSVR创建
配置文件生成
mkdir configsvr
vi configsvr/config.conf
storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
net:
bindIp: 0.0.0.0
replication:
replSetName: config1
sharding:
clusterRole: configsvr
#security:
# authorization: enabled
# keyFile: /mongodb/security/mongodbkeyfile
###创建CONFIGSVR容器
vi configsvr-create.sh
#!/bin/sh
port=28101;
ip=20;
#docker pull mongo:4.0
for num in seq `1 3`; do
docker run -p ${28101}:27019 \
--name mongoconfig_${num} \
--net=mongodb --ip=172.100.100.${ip} \
-v $PWD/configsvr:/etc/mongo \
-v $PWD/mongokeyfile:/mongodb/security \
-d mongo:4.0 --config /etc/mongo/config.conf;
let ip +=1;
let port +=1;
done
初始化复制集
docker exec -it mongoconfig_1 /bin/bash
mongo ip:port
- ip: 宿主机地址
- port: 映射在宿主机上的端口
rs.initiate(
{
_id: "config1",
members: [
{ _id : 1, host : "宿主机IP:28101"},
{ _id : 2, host : "宿主机IP:28102"},
{ _id : 3, host : "宿主机IP:28103"}
]
}
)
SHARD 创建
Shard-1配置文件
mkdir shardsvr
vi shardsvr/shard.conf
storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
net:
bindIp: 0.0.0.0
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
#security:
# authorization: enabled
# keyFile: /mongodb/security/mongodbkeyfile
Shard-1容器创建
vi shardsvr-create-1.sh
#!/bin/sh
port=28201;
ip=31;
for num in `seq 1 3`; do
docker run -p ${port}:27018 \
--name mongoshard_${num} \
--net=mongodb --ip=172.100.100.${ip} \
-v $PWD/shardsvr:/etc/mongo \
-v $PWD/mongokeyfile:/mongodb/security \
-d mongo:4.0 --config /etc/mongo/shard.conf;
let ip+=1;
let port+=1;
done
Shard-1初始化
docker exec -it mongoshard_1_1 /bin/bash
mongo --port 27018
rs.initiate(
{
_id: "shard1",
members: [
{ _id : 1, host : "宿主机IP:28201"},
{ _id : 2, host : "宿主机IP:28202"}
]
}
)
rs.addArb("宿主机IP:28203")
rs.status()
重复操作生成 Shard-2,Shard-3容器
shard-2
- ports:28221,28222,28223
- _id: shard2
- names: mongoshard_2_1,mongoshard_2_2,mongoshard_2_3
- conf: shard2.conf
shard-3
- ports:28231,28232,28233
- _id: shard2
- names: mongoshard_3_1,mongoshard_3_2,mongoshard_3_3
- conf: shard3.conf
Shard-2初始化
docker exec -it mongoshard_2_1 /bin/bash
mongo --port 27018
rs.initiate(
{
_id: "shard2",
members: [
{ _id : 1, host : "宿主机IP:28221"},
{ _id : 2, host : "宿主机IP:28222"}
]
}
)
rs.addArb("宿主机IP:28223")
rs.status()
Shard-3初始化
docker exec -it mongoshard_3_1 /bin/bash
mongo --port 27018
rs.initiate(
{
_id: "shard3",
members: [
{ _id : 1, host : "宿主机IP:28231"},
{ _id : 2, host : "宿主机IP:28232"}
]
}
)
rs.addArb("宿主机IP:28233")
rs.status()
MONGOS 创建
Mongos 配置文件
mkdir mongos
vi mongos/mongos.conf
systemLog:
destination: file
path: /var/log/mongodb/mongos.log
net:
bindIp: 0.0.0.0
sharding:
configDB: config1/宿主机IP:28101,宿主机IP:28102,宿主机IP:28103
#security:
# keyFile: /mongodb/security/mongodbkeyfile
Mongos 创建容器
vi mongos-create.sh
#/bin/sh
docker run -p 28017:27017 \
--name mongos1 \
--net=mongodb --ip=172.100.100.50 \
-v $PWD/mongos:/etc/mongo \
-v $PWD/mongokeyfile:/mongodb/security \
-d mongo:4.0 mongos \
--config /etc/mongo/mongos.conf \
--bind_ip 0.0.0.0
Mongos 添加分片关系
docker exec -it mongos1 /bin/bash
mongo --port 27017
//添加分片复制集1
sh.addShard("shard1/宿主机IP:28201,宿主机IP:28202")
//添加分片复制集2
sh.addShard("shard2/宿主机IP:28221,宿主机IP:28222")
//添加分片复制集3
sh.addShard("shard3/宿主机IP:28231,宿主机IP:28232")
//对test_db数据库进行分片
sh.enableSharding("dbName")
// 启用hash分片
sh.shardCollection("dbName.collectionName", {"tag": "hashed"})
sh.status()
keyfile验证
Mongos 添加用户
docker exec -it mongos1 /bin/bash
mongo --port 27017
use admin
db.createUser(
{
user: "root",
pwd: "rootpwd",
roles: [
{ role: "root", db: "admin" }
]
}
)
创建密钥文件
mkdir mongokeyfile && cd mongokeyfile
openssl rand -base64 756 > ./mongokeyfile
chmod 400 keyfile
sudo chown 999 keyfile