mkdir /home/soft/redis
cd /home/soft/redis
wget https://download.redis.io/releases/redis-6.2.4.tar.gz
tar xzf redis-6.2.4.tar.gz
cd redis-6.2.4
make
规划:7000、7002、7004为主,7001、7003、7005为从
cd /home/soft/redis
mkdir -p cluster
cd cluster
mkdir -p redis_{7000..7005}/{data,log,node,pid}
cd /home/soft/redis/cluster
cp /home/soft/redis/redis-6.2.4/redis.conf /home/soft/redis/cluster/redis7000.conf
cd /home/soft/redis/cluster
vi redis7000.conf
#注释掉不绑定就是不限制本机访问
#bind 127.0.0.1 -::1
#指定端口
port 7000
# 启动后台运行
daemonize yes
# 存放进程Id
pidfile /home/soft/redis/cluster/redis_7000/pid/redis_7000.pid
# 日志文件
logfile "/home/soft/redis/cluster/redis_7000/log/redis7000.log"
# 快照存放文件,存储在dir配置的工作目录下
dbfilename dump7000.rdb
# 工作目录
dir /home/soft/redis/cluster/redis_7000/data/
# 是否启动保存操作日志
appendonly yes
# 操作日志存放文件
appendfilename "appendonly7000.aof"
# 是否开启集群
cluster-enabled yes
# 集群配置文件(启动后自动生成),存储在dir配置的工作目录下data
cluster-config-file nodes-7000.conf
# 集群节点ping、pong超时时间
cluster-node-timeout 10000
# 节点访问密码
requirepass zsoft
# 客户的最大连接数
maxclients 100000
#最大内存
maxmemory 10G
#主节点设置了密码,从节点需要配置主节点的密码,否则无法主从同步
masterauth zsoft
cd /home/soft/redis/cluster
sed 's/7000/7001/g' redis7000.conf > redis7001.conf
sed 's/7000/7002/g' redis7000.conf > redis7002.conf
sed 's/7000/7003/g' redis7000.conf > redis7003.conf
sed 's/7000/7004/g' redis7000.conf > redis7004.conf
sed 's/7000/7005/g' redis7000.conf > redis7005.conf
cd /home/soft/redis/redis-6.2.4
src/redis-server /home/soft/redis/cluster/redis7000.conf
src/redis-server /home/soft/redis/cluster/redis7001.conf
src/redis-server /home/soft/redis/cluster/redis7002.conf
src/redis-server /home/soft/redis/cluster/redis7003.conf
src/redis-server /home/soft/redis/cluster/redis7004.conf
src/redis-server /home/soft/redis/cluster/redis7005.conf
ps -ef| grep redis
cd /home/soft/redis/redis-6.2.4
src/redis-cli --cluster create 192.168.10.14:7000 192.168.10.14:7002 192.168.10.14:7004 192.168.10.14:7001 192.168.10.14:7003 192.168.10.14:7005 --cluster-replicas 1 -a zsoft
注意:但提示Can I set the above configuration?时,请输入yes
ps:最后这个 --cluster-replicas 1,表示每个主节点一个备份节点,即一主一从,–cluster-replicas 2, 即一主两从(需要9个节点)
PS:大坑,网上大部分建立集群的时候都是使用127.0.0.1,这样会导致java客户连接集群时默认使用127.0.0.1来连接,所以不能使用以下命令来建立集群
src/redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7002 127.0.0.1:7004 127.0.0.1:7001 127.0.0.1:7003 127.0.0.1:7005 --cluster-replicas 1 -a zsoft
cd /home/soft/redis/redis-6.2.4
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft cluster nodes
cd /home/soft/redis/redis-6.2.4
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft shutdown
src/redis-cli -h 127.0.0.1 -p 7001 -a zsoft shutdown
src/redis-cli -h 127.0.0.1 -p 7002 -a zsoft shutdown
src/redis-cli -h 127.0.0.1 -p 7003 -a zsoft shutdown
src/redis-cli -h 127.0.0.1 -p 7004 -a zsoft shutdown
src/redis-cli -h 127.0.0.1 -p 7005 -a zsoft shutdown
模式一、全部数据删除重建集群
必须先第九步关停所有节点服务
cd /home/soft/redis/cluster
rm -rf redis_{7000..7005}/{data,log,node,pid}
cd /home/soft/redis/cluster
mkdir -p redis_{7000..7005}/{data,log,node,pid}
然后重新执行第五步启动7000到7005节点
然后执行第七步重建集群
模式二、删除配置重建集群
必须先第九步关停所有节点服务
cd /home/soft/redis/cluster
rm -rf redis_{7000..7005}/data/nodes-{7000..7005}.conf
然后重新执行第五步启动7000到7005节点
然后执行第七步重建集群
firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --zone=public --add-port=7001/tcp --permanent
firewall-cmd --zone=public --add-port=7002/tcp --permanent
firewall-cmd --zone=public --add-port=7003/tcp --permanent
firewall-cmd --zone=public --add-port=7004/tcp --permanent
firewall-cmd --zone=public --add-port=7005/tcp --permanent
firewall-cmd --zone=public --add-port=7006/tcp --permanent
firewall-cmd --zone=public --add-port=7007/tcp --permanent
firewall-cmd --reload
集群扩容7006、7007
cd /home/soft/redis/cluster
mkdir -p redis_{7006,7007}/{data,log,node,pid}
sed 's/7000/7006/g' redis7000.conf > redis7006.conf
sed 's/7000/7007/g' redis7000.conf > redis7007.conf
cd /home/soft/redis/redis-6.2.4
src/redis-server /home/soft/redis/cluster/redis7006.conf
src/redis-server /home/soft/redis/cluster/redis7007.conf
将新节点(127.0.0.1:7006)加入已有集群(127.0.0.1:7000)
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft --cluster add-node 127.0.0.1:7006 127.0.0.1:7000
查看新节点(127.0.0.1:7006)集群Id(默认为master节点)
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft cluster nodes
将从节点7007加入集群并指定master节点为7006(id)
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave --cluster-master-id 这里填7006master的id
查看集群信息
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft cluster nodes
给新master节点(127.0.0.1:7006)分配槽位
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft --cluster reshard 127.0.0.1:7006
然后输入需要分配的槽位数,例如100
然后再输入接受槽位的masterId,即新增的7006节点
然后再输入被分割槽位的masterID,例如7004节点
最后输入done结束分配。
去掉7006、7007节点
查看集群信息
cd /home/soft/redis/redis-6.2.4
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft cluster nodes
#将7006节点的槽位归还7004
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft --cluster reshard --cluster-from 7006节点id --cluster-to 7004节点id --cluster-slots 槽位数 127.0.0.1:7006
将7006、7007节点移出集群
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft --cluster del-node 127.0.0.1:7000 7007节点id
src/redis-cli -h 127.0.0.1 -p 7000 -a zsoft --cluster del-node 127.0.0.1:7000 7006节点id
关停7006、7007节点服务
src/redis-cli -h 127.0.0.1 -p 7006 -a zsoft shutdown
src/redis-cli -h 127.0.0.1 -p 7007 -a zsoft shutdown
查看redis服务
ps -ef| grep redis