Mongodb-单机多节点部署 【亲测】
一、集群规划
1.普通分片集,没有数据冗余能力,如果任意分片节点出现故障,则会数据丢失,所以在生产中一般都采用分片+副本集的方式,该方式即解决读写负载均衡高可用,也解决了数据冗余高可用的问题;
2.主机充裕的情况下分片副本集集群,至少需要9台主机节点,其中每三台组成一组副本集,然后再把三组副本集的主节点组合成分片集群,这样当用户或应用程序请求操作数据库时,mongos会把请求分流到不同分片节点(也就是每组副本集的主节点),当某一个分片节点挂掉后,也就是相当于某组副本集的主节点挂掉了,此时副本集中的其他节点会重新选举出新的主节点,从而实现了数据冗余;
3.在主机资源富裕的情况下,除了9台数据节点外,还需要部署6台configserver(3台)和mongos(3台),当然在流量请求量不大的情况下,也可以部署到数据节点主机上;
4.本次使用三台主机,模拟15个节点的分片副本集集群,模拟方式就是每台主机上启动3个不同端口的mongod服务,另外每台主机是各部署configserver和mongos服务组件,这样就达到了3台完成部署分片+副本集的高可用集群;
为了不影响操作,读者可忽略文中10开头的ip地址,本文介绍单机安装,ip一律使用127.0.0.1
环境:
CentOS Linux release 7.6.1810 (Core)
MongoDB4.2.2 (4.2版本开始支持强大的跨分片事务和其他更多特性,详见官网)
1.1、主机节点以及应用组件规划
hostname | k8s-mongodb-01 | k8s-mongodb-02 | k8s-mongodb-03 |
---|---|---|---|
IP | 127.0.0.1 | 127.0.0.1 | 127.0.0.1 |
shard01 | shard_replset_01=27018 | shard_replset_01=28018 | shard_replset_01=29018 |
shard02 | shard_replset_02=27028 | shard_replset_02=28028 | shard_replset_02=29028 |
shard03 | shard_replset_03=27038 | shard_replset_03=28038 | shard_replset_03=29038 |
configsvr | configserver=27019 | configserver=28019 | configserver=29019 |
mongos | mongos=27017 | mongos=28017 | mongos=29017 |
1.2、主机节点基本配置
官方内核优化:
https://docs.mongodb.com/manual/reference/ulimit/
# 命令别名
echo "alias ll='ls -l --color=auto'" >> /etc/profile && source /etc/profile
# 内核优化
echo -e "* soft noproc 64000\n* hard noproc 64000\n* soft nofile 64000\n* hard nofile 64000" >> /etc/security/limits.conf && sysctl -p
1.3、安装基础软件
apt-get install ntpdate tree lrzsz
1.4、时间同步
# 改为上海市时区;
timedatectl set-timezone Asia/Shanghai
# 时间同步每5分钟;
cat<<EOF>>/var/spool/cron/crontabs/root
*/5 * * * * /usr/sbin/ntpdate 169.254.169.123 >/dev/null 2>&1
EOF
1.5、升级openssl
升级到openssl v1.1.1d 版本,使其支持 TLS/SSL V3 和国密标准;
# 安装编译环境
apt-get install build-essential
# 下载openssl 1.1.1d
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz -P /shared/download
# 解压
cd /shared/download/ && tar -xf openssl-1.1.1d.tar.gz
# 编译前配置
cd /shared/download/openssl-1.1.1d && ./config --prefix=/opt/openssl-1.1.1d --openssldir=/opt/openssl-1.1.1d/ssl
# 编译并安装;
make -j4 && make install
# 创建软连接
ln -fs /opt/openssl-1.1.1d/ /opt/openssl
# 配置系统库文件,是系统可以找到刚编译的openssl的lib库;
echo '/opt/openssl-1.1.1d/lib' >> /etc/ld.so.conf.d/openssl-1.1.1d.conf && ldconfig
# 测试openssl命令;
/opt/openssl/bin/openssl version
/opt/openssl/bin/openssl version -a
# 备份源openssl
mv $(which openssl) $(which openssl).bak
# 添加PATH变量
echo 'export PATH=$PATH:/opt/openssl/bin' >> /etc/profile && source /etc/profile
1.6、规划集群IP和主机名
解决debian不能修改resovle.conf的问题
https://wiki.debian.org/resolv.conf
echo ‘make_resolv_conf() { :; }’ > /etc/dhcp/dhclient-enter-hooks.d/leave_my_resolv_conf_alone
chmod 755 /etc/dhcp/dhclient-enter-hooks.d/leave_my_resolv_conf_alone
# 修改主机名
hostnamectl set-hostname k8s-mongodb-01
# 各主机添加主机名ip映射
cat<<EOF>/etc/hosts
127.0.1.1 ip-10-20-1-101.ap-southeast-1.compute.internal ip-10-20-1-101
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
## MongoDB_host映射
127.0.0.1 k8s-mongodb-01
127.0.0.1 k8s-mongodb-02
127.0.0.1 k8s-mongodb-03
## 多台机器host映射方式
#10.20.1.101 k8s-mongodb-01
#10.20.1.102 k8s-mongodb-02
#10.20.1.103 k8s-mongodb-03
EOF
二、部署mongodb
官方资料:
https://www.mongodb.com/download-center/community
# wget下载mongodb-x86_64-debian92-4.2.3.tgz
wget -P /shared/download/ https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.2.tgz
# 解压到指定目录
tar -xf /shared/download/mongodb-linux-x86_64-rhel70-4.2.2.tgz -C /usr/local
# 更改目录名 【和软连接二选一】
cd /usr/local && mv mongodb-linux-x86_64-rhel70-4.2.2 mongodb # 也可通过下面的创建软连接:ln -nfs /usr/local/mongodb-linux-x86_64-rhel70-4.2.2/ /usr/local/mongodb
# 添加PATH环境变量
echo 'export PATH=$PATH:/usr/local/mongodb/bin' >> /etc/profile && source /etc/profile
# 每个主机均创建所需目录
mkdir -p /usr/local/mongodb/{etc,keyfile,cert}
mkdir -p /data/mongodb/cluster/{shard{01,02,03}/{data,logs},configserver/{data,logs},mongos/logs}
# 复制两份数据目录结构
cp -a /data/mongodb/cluster/ /data/mongodb/cluster2 && cp -a /data/mongodb/cluster/ /data/mongodb/cluster3
cp -a /usr/local/mongodb/etc /usr/local/mongodb/etc2 && cp -a /usr/local/mongodb/etc /usr/local/mongodb/etc3
# 软连接 【】
ln -s /usr/local/mongodb/bin/mongod /etc/default/mongod
2.1、创建用户并授权
# 创建系统用户
useradd -s /usr/sbin/nologin -M mongodb
# 修改目录数主数据组
chown -R mongodb:mongodb /data/mongodb /usr/local/mongodb/
2.2、创建集群通信密钥
# 创建并复制密钥到所有主机相同目录
openssl rand -base64 756 > /usr/local/mongodb/keyfile/secret.key \
&& chmod 400 /usr/local/mongodb/keyfile/secret.key \
&& chown mongodb:mongodb /usr/local/mongodb/keyfile/secret.key
# 修改各主机节点权限
chmod 400 /usr/local/mongodb/keyfile/secret.key
# 复制两份
cp -a /usr/local/mongodb/keyfile/secret.key /usr/local/mongodb/keyfile/secret2.key && cp -a /usr/local/mongodb/keyfile/secret.key /usr/local/mongodb/keyfile/secret3.key
# 查看文件
ll /usr/local/mongodb/keyfile
2.3、创建配置组件的配置文件
# 每台主机节点都创建配置服务器配置文件
cat<<EOF>/usr/local/mongodb/etc/configserver.yaml
sharding:
clusterRole: configsvr
replication:
replSetName: configserver
net:
bindIp: 0.0.0.0
port: 27019
wireObjectCheck: true
processManagement:
fork: false
pidFilePath: "/data/mongodb/cluster/configserver/configserver.pid"
timeZoneInfo: "/usr/share/zoneinfo"
storage:
dbPath: "/data/mongodb/cluster/configserver/data"
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
systemLog:
destination: file
path: "/data/mongodb/cluster/configserver/logs/configserver.log"
logAppend: true
setParameter:
enableLocalhostAuthBypass: true
security:
keyFile: "/usr/local/mongodb/keyfile/secret.key"
clusterAuthMode: keyFile
authorization: enabled
EOF
# -----------
# 每台主机节点都创建配置服务器配置文件【02节点】
cat<<EOF>/usr/local/mongodb/etc2/configserver.yaml
sharding:
clusterRole: configsvr
replication:
replSetName: configserver
net:
bindIp: 0.0.0.0
port: 28019
wireObjectCheck: true
processManagement:
fork: false
pidFilePath: "/data/mongodb/cluster2/configserver/configserver.pid"
timeZoneInfo: "/usr/share/zoneinfo"
storage:
dbPath: "/data/mongodb/cluster2/configserver/data"
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
systemLog:
destination: file
path: "/data/mongodb/cluster2/configserver/logs/configserver.log"
logAppend: true
setParameter:
enableLocalhostAuthBypass: true
security:
keyFile: "/usr/local/mongodb/keyfile/secret2.key"
clusterAuthMode: keyFile
authorization: enabled
EOF
# -----------
# 每台主机节点都创建配置服务器配置文件 [03节点]
cat<<EOF>/usr/local/mongodb/etc3/configserver.yaml
sharding:
clusterRole: configsvr
replication:
replSetName: configserver
net:
bindIp: 0.0.0.0
port: 29019
wireObjectCheck: true
processManagement:
fork: false
pidFilePath: "/data/mongodb/cluster3/configserver/configserver.pid"
timeZoneInfo: "/usr/share/zoneinfo"
storage:
dbPath: "/data/mongodb/cluster3/configserver/data"
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
systemLog:
destination: file
path: "/data/mongodb/cluster3/configserver/logs/configs