某电商大型项目,由于业务大规模升级。项目中的前置缓存节点,已经不能满足当前的业务需求,现在对redis集群扩容一套redis主备节点,对于实际的生产环境,一般规划是在晚上2凌晨12点做操作,操作之前,提前做好redis安装部署,晚上凌晨12点左右,业务量比较少,影响范围比较少,redis对接的底层数据库,先要锁库、锁表,不产生业务流数据,这样redis集群可产生的影响因素较小。
Redis集群部署的规划:
本实验使用6台Centos 7.6 主机做集群,其中3台为master 其中3台为slave,IP地址分别为
节点类型 | IP地址 |
---|---|
master1 | 192.168.100.128 |
master2 | 192.168.100.129 |
master3 | 192.168.100.131 |
slave1 | 192.168.100.132 |
slave2 | 192.168.100.133 |
slave3 | 192.168.100.134 |
安装包百度云分享链接:https://pan.baidu.com/s/1pYuNA9mBjzsuaI2iQ4jVFg
提取码:tjj5
准备 6台Centos 7.6 的虚拟机,关闭防火墙、关闭核心防护,挂载光盘,搭建本地yum仓库,配置相对应的ip地址
每个节点服务器都安装redis服务,步骤一样。
[root@localhost opt]# tar xzvf redis-5.0.7.tar.gz -C /usr/local/
[root@localhost opt]# cd /usr/local/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install
####如果安装过程中,更改安装路径可以用make PRRFIX=安装路径 install
//设置Redis相关配置文件
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-5.0.7]# cd /usr/local/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/bin/redis-server ####手动输入
Selected config:
Port : 6379 ####端口号
Config file : /etc/redis/6379.conf ####默认配置文件
Log file : /var/log/redis_6379.log ####日志文件
Data dir : /var/lib/redis/6379 ####数据目录
Executable : /usr/local/bin/redis-server ####执行命令
Cli Executable : /usr/local/bin/redis-cli ####客户端命令
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
修改各个redis主机的6379.conf配置文件,注意:6个节点都要修改。
[root@localhost utils]# vi /etc/redis/6379.conf
bind 192.168.100.128 ###删除原来的127.0.0.1 然后添加各主机对应的IP地址
logfile /var/log/redis_6379.log ###不需要改
daemonize yes ###不需要改
port 6379 ###不需要改
839/cluster-enabled yes ###去掉注释,打开集群
847/cluster-config-file nodes-6379.conf ###去掉注释,集群的主机名
853/cluster-node-timeout 15000 ###去掉注释
930/cluster-require-full-coverage no ###去掉注释, yes改成no
700/appendonly yes ###打开AOF持久化
/etc/init.d/redis_6379 restart
netstat -antp |grep 6379
//安装ruby
[root@localhost utils]# yum -y install ruby rubygems
上传redis-3.2.0.gem 这个包到/opt目录下执行下面命令
[root@localhost utils]# cd /opt
[root@localhost opt]# gem install redis --version 3.2.0
Successfully installed redis-3.2.0
Parsing documentation for redis-3.2.0
Installing ri documentation for redis-3.2.0
1 gem installed
集群创建只在一个节点上敲这条命令即可
先关闭防火墙,否则集群创建报错:Could not connect to Redis at 192.168.100.131:6379: No route to host
[root@localhost src]# systemctl stop firewalld
[root@localhost src]# setenforce 0
//创建集群
[root@localhost opt]# cd /usr/local/redis-5.0.7/src/
[root@localhost src]# redis-cli --cluster create --cluster-replicas 1 192.168.100.128:6379 192.168.100.129:6379 192.168.100.131:6379 192.168.100.132:6379 192.168.100.133:6379 192.168.100.134:6379
加入两个新的节点。
IP地址如下:
master4 :192.168.100.135
slave4 :192.168.100.136
首先把这两个服务器安装redis服务,步骤和上面一样,这里就不在演示
将2台redis节点添加到集群中,并确认集群状态信息(进入节点192.168.100.128)
redis-cli --cluster add-node 192.168.100.135:6379 192.168.100.128:6379
redis-cli --cluster add-node 192.168.100.136:6379 192.168.100.128:6379
[root@localhost ~]# redis-cli -h 192.168.100.128 -p 6379
192.168.100.128:6379> cluster nodes
c561fee83d143a3c93e9277b4cb559ce5567f21c 192.168.100.135:6379@16379 master - 0 1584950082077 0 connected
a5c7a5bf4b261a2a1fbc96aeaa8595d497026724 192.168.100.131:6379@16379 master - 0 1584950084000 3 connected 10923-16383
f71d695207fe05d2a50b28aef9790234f050fd82 192.168.100.132:6379@16379 slave a5c7a5bf4b261a2a1fbc96aeaa8595d497026724 0 1584950082000 4 connected
6794da9f27f442bc8b67227765f6f0e36f71e8e0 192.168.100.134:6379@16379 slave 7d1ebdb236a9496ad7401be17aad222a1aae50a5 0 1584950080060 6 connected
7d1ebdb236a9496ad7401be17aad222a1aae50a5 192.168.100.129:6379@16379 master - 0 1584950085000 2 connected 5461-10922
8911b525151ef8f44d58cb0cd311527c3a26970e 192.168.100.128:6379@16379 myself,master - 0 1584950082000 1 connected 0-5460
13297df3b08b66c88a328dc95f5eb35e6d651e00 192.168.100.136:6379@16379 master - 0 1584950085116 7 connected
b01d8ff628f5f6bc7297b90a7e707eddcfa2eb6b 192.168.100.133:6379@16379 slave 8911b525151ef8f44d58cb0cd311527c3a26970e 0 1584950083088 5 connected
新加入的节点是没有哈希槽,需要重新分配槽位。
根据redis 槽位分析,一共有16384个槽位,如果分4个配套master节点:每个节点是4096个槽位.
[root@localhost 6379]# redis-cli --cluster reshard 192.168.100.128:6379
[root@master1 ~]# redis-cli --cluster reshard 192.168.100.41:6379
How many slots do you want to move (from 1 to 16384)? 4096 #要迁移多少个槽
What is the receiving node ID? c561fee83d143a3c93e9277b4cb559ce5567f21c #迁移到master4节点
//注意:输入master4的IDS值
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
#要求输入源节点的ids值,这里输入128、128、131三个节点,输入done表示结束
Source node #1: 8911b525151ef8f44d58cb0cd311527c3a26970e
Source node #2: 7d1ebdb236a9496ad7401be17aad222a1aae50a5
Source node #3: a5c7a5bf4b261a2a1fbc96aeaa8595d497026724
Source node #4: done #只有三个master,此处输入done
//稍等片刻。
最后会有一个迁移方案,输入yes表示同意,迁移开始。输入no表示不同意,重新设置迁移方案。
[root@localhost ~]# redis-cli -h 192.168.100.128 -p 6379
192.168.100.128:6379> cluster nodes
可以看出,节点负责的槽数据差异在2%以内,因此槽位分配是均衡的。
进入slave4 的数据库中
[root@localhost 6379]# redis-cli -h 192.168.100.136 -p 6379
192.168.100.136:6379> cluster replicate c561fee83d143a3c93e9277b4cb559ce5567f21c
OK
192.168.100.136:6379> cluster nodes
[root@localhost 6379]# redis-cli --cluster rebalance --cluster-threshold 1 192.168.100.128:6379