作者:余振兴

爱可生 DBA 团队成员,热衷技术分享、编写技术文档。

本文来源:原创投稿

*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。


目录:

  • 一、mlaunch 及 MongoDB软件安装
  • 二、mlaunch 部署副本集及运维操作
  • 三、mlaunch 部署分片集群
  • 四、mlaunch 常用部署架构命令示例
  • 五、参考链接

我们在使用和学习 MongoDB 过程中难免遇到需要部署各类架构进行功能性验证,由于不同的架构和配置,使得我们在搭建环境过程中需要花费大量时间,这里推荐使用一款可快速部署 MongoDB 测试环境各类架构的工具 mlaunch ,它是 MongoDB 一组开源工具包 mtools 中的一个(对 mtools 的其他工具有兴趣也可查看文末链接地址了解详情),可用来在测试环境快速部署不同架构的 MongoDB 服务,支持单节点、副本集以及分片集群,安装及操作均十分简单,以下给到常用场景和架构的部署示例

一、mlaunch 及 MongoDB软件安装

## 安装python3,及MongoDB服务的依赖包
shell> yum install -y python3 python3-devel net-snmp-agent-libs
  
## 安装 mlaunch 依赖包
shell> pip3 install mtools psutil pymongo
 
## MongoDB软件包安装(解压即可),MongoDB软件包自行下载,当前示例服务器下载了2个版本软件包,可以任意指定其中版本
shell> ll -ld /opt/mongodb-linux-x86_64-*
drwxr-xr-x 4 root root       148 8月  16 18:30 /opt/mongodb-linux-x86_64-enterprise-rhel70-4.0.16
-rw-r--r-- 1 root root 120608697 8月  16 18:30 /opt/mongodb-linux-x86_64-enterprise-rhel70-4.0.16.tgz
drwxr-xr-x 3 root root       100 8月  16 18:31 /opt/mongodb-linux-x86_64-rhel70-4.4.14
-rw-r--r-- 1 root root  72399766 8月  16 18:25 /opt/mongodb-linux-x86_64-rhel70-4.4.14.tgz

二、mlaunch 部署副本集及运维操作

## 初始化一个3节点的副本集
## 副本集名称为 demo_replicatset
## 起始端口为 30000
## 指定30000端口的优先级最高(优先级默认为1,主节点调整为10,值越高,优先级越高)
## 版本为 4.4.14
## 数据目录为 /data/demo_rs
shell> mlaunch init --replicaset --nodes 3 --name "demo_replicatset" --priority --port 30000 --binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin --dir /data/demo_rs
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30000
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30001
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30002
replica set 'demo_replicatset' initialized.
  
## 查看部署运行的副本集状态
shell> mlaunch list --dir /data/demo_rs/
PROCESS    PORT     STATUS     PID
mongod     30000    running    9017
mongod     30001    running    9065
mongod     30002    running    9113
  
## 登录副本集(默认无密码,可也在初始化时指定)
shell> /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongo --host=localhost:30000
demo_replicatset:PRIMARY>
  
## 停止该副本集
shell> mlaunch stop --dir /data/demo_rs/
sent signal Signals.SIGTERM to 3 processes.
  
## 重启启动该副本集
shell> mlaunch start --dir /data/demo_rs/
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30000
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30001
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30002
  
## 删除该副本集(停止后直接删除目录即可)
shell> rm /data/demo_rs/ -rf

三、mlaunch 部署分片集群

## 创建3节点的分片,每个节点只有1个主(考虑服务器配置有限)
## 配置3节点configServer
## 配置1个mongos
shell> mlaunch init --sharded 3 --nodes 1 --replicaset --config 3 --csrs --mongos 1 --priority --name "demo_shard" --port 30000 --binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin --dir /data/demo_shard
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30001
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30002
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30003
launching: config server on port 30004
launching: config server on port 30005
launching: config server on port 30006
replica set 'configRepl' initialized.
replica set 'shard01' initialized.
replica set 'shard02' initialized.
replica set 'shard03' initialized.
launching: /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongos on port 30000
adding shards. can take up to 30 seconds...
  
## 查看部署的集群架构及基本信息
[root@10-186-61-159 ~]# mlaunch list --dir /data/demo_shard/
PROCESS          PORT     STATUS     PID
mongos           30000    running    15820
config server    30004    running    15514
config server    30005    running    15567
config server    30006    running    15620
shard01
    mongod       30001    running    15377
shard02
    mongod       30002    running    15422
shard03
    mongod       30003    running    15469
  
## 登录集群
shell> /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongo --host=localhost:30000
mongos>

四、mlaunch 常用部署架构命令示例

## 单节点副本集
mlaunch init \
--replicaset \
--priority \
--nodes 1 \
--name "demo_single_rs" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_single_rs
  
## 副本集 PSS 架构
mlaunch init \
--replicaset \
--priority \
--nodes 3 \
--name "demo_replicatset" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_rs
 
  
## 副本集 PSA 架构
mlaunch init \
--replicaset \
--priority \
--arbiter \
--nodes 3 \
--name "demo_replicatset" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_rs
  
## 分片集群高可用架构架构
## 3节点ConfigServer 副本集
## 3节点shard分片节点 副本集
## 3节点mongos 高可用
mlaunch init \
--sharded 3 \
--nodes 3 \
--replicaset \
--config 3 \
--mongos 3 \
--csrs \
--priority \
--name "demo_shard" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_shard
五、参考链接

http://blog.rueckstiess.com/mtools/mlaunch.html

https://github.com/rueckstiess/mtools


avatar
100
  Subscribe  
提醒