基于redis5的session共享:【redis 5.x集群应用研究】

隐身守侯 提交于 2020-03-18 07:24:31

基于springsession构建一个session共享的模块。 这里,基于redis的集群(Redis-5.0.3版本),为了解决整个物联网平台的各个子系统之间共享session需求,且方便各个子系统使用,将这个模块设计为一个pom组件,直接在pom.xml文件里面配置为dependency。

 

今天的主题,就是redis 5.0.3环境的构建。

和我之前介绍过的redis 3.2.8 (https://www.cnblogs.com/shihuc/p/7882004.html)有些类似,只是redis 5里面,不再依赖ruby脚本进行集群配置,而是直接用c程序实现,直接基于redis-cli指令完成。这些就不做介绍,直接介绍如何配置。

 

首先,需要修改的配置项如下:

bind 10.95.200.12
protected-mode no
port 7380
daemonize yes
pidfile /var/run/redis_7380.pid
dbfilename dump-7380.rdb
appendonly yes
appendfilename "appendonly-7380.aof"
cluster-enabled yes
cluster-config-file nodes-7380.conf
cluster-node-timeout 15000
notify-keyspace-events "Ex"

我的配置环境,是3对主从,上面的配置项,是其中一个redis节点的信息,IP是10.95.200.12,端口为7380. 参照这个配置信息,将下面的几个节点都配置上,我这里一共有三台虚拟机,分别是IP:10.95.200.12,10.95.200.13, 10.95.200.14,每台机器上部署两个实例,分别为端口7380以及7381.

 

配置信息配置好后,需要将每一个实例启动起来。例如,下面启动10.95.200.12端口为7380的实例。

[tkiot@tkwh-kfcs-app2 redis]$ ./bin/redis-server redis-7380.conf 
23941:C 10 Jul 2019 08:34:05.607 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23941:C 10 Jul 2019 08:34:05.607 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=23941, just started
23941:C 10 Jul 2019 08:34:05.607 # Configuration loaded

这里有个小插曲

每个服务器上的redis的配置文件,能够正常运行的配置文件名称如上面:redis-7380.conf,刚开始,我没有太注意,或者说是自己的马虎,将这个配置文件命名为nodes-7380.conf,和配置文件内容配置项cluster-config-file的值搞成一样的了,真是想骂娘的这个坑,启动过程同样显示上面的输出,和正确的程序启动一样的效果,但是呢,ps查看进程后,发现没有redis的进程。。。。这个坑,让我查了一上午。。。。

 

上面指令启动redis-7380.conf的操作结束后,可以查看下启动状态(cluster nodes):

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7380
10.95.200.12:7380> cluster nodes
7f4cf1bffc7e42a0e2d15bcc5a0a5386711813e8 :7380@17380 myself,master - 0 0 0 connected

 

先看看cluster相关的指令都有哪些:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster help           
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help           

接下来,就是通过redis-cli创建集群,配置成为一个shell脚本

#!/bin/bash
/u02/redis/bin/redis-cli --cluster create 10.95.200.12:7380 10.95.200.13:7380 10.95.200.14:7380 10.95.200.12:7381 10.95.200.13:7381 10.95.200.14:7381 --cluster-replicas 1

这里,脚本当中,一共6个节点,不能确定哪些是master,哪些是slave,主从关系,是在构建集群的时候,自动配置的。

 

集群查看节点常用指令:

CLUSTER INFO 打印集群的信息  
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。
[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7380
10.95.200.12:7380> 
10.95.200.12:7380> cluster nodes
ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380@17380 master - 0 1562720545267 2 connected 5461-10922
2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381@17381 slave cf6ca00cb36850762fdff1223684edf1fb9bd4ba 0 1562720546000 4 connected
cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380@17380 master - 0 1562720546000 3 connected 10923-16383
26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380@17380 myself,slave 467d4c7508d1cb371ed52c4c6574506cba40c328 0 1562720546000 1 connected
467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381@17381 master - 0 1562720547271 8 connected 0-5460
fbece4571b50904d93a45afbcce66941d53a45b5 10.95.200.14:7381@17381 slave ed309033dbefe2b0b64ad7fb643c4d2531e53b95 0 1562720546269 6 connected

 

剔除掉一个指定的节点(通过nodeId进行指示要删除的节点,nodeId就是上面cluster nodes输出结果的最左边的那一列

10.95.200.12:7380> cluster forget ed309033dbefe2b0b64ad7fb643c4d2531e53b95
OK
10.95.200.12:7380> 
10.95.200.12:7380> cluster nodes
2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381@17381 slave cf6ca00cb36850762fdff1223684edf1fb9bd4ba 0 1562720579324 4 connected
cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380@17380 master - 0 1562720578323 3 connected 10923-16383
26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380@17380 myself,slave 467d4c7508d1cb371ed52c4c6574506cba40c328 0 1562720576000 1 connected
467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381@17381 master - 0 1562720577320 8 connected 0-5460
fbece4571b50904d93a45afbcce66941d53a45b5 10.95.200.14:7381@17381 slave - 0 1562720577000 6 connected
10.95.200.12:7380>

不能自己把自己忘记哟:

10.95.200.12:7380> cluster forget 26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135
(error) ERR I tried hard but I can't forget myself...
10.95.200.12:7380>

 

不能将自己的master忘记哟:

10.95.200.12:7380> cluster forget 467d4c7508d1cb371ed52c4c6574506cba40c328
(error) ERR Can't forget my master!
10.95.200.12:7380> 

上面的forget指令是不能讲节点真实删除的,只是一段时间内forget遗忘而已。因为过一段时间后,再次执行cluster nodes指令,还是可以看到完整集群的节点。

 

删除集群节点,可以采用下面的指令:

redis-cli --cluster del-node <ip>:<port> <node_id>

 

下面看看我的操作,这里是一个错误的操作,结果如下:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster del-node 10.95.200.12:7380 ed309033dbefe2b0b64ad7fb643c4d2531e53b95
>>> Removing node ed309033dbefe2b0b64ad7fb643c4d2531e53b95 from cluster 10.95.200.12:7380
[ERR] Node 10.95.200.13:7380 is not empty! Reshard data away and try again.

1)查看Node 10.95.200.13:7380上是否有信息:

10.95.200.13:7380> keys *
1) "taikang#session:sessions:expires:b6cd8269-dff0-463e-aefb-03167a167292"

2)清除所有的内容:

10.95.200.13:7380> flushdb 
OK

到此,继续执行redis-cli --cluster del-node 10.95.200.12:7380 ed309033dbefe2b0b64ad7fb643c4d2531e53b95,最终还是失败,不成功的原因一样,不是空节点。为何?原因是没有依据redis的指令说明在使用del-node指令

 

注意,删除节点,按照上面的操作,显然是不对的,提示Node不是空的,需要将数据重新分片到其他节点,这个提示,有点不是太好理解,依据redis的集群工作原理,是全对等(master之间)节点集群,删除节点有三个步骤:

A. 首先删除slave节点,命令:redis-cli --cluster del-node <ip>:<port> <node_id>
B. 将被删除的slave对应的master节点的slot进行reshard到其他节点,命令:redis-cli --cluster reshard <master的ip:port> --cluster-from <同一个master的node_id> --cluster-to <接收slot的master的node_id> --cluster-slots <将要参与分片的slot数量,这里就是待删除节点的全部slot> --cluster-yes
C. 将reshard后的master执行删除操作,命令:redis-cli --cluster del-node <ip>:<port> <node_id>

 

下面演示基本操作redis cluster

操作前的节点信息:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7380 cluster nodes
ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380@17380 master - 0 1563276949623 11 connected 0-5459 5461-16382
2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381@17381 slave cf6ca00cb36850762fdff1223684edf1fb9bd4ba 0 1563276951624 10 connected
cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380@17380 master - 0 1563276950000 10 connected 16383
26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380@17380 myself,slave 467d4c7508d1cb371ed52c4c6574506cba40c328 0 1563276950000 1 connected
fbece4571b50904d93a45afbcce66941d53a45b5 10.95.200.14:7381@17381 slave ed309033dbefe2b0b64ad7fb643c4d2531e53b95 0 1563276950000 11 connected
467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381@17381 master - 0 1563276950624 8 connected 5460

1. 删除节点:

1)先删除slave节点

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster del-node 10.95.200.14:7381 fbece4571b50904d93a45afbcce66941d53a45b5
>>> Removing node fbece4571b50904d93a45afbcce66941d53a45b5 from cluster 10.95.200.14:7381
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

slave节点删除后的结构:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7380 cluster nodes                                     
ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380@17380 master - 0 1563277493737 11 connected 0-5459 5461-16382
2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381@17381 slave cf6ca00cb36850762fdff1223684edf1fb9bd4ba 0 1563277492000 10 connected
cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380@17380 master - 0 1563277493000 10 connected 16383
26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380@17380 myself,slave 467d4c7508d1cb371ed52c4c6574506cba40c328 0 1563277493000 1 connected
467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381@17381 master - 0 1563277494738 8 connected 5460

 

2)再对刚才删除的slave的master进行slot的reshard:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster reshard 10.95.200.13:7381 --cluster-from 467d4c7508d1cb371ed52c4c6574506cba40c328 --cluster-to ed309033dbefe2b0b64ad7fb643c4d2531e53b95 --cluster-slots 5460 --cluster-yes                >>> Performing Cluster Check (using node 10.95.200.13:7381)
M: 467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381
   slots:[5460] (1 slots) master
   1 additional replica(s)
M: ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380
   slots:[0-5459],[5461-16382] (16382 slots) master
S: 26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380
   slots: (0 slots) slave
   replicates 467d4c7508d1cb371ed52c4c6574506cba40c328
M: cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380
   slots:[16383] (1 slots) master
   1 additional replica(s)
S: 2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381
   slots: (0 slots) slave
   replicates cf6ca00cb36850762fdff1223684edf1fb9bd4ba
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Ready to move 5460 slots.
  Source nodes:
    M: 467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381
       slots:[5460] (1 slots) master
       1 additional replica(s)
  Destination node:
    M: ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380
       slots:[0-5459],[5461-16382] (16382 slots) master
  Resharding plan:
    Moving slot 5460 from 467d4c7508d1cb371ed52c4c6574506cba40c328
Moving slot 5460 from 10.95.200.13:7381 to 10.95.200.13:7380: 

参数基本说明:

--cluster-from:表示分片的源头节点,即从这个标识指定的参数节点上将分片分配出去给别的节点,参数节点可以有多个,用逗号分隔。

--cluster-to:和--cluster-from相对,标识接收分片的节点,这个参数指定的节点只能有一个。

--cluster-slots:参与重新分片的slot号。

--cluster-yes:不用显示分片的过程信息,直接后台操作。

 

操作后的集群信息:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7380 cluster nodes
ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380@17380 master - 0 1563277825000 11 connected 0-16382
2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381@17381 slave cf6ca00cb36850762fdff1223684edf1fb9bd4ba 0 1563277825000 10 connected
cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380@17380 master - 0 1563277825386 10 connected 16383
26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380@17380 myself,slave ed309033dbefe2b0b64ad7fb643c4d2531e53b95 0 1563277824000 1 connected
467d4c7508d1cb371ed52c4c6574506cba40c328 10.95.200.13:7381@17381 master - 0 1563277826390 8 connected

标红的节点,已经connected的slot没有了,在reshard之前,他有5460

 

3)将master进行下线操作:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster del-node 10.95.200.13:7381 467d4c7508d1cb371ed52c4c6574506cba40c328
>>> Removing node 467d4c7508d1cb371ed52c4c6574506cba40c328 from cluster 10.95.200.13:7381
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

操作后的集群信息(是不是从原来的6个节点变成了现在的4个了):

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7380 cluster nodes                                     
ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380@17380 master - 0 1563278285304 11 connected 0-16382
2cb8c6a7c86f512db2c6ca88eb67449197f5b885 10.95.200.12:7381@17381 slave cf6ca00cb36850762fdff1223684edf1fb9bd4ba 0 1563278286304 10 connected
cf6ca00cb36850762fdff1223684edf1fb9bd4ba 10.95.200.14:7380@17380 master - 0 1563278284000 10 connected 16383
26c3a7cd48fe4bddf9cf0c60a3c30b6b1f273135 10.95.200.12:7380@17380 myself,slave ed309033dbefe2b0b64ad7fb643c4d2531e53b95 0 1563278284000 1 connected

 

依据上面的操作,全部删除后,最后只剩下一个master了,就直接kill掉,停服务就好了。

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.13 -p 7380 cluster nodes 
ed309033dbefe2b0b64ad7fb643c4d2531e53b95 10.95.200.13:7380@17380 myself,master - 0 1563321180000 11 connected 0-16383

 

下面记录一下创建一组4个节点的集群的操作,并添加一主一从的过程。

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster create 10.95.200.12:7380 10.95.200.13:7380 10.95.200.12:7381 10.95.200.13:7381 --cluster-replicas 1
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 4 nodes and 1 replicas per node.
*** At least 6 nodes are required.

这个过程失败了,呵呵。那就6个节点吧,然后删除再添加

 

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster create 10.95.200.12:7380 10.95.200.13:7380 10.95.200.14:7380 10.95.200.12:7381 10.95.200.13:7381 10.95.200.14:7381 --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 10.95.200.13:7381 to 10.95.200.12:7380
Adding replica 10.95.200.12:7381 to 10.95.200.13:7380
Adding replica 10.95.200.14:7381 to 10.95.200.14:7380
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
。。。。。
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

删除两个节点,10.95.200.13:7380 10.95.200.14:7381,删除的过程不多记录(当前是4个节点)。

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7381 cluster nodes                                     
f855a20340af517e1502f610d82e8280cc6cd803 10.95.200.12:7381@17381 myself,slave 1591155d7df58218a26974b16996eeeba88c84f1 0 1563331430000 4 connected
1cf9c2ef22a7ce7831cfee357aca36bfb6f275b5 10.95.200.13:7381@17381 slave d46ae46cf66445ddeba923d6af84b78ca5f789cb 0 1563331431232 7 connected
d46ae46cf66445ddeba923d6af84b78ca5f789cb 10.95.200.12:7380@17380 master - 0 1563331430000 7 connected 0-10922
1591155d7df58218a26974b16996eeeba88c84f1 10.95.200.14:7380@17380 master - 0 1563331430229 3 connected 10923-16383

记得删除掉的节点,其实已经shutdown了,要将他们起起来后,再执行添加操作。

 

1)将新的节点10.95.200.13:7380添加到集群(选择一个已经在集群中的节点即可)

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster add-node 10.95.200.13:7380 10.95.200.14:7380
>>> Adding node 10.95.200.13:7380 to cluster 10.95.200.14:7380
>>> Performing Cluster Check (using node 10.95.200.14:7380)
M: 1591155d7df58218a26974b16996eeeba88c84f1 10.95.200.14:7380
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 1cf9c2ef22a7ce7831cfee357aca36bfb6f275b5 10.95.200.13:7381
   slots: (0 slots) slave
   replicates d46ae46cf66445ddeba923d6af84b78ca5f789cb
S: f855a20340af517e1502f610d82e8280cc6cd803 10.95.200.12:7381
   slots: (0 slots) slave
   replicates 1591155d7df58218a26974b16996eeeba88c84f1
M: d46ae46cf66445ddeba923d6af84b78ca5f789cb 10.95.200.12:7380
   slots:[0-10922] (10923 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.
>>> Send CLUSTER MEET to node 10.95.200.13:7380 to make it join the cluster.
[OK] New node added correctly.

加完后,看看集群:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7381 cluster nodes              
f855a20340af517e1502f610d82e8280cc6cd803 10.95.200.12:7381@17381 myself,slave 1591155d7df58218a26974b16996eeeba88c84f1 0 1563332042000 4 connected
1cf9c2ef22a7ce7831cfee357aca36bfb6f275b5 10.95.200.13:7381@17381 slave d46ae46cf66445ddeba923d6af84b78ca5f789cb 0 1563332044428 7 connected
4fd334f64bc5b121f5810da3b5800a29d4e8c3ee 10.95.200.13:7380@17380 master - 0 1563332044000 0 connected
d46ae46cf66445ddeba923d6af84b78ca5f789cb 10.95.200.12:7380@17380 master - 0 1563332043426 7 connected 0-10922
1591155d7df58218a26974b16996eeeba88c84f1 10.95.200.14:7380@17380 master - 0 1563332043000 3 connected 10923-16383

 

2)新加入的一个节点默认会变成master,下面给这个master分配slot,就是一个reshard的过程

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster reshard 10.95.200.13:7380 --cluster-from d46ae46cf66445ddeba923d6af84b78ca5f789cb,1591155d7df58218a26974b16996eeeba88c84f1 --cluster-to 4fd334f64bc5b121f5810da3b5800a29d4e8c3ee --cluster-slots 5642

添加完后,看看集群:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7381 cluster nodes
f855a20340af517e1502f610d82e8280cc6cd803 10.95.200.12:7381@17381 myself,slave 1591155d7df58218a26974b16996eeeba88c84f1 0 1563332267000 4 connected
1cf9c2ef22a7ce7831cfee357aca36bfb6f275b5 10.95.200.13:7381@17381 slave d46ae46cf66445ddeba923d6af84b78ca5f789cb 0 1563332267021 7 connected
4fd334f64bc5b121f5810da3b5800a29d4e8c3ee 10.95.200.13:7380@17380 master - 0 1563332268022 8 connected 0-3761 10923-12802
d46ae46cf66445ddeba923d6af84b78ca5f789cb 10.95.200.12:7380@17380 master - 0 1563332267521 7 connected 3762-10922
1591155d7df58218a26974b16996eeeba88c84f1 10.95.200.14:7380@17380 master - 0 1563332267000 3 connected 12803-16383

 

3)给刚才添加的master节点添加slave节点

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli --cluster add-node 10.95.200.14:7381 10.95.200.13:7380 --cluster-slave --cluster-master-id 4fd334f64bc5b121f5810da3b5800a29d4e8c3ee
>>> Adding node 10.95.200.14:7381 to cluster 10.95.200.13:7380
>>> Performing Cluster Check (using node 10.95.200.13:7380)
M: 4fd334f64bc5b121f5810da3b5800a29d4e8c3ee 10.95.200.13:7380
   slots:[0-3761],[10923-12802] (5642 slots) master
   1 additional replica(s)
S: 1cf9c2ef22a7ce7831cfee357aca36bfb6f275b5 10.95.200.13:7381
   slots: (0 slots) slave
   replicates 4fd334f64bc5b121f5810da3b5800a29d4e8c3ee
M: d46ae46cf66445ddeba923d6af84b78ca5f789cb 10.95.200.12:7380
   slots:[3762-10922] (7161 slots) master
M: 1591155d7df58218a26974b16996eeeba88c84f1 10.95.200.14:7380
   slots:[12803-16383] (3581 slots) master
   1 additional replica(s)
S: f855a20340af517e1502f610d82e8280cc6cd803 10.95.200.12:7381
   slots: (0 slots) slave
   replicates 1591155d7df58218a26974b16996eeeba88c84f1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.95.200.14:7381 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 10.95.200.13:7380.
[OK] New node added correctly.

最后再看看集群:

[tkiot@tkwh-kfcs-app2 redis]$ redis-cli -c -h 10.95.200.12 -p 7381 cluster nodes
f855a20340af517e1502f610d82e8280cc6cd803 10.95.200.12:7381@17381 myself,slave 1591155d7df58218a26974b16996eeeba88c84f1 0 1563332620000 4 connected
f6093d443470ae37b8330407d20291ae959fc22f :0@0 slave,fail,noaddr d46ae46cf66445ddeba923d6af84b78ca5f789cb 1563332505335 1563332504432 7 disconnected
1cf9c2ef22a7ce7831cfee357aca36bfb6f275b5 10.95.200.13:7381@17381 slave d46ae46cf66445ddeba923d6af84b78ca5f789cb 0 1563332621533 8 connected
4fd334f64bc5b121f5810da3b5800a29d4e8c3ee 10.95.200.13:7380@17380 master - 0 1563332621633 8 connected 0-3761 10923-12802
d46ae46cf66445ddeba923d6af84b78ca5f789cb 10.95.200.12:7380@17380 master - 0 1563332621533 7 connected 3762-10922
1591155d7df58218a26974b16996eeeba88c84f1 10.95.200.14:7380@17380 master - 0 1563332621000 3 connected 12803-16383
9f069b1f62cc93b95cb432f3726bc5bbfb0c8c76 10.95.200.14:7381@17381 slave 4fd334f64bc5b121f5810da3b5800a29d4e8c3ee 0 1563332620533 8 connected

 

 

总结一下集群节点添加的过程
A) 增加节点(前提是对应节点服务器已经运行了),默认成为master节点,命令:redis-cli --cluster add-node <new_ip:new_port> <existing_ip:existing_port>
B) 给新增加的这个master节点分配slot,命令:redis-cli --cluster reshard <master_ip:master_port> --cluster-from <集群已存在master的nodeId,多个需要逗号分隔> --cluster-to <当前master的nodeId> --cluster-slots <分配slot的数量>
C) 给这个master添加slave,slot就不用了,因为是slave,命令:redis-cli --cluster add-node <slave_ip:slave_port> <master_ip:master_port> --cluster-slave --cluster-master-id <mastr node_id>

 

最后,有一个问题,没有搞清楚,就是将节点从集群删除后,再添加进去的时候,总会遇到提示说该节点不是空节点,导致添加失败,采取的策略就是将待添加的节点对应的数据文件全部删除后,重新启动,再执行添加操作指令,就成功了

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!