.

参考如下链接:
https://redis.io/topics/cluster-tutorial
http://www.redis.cn/topics/cluster-tutorial.html
https://segmentfault.com/a/1190000008448919

redis常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ /root/redis-5.0.0/src/redis-cli127.0.0.1:6379> 
* set key value #添加键值
* get key #获取键的值
* del key #删除该键
* dbsize #查询当前库中key总数
* keys * #显示当前库中所有key
* flushall #清空所有库所有key数据
* select num #进入某库,默认是0库
* flushdb #与select搭配使用,选选择库然后清除数据
* save # Save是在Redis进程中执行的,由于Redis是单线程实现,所以当save命令在执行时会阻塞Redis服务器一直到该命令执行完成为止。
* bgsave #bgsave命令会先fork出一个子进程,然后在子进程中生成RDB文件。由于在子进程中执行IO操作,所以bgsave命令不会阻塞Redis服务器进程,Redis服务器进程在此期间可以继续对外提供服务。
* mset key1 value1 key2 value2 #存储多条键值(集群中该命令不可用)
* info keyspace #列出所有库中所存key总数
* config get databases #列出所有库的总数(默认16库)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
127.0.0.1:6379> mset 1 111 2 222 3 333
OK
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> mset 1 111 2 222 3 333 4 444
OK
127.0.0.1:6379[1]> select 5
OK
127.0.0.1:6379[5]> mset 1 111 2 222 3 333 4 444 5 555
OK
127.0.0.1:6379[5]> info keyspace
# Keyspacedb0:
keys=3,expires=0,avg_ttl=0
db1:
keys=4,expires=0,avg_ttl=0
db5:
keys=5,expires=0,avg_ttl=0
127.0.0.1:6379[5]> config get databases
1) "databases"
2) "16"

redis集群搭建

3台centos7虚拟机

计算机名 IP地址 端口
k8s1 192.168.26.135 7000,7001,7002
k8s2 192.168.26.136 7003,7004,7005
k8s3 192.168.26.137 7006,7007,7008

三台分别做如下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
k8s1:
$ systemctl stop firewalld #关闭防火墙
$ firewall-cmd --state #查看防火墙状态
not running
$ mkdir -p /system/redis/redis-cluster
$ cd /system/redis
$ wget http://download.redis.io/releases/redis-5.0.0.tar.gz
$ tar zxvf redis-5.0.0.tar.gz
$ cd redis-5.0.0.tar.gz
$ make
$ cp /system/redis/redis-5.0.0/src/redis-server /usr/sbin/redis-server
$ cp /system/redis/redis-5.0.0/src/redis-cli /usr/sbin/redis-cli
$ mkdir -p /system/redis/redis-cluster/{7000,7001,7002}
$ for i in {0..2}; do cp /system/redis/redis-5.0.0/redis.conf /system/redis/redis-cluster/700$i/; done

分别修改这三个配置文件,修改如下内容

1
2
3
4
5
6
7
8
* port 7000 #端口7000,7002,7003
* bind 本机ip //默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
* daemonize yes //redis后台运行
* pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
* cluster-enabled yes //开启集群,把注释#去掉
* cluster-config-file nodes_7000.conf //集群的配置,配置文件首次启动自动生成 7000,7001,7002
* cluster-node-timeout 15000 //请求超时,默认15秒,可自行设置
* appendonly yes //aof日志开启,有需要就开启,它会每次写操作都记录一条日志

在另外两台机器上(k8s2、k8s3)重复以上三步,只是把目录改为7003、7004、7005、7006、7007、7008对应的配置文件也按照这个规则修改即可

三台设备都启动redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
k8s1:
$ cd /system/redis/redis-cluster/7000&&redis-server /system/redis/redis-cluster/7000/redis.conf
$ cd /system/redis/redis-cluster/7001&&redis-server /system/redis/redis-cluster/7001/redis.conf
$ cd /system/redis/redis-cluster/7002&&redis-server /system/redis/redis-cluster/7002/redis.conf

k8s2:
$ cd /system/redis/redis-cluster/7003&&redis-server /system/redis/redis-cluster/7003/redis.conf
$ cd /system/redis/redis-cluster/7004&&redis-server /system/redis/redis-cluster/7004/redis.conf
$ cd /system/redis/redis-cluster/7005&&redis-server /system/redis/redis-cluster/7005/redis.conf

k8s3:
$ cd /system/redis/redis-cluster/7006&&redis-server /system/redis/redis-cluster/7006/redis.conf
$ cd /system/redis/redis-cluster/7007&&redis-server /system/redis/redis-cluster/7007/redis.conf
$ cd /system/redis/redis-cluster/7008&&redis-server /system/redis/redis-cluster/7008/redis.conf

检查redis各节点启动情况

1
2
3
4
5
6
7
8
$ ps -ef | grep redis           //redis是否启动成功
$ netstat -tnlp | grep redis //监听redis端口
tcp 0 0 192.168.26.135:7000 0.0.0.0:* LISTEN 2072/redis-server 1
tcp 0 0 192.168.26.135:7001 0.0.0.0:* LISTEN 2077/redis-server 1
tcp 0 0 192.168.26.135:7002 0.0.0.0:* LISTEN 2082/redis-server 1
tcp 0 0 192.168.26.135:17000 0.0.0.0:* LISTEN 2072/redis-server 1
tcp 0 0 192.168.26.135:17001 0.0.0.0:* LISTEN 2077/redis-server 1
tcp 0 0 192.168.26.135:17002 0.0.0.0:* LISTEN 2082/redis-server 1

如果redis版本为3-4,则需要安装ruby并执行脚本(位于redis压缩包中src目录下)

1
2
3
4
5
安装ruby

$ yum -y install ruby ruby-devel rubygems rpm-build
$ gem install redis
$ /root/redis-5.0.0/src/redis-trib.rb create --replicas 1 192.168.26.135:7000 192.168.26.135:7001 192.168.26.135:7002 192.168.26.136:7003 192.168.26.136:7004 192.168.26.136:7005 192.168.26.137:7006 192.168.26.137:7007 192.168.26.137:7008

如果redis版本为5则命令如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ redis-cli --cluster create 192.168.26.135:7000 192.168.26.135:7001 192.168.26.135:7002 192.168.26.136:7003 192.168.26.136:7004 192.168.26.136:7005 192.168.26.137:7006 192.168.26.137:7007 192.168.26.137:7008 --cluster-replicas 1

执行完输入yes确认以上的配置信息

>>> Performing hash slots allocation on 9 nodes...
Master[0] -> Slots 0 - 4095
Master[1] -> Slots 4096 - 8191
Master[2] -> Slots 8192 - 12287
Master[3] -> Slots 12288 - 16383
Adding replica 192.168.26.136:7004 to 192.168.26.135:7000
Adding replica 192.168.26.137:7007 to 192.168.26.136:7003
Adding replica 192.168.26.135:7002 to 192.168.26.137:7006
Adding replica 192.168.26.136:7005 to 192.168.26.135:7001
Adding extra replicas...
Adding replica 192.168.26.137:7008 to 192.168.26.135:7000
M: c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000
slots:[0-4095] (4096 slots) master
M: 0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001
slots:[12288-16383] (4096 slots) master
S: d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002
replicates a09398047f96dc86e11d4e1476101608163ef8ad
M: 9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003
slots:[4096-8191] (4096 slots) master
S: 768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004
replicates c0a4c9f79df4004039a8833dea38af9323fde4bb
S: a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005
replicates 0b8005efd03a602b31854030619a5b3cbbda2561
M: a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006
slots:[8192-12287] (4096 slots) master
S: 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007
replicates 9c65a69e505ce3693666aa1e4403887789b37e32
S: 550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008
replicates c0a4c9f79df4004039a8833dea38af9323fde4bb
Can I set the above configuration? (type 'yes' to accept):

键入yes接受建议的配置。将配置并加入群集,这意味着实例将被引导为彼此通信。最后,如果一切顺利,你会看到这样的消息:

1
2
[OK] 
All 16384 slots covered

这意味着至少有一个主实例为16384个可用slot提供服务。

查询集群节点信息

1
2
3
4
5
6
7
8
9
10
$  redis-cli -c -h 192.168.26.136 -p 7003 cluster nodes
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542456706374 10 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542456706000 7 connected 8192-12287
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542456707000 7 connected
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542456704000 5 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 master - 0 1542456705368 1 connected 0-4095
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 slave 0b8005efd03a602b31854030619a5b3cbbda2561 0 1542456704555 6 connected
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 master - 0 1542456704354 2 connected 12288-16383
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 myself,slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542456704000 4 connected
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master - 0 1542456707384 10 connected 4096-8191

测试集群中一节点set,令一节点get

1
2
3
4
5
6
7
8
9
10
$ redis-cli -c -p 7006 -h 192.168.26.137
192.168.26.137:7006> set guang zeng
-> Redirected to slot [6151] located at 192.168.26.137:7007
OK
192.168.26.137:7007> exit
$ redis-cli -c -p 7000 -h 192.168.26.135
192.168.26.137:7006> get guang
-> Redirected to slot [6151] located at 192.168.26.137:7007
"zeng"
192.168.26.137:7007> exit

停止192.168.26.137:7007此master节点,看是否还能查get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
停止前
$ redis-cli -c -h 192.168.26.136 -p 7003 cluster nodes
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542456706374 10 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542456706000 7 connected 8192-12287
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542456707000 7 connected
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542456704000 5 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 master - 0 1542456705368 1 connected 0-4095
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 slave 0b8005efd03a602b31854030619a5b3cbbda2561 0 1542456704555 6 connected
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 master - 0 1542456704354 2 connected 12288-16383
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 myself,slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542456704000 4 connected
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master - 0 1542456707384 10 connected 4096-8191
停止后
$ redis-cli -c -h 192.168.26.136 -p 7003 cluster nodes
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 master - 0 1542457349356 11 connected 4096-8191
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542457347320 7 connected 8192-12287
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542457346000 7 connected
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542457348000 5 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 master - 0 1542457348343 1 connected 0-4095
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 slave 0b8005efd03a602b31854030619a5b3cbbda2561 0 1542457348000 6 connected
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 master - 0 1542457343000 2 connected 12288-16383
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 myself,slave 550c49ec567b4c0cb9eecf778e4473859606fc6d 0 1542457342000 4 connected
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master,fail - 1542457161169 1542457158949 10 disconnected
查询key仍可以查到
$ redis-cli -c -p 7000 -h 192.168.26.135
192.168.26.135:7000> get guang
-> Redirected to slot [6151] located at 192.168.26.137:7008
"zeng"

停止192.168.26.137:7008 master节点看192.168.26.136:7003是否会成为master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
192.168.26.135:7001> get guang
-> Redirected to slot [6151] located at 192.168.26.136:7003
"zeng"

$ redis-cli -c -h 192.168.26.136 -p 7003 cluster nodes
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 master,fail - 1542457752803 1542457752602 11 disconnected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542457827000 7 connected 8192-12287
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542457830000 7 connected
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542457830974 5 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 master - 0 1542457830000 1 connected 0-4095
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 slave 0b8005efd03a602b31854030619a5b3cbbda2561 0 1542457826000 6 connected
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 master - 0 1542457827923 2 connected 12288-16383
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 myself,master - 0 1542457828000 12 connected 4096-8191
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master,fail - 1542457161169 1542457158949 10 disconnected

总上述实验得知,redis集群3主2从,如果其中任一个master节点kill掉,都会由其他2个从节点替换成为新master,原master启动后会变成从节点;如其中从节点kill掉再启用后会同步master数据。
启动时注意要在原*.node路径下启动kill掉的集群,否则会有问题。

手动故障转移(用7005替换7001)注意要在该主节点的从节点上操作cluster failover

其基本过程如下:客户端不再链接我们淘汰的主节点,同时主节点向从节点发送复制偏移量,从节点得到复制偏移量后故障转移开始,接着通知主节点进行配置切换,当客户端在旧的master上解锁后重新连接到新的主节点上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ redis-cli -c -h 192.168.26.135 -p 7000 cluster nodes
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542459284374 7 connected
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 slave 0b8005efd03a602b31854030619a5b3cbbda2561 0 1542459286397 6 connected
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542459288436 5 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 myself,master - 0 1542459286000 1 connected 0-4095
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 master - 0 1542459285000 2 connected 12288-16383
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master - 0 1542459287405 13 connected 4096-8191
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542459285000 13 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542459285385 7 connected 8192-12287
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542459283366 13 connected

在192.168.26.136设备中执行:
$ redis-cli -c -h 192.168.26.136 -p 7005 cluster failover
OK

$ redis-cli -c -h 192.168.26.135 -p 7000 cluster nodes
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542459383211 7 connected
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 master - 0 1542459383000 14 connected 12288-16383
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542459385228 5 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 myself,master - 0 1542459382000 1 connected 0-4095
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 slave a11c0878e4feab9bcf3c85d2a517b883670137ab 0 1542459383516 14 connected
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master - 0 1542459385731 13 connected 4096-8191
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542459384221 13 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542459384000 7 connected 8192-12287
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542459382000 13 connected

k8s3中添加一个主节点7009加入到现有redis集群中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ cd /system/redis/redis-cluster
$ mkdir 7009
$ cp 7008/redis.conf 7009/
$ vim 7009/redis.conf(将里面的7008更改为7009 0,$s/7008/7009/g)
$ cd 7009
$ redis-server redis.conf #启动7009节点
$ redis-cli -h 192.168.26.137 -p 7006 -c --cluster add-node 192.168.26.137:7009 192.168.26.137:7006 #将7009节点加入到现有redis集群中

$ redis-cli -c -h 192.168.26.135 -p 7000 cluster nodes #查询7009节点已加入到集群中成为master
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542461307229 7 connected
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 master - 0 1542461308000 14 connected 12288-16383
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542461307000 5 connected
78a6892d261c4756603d09a6062d6e6a48b39a27 192.168.26.137:7009@17009 master - 0 1542461309241 0 connected
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 myself,master - 0 1542461305000 1 connected 0-4095
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 slave a11c0878e4feab9bcf3c85d2a517b883670137ab 0 1542461307000 14 connected
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 master - 0 1542461305000 13 connected 4096-8191
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542461307000 13 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542461308739 7 connected 8192-12287
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542461309000 13 connected

新节点现在已经连接上了集群, 成为集群的一份子, 并且可以对客户端的命令请求进行转向了, 但是和其他主节点相比, 新节点还有两点区别:

  • 新节点没有包含任何数据, 因为它没有包含任何哈希槽.
  • 尽管新节点没有包含任何哈希槽, 但它仍然是一个主节点, 所以在集群需要将某个从节点升级为新的主节点时, 这个新节点不会被选中。

添加7009从节点方法:

1
$ redis-cli -h 192.168.26.137 -p 7006 -c --cluster add-node 192.168.26.137:7010 192.168.26.137:7006 --cluster-slave --cluster-master-id 78a6892d261c4756603d09a6062d6e6a48b39a27

redis分片

如上述目前master节点7009目前没有slot,现在可以给他划分1000slots

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$ redis-cli -h 192.168.26.137 -p 7009 -c --cluster reshard 192.168.26.137:7008
How many slots do you want to move (from 1 to 16384)?1000(输入要划分出来的slots)
What is the receiving node ID?78a6892d261c4756603d09a6062d6e6a48b39a27(要接收slots的master节点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:(这里可以添加从多个原master节点ID,最后输入done;也可以输入all,意思是平均从其他master节点处分片出来,凑齐1000slots)

Do you want to proceed with the proposed reshard plan (yes/no)? yes (确认划分)
等待完成即可
192.168.26.137:7007> cluster nodes(未分片前)
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542465997557 16 connected
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542465996000 13 connected
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 master - 0 1542465997000 14 connected 12288-16383
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 master - 0 1542465995000 16 connected 0-4095 8192
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542465997151 13 connected
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 slave a11c0878e4feab9bcf3c85d2a517b883670137ab 0 1542465998163 14 connected
78a6892d261c4756603d09a6062d6e6a48b39a27 192.168.26.137:7009@17009 master - 0 1542465995000 0 connected
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 myself,master - 0 1542465994000 13 connected 4096-8191
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542465996145 7 connected
57fefbbf7295a65d9cceb62dab860c952c493dde 192.168.26.137:7010@17010 slave 78a6892d261c4756603d09a6062d6e6a48b39a27 0 1542465994125 0 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542465995136 7 connected 8193-12287
192.168.26.137:7007> cluster nodes(分片后)
768225f7fd95218f47747b1113d6a34439beb68b 192.168.26.136:7004@17004 slave c0a4c9f79df4004039a8833dea38af9323fde4bb 0 1542466396000 16 connected
9c65a69e505ce3693666aa1e4403887789b37e32 192.168.26.136:7003@17003 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542466400000 13 connected
a11c0878e4feab9bcf3c85d2a517b883670137ab 192.168.26.136:7005@17005 master - 0 1542466401639 14 connected 12538-16383
c0a4c9f79df4004039a8833dea38af9323fde4bb 192.168.26.135:7000@17000 master - 0 1542466398598 16 connected 251-4095 8192
550c49ec567b4c0cb9eecf778e4473859606fc6d 192.168.26.137:7008@17008 slave 381a3355a3cf97f0f07e783a4580dc8d07da8c9f 0 1542466399605 13 connected
0b8005efd03a602b31854030619a5b3cbbda2561 192.168.26.135:7001@17001 slave a11c0878e4feab9bcf3c85d2a517b883670137ab 0 1542466400631 14 connected
78a6892d261c4756603d09a6062d6e6a48b39a27 192.168.26.137:7009@17009 master - 0 1542466400000 17 connected 0-250 4096-4345 8193-8441 12288-12537
381a3355a3cf97f0f07e783a4580dc8d07da8c9f 192.168.26.137:7007@17007 myself,master - 0 1542466397000 13 connected 4346-8191
d95d7508646992b00a55ecd875175c0e78e91124 192.168.26.135:7002@17002 slave a09398047f96dc86e11d4e1476101608163ef8ad 0 1542466397000 7 connected
57fefbbf7295a65d9cceb62dab860c952c493dde 192.168.26.137:7010@17010 slave 78a6892d261c4756603d09a6062d6e6a48b39a27 0 1542466398000 17 connected
a09398047f96dc86e11d4e1476101608163ef8ad 192.168.26.137:7006@17006 master - 0 1542466397000 7 connected 8442-12287
192.168.26.137:7007> exit

编写重新分区操作的脚本
可以自动执行重新分片,而无需以交互方式手动输入参数。这可以使用如下命令行:

1
$ redis-cli reshard <host>:<port> --cluster-from <node-id> --cluster-to <node-id> --cluster-slots <number of slots> --cluster-yes

如果您可能经常重新设置,则允许构建一些自动操作,但是目前无法redis-cli自动重新平衡群集,检查跨群集节点的密钥分发以及根据需要智能地移动插槽。此功能将在未来添加。

删除redis集群中节点,注意如果是从节点可直接删除,如果是master节点,需要将master节点中的slots分片到其他master节点中去,并删除其下的从节点,最后再删除master节点。

1
redis-cli -h 192.168.26.137 -p 7008 -c --cluster del-node 192.168.26.137:7010 57fefbbf7295a65d9cceb62dab860c952c493dde

redis集群总结

引用:https://segmentfault.com/a/1190000008448919

redis cluster在设计的时候,就考虑到了去中心化、去中间件,也就是说,集群中的每个节点都是平等关系,都是对等的,每个节点都保存各自的数据和整个集群的状态。每个节点都和其他所有节点连接,而且这些连接保持活跃,这样就保证了我们只需要连接集群中的任意一个节点,就可以获取到其他节点的数据。
Redis 集群没有并使用传统的一致性哈希来分配数据,而是采用另外一种叫做哈希槽 (hash slot)的方式来分配的。redis cluster 默认分配了 16384 个 slot,当我们 set 一个 key 时,会用CRC16算法来取模得到所属的 slot,然后将这个 key 分到哈希槽区间的节点上,具体算法就是:CRC16(key) % 16384。所以我们在测试的时候看到 set 和 get 的时候,直接跳转到了7000端口的节点。
Redis 集群会把数据存在一个 master 节点,然后在这个 master 和其对应的 salve 之间进行数据同步。当读取数据时,也根据一致性哈希算法到对应的 master 节点获取数据。只有当一个 master 挂掉之后,才会启动一个对应的 salve 节点,充当 master 。

需要注意的是:必须要3个或以上的主节点,否则在创建集群时会失败,并且当存活的主节点数小于总节点数的一半时,整个集群就无法提供服务了。