MongoDB单数据中心集群部署方案
1 网络规划
本方案要求服务器数量为奇数,可允许少于一半服务器宕机或断网。如果有多于3个节点,可按需调整分片数量和分布规则。
主机名 | IP地址 | 运行进程 | 防火墙开放端口 |
---|---|---|---|
MONGO01 | 192.168.10.36 | 分片1-primary(主),分片2-arbiter(仲裁),分片3-secondary(从),mongos,config | 20000,21000,22001~22003 |
MONGO02 | 192.168.10.37 | 分片1-secondary(从),分片2-primary(主),分片3-arbiter(仲裁),mongos,config | 20000,21000,22001~22003 |
MONGO03 | 192.168.10.38 | 分片1-arbiter(仲裁),分片2-secondary(从),分片3-primary(主),mongos,config | 20000,21000,22001~22003 |
2 部署环境
环境 | 版本 |
---|---|
操作系统 | CentOS7.4 64位系统 |
MongoDB版本 | mongodb-linux-x86_64-rhel70-4.2.2 |
同步模块版本 | mongo-shake-v2.2.1 |
内存 | 4GB+ |
CPU | 2c+ |
存储空间 | 200GB |
mongodb-linux-x86_64-rhel70-4.2.2下载地址:
https://download.youkuaiyun.com/download/Zhuge_Dan/12557607
mongo-shake-v2.2.1下载地址:
https://download.youkuaiyun.com/download/Zhuge_Dan/12557611
3 部署步骤
3.1 基础配置
3.1.1 关闭SELinux
[root@MONGO01 ~]# setenforce 0
[root@MONGO01 ~]# vi /etc/selinux/config
SELINUX=disabled #将SELINUX设置为disabled
[root@MONGO01 ~]# getenforce
Permissive
3.1.2 关闭防火墙或开放需要的业务端口
[root@MONGO01 ~]# systemctl stop firewalld #关闭防火墙
[root@MONGO01 ~]# systemctl disable firewalld #禁止防火墙开机自启
3.1.3 调整max_open_files
[root@MONGO01 ~]# echo '* soft nofile 65535' >> /etc/security/limits.conf
[root@MONGO01 ~]# echo '* hard nofile 65535' >> /etc/security/limits.conf
[root@MONGO01 ~]# cat /etc/security/limits.conf | tail -2
* soft nofile 65535
* hard nofile 65535
[root@MONGO01 ~]# echo 'ulimit -SHn 65535' >> /etc/rc.d/rc.local
[root@MONGO01 ~]# cat /etc/rc.d/rc.local | tail -1
ulimit -SHn 65535
[root@MONGO01 ~]# chmod +x /etc/rc.d/rc.local #为rc.local增加可执行权限
3.1.4 重启检查SELinux、firewalld和open files
[root@MONGO01 ~]# reboot
[root@MONGO01 ~]# getenforce #查看SELinux是否关闭成功
Disabled #关闭成功
[root@MONGO01 ~]# systemctl status firewalld #查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) #防火墙已关闭
Docs: man:firewalld(1)
[root@MONGO01 ~]# systemctl is-enabled firewalld #查看防火墙是否开机自启
disabled #防火墙已禁止开机自启
[root@MONGO01 ~]# ulimit -a | grep open
open files (-n) 65535 #open files已调整为65535
3.1.5 配置其它设备
在其它所有服务器上操作前4步。
方法:可使用Xshell,在菜单栏“工具”中选择“发送键输入到所有会话”,同时控制多台设备。
3.2 上传软件包
上传mongodb-linux-x86_64-rhel70-4.2.2.tgz到各服务节点,然后解压缩、重命名。以上传到/usr/local/目录下为例:
[root@MONGO01 ~]# cd /usr/local/
[root@MONGO01 local]# tar -zxvf mongodb-linux-x86_64-rhel70-4.2.2.tgz
[root@MONGO01 local]# mv mongodb-linux-x86_64-rhel70-4.2.2 mongo
[root@MONGO01 local]# rm -f mongodb-linux-x86_64-rhel70-4.2.2.tgz
3.3 创建数据与日志目录
根据节点上的运行进程,依次创建数据目录。假设数据盘挂载目录是/data/。
[root@MONGO01 local]# mkdir -p /data/mongo/config/
[root@MONGO01 local]# mkdir -p /data/mongo/shard1/
[root@MONGO01 local]# mkdir -p /data/mongo/shard2/
[root@MONGO01 local]# mkdir -p /data/mongo/shard3/
[root@MONGO01 local]# mkdir -p /var/log/mongo/config/
[root@MONGO01 local]# mkdir -p /var/log/mongo/mongos/
[root@MONGO01 local]# mkdir -p /var/log/mongo/shard1/
[root@MONGO01 local]# mkdir -p /var/log/mongo/shard2/
[root@MONGO01 local]# mkdir -p /var/log/mongo/shard3/
3.4 创建密钥
密钥用于副本集和集群的各个节点成员之间的内部身份验证。
3.4.1 生成密钥文件
可以使用openssl生成。
[root@MONGO01 local]# openssl rand -base64 756 > /data/mongo/key
3.4.2 将密钥文件上传到所有服务器
所有节点使用的密钥文件必须为同一个。
[root@MONGO01 local]# scp -r /data/mongo/key 192.168.10.37:/data/mongo/
[root@MONGO01 local]# scp -r /data/mongo/key 192.168.10.38:/data/mongo/
修改权限为600。
[root@MONGO01 local]# chmod 600 /data/mongo/key
[root@MONGO02 local]# chmod 600 /data/mongo/key
[root@MONGO03 local]# chmod 600 /data/mongo/key
3.5 创建配置文件
创建配置文件目录,然后根据节点上的运行进程,依次创建配置文件。
[root@MONGO01 local]# mkdir -p mongo/conf/
[root@MONGO01 local]# cd mongo/conf/
[root@MONGO01 conf]# touch config.conf
[root@MONGO01 conf]# touch mongos.conf
[root@MONGO01 conf]# touch shard1.conf
[root@MONGO01 conf]# touch shard2.conf
[root@MONGO01 conf]# touch shard3.conf
各文件内容如下。
3.5.1 config.conf
[root@MONGO01 conf]# vi config.conf
#声明本节点是配置服务器
configsvr=true
#副本集名称,和mongos中的configdb配置项的内容保持一致
replSet=cfgReplSet
#指定端口
port=21000
#数据目录位置,上一步中创建的目录
dbpath=/data/mongo/config/
#日志输出的目录
logpath=/var/log/mongo/config/config.log
#以追加方式添加日志
logappend=true
#后台启动
fork=true
#默认端口监听在127.0.0.1,只有本地可以访问。修改为监听所有IP
bind_ip_all=true
#秘钥文件的路径
keyFile=/data/mongo/key
3.5.2 mongos.conf
[root@MONGO01 conf]# vi mongos.conf
#指定3台config server的ip和端口。cfgReplSet和config中副本集名称保持一致
configdb=cfgReplSet/192.168.9.61:21000,192.168.9.62:21000,192.168.9.63:21000
#指定mongos端口,也就是应用程序访问的端口
port=20000
#日志输出的位置
logpath=/var/log/mongo/mongos/mongos.log
#以追加方式添加日志
logappend=true
#后台启动
fork=true
#默认端口监听在127.0.0.1,只有本地可以访问。修改为监听所有IP
bind_ip_all=true
#秘钥文件的路径
keyFile=/data/mongo/key
3.5.3 shard1.conf
[root@MONGO01 conf]# vi shard1.conf
#声明是分片服务器
shardsvr=true
#指定副本集名称
replSet=shard1
#指定端口
port=22001
#数据目录位置,上一步中创建的目录
dbpath=/data/mongo/shard1/
#日志输出的目录
logpath=/var/log/mongo/shard1/shard1.log
#以追加方式添加日志
logappend=true
#开启日志信息
journal=true
#后台启动
fork=true
#默认端口监听在127.0.0.1,仅本地可以访问修改为监听所有IP
bind_ip_all=true
#最多可以使用的内存(GB),默认为物理内存的一半,如果运行了多个实例,按实际情况调小
wiredTigerCacheSizeGB=2
#oplog保存的大小(MB),根据硬盘空间调整,默认为硬盘分区空间的5%。越大能支持的"单节点宕机启动后,可以自动恢复"的时间越长
oplogSize=10240
#秘钥文件的路径
keyFile=/data/mongo/key
#开启验证
auth=true
3.5.4 shard2.conf
[root@MONGO01 conf]# vi shard2.conf
#声明是分片服务器
shardsvr=true
#指定副本集名称
replSet=shard2
#指定端口
port=22002
#数据目录位置,上一步中创建的目录
dbpath=/data/mongo/shard2/
#日志输出的目录
logpath=/var/log/mongo/shard2/shard2.log
#以追加方式添加日志
logappend=true
#开启日志信息
journal=true
#后台启动
fork=true
#默认端口监听在127.0.0.1&#x