首页 > 数据库 >10、Redis集群模式

10、Redis集群模式

时间:2023-03-08 15:46:07浏览次数:44  
标签:10 Redis redis 192.168 cluster 集群 master slots connected

1.Redis主从模式和cluster分片集群区别
redis主从模式,是所有Redis数据一致,但是key过多了会影响性能
cluster分片集群,可以将数据分散到多个Redis节点,数据分片存储,能够提高redis的吞吐量

2.Redis Cluster集群特点
数据分片
多个入口
故障自动切换

3.Redis cluster集群的搭建架构
Redis集群至少需要三主三从
三从是保证主有问题时能够切换

4.Redis自带集群搭建

a).目录和机器规划:
目录: /data/cluster
三主: 192.168.27.128:7000                     192.168.27.129:7001                       192.168.27.130:7002
三从: 192.168.27.128:8000(7002的从)      192.168.27.129:8001(7000的从)       192.168.27.130:8002(7001的从)
注意:上面设计是为了避免主从都在同一个节点,防止同一个节点挂了后,主从都挂掉,就没法进行主从切换,导致数据丢失

b).修改redis的配置文件:           6台redis配置都一样,只是下面两项:端口和路径不一样
vim /data/cluster/7000/redis.conf          各自目录下创建配置文件: 7000 7001  7002  8000  8001  8002
cluster-enabled yes
port 7000
dir "/data/cluster/7000"
logfile "redis.log"
pidfile "redis.pid"
daemonize yes
bind 0.0.0.0
requirepass redispwd
masterauth redispwd
tcp-backlog 1024
tcp-keepalive 0
loglevel notice
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
save 900 1
save 300 10
save 60 10000
dbfilename "dump.rdb"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

c).先搭建3主集群
(1).先启动3个主redis   192.168.27.128:7000      192.168.27.129:7001       192.168.27.130:7002
128上:
# mkdir /data/cluster/7000 -p
# redis-server /data/cluster/7000/redis.conf 
# ps -ef |grep redis
root       2285      1  1 22:55 ?        00:00:00 redis-server 0.0.0.0:7000 [cluster]
root       2226   1362  0 22:55 pts/0    00:00:00 grep --color=auto redis

129上:
# mkdir /data/cluster/7001 -p
# redis-server /data/cluster/7001/redis.conf 
# ps -ef |grep redis
root       4385      1  0 22:56 ?        00:00:00 redis-server 0.0.0.0:7001 [cluster]
root       4354   1356  0 22:56 pts/0    00:00:00 grep --color=auto redis

130上:
# mkdir /data/cluster/7002 -p
# redis-server /data/cluster/7002/redis.conf 
# ps -ef |grep redis
root       2298      1  0 22:57 ?        00:00:00 redis-server 0.0.0.0:7002 [cluster]
root       2270   1370  0 22:57 pts/0    00:00:00 grep --color=auto redis

(2).创建3主集群,在任何一个节点上操作都可以
# redis-cli -a redispwd --cluster create 192.168.27.128:7000 192.168.27.129:7001 192.168.27.130:7002
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 slots) master
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
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.27.128:7000)
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 slots) master
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

(3).查看3主集群:  在任何一个节点上操作都行,下面以7000端口这个redis为例查看集群信息
# redis-cli -a redispwd -p 7000 cluster info
# redis-cli -a redispwd -p 7000 cluster nodes   #注意slot分配

# redis-cli -a redispwd -p 7000 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:174
cluster_stats_messages_pong_sent:169
cluster_stats_messages_sent:343
cluster_stats_messages_ping_received:167
cluster_stats_messages_pong_received:174
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:343

# redis-cli -a redispwd -p 7000 cluster nodes 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668524637000 1 connected 0-5460
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668524640251 2 connected 5461-10922
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668524640000 3 connected 10923-16383

(4).集群操作,在集群中写入数据   在任何一个节点作为入口都可以,下面以128:7000端口的redis为例写入数据
集群操作需要加-c参数
# redis-cli -p 7000 -a redispwd              #不加-c参数,表示不是以集群方式写入,无法写入    
# redis-cli -p 7000 -a redispwd -c          #增加-c参数,表示是以集群方式写入,可以写入
127.0.0.1:7000> set k1 v1
-> Redirected to slot [12706] located at 192.168.27.130:7002     #可以看到该key,k1是存放到130:7002的redis
OK
192.168.27.130:7002> set k2 v2
-> Redirected to slot [449] located at 192.168.27.128:7000         #可以看到该key,k2是存放在本机128:7000的redis,可以看出数据是分布存储
OK
192.168.27.128:7000> keys *                                                #在128:7000的redis可以看到只存储了k2,是按槽位分配数据,不是按节点分配
1) "k2"
192.168.27.128:7000> get k2
"v2"

在130:7002redis上可以查看存储的另一个k1
# redis-cli -p 7002 -a redispwd -c
127.0.0.1:7002> keys *
1) "k1"
127.0.0.1:7002> get k1
"v1"


d).增加搭建3从集群,实现redis高可用
(1).先启动3个从redis   192.168.27.128:8000      192.168.27.129:8001       192.168.27.130:8002
128上:
# mkdir /data/cluster/8000 -p
# redis-server /data/cluster/8000/redis.conf 
# ps -ef |grep redis
root       2285      1  0 23:35 ?        00:00:00 redis-server 0.0.0.0:7000 [cluster]
root       2301      1  0 23:41 ?        00:00:00 redis-server 0.0.0.0:8000 [cluster]
root       2307   1362  0 23:41 pts/0    00:00:00 grep --color=auto redis

129上:
# mkdir /data/cluster/8001 -p
# redis-server /data/cluster/8001/redis.conf 
# ps -ef |grep redis
root       4385      1  0 23:36 ?        00:00:00 redis-server 0.0.0.0:7001 [cluster]
root       4404      1  1 23:42 ?        00:00:00 redis-server 0.0.0.0:8001 [cluster]
root       4410   1356  0 23:42 pts/0    00:00:00 grep --color=auto redis

130上:
# mkdir /data/cluster/8002 -p
# redis-server /data/cluster/8002/redis.conf 
# ps -ef |grep redis
root       2298      1  0 23:36 ?        00:00:00 redis-server 0.0.0.0:7002 [cluster]
root       2311      1  0 23:42 ?        00:00:00 redis-server 0.0.0.0:8002 [cluster]
root       2317   1370  0 23:42 pts/0    00:00:00 grep --color=auto redis

(2).分别给3个主节点添加从库,在任何一节操作即可,下面以在128:7000的redis为例操作
别忘了主从的规划:
三主: 192.168.27.128:7000                     192.168.27.129:7001                       192.168.27.130:7002
三从: 192.168.27.128:8000(7002的从)      192.168.27.129:8001(7000的从)       192.168.27.130:8002(7001的从)
注意:上面设计是为了避免主从都在同一个节点,防止同一个节点挂了后,主从都挂掉,就没法进行主从切换,导致数据丢失

# redis-cli -a redispwd -p 7000 cluster nodes                                                       #查看集群的各个节点状态和master-id                
# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  7000的master-id  192.168.27.129:8001 192.168.27.128:7000
# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  7001的master-id  192.168.27.130:8002 192.168.27.129:7001
# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  7002的master-id  192.168.27.128:8000 192.168.27.130:7002

给3主分别加入从节点操作:
# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668527129000 1 connected 0-5460
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668527131454 3 connected 10923-16383
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668527130439 2 connected 5461-10922

注意:下面命令中后面两个ip顺序不能颠倒,分别对应的一组主从,ip顺序为:  从redis的ip:从端口   主redis的ip:主端口

# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c  192.168.27.129:8001 192.168.27.128:7000      #注意:后面两个ip的redis的顺序是不能颠倒的,否则加入不成功 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.27.129:8001 to cluster 192.168.27.128:7000
>>> Performing Cluster Check (using node 192.168.27.128:7000)
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 slots) master
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
[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.27.129:8001 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.27.128:7000.
[OK] New node added correctly.

# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  4301c9c511c84d99b4aa07fd53207c48da8e449e  192.168.27.130:8002 192.168.27.129:7001    #注意:后面两个ip的redis的顺序是不能颠倒的,否则加入不成功 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.27.130:8002 to cluster 192.168.27.129:7001
>>> Performing Cluster Check (using node 192.168.27.129:7001)
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
S: 122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001
   slots: (0 slots) slave
   replicates 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 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 192.168.27.130:8002 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.27.129:7001.
[OK] New node added correctly.

# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  390a2f7cb23d038982a42569f98c03cb13ff5a87   192.168.27.128:8000 192.168.27.130:7002    #注意:后面两个ip的redis的顺序是不能颠倒的,否则加入不成功 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.27.128:8000 to cluster 192.168.27.130:7002
>>> Performing Cluster Check (using node 192.168.27.130:7002)
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
S: 122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001
   slots: (0 slots) slave
   replicates 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c
S: 57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002
   slots: (0 slots) slave
   replicates 4301c9c511c84d99b4aa07fd53207c48da8e449e
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 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 192.168.27.128:8000 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.27.130:7002.
[OK] New node added correctly.

加入从节点后查看节点状态和各自角色:  (在任何一个节点操作即可,下面以128:7000redis为例)
# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668528411018 3 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668528412033 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668528413000 1 connected 0-5460
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668528412000 2 connected
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668528411000 1 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668528413046 2 connected 5461-10922

(3).验证cluster集群主从的高可用切换,在任何一节操作即可,下面以在128:7000的redis为例操作
停止其中128:7000的主库redis,查看129:8001的从库是否能提升为主库:
# ps -ef |grep redis
root       2285      1  0 Nov15 ?        00:00:04 redis-server 0.0.0.0:7000 [cluster]
root       2301      1  0 Nov15 ?        00:00:02 redis-server 0.0.0.0:8000 [cluster]
root       2343   1362  0 00:12 pts/0    00:00:00 grep --color=auto redis
# kill 2285
# ps -ef |grep redis
root       2301      1  0 Nov15 ?        00:00:03 redis-server 0.0.0.0:8000 [cluster]
root       2346   1362  0 00:15 pts/0    00:00:00 grep --color=auto redis

以128:8000的redis作为入口,查看节点状态和角色信息,原主128:7000宕机,对应的原从129:8001提升为了新主
# redis-cli -a redispwd -p 8000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668528958783 3 connected 10923-16383
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 master - 0 1668528959000 4 connected 0-5460
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 myself,slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668528958000 3 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668528960000 2 connected 5461-10922
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master,fail - 1668528910068 1668528906003 1 disconnected
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668528960815 2 connected

当原主128:7000redis恢复,自动作为从角色:
# redis-server /data/cluster/7000/redis.conf 
# ps -ef |grep redis
root       2301      1  0 Nov15 ?        00:00:04 redis-server 0.0.0.0:8000 [cluster]
root       2355      1  0 00:22 ?        00:00:00 redis-server 0.0.0.0:7000 [cluster]
root       2362   1362  0 00:22 pts/0    00:00:00 grep --color=auto redis

# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668529386000 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668529386892 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 master - 0 1668529387000 4 connected 0-5460
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668529387000 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,slave 122bfd4294666e2d60502f0466010783a9af12bd 0 1668529385000 4 connected
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668529387910 3 connected

如果还是习惯于让原来主库作为作为主库,可以用下面命令,手动提升为主库。
从库手动提升为主库的方法: CLUSTER FAILOVER
登录原主库现从库redis:  128:7000
# redis-cli -a redispwd -p 7000
127.0.0.1:7000> info Replication
# Replication
role:slave
master_host:192.168.27.129
master_port:8001
master_link_status:up
127.0.0.1:7000> CLUSTER FAILOVER         #从库手动提升为主库的方法       
127.0.0.1:7000> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.27.129,port=8001,state=online,offset=2030,lag=1
master_failover_state:no-failover

# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668529921336 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668529921000 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668529920314 5 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668529920000 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668529921000 5 connected 0-5460
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668529922355 3 connected

二、Redis cluster集群增删节点

1.首先搭建3主3从(见前面,略)
机器规划:
三主: 192.168.27.128:7000 192.168.27.129:7001 192.168.27.130:7002
三从: 192.168.27.128:8000(7002的从) 192.168.27.129:8001(7000的从) 192.168.27.130:8002(7001的从)
增加一个主节点: 192.168.27.131:7003 从节点 192.168.27.131:7004 (正常主从不能在一台机器,此处只是演示)

查看3主3从节点状态:

# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668657203555 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668657201521 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668657202000 5 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668657202533 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668657202000 5 connected 0-5460
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668657200000 3 connected

scp拷贝redis的二进制启动文件到新节点
# scp -r /usr/local/bin/redis-* [email protected]:/usr/local/bin/

2.新增节点主、从redis先启动(192.168.27.131机器操作)

redis-server -v
Redis server v=6.2.1 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=3bc8e110479b329d
# mkdir /data/cluster/7003 -p
# mkdir /data/cluster/7004 -p

修改redis的配置文件: 所有节点的redis配置都一样,只是下面两项:端口和路径不一样

vim /data/cluster/7003/redis.conf          各自目录下创建配置文件: 7003  7004
cluster-enabled yes
port 7003
dir "/data/cluster/7003"
logfile "redis.log"
pidfile "redis.pid"
daemonize yes
bind 0.0.0.0
requirepass redispwd
masterauth redispwd
tcp-backlog 1024
tcp-keepalive 0
loglevel notice
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
save 900 1
save 300 10
save 60 10000
dbfilename "dump.rdb"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

启动两个新增准备作为主从的redis:
# redis-server /data/cluster/7003/redis.conf 
# redis-server /data/cluster/7004/redis.conf 
# ps -ef |grep redis
root       1271      1  0 20:22 ?        00:00:00 redis-server 0.0.0.0:7003 [cluster]
root       1277      1  0 20:22 ?        00:00:00 redis-server 0.0.0.0:7004 [cluster]

2.在redis集群中加入一个新的主节点(在任意一台集群的节点操作即可,此处在128:7000的节点操作)

redis-cli -a redispwd --cluster add-node 新节点ip:新节点端口 集群任意主节点ip:集群任意主节点对应端口    
操作:
redis-cli -a redispwd --cluster add-node 192.168.27.131:7003 192.168.27.128:7000          #在128:7000节点操作,加入新主节点,131:7003
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.27.131:7003 to cluster 192.168.27.128:7000
>>> Performing Cluster Check (using node 192.168.27.128:7000)
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002
   slots: (0 slots) slave
   replicates 4301c9c511c84d99b4aa07fd53207c48da8e449e
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001
   slots: (0 slots) slave
   replicates 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000
   slots: (0 slots) slave
   replicates 390a2f7cb23d038982a42569f98c03cb13ff5a87
[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.27.131:7003 to make it join the cluster.
[OK] New node added correctly.

查看集群节点信息:
# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668659552000 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668659554073 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668659555089 5 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668659553000 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668659553000 5 connected 0-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 master - 0 1668659555000 0 connected
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668659553052 3 connected

2.在redis集群中加入一个新的从节点(在任意一台集群的节点操作即可,注意后面两个ip顺序即可,此处在128:7000节点机器操作)

注意:下面命令中后面两个ip顺序不能颠倒,分别对应的一组主从,ip顺序为:  从redis的ip:从端口   主redis的ip:主端口
# redis-cli -a redispwd --cluster add-node --cluster-slave --cluster-master-id  01ab5c82622921138dd28d2b2fd2772e4ad65041  192.168.27.131:7004 192.168.27.131:7003        #注意:后面两个ip的redis的顺序是不能颠倒的,否则加入不成功 
>>> Performing Cluster Check (using node 192.168.27.131:7003)
M: 01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003
   slots: (0 slots) master
S: 781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000
   slots: (0 slots) slave
   replicates 390a2f7cb23d038982a42569f98c03cb13ff5a87
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001
   slots: (0 slots) slave
   replicates 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c
S: 57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002
   slots: (0 slots) slave
   replicates 4301c9c511c84d99b4aa07fd53207c48da8e449e
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 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 192.168.27.131:7004 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.27.131:7003.
[OK] New node added correctly.

查看集群节点信息:发现新增加主从节点没有问题,原来主节点都分配了槽位,但新主节点没有分配槽位,所以也不会有数据
# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668660112000 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668660112570 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668660113893 5 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668660111000 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668660111000 5 connected 0-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 master - 0 1668660110839 0 connected
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668660109000 3 connected
ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004@17004 slave 01ab5c82622921138dd28d2b2fd2772e4ad65041 0 1668660112873 0 connected

3.给新加主redis分配槽位 (在集群中任何一个节点操作即可,此处以128:7000节点上操作)

注意槽位分配,可以将某个节点槽位分到新节点,也可将所有主节点再做一个平均分配,此处是从128:7000主节点的槽位移动到新主节点一部分)
# redis-cli -a redispwd --cluster reshard 192.168.27.128:7000    #连接集群中任意一个节点的redis
# redis-cli -a redispwd --cluster reshard 192.168.27.128:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.27.128:7000)
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002
   slots: (0 slots) slave
   replicates 4301c9c511c84d99b4aa07fd53207c48da8e449e
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001
   slots: (0 slots) slave
   replicates 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003
   slots: (0 slots) master
   1 additional replica(s)
S: 781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000
   slots: (0 slots) slave
   replicates 390a2f7cb23d038982a42569f98c03cb13ff5a87
S: ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004
   slots: (0 slots) slave
   replicates 01ab5c82622921138dd28d2b2fd2772e4ad65041
[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)? 2000   #要分配出去多少个槽位
What is the receiving node ID? 01ab5c82622921138dd28d2b2fd2772e4ad65041    #接收的redis的id的节点,131:7003
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: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c    #从哪个节点id移动过去,all是所有节点平均分,此处选从128:7000节点移过去
Source node #2: done                             #也可以选择从多个节点id移动过去,可输入多个节点id,此处选一个,如果选择节点完成就输入done
....
Do you want to proceed with the proposed reshard plan (yes/no)? yes    #输入yes,他就会帮我们移动
...

移动完槽位后,重新查看集群节点和槽位信息:(在任意一个节点操作,此处以128:7000节点操作)
# redis-cli -a redispwd -p 7000 cluster nodes      #发现槽位从128:7000节点分走了2000个槽位给131:7003节点
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668661776042 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668661773000 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668661776000 5 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668661775025 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668661774000 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 master - 0 1668661775000 6 connected 0-1999
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668661777058 3 connected
ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004@17004 slave 01ab5c82622921138dd28d2b2fd2772e4ad65041 0 1668661776000 6 connected

4.给新主从redis写入数据,验证能正常存放和获取数据 (在集群中任何一个节点操作即可,此处以128:7000节点上操作)

# redis-cli -a redispwd -h 192.168.27.131 -p 7003 -c
192.168.27.131:7003> set k1 v1
-> Redirected to slot [12706] located at 192.168.27.130:7002                     #存的key在主节点130:7002
OK
192.168.27.130:7002> set k2 v2
-> Redirected to slot [449] located at 192.168.27.131:7003                         #存的key在主节点131:7003               
OK
192.168.27.131:7003> get k1
-> Redirected to slot [12706] located at 192.168.27.130:7002
"v1"
192.168.27.130:7002> get k2
-> Redirected to slot [449] located at 192.168.27.131:7003
"v2"

5.验证新主从redis的高可用切换(集群信息查看可在任意节点,停止新主redis在192.168.27.131上操作)

1).停掉新主redis服务,查看新从提升为主
# ps -ef |grep redis                     #在新主131:7003上操作
root       1271      1  0 20:22 ?        00:00:08 redis-server 0.0.0.0:7003 [cluster]
root       1277      1  0 20:22 ?        00:00:06 redis-server 0.0.0.0:7004 [cluster]
# kill 1271                                   #停止新主131:7003节点
[root@localhost ~]# ps -ef |grep redis
root       1277      1  0 20:22 ?        00:00:06 redis-server 0.0.0.0:7004 [cluster]

# redis-cli -a redispwd -p 7004 cluster nodes     #查看集群节点信息,在新从后提升为主的131:7004上操作,发现新从131:7004提升为新主
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668692023499 2 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668692022484 3 connected 10923-16383
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668692025529 5 connected
ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004@17004 myself,master - 0 1668691933000 7 connected 0-1999
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668692021472 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 master,fail - 1668691946871 1668691939271 6 disconnected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668692024514 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668692023293 3 connected

6.新主恢复,自动转为从角色(在新主从131上操作)

redis-server /data/cluster/7003/redis.conf                          #原新主131:7003节点恢复
[root@localhost ~]# ps -ef |grep redis
root       1277      1  0 20:22 ?        00:00:08 redis-server 0.0.0.0:7004 [cluster]
root      10553      1  0 21:38 ?        00:00:00 redis-server 0.0.0.0:7003 [cluster]

redis-cli -a redispwd -p 7004 cluster nodes             #查看集群节点信息,发现原新主131:7003自动以从角色运行
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668692400101 2 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668692399087 3 connected 10923-16383
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668692397000 5 connected
ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004@17004 myself,master - 0 1668692385000 7 connected 0-1999
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668692397566 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 slave ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 0 1668692398074 7 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668692395526 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668692401111 3 connected

7.为了习惯,将原新主的从角色还提升为新主角色(在新主从131上操作)

redis-cli -a redispwd -p 7003
127.0.0.1:7003> info Replication
# Replication
role:slave
master_host:192.168.27.131
master_port:7004
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:4808
slave_priority:100
slave_read_only:1
connected_slaves:0
master_failover_state:no-failover
master_replid:00cfcba9c71d934c8fb431faf729016065a408e8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4808
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4361
repl_backlog_histlen:448
127.0.0.1:7003> CLUSTER FAILOVER       #从库手动提升为主库的方法
127.0.0.1:7003> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.27.131,port=7004,state=online,offset=4906,lag=1
master_failover_state:no-failover
master_replid:86d75ff96f534cb43c0b17a5de688eced7dc2584
master_replid2:00cfcba9c71d934c8fb431faf729016065a408e8
master_repl_offset:4906
second_repl_offset:4851
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4361
repl_backlog_histlen:546
# redis-cli -a redispwd -p 7003 cluster nodes           #查看集群节点信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004@17004 slave 01ab5c82622921138dd28d2b2fd2772e4ad65041 0 1668692807732 8 connected
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668692812829 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 myself,master - 0 1668692797000 8 connected 0-1999
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668692811808 5 connected
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668692810785 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668692807122 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668692808751 3 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668692809769 3 connected 10923-16383

7.删除主节点和从节点(删除新主节点和新从节点,集群相关操作可在任意节点操作,此处在新主从131上操作)

1).Redis集群移除从库:     
移除从库格式:# redis-cli -a redispwd --cluster del-node 对应主库的ip:主端口 对应从库的id    
从库移除之前可以有数据,但移除从库后会关闭从库,清空数据

# redis-cli -a redispwd -p 7003 cluster nodes           #查看集群的节点信息和id
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc 192.168.27.131:7004@17004 slave 01ab5c82622921138dd28d2b2fd2772e4ad65041 0 1668693479652 8 connected
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668693475578 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 myself,master - 0 1668693456000 8 connected 0-1999
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668693476599 5 connected
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668693478533 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668693477618 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668693477010 3 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668693478634 3 connected 10923-16383

# redis-cli -a redispwd --cluster del-node 192.168.27.131:7003 ebec3752a21d858a3b7db1cedf45dc4a5d5ccddc   #移除从库131:7004 
# redis-cli -a redispwd -p 7003 cluster nodes                #查看只有主库131:7003
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668693618465 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 myself,master - 0 1668693596000 8 connected 0-1999
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668693613991 5 connected
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668693617036 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668693619076 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668693618055 3 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668693616024 3 connected 10923-16383


2).Redis集群移除主库:  
注意:主节点如果有数据,是删除不掉的,删除之前先进行主节点数据的迁移,此处将新主131:7003还迁移回主节点128:7000
集群移除主节点的格式:# redis-cli -a redispwd --cluster del-node 随便一个其他主节点:主端口  要移除主节点的id

# redis-cli -a redispwd -p 7003 cluster nodes      #查看集群节点信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668694034612 5 connected 2000-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 myself,master - 0 1668693596000 8 connected 0-1999
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668694032570 5 connected
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668694032368 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668694033587 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668694035627 3 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668694031552 3 connected 10923-16383

移除新主131:7003前先迁移数据:
# redis-cli -a redispwd --cluster reshard 192.168.27.131:7003  #任意一个节点连接集群即可,重新分配槽位,将新主131:7003的槽位移动到128:7000
>>> Performing Cluster Check (using node 192.168.27.131:7003)
M: 01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003
   slots:[0-1999] (2000 slots) master
M: 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000
   slots:[2000-5460] (3461 slots) master
   1 additional replica(s)
S: 122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001
   slots: (0 slots) slave
   replicates 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c
S: 57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002
   slots: (0 slots) slave
   replicates 4301c9c511c84d99b4aa07fd53207c48da8e449e
M: 4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000
   slots: (0 slots) slave
   replicates 390a2f7cb23d038982a42569f98c03cb13ff5a87
M: 390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002
   slots:[10923-16383] (5461 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.
How many slots do you want to move (from 1 to 16384)? 2000    #移动2000个槽位
What is the receiving node ID? 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c     #接收的id,让128:7000接收
Source node #1: 01ab5c82622921138dd28d2b2fd2772e4ad65041                        #把哪个节点的槽位移走,131:7003
Source node #2: done                                                                                     #选择完毕了,输入done,让其操作
...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
# redis-cli -a redispwd -p 7003 cluster nodes      #查看集群节点信息,131:7003的槽位已经转移给128:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 master - 0 1668695021180 9 connected 0-5460
01ab5c82622921138dd28d2b2fd2772e4ad65041 192.168.27.131:7003@17003 myself,master - 0 1668693596000 8 connected
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668695024251 9 connected
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668695023633 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668695018534 2 connected 5461-10922
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668695023228 3 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668695022205 3 connected 10923-16383


指定主128:7000,移除新主131:7003的id:  
# redis-cli -a redispwd --cluster del-node 192.168.27.128:7000 01ab5c82622921138dd28d2b2fd2772e4ad65041  
>>> Removing node 01ab5c82622921138dd28d2b2fd2772e4ad65041 from cluster 192.168.27.128:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

上面将131上主从节点移除主从之前都可以使用集群操作命令,但主从都移除集群后,再执行查看集群等操作就需要在其他集群节点了
在128:7000节点查看现在集群状态:发现新主新从节点都已经移除出去:
# redis-cli -a redispwd -p 7000 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
57c57905eea202b9fe1b47515cc4ea2851f608e6 192.168.27.130:8002@18002 slave 4301c9c511c84d99b4aa07fd53207c48da8e449e 0 1668666506840 2 connected
4301c9c511c84d99b4aa07fd53207c48da8e449e 192.168.27.129:7001@17001 master - 0 1668666504000 2 connected 5461-10922
122bfd4294666e2d60502f0466010783a9af12bd 192.168.27.129:8001@18001 slave 69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 0 1668666505827 9 connected
390a2f7cb23d038982a42569f98c03cb13ff5a87 192.168.27.130:7002@17002 master - 0 1668666504810 3 connected 10923-16383
69d305ad0b4ba25b7d1d84c7ea7cb86218a28a8c 192.168.27.128:7000@17000 myself,master - 0 1668666505000 9 connected 0-5460
781c3d1823c2efe8930eaabab7ff3c1e49b42f93 192.168.27.128:8000@18000 slave 390a2f7cb23d038982a42569f98c03cb13ff5a87 0 1668666503798 3 connected

标签:10,Redis,redis,192.168,cluster,集群,master,slots,connected
From: https://www.cnblogs.com/fxxy/p/17192190.html

相关文章