redis集群
redis集群共有16384个hash槽,每个redis key通过crc16检验后,对16384取模来决定放在哪个槽中,每个节点负责一部分槽,如果集群中有3个主库(从库节点槽跟随他的主库),那么节点与槽安置见下:
节点1:0至5460号hash槽
节点2:5461至10922号hash槽
节点3:10922至16384号hash槽
至少有3台redis主库,从库一般也至少3台,一一对应,当主库宕机时,从库顶上变主库,当旧主库上线时,变成从库(隶属于新主库)
案例(群集搭建+添加与删除节点演示)
一、 环境
二、源码安装redis5
安装环境软件支持
[root@localhost src]# yum install gcc-c++ -y
下载redis
[root@localhost src]# wget -c http://download.redis.io/releases/redis-5.0.5.tar.gz
编译安装
[root@localhost src]# tar xf redis-5.0.5.tar.gz
[root@localhost src]# cd redis-5.0.5
[root@localhost redis-5.0.5]# vim src/Makefile
PREFIX?=/usr/local/redis
[root@localhost redis-5.0.5]# make PREFIX=/usr/local/redis MALLOC=libc install
[root@localhost redis-5.0.5]# make install
[root@localhost redis-5.0.5]# mkdir -p /usr/local/redis/6379
[root@localhost redis-5.0.5]# mkdir -p /usr/local/redis/6380 准备两台实例
[root@localhost redis-5.0.5]# cp redis.conf /usr/local/redis/6379/6379.conf
[root@localhost redis-5.0.5]# cp redis.conf /usr/local/redis/6380/6380.conf
[root@localhost redis-5.0.5]# cp /usr/local/redis/bin/redis-server /usr/local/redis/bin/redis6379-server
[root@localhost redis-5.0.5]# cp /usr/local/redis/bin/redis-server /usr/local/redis/bin/redis6380-server
[root@localhost redis-5.0.5]# ls /usr/local/redis/
6379 6380 bin
[root@localhost redis-5.0.5]# ls /usr/local/redis/6379/
6379.conf
[root@localhost redis-5.0.5]# ls /usr/local/redis/bin/
redis6379-server redis6380-server redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
编译sysctl.conf文件
[root@localhost redis-5.0.5]# vim /etc/sysctl.conf
net.core.somaxconn=512
vm.overcommit_memory=1
让redis负责内存管理
[root@localhost redis-5.0.5]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@localhost redis-5.0.5]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
执行sysctl -p,让内核重新读取sysctl
[root@localhost redis-5.0.5]# sysctl -p
net.core.somaxconn = 512
vm.overcommit_memory = 1
配置启动脚本
执行两次
[root@localhost redis-5.0.5]# /usr/src/redis-5.0.5/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] 6379 #端口
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis/6379/6379.conf #配置文件
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/6379/redis_6379.log #日志文件
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/6379/ #数据目录
Please select the redis executable path [] /usr/local/redis/bin/redis6379-server #启动脚本
Selected config:
Port : 6379
Config file : /usr/local/redis/6379/6379.conf
Log file : /usr/local/redis/6379/redis_6379.log
Data dir : /usr/local/redis/6379/
Executable : /usr/local/redis/bin/redis6379-server
Cli Executable : /usr/local/redis/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!
[root@localhost redis-5.0.5]# /usr/src/redis-5.0.5/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] 6380
Please select the redis config file name [/etc/redis/6380.conf] /usr/local/redis/6380/6380.conf
Please select the redis log file name [/var/log/redis_6380.log] /usr/local/redis/6380/redis_6380.log
Please select the data directory for this instance [/var/lib/redis/6380] /usr/local/redis/6380/
Please select the redis executable path [] /usr/local/redis/bin/redis6380-server
Selected config:
Port : 6380
Config file : /usr/local/redis/6380/6380.conf
Log file : /usr/local/redis/6380/redis_6380.log
Data dir : /usr/local/redis/6380/
Executable : /usr/local/redis/bin/redis6380-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6380.conf => /etc/init.d/redis_6380
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
如此,就可以通过以下命令,直接进行启动、停止及重启了
[root@localhost redis-5.0.5]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
编辑配置文件
vim /usr/local/redis/6379/6379.conf
vim /usr/local/redis/6380/6380.conf port 6380
配置rdb与aof持久化
注:这里只要配置下面4行,其他默认即可
bind 0.0.0.0 监听IP
port 6379 监听端口
appendonly yes 开启AOF持久化
no-appendfsync-on-rewrite yes 当rdb写入时,停止aof写入
[root@localhost redis-5.0.5]# vim /usr/local/redis/6379/6379.conf
70 bind 0.0.0.0 监听IP
89 protected-mode yes 可以查看键值
93 port 6379 监听端口
102 tcp-backlog 511 tcp队列长度,这里默认
114 timeout 0 客户端与服务器之间的连接超时时间,0为永不超时
131 tcp-keepalive 300 每300秒给客户端发送ACK握手包
137 daemonize yes 后台启动
148 supervised no 可以通过upstart和systemd管理redis守护进程
159 pidfile /var/run/redis_6379.pid 进程文件
167 loglevel notice 日志级别,默认即可
172 logfile /usr/local/redis/6379/redis_6379.log 日志文件路径
176 # syslog-enabled no 是否输出到系统日志
187 databases 16 数据库个数0-15编号,共16个数据库
195 always-show-logo yes 启动时是否显示日志
219 save 900 1
220 save 300 10
221 save 60 10000
236 stop-writes-on-bgsave-error yes 快照出问题时,不可写
242 rdbcompression yes rdb模式,是否启动压缩,启用
251 rdbchecksum yes 对rdb数据文件操作时,启动校验
254 dbfilename dump.rdb rdb持久化的快照文件名
264 dir /usr/local/redis/6379/ rdb文件路径
以下是AOF模式配置,这里rdb和AOF持久化都启用
700 appendonly yes 开启AOF持久化
704 appendfilename "appendonly.aof" AOF持久化快照文件名
730 appendfsync everysec 每秒保存至快照
752 no-appendfsync-on-rewrite yes 当rdb写入时,停止aof写入
771 auto-aof-rewrite-percentage 100 aof文件增长100%时,重写
772 auto-aof-rewrite-min-size 64mb aof文件大于64M时,重写
796 aof-load-truncated yes aof文件加载时,忽略最后一条命令
服务重启
[root@localhost redis-5.0.5]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost redis-5.0.5]# ps -ef|grep redis
root 2835 1 0 12:29 ? 00:00:00 /usr/local/redis/bin/redis6379-server 0.0.0.0:6379
root 2840 1672 0 12:29 pts/1 00:00:00 grep --color=auto redis
关闭selinux及配置防火墙
[root@localhost redis-5.0.5]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost redis-5.0.5]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost redis-5.0.5]# firewall-cmd --zone=public --add-port=6380/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=16379/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=16380/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports
80/tcp 22/tcp 6379/tcp 26379/tcp 6380/tcp 16379/tcp 16380/tcp
注:三台都做以上安装及配置
二、集群配置
配置下别名
[root@localhost src]# alias redis-cli="/usr/local/redis/bin/redis-cli"
1、开启redis 集群,在实例配置文件中
更改所有实例配置文件,这里只台粘出一台
vim /usr/local/redis/6379/6379.conf
[root@localhost ~]# vim /usr/local/redis/6379/6379.conf
833 cluster-enabled yes 开启集群
841 cluster-config-file nodes-6379.conf 集群配置文件
847 cluster-node-timeout 15000 主库超时时间
700 appendonly yes 开启AOF持久化
服务重启
[root@localhost ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# /etc/init.d/redis_6380 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# ps -ef|grep redis
root 3115 1 0 14:51 ? 00:00:00 /usr/local/redis/bin/redis6379-server 0.0.0.0:6379 [cluster]
root 3125 1 0 14:51 ? 00:00:00 /usr/local/redis/bin/redis6380-server 0.0.0.0:6380 [cluster]
root 3130 1947 0 14:51 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]#
[root@localhost ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1471/master
tcp 0 0 0.0.0.0:16379 0.0.0.0:* LISTEN 2908/redis6379-serv
tcp 0 0 0.0.0.0:16380 0.0.0.0:* LISTEN 2918/redis6380-serv
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 2908/redis6379-serv
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 2918/redis6380-serv
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1383/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1471/master
tcp6 0 0 :::22 :::* LISTEN 1383/sshd
2、创建集群
在集群中任意一台中进行
[root@localhost ~]# redis-cli --cluster create 192.168.1.21:6379 192.168.1.21:6380 192.168.1.22:6379 192.168.1.22:6380 192.168.1.23:6379 192.168.1.23:6380 --cluster-replicas 1
[root@localhost ~]# redis-cli --cluster create 192.168.1.21:6379 192.168.1.21:6380 192.168.1.22:6379 192.168.1.22:6380 192.168.1.23:6379 192.168.1.23:6380 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.22:6380 to 192.168.1.21:6379
Adding replica 192.168.1.23:6380 to 192.168.1.22:6379
Adding replica 192.168.1.21:6380 to 192.168.1.23:6379
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[5461-10922] (5462 slots) master
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.........
>>> Performing Cluster Check (using node 192.168.1.21:6379)
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
检查主从关系
[root@localhost ~]# redis-cli -p 6379 --cluster check 192.168.1.21:6379
192.168.1.21:6379 (905200f0...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.23:6379 (d1caa936...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.22:6379 (c6338fc8...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.21:6379)
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
由上得出主从关系为
master: 192.168.1.22:6379 -> slave: 192.168.1.23:6380
master: 192.168.1.21:6379 -> slave: 192.168.1.22:6380
master: 192.168.1.23:6379 -> slave: 192.168.1.21:6380
三、集群演示
1、插入数据后
[root@localhost ~]# redis-cli -p 6380 -c 登录集群模式
127.0.0.1:6380> set name toyix
-> Redirected to slot [5798] located at 192.168.1.22:6379
OK
192.168.1.22:6379> get name
"toyix"
192.168.1.22:6379> KEYS *
1) "name"
192.168.1.22:6379>
注:集群上的数据,除了所在主库外,其余都不可直接可,包括此主库的从库,除非加-c 进入集群模式
2、添加节点(主库)
现加入一节点: 192.168.1.24:6379主库
[root@localhost ~]# redis-cli --cluster add-node 192.168.1.24:6379 192.168.1.22:6379
>>> Adding node 192.168.1.24:6379 to cluster 192.168.1.22:6379
>>> Performing Cluster Check (using node 192.168.1.22:6379)
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.24:6379 to make it join the cluster.
[OK] New node added correctly.
检查
[root@localhost ~]# redis-cli --cluster check 192.168.1.23:6379
192.168.1.23:6379 (d1caa936...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.24:6379 (8da8a6b0...) -> 0 keys | 0 slots | 0 slaves.
192.168.1.21:6379 (905200f0...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.22:6379 (c6338fc8...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.23:6379)
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: 8da8a6b054e35f8412e94823be7d62cbe6f4c52e 192.168.1.24:6379 新加入的节点
slots: (0 slots) master
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]#
发现192.168.1.24:6379 ,但因为没有多余的hash槽了,此时,所以要重新分配hash槽
分配hash槽
[root@localhost ~]# redis-cli --cluster reshard 192.168.1.23:6379
How many slots do you want to move (from 1 to 16384)? 3000 分配3000个hash槽
What is the receiving node ID?
8da8a6b054e35f8412e94823be7d62cbe6f4c52e 新节点ID
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.
Source node #1: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 从192.168.1.22上分配
Source node #2: done
Do you want to proceed with the proposed reshard plan
(yes/no)? yes
Moving slot 7931 from 192.168.1.22:6379 to 192.168.1.24:6379:
Moving slot 7932 from 192.168.1.22:6379 to 192.168.1.24:6379:
Moving slot 7933 from 192.168.1.22:6379 to 192.168.1.24:6379:
Moving slot 7934 from 192.168.1.22:6379 to 192.168.1.24:6379:
Moving slot 7935 from 192.168.1.22:6379 to 192.168.1.24:6379:
Moving slot 7936 from 192.168.1.22:6379 to 192.168.1.24:6379:
检查分配后结果
再次添加一节点192.168.1.24:6380
[root@localhost ~]# redis-cli --cluster add-node 192.168.1.24:6380 192.168.1.22:6379
>>> Adding node 192.168.1.24:6380 to cluster 192.168.1.22:6379
>>> Performing Cluster Check (using node 192.168.1.22:6379)
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[8461-10922] (2462 slots) master
1 additional replica(s)
M: 8da8a6b054e35f8412e94823be7d62cbe6f4c52e 192.168.1.24:6379
slots:[5461-8460] (3000 slots) master
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.24:6380 to make it join the cluster.
[OK] New node added correctly.
检查
[root@localhost ~]# redis-cli --cluster check 192.168.1.23:6379
192.168.1.23:6379 (d1caa936...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.24:6379 (8da8a6b0...) -> 1 keys | 3000 slots | 0 slaves.
192.168.1.24:6380 (437378da...) -> 0 keys | 0 slots | 0 slaves.
192.168.1.21:6379 (905200f0...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.22:6379 (c6338fc8...) -> 0 keys | 2462 slots | 1 slaves.
[OK] 1 keys in 5 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.23:6379)
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: 8da8a6b054e35f8412e94823be7d62cbe6f4c52e 192.168.1.24:6379
slots:[5461-8460] (3000 slots) master
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: 437378da815f11a9664f56fbde66c6bd6c0971fb 192.168.1.24:6380 新增加节点
slots: (0 slots) master
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[8461-10922] (2462 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]#
由上可以,新增的节点都为master
检查节点并分槽
[root@localhost ~]# redis-cli --cluster reshard 192.168.1.23:6379
>>> Performing Cluster Check (using node 192.168.1.23:6379)
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: 8da8a6b054e35f8412e94823be7d62cbe6f4c52e 192.168.1.24:6379
slots:[5461-8460] (3000 slots) master
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: 437378da815f11a9664f56fbde66c6bd6c0971fb 192.168.1.24:6380
slots: (0 slots) master
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[8461-10922] (2462 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 3000
What is the receiving node ID? 437378da815f11a9664f56fbde66c6bd6c0971fb
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.
Source node #1: all
Do you want to proceed with the proposed reshard plan
(yes/no)? yes
.......
删除节点
现在是5主3从,删除2个没有从库的节点
将24:6380节点分配给24:6379
[root@localhost ~]# redis-cli --cluster reshard 192.168.1.23:6379
How many slots do you want to move (from 1 to 16384)? 2998
24:6379的ID,表示他要得到hash槽
What is the receiving node ID? 8da8a6b054e35f8412e94823be7d62cbe6f4c52e
24:6380的ID,表示他要分出去2998个hash槽
Source node #1: 437378da815f11a9664f56fbde66c6bd6c0971fb
Source node #2: done
.....
Moving slot 11922 from 437378da815f11a9664f56fbde66c6bd6c0971fb
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 11910 from 192.168.1.24:6380 to 192.168.1.24:6379:
查看节点信息,发现24:6380 hash槽为0了
删除节点
[root@localhost ~]# redis-cli --cluster del-node 192.168.1.24:6380 437378da815f11a9664f56fbde66c6bd6c0971fb
>>> Removing node 437378da815f11a9664f56fbde66c6bd6c0971fb from cluster 192.168.1.24:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
查看节点
[root@localhost ~]# redis-cli --cluster check 192.168.1.23:6379
192.168.1.23:6379 (d1caa936...) -> 0 keys | 4461 slots | 1 slaves.
192.168.1.24:6379 (8da8a6b0...) -> 1 keys | 5449 slots | 0 slaves.
192.168.1.21:6379 (905200f0...) -> 0 keys | 4462 slots | 1 slaves.
192.168.1.22:6379 (c6338fc8...) -> 0 keys | 2012 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.23:6379)
M: d1caa936095ed03513f103ee691d7d1b56e97145 192.168.1.23:6379
slots:[11923-16383] (4461 slots) master
1 additional replica(s)
S: 99556144dc4ff7b7aa7c74c02a9bc828f6bf83b3 192.168.1.22:6380
slots: (0 slots) slave
replicates 905200f086c8e1bd593e0be87d88232b19132829
M: 8da8a6b054e35f8412e94823be7d62cbe6f4c52e 192.168.1.24:6379
slots:[0-998],[5461-8910],[10923-11922] (5449 slots) master
S: 90b6081dafecc3aa3e21bcedfe3584b4a300108c 192.168.1.21:6380
slots: (0 slots) slave
replicates d1caa936095ed03513f103ee691d7d1b56e97145
M: 905200f086c8e1bd593e0be87d88232b19132829 192.168.1.21:6379
slots:[999-5460] (4462 slots) master
1 additional replica(s)
M: c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879 192.168.1.22:6379
slots:[8911-10922] (2012 slots) master
1 additional replica(s)
S: 95853c2d5b17543f90d4377b14287701882167a5 192.168.1.23:6380
slots: (0 slots) slave
replicates c6338fc863dcb7bb9e2d00bbd5890dc8a9d58879
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
找不到24:6380节点了
宕机主节点,再启动
192.168.1.23:6380 从 192.168.1.22:6379 主
停止22:6379节点
[root@localhost ~]# ifconfig|grep 192.168.1.
inet 192.168.1.22 netmask 255.255.255.0 broadcast 192.168.1.255
[root@localhost ~]# ps -ef|grep redis
root 3254 1 0 15:21 ? 00:00:35 /usr/local/redis/bin/redis6379-server 0.0.0.0:6379 [cluster]
root 3257 1 0 15:21 ? 00:00:29 /usr/local/redis/bin/redis6380-server 0.0.0.0:6380 [cluster]
root 3576 1947 0 18:36 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
查看节点信息
23:6380由从变成了主
启动22:6379
[root@localhost ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@localhost ~]# ps -ef|grep redis
root 3257 1 0 15:21 ? 00:00:31 /usr/local/redis/bin/redis6380-server 0.0.0.0:6380 [cluster]
root 3591 1 0 18:41 ? 00:00:00 /usr/local/redis/bin/redis6379-server 0.0.0.0:6379 [cluster]
root 3597 1947 0 18:41 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]# ifconfig|grep 255
inet 192.168.1.22 netmask 255.255.255.0 broadcast 192.168.1.255
查看节点
22:6379变为了23:6380的从
注:
1、删除节点前要先将hash槽分配出去,也可以通过以下脚本来分配hash槽
2、在对集群操作时,尽量不要使用127.0.0.1,要写对外的其他节点的真实IP
3、如果创建出现问题,需要先停止服务,然后删除6379下除了6379.conf、log外所有内容,才可以继续创建,见下
pkill redis rm -rf /usr/local/redis/6379/nodes-6379.conf
rm -rf /usr/local/redis/6379/appendonly.aof
rm -rf/usr/local/redis/6379/dump.rdb
rm -rf /usr/local/redis/6380/dump.rdb
rm -rf /usr/local/redis/6380/nodes-6379.conf
rm -rf /usr/local/redis/6380/appendonly.aof
ls /usr/local/redis/6379/
ls /usr/local/redis/6380/
/etc/init.d/redis_6379 start
/etc/init.d/redis_6380 start
ps -ef|grep redis
netstat -tnlp
-------------------------end