1、创建 redis cluster集群的环境准备
![图片[1]-基于Redis 5 以上版本的 redis cluster 部署-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/11/image-212.png)
- 每个Redis 节点采用相同的相同的Redis版本、相同的密码、硬件配置
- 所有Redis服务器必须没有任何数据
redis01 192.168.1.41
redis02 192.168.1.42
redis03 192.168.1.43
redis04 192.168.1.44
redis05 192.168.1.45
redis06 192.168.1.46
2、启用 redis cluster 配置
# 所有节点安装redis
# 每个节点修改redis配置,必须开启cluster功能的参数
[root@redis01 ~]# vim /apps/redis/etc/redis.conf
bind 0.0.0.0
masterauth 123456
# 建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes
# 取消此行注释,必须开启集群,开启后 redis 进程会有cluster标识
cluster-config-file nodes-6379.conf
# 取消此行注释,此为集群状态数据文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no
# 默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可用
# 批量修改
sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf
# 重启redis服务
[root@redis01 ~]# systemctl restart redis
# 验证当前Redis服务状态
[root@redis01 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 511 *:16379 *:*
LISTEN 0 511 *:6379 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@redis01 ~]# ps -ef | grep redis
redis 27229 1 0 09:42 ? 00:00:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root 27242 27133 0 09:44 pts/0 00:00:00 grep --color=auto redis
3、创建集群
[root@redis01 ~]# redis-cli -a 123456 --cluster create 192.168.1.41:6379 192.168.1.42:6379 192.168.1.43:6379 192.168.1.44:6379 192.168.1.45:6379 192.168.1.46:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.45:6379 to 192.168.1.41:6379
Adding replica 192.168.1.46:6379 to 192.168.1.42:6379
Adding replica 192.168.1.44:6379 to 192.168.1.43:6379
M: a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379 # 带M的为master
slots:[0-5460] (5461 slots) master # 当前master的槽位起始和结束位
M: 41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379
slots:[5461-10922] (5462 slots) master
M: 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379
slots:[10923-16383] (5461 slots) master
S: c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379 # 带S的slave
replicates 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba
S: 8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379
replicates a3b6fea4114b24349507c06c636cd522e1bf60ac
S: 2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379
replicates 41e6ca3b394ec3b4210f1bad884ae94d88ebd195
Can I set the above configuration? (type 'yes' to accept): yes # yes自动创建集群
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.1.41:6379)
M: a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379
slots:[0-5460] (5461 slots) master # 已经分配的槽位
1 additional replica(s) # 分配了一个slave
S: c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379
slots: (0 slots) slave # slave没有分配槽位
replicates 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba
#对应master的192.168.1.43的ID
S: 2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379
slots: (0 slots) slave
replicates 41e6ca3b394ec3b4210f1bad884ae94d88ebd195
S: 8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379
slots: (0 slots) slave
replicates a3b6fea4114b24349507c06c636cd522e1bf60ac
M: 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration. # 所有节点槽位分配完成
>>> Check for open slots... # 检查打开的槽位
>>> Check slots coverage... # 检查插槽覆盖范围
[OK] All 16384 slots covered. # 所有槽位(16384个)分配完成
# 观察以上结果,可以看到3组master/slave
master:192.168.1.41---slave:192.168.1.45
master:192.168.1.42---slave:192.168.1.46
master:192.168.1.43---slave:192.168.1.44
#如果节点少于3个会出下面提示错误
[root@redis01 ~]# redis-cli -a 123456 --cluster create 192.168.1.41:6379 192.168.1.42:6379
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 2 nodes and 0 replicas per node.
*** At least 3 nodes are required.
4、验证集群
4.1、查看主从状态
[root@redis01 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.45,port=6379,state=online,offset=616,lag=0
master_failover_state:no-failover
master_replid:ad9c77428f25752ae6bad1eba1fc949ab0b80516
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
[root@redis02 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.46,port=6379,state=online,offset=616,lag=1
master_failover_state:no-failover
master_replid:fe2aa532f3d1d892e6ac99a5bd88172894897e7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
[root@redis03 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.44,port=6379,state=online,offset=616,lag=1
master_failover_state:no-failover
master_replid:1c1551b183d80775db38190f986abb652a3b5710
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
[root@redis04 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.43
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:616
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:1c1551b183d80775db38190f986abb652a3b5710
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
[root@redis05 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.41
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:616
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:ad9c77428f25752ae6bad1eba1fc949ab0b80516
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
[root@redis06 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.42
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:616
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:fe2aa532f3d1d892e6ac99a5bd88172894897e7e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
# 查看指定master节点的slave节点信息
[root@redis01 ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379@16379 slave 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 0 1669600580000 3 connected
2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379@16379 slave 41e6ca3b394ec3b4210f1bad884ae94d88ebd195 0 1669600581180 2 connected
8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379@16379 slave a3b6fea4114b24349507c06c636cd522e1bf60ac 0 1669600580000 1 connected
2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379@16379 master - 0 1669600580173 3 connected 10923-16383
41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379@16379 master - 0 1669600579165 2 connected 5461-10922
a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379@16379 myself,master - 0 1669600580000 1 connected 0-5460
[root@redis01 ~]# redis-cli -a 123456 cluster slaves a3b6fea4114b24349507c06c636cd522e1bf60ac
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379@16379 slave a3b6fea4114b24349507c06c636cd522e1bf60ac 0 1669600637589 1 connected"
4.2、验证集群状态
[root@redis01 ~]# redis-cli -a 123456 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:6 # 节点数
cluster_size:3 # 三个集群
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:849
cluster_stats_messages_pong_sent:898
cluster_stats_messages_sent:1747
cluster_stats_messages_ping_received:893
cluster_stats_messages_pong_received:849
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1747
# 查看任意节点的集群状态
[root@redis01 ~]# redis-cli -a 123456 --cluster info 192.168.1.45:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.41:6379 (a3b6fea4...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.42:6379 (41e6ca3b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.1.43:6379 (2c8e2291...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
4.3、查看对应关系
[root@redis01 ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379@16379 slave 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 0 1669600580000 3 connected
2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379@16379 slave 41e6ca3b394ec3b4210f1bad884ae94d88ebd195 0 1669600581180 2 connected
8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379@16379 slave a3b6fea4114b24349507c06c636cd522e1bf60ac 0 1669600580000 1 connected
2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379@16379 master - 0 1669600580173 3 connected 10923-16383
41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379@16379 master - 0 1669600579165 2 connected 5461-10922
a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379@16379 myself,master - 0 1669600580000 1 connected 0-5460
[root@redis01 ~]# redis-cli -a 123456 --cluster check 192.168.1.44:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.42:6379 (41e6ca3b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.1.43:6379 (2c8e2291...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.41:6379 (a3b6fea4...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.44:6379)
S: c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379
slots: (0 slots) slave
replicates 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba
M: 41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379
slots: (0 slots) slave
replicates a3b6fea4114b24349507c06c636cd522e1bf60ac
S: 2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379
slots: (0 slots) slave
replicates 41e6ca3b394ec3b4210f1bad884ae94d88ebd195
M: a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379
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.
5、测试集群写入数据
![图片[2]-基于Redis 5 以上版本的 redis cluster 部署-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/11/image-213.png)
5.1、redis cluster 写入key
# 经过算法计算,当前key的槽位需要写入指定的node
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.41 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 192.168.1.42:6379 # 槽位不在当前node所以无法写入
# 指定槽位对应node可写入
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.42 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.42 GET key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"values1"
# 对应的slave节点可以KEYS *,但GET key1失败,可以到master上执行GET key1
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.46 KEYS "*"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "key1"
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.46 GET key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 192.168.1.42:6379
5.2、redis cluster 计算key所属的slot
[root@redis01 ~]# redis-cli -h 192.168.1.41 -a 123456 --no-auth-warning cluster nodes
c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379@16379 slave 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 0 1669601383175 3 connected
2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379@16379 slave 41e6ca3b394ec3b4210f1bad884ae94d88ebd195 0 1669601385000 2 connected
8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379@16379 slave a3b6fea4114b24349507c06c636cd522e1bf60ac 0 1669601386200 1 connected
2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379@16379 master - 0 1669601385192 3 connected 10923-16383
41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379@16379 master - 0 1669601386000 2 connected 5461-10922
a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379@16379 myself,master - 0 1669601384000 1 connected 0-5460
# 计算得到hello对应的slot
[root@redis01 ~]# redis-cli -h 192.168.1.41 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866
[root@redis01 ~]# redis-cli -h 192.168.1.41 -a 123456 --no-auth-warning set hello world
OK
[root@redis01 ~]# redis-cli -h 192.168.1.41 -a 123456 --no-auth-warning cluster keyslot name
(integer) 5798
[root@redis01 ~]# redis-cli -h 192.168.1.41 -a 123456 --no-auth-warning set name test
(error) MOVED 5798 192.168.1.42:6379
[root@redis01 ~]# redis-cli -h 192.168.1.42 -a 123456 --no-auth-warning set name test
OK
[root@redis01 ~]# redis-cli -h 192.168.1.42 -a 123456 --no-auth-warning get name
"test"
# 使用选项-c 以集群模式连接
[root@redis01 ~]# redis-cli -c -h 192.168.1.41 -a 123456 --no-auth-warning
192.168.1.41:6379> CLUSTER KEYSLOT linux
(integer) 12299
192.168.1.41:6379> set linux hello
-> Redirected to slot [12299] located at 192.168.1.43:6379
OK
192.168.1.43:6379> get linux
"hello"
192.168.1.43:6379> exit
[root@redis01 ~]# redis-cli -h 192.168.1.43 -a 123456 --no-auth-warning get linux
"hello"
6、python 程序实现Redis Cluster 访问
[root@redis01 ~]# yum install -y python3
[root@redis01 ~]# pip3 install redis-py-cluster
[root@redis01 ~]# vim redis_cluster_test.py
#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
{"host":"192.168.1.41", "port":6379},
{"host":"192.168.1.42", "port":6379},
{"host":"192.168.1.43", "port":6379},
{"host":"192.168.1.44", "port":6379},
{"host":"192.168.1.45", "port":6379},
{"host":"192.168.1.46", "port":6379}
]
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', decode_responses=True)
for i in range(0, 10000):
redis_conn.set('key'+str(i),'value'+str(i))
print('key'+str(i)+':',redis_conn.get('key'+str(i)))
[root@redis01 ~]# chmod +x redis_cluster_test.py
[root@redis01 ~]# ./redis_cluster_test.py
......
key9996: value9996
key9997: value9997
key9998: value9998
key9999: value9999
# 验证数据
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.41
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.41:6379> DBSIZE
(integer) 3332
192.168.1.41:6379> GET key1
(error) MOVED 9189 192.168.1.42:6379
192.168.1.41:6379> GET key2
"value2"
192.168.1.41:6379> GET key3
"value3"
192.168.1.41:6379> KEYS *
......
3330) "key3475"
3331) "key5482"
3332) "key5072"
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.42 DBSIZE
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3341
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.42 GET key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"value1"
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.43 DBSIZE
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3330
7、模拟故障实现故障转移
# 模拟node2节点出故障,需要相应的数秒故障转移时间
[root@redis01 ~]# redis-cli -a 123456 --no-auth-warning --cluster info 192.168.1.41:6379
192.168.1.41:6379 (a3b6fea4...) -> 3332 keys | 5461 slots | 1 slaves.
192.168.1.43:6379 (2c8e2291...) -> 3330 keys | 5461 slots | 1 slaves.
192.168.1.42:6379 (41e6ca3b...) -> 3341 keys | 5462 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@redis02 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@redis02 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@redis01 ~]# redis-cli -a 123456 --no-auth-warning --cluster info 192.168.1.41:6379
Could not connect to Redis at 192.168.1.42:6379: Connection refused
192.168.1.41:6379 (a3b6fea4...) -> 3332 keys | 5461 slots | 1 slaves.
192.168.1.46:6379 (2172dd03...) -> 3341 keys | 5462 slots | 0 slaves.
192.168.1.43:6379 (2c8e2291...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@redis01 ~]# redis-cli -a 123456 --no-auth-warning --cluster info 192.168.1.41:6379
Could not connect to Redis at 192.168.1.42:6379: Connection refused
192.168.1.41:6379 (a3b6fea4...) -> 3332 keys | 5461 slots | 1 slaves.
192.168.1.46:6379 (2172dd03...) -> 3341 keys | 5462 slots | 0 slaves.
192.168.1.43:6379 (2c8e2291...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@redis01 ~]# redis-cli -a 123456 --no-auth-warning --cluster check 192.168.1.41:6379
Could not connect to Redis at 192.168.1.42:6379: Connection refused
192.168.1.41:6379 (a3b6fea4...) -> 3332 keys | 5461 slots | 1 slaves.
192.168.1.46:6379 (2172dd03...) -> 3341 keys | 5462 slots | 0 slaves.
192.168.1.43:6379 (2c8e2291...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.41:6379)
M: a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379
slots: (0 slots) slave
replicates 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba
M: 2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379
slots:[5461-10922] (5462 slots) master
S: 8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379
slots: (0 slots) slave
replicates a3b6fea4114b24349507c06c636cd522e1bf60ac
M: 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379
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.
[root@redis01 ~]# redis-cli -a 123456 -h 192.168.1.46
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.46:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:a4c8ab36dfa7f0ff49303a30c2900e5fe2a26dee
master_replid2:fe2aa532f3d1d892e6ac99a5bd88172894897e7e
master_repl_offset:139520
second_repl_offset:139521
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:139520
# 恢复故障节点node2自动成为slave节点
[root@redis02 ~]# systemctl start redis
# 查看自动生成的配置文件,可以查看node2自动成为slave节点
[root@redis02 ~]# cat /apps/redis/data/nodes-6379.conf
8fbbcce6dc071befce0895904d0a9e679c0c9758 192.168.1.45:6379@16379 slave a3b6fea4114b24349507c06c636cd522e1bf60ac 0 1669602535279 1 connected
2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 192.168.1.43:6379@16379 master - 0 1669602535279 3 connected 10923-16383
2172dd0392baeab22705c2d860a8723c7fbec552 192.168.1.46:6379@16379 master - 0 1669602535279 7 connected 5461-10922
a3b6fea4114b24349507c06c636cd522e1bf60ac 192.168.1.41:6379@16379 master - 1669602535279 1669602535274 1 connected 0-5460
41e6ca3b394ec3b4210f1bad884ae94d88ebd195 192.168.1.42:6379@16379 myself,slave 2172dd0392baeab22705c2d860a8723c7fbec552 0 1669602535274 7 connected
c6f7fd6b6a5296eebfacfe1b391ba1248b306c5c 192.168.1.44:6379@16379 slave 2c8e22910e6240072b7b74e6b81a6ebde4f2bdba 1669602535279 1669602535275 3 connected
vars currentEpoch 7 lastVoteEpoch 0
192.168.1.46:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.42,port=6379,state=online,offset=139590,lag=1
master_failover_state:no-failover
master_replid:a4c8ab36dfa7f0ff49303a30c2900e5fe2a26dee
master_replid2:fe2aa532f3d1d892e6ac99a5bd88172894897e7e
master_repl_offset:139590
second_repl_offset:139521
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:139590
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END