文章目录
1、集群方式登录主机6379 2、计算key应该保存在那个插槽 3、计算某个插槽中保存的key的数量 4、返回指定槽中的键 5、查看redis的版本 5.1、Redis集群的自动故障转移 5.2、主节点下线,从节点自动升为主节点 5.2.1、杀死主节点6379 5.2.2、登录从机6383,查看集群节点信息 5.2.3、查看6383的主从节点的信息
5.3、复活6379 5.4、查看6379的主从节点信息
1、集群方式登录主机6379
[ root@localhost redis]
192.168 .74.148:6379 > keys *
1 ) "k2{aa}"
2 ) "k1{aa}"
192.168 .74.148:6379 > flushall
OK
192.168 .74.148:6379 > keys *
( empty array)
192.168 .74.148:6379 >
2、计算key应该保存在那个插槽
192.168 .74.148:6379 > set k1 v1
-> Redirected to slot [ 12706 ] located at 192.168 .74.148:6381
OK
192.168 .74.148:6381 > CLUSTER KEYSLOT k1
( integer) 12706
192.168 .74.148:6381 > keys *
1 ) "k1"
192.168 .74.148:6381 >
3、计算某个插槽中保存的key的数量
192.168 .74.148:6381 > CLUSTER COUNTKEYSINSLOT 12706
( integer) 1
192.168 .74.148:6381 > keys *
1 ) "k1"
192.168 .74.148:6381 >
4、返回指定槽中的键
CLUSTER GETKEYSINSLOT <slot> <count> 命令用于返回指定槽中的键。其中,<slot> 表示要获取键的槽的索引,<count> 表示要返回的键的数量。 该命令会从指定的槽中随机选择 count 个键,并返回这些键的列表。如果槽中没有足够的键,则返回实际存在的键。
192.168 .74.148:6381 > CLUSTER GETKEYSINSLOT 12706 5
1 ) "k1"
192.168 .74.148:6381 >
5、查看redis的版本
[ root@localhost ~]
Redis server v = 7.0 .10 sha = 00000000:0 malloc = jemalloc-5.2.1 bits = 64 build = d8b35e70c0a7be11
[ root@localhost ~]
5.1、Redis集群的自动故障转移
在Redis集群的某些版本中(如Redis 6.x及更高版本),集群本身已经内置了自动故障转移机制
。当集群中的某个主节点宕机时,集群中的其他节点会进行投票,选择一个从节点并将其提升为主节点。 选举过程基于Raft等共识算法,确保在多个节点之间达成一致。 选举成功后,新的主节点会开始处理写请求,并同步数据到其他从节点。
5.2、主节点下线,从节点自动升为主节点
[ root@localhost ~]
root 11403 1 0 14 :08 ? 00:00:17 /usr/local/redis/bin/redis-server *:6379 [ cluster]
root 11412 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6380 [ cluster]
root 11418 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6381 [ cluster]
root 11424 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6382 [ cluster]
root 11430 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6383 [ cluster]
root 11436 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6384 [ cluster]
root 12441 10116 0 15 :32 pts/0 00:00:00 /usr/local/redis/bin/redis-cli -c -h 192.168 .74.148 -p 6379
[ root@localhost ~]
5.2.1、杀死主节点6379
[ root@localhost ~]
[ root@localhost ~]
root 11412 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6380 [ cluster]
root 11418 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6381 [ cluster]
root 11424 1 0 14 :09 ? 00:00:18 /usr/local/redis/bin/redis-server *:6382 [ cluster]
root 11430 1 0 14 :09 ? 00:00:18 /usr/local/redis/bin/redis-server *:6383 [ cluster]
root 11436 1 0 14 :09 ? 00:00:17 /usr/local/redis/bin/redis-server *:6384 [ cluster]
[ root@localhost ~]
5.2.2、登录从机6383,查看集群节点信息
[ root@localhost ~]
192.168 .74.148:6383 > CLUSTER NODES
3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 192.168 .74.148:6381@16381 master - 0 1719305582937 3 connected 10923 -16383
dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 192.168 .74.148:6379@16379 master,fail - 1719305350801 1719305346760 1 disconnected
aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 192.168 .74.148:6380@16380 master - 0 1719305580920 2 connected 5461 -10922
5a6df4b92983ecfae646e3c7f5f2851ef118f9c0 192.168 .74.148:6383@16383 myself,master - 0 1719305583000 7 connected 0 -5460
f749b8d27581caf20b7a3d339d43621fbffa4962 192.168 .74.148:6384@16384 slave aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 0 1719305581929 2 connected
a569032e711a267a419c99719c3b87683c2fea6c 192.168 .74.148:6382@16382 slave 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 0 1719305583948 3 connected
192.168 .74.148:6383 >
5.2.3、查看6383的主从节点的信息
[ root@localhost ~]
192.168 .74.148:6383 > CLUSTER NODES
3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 192.168 .74.148:6381@16381 master - 0 1719305582937 3 connected 10923 -16383
dcb82723f90f0a2ab6af8fb008b5847f5aa0df45 192.168 .74.148:6379@16379 master,fail - 1719305350801 1719305346760 1 disconnected
aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 192.168 .74.148:6380@16380 master - 0 1719305580920 2 connected 5461 -10922
5a6df4b92983ecfae646e3c7f5f2851ef118f9c0 192.168 .74.148:6383@16383 myself,master - 0 1719305583000 7 connected 0 -5460
f749b8d27581caf20b7a3d339d43621fbffa4962 192.168 .74.148:6384@16384 slave aa4ecdba6e2cc523dcefe6244a72f768fc8fbae9 0 1719305581929 2 connected
a569032e711a267a419c99719c3b87683c2fea6c 192.168 .74.148:6382@16382 slave 3eac3019bcf18e8959a49f5e1389c39d51a1b0d8 0 1719305583948 3 connected
192.168 .74.148:6383 > info replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:48cb3b42ac8412712192e5d4747dc3337bc5747d
master_replid2:ab2c4b92097802299003be0ec17821ed7eb68d27
master_repl_offset:12279
second_repl_offset:12280
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:12279
192.168 .74.148:6383 >
5.3、复活6379
[ root@localhost ~]
[ root@localhost ~]
root 11412 1 0 14 :09 ? 00:00:19 /usr/local/redis/bin/redis-server *:6380 [ cluster]
root 11418 1 0 14 :09 ? 00:00:19 /usr/local/redis/bin/redis-server *:6381 [ cluster]
root 11424 1 0 14 :09 ? 00:00:19 /usr/local/redis/bin/redis-server *:6382 [ cluster]
root 11430 1 0 14 :09 ? 00:00:19 /usr/local/redis/bin/redis-server *:6383 [ cluster]
root 11436 1 0 14 :09 ? 00:00:19 /usr/local/redis/bin/redis-server *:6384 [ cluster]
root 13301 1 0 17 :00 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379 [ cluster]
[ root@localhost ~]
5.4、查看6379的主从节点信息
[ root@localhost ~]
192.168 .74.148:6379 > info replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:587c729d1697052a7424bb5e74321031682790ee
master_replid2:78a6a3071e3ec590e70826d029320ccd1f91e0fa
master_repl_offset:319897
second_repl_offset:319898
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:319898
repl_backlog_histlen:0
192.168 .74.148:6379 > CLUSTER NODES
24a1c81388b823ab2dfb411049625541562c8b1c 192.168 .74.148:6379@16379 myself,master - 0 0 0 connected 10359