第二十一周作业

一、简述redis集群的实现原理

Redis集群由多个实例组成的一个分布式系统,每个节点保存当前节点数据和整个集群状态,每个节点都和其他节点连接。

  • 各个节点之间互联,使用ping机制
  • 集群中超过半数的节点检测失效,才算该节点真正失效
  • 客户端不需要proxy即可直接连接Redis,因此应用程序需要知道所有Redis服务器的IP
  • Redis cluster将所有Redis Node平均映射到0-16383个槽位(slot)上,读写操作需要到指定的Redis Node上进行操作,因此有多少个node就相当于扩展了多少倍,每个node承担16384/N个槽位
  • Redis Cluster预先分配16384的slot,当需要在Redis Cluster中写入一个key-value的时候,会使用CRC16(key) mod 18 之后的值,再决定将key写入哪一个slot从而决定是哪一个redis node上,解决单机瓶颈问题。

二、基于redis5的redis cluster部署

实验环境

Rocky8:
  192.168.119.128   node1
  192.168.119.129   node2
  192.168.119.138   node3
  192.168.119.150   node4
  192.168.119.146   node5
  192.168.119.148   node6

Redis:5.0

2.1 安装redis,并修改相关配置

# 其余节点执行相同操作
[root@Rocky8-mini5 ~]# dnf -y install redis
[root@Rocky8-mini5 ~]# vim /etc/redis.conf
bind 0.0.0.0
masterauth wuhaolam 
requirepass wuhaolam
cluster-enabled yes 
cluster-config-file nodes-6379.conf 
cluster-require-full-coverage no
[root@Rocky8-mini5 ~]# systemctl  enable --now redis.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@Rocky8-mini5 ~]# ss -ntl
State             Recv-Q            Send-Q                       Local Address:Port                        Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:22                               0.0.0.0:*                                  
LISTEN            0                 128                                0.0.0.0:16379                            0.0.0.0:*                                  
LISTEN            0                 128                                0.0.0.0:6379                             0.0.0.0:*                                  
LISTEN            0                 128                                   [::]:22                                  [::]:* 
# 进程有cluster状态 
[root@Rocky8-mini5 ~]# ps -ef | grep redis
redis       1979       1  0 07:27 ?        00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root        1992    1937  0 07:29 pts/0    00:00:00 grep --color=auto redis

2.2 创建集群

# 随便选取一个节点执行命令
# 默认前三个IP为主节点,后三个为备份节点
[root@node1 ~]# redis-cli -a wuhaolam --cluster create 192.168.119.128:6379 192.168.119.129:6379 192.168.119.138:6379 192.168.119.150:6379 192.168.119.146:6379 192.168.119.148: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.119.150:6379 to 192.168.119.128:6379
Adding replica 192.168.119.146:6379 to 192.168.119.129:6379
Adding replica 192.168.119.148:6379 to 192.168.119.138:6379
M: b4c58a67b3af9df66a453514e504940dd583e742 192.168.119.128:6379
   slots:[0-5460] (5461 slots) master
M: e64712307f538b452e7b191433aeabe3e826293e 192.168.119.129:6379
   slots:[5461-10922] (5462 slots) master
M: 32e7bbaa0ab428a9dd0c3b816c2a87860d7c8ffa 192.168.119.138:6379
   slots:[10923-16383] (5461 slots) master
S: 2348166f7a016fd06e0a983a6fc54cb015fc6016 192.168.119.150:6379
   replicates b4c58a67b3af9df66a453514e504940dd583e742
S: ab01eb7f02c57baafba5f610a2201ed7685ba830 192.168.119.146:6379
   replicates e64712307f538b452e7b191433aeabe3e826293e
S: 85cf738640a16e9658d290837b40ee1049a6d48d 192.168.119.148:6379
   replicates 32e7bbaa0ab428a9dd0c3b816c2a87860d7c8ffa
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.119.128:6379)
M: b4c58a67b3af9df66a453514e504940dd583e742 192.168.119.128:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: e64712307f538b452e7b191433aeabe3e826293e 192.168.119.129:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 2348166f7a016fd06e0a983a6fc54cb015fc6016 192.168.119.150:6379
   slots: (0 slots) slave
   replicates b4c58a67b3af9df66a453514e504940dd583e742
S: 85cf738640a16e9658d290837b40ee1049a6d48d 192.168.119.148:6379
   slots: (0 slots) slave
   replicates 32e7bbaa0ab428a9dd0c3b816c2a87860d7c8ffa
M: 32e7bbaa0ab428a9dd0c3b816c2a87860d7c8ffa 192.168.119.138:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ab01eb7f02c57baafba5f610a2201ed7685ba830 192.168.119.146:6379
   slots: (0 slots) slave
   replicates e64712307f538b452e7b191433aeabe3e826293e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 查看集群信息
[root@node1 ~]# redis-cli -a wuhaolam 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:112
cluster_stats_messages_pong_sent:115
cluster_stats_messages_sent:227
cluster_stats_messages_ping_received:110
cluster_stats_messages_pong_received:112
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:227
[root@node1 ~]# cat /var/lib/redis/nodes-6379.conf 
e64712307f538b452e7b191433aeabe3e826293e 192.168.119.129:6379@16379 master - 0 1660134010000 2 connected 5461-10922
b4c58a67b3af9df66a453514e504940dd583e742 192.168.119.128:6379@16379 myself,master - 0 1660134008000 1 connected 0-5460
2348166f7a016fd06e0a983a6fc54cb015fc6016 192.168.119.150:6379@16379 slave b4c58a67b3af9df66a453514e504940dd583e742 0 1660134006000 4 connected
85cf738640a16e9658d290837b40ee1049a6d48d 192.168.119.148:6379@16379 slave 32e7bbaa0ab428a9dd0c3b816c2a87860d7c8ffa 0 1660134008000 6 connected
32e7bbaa0ab428a9dd0c3b816c2a87860d7c8ffa 192.168.119.138:6379@16379 master - 0 1660134009643 3 connected 10923-16383
ab01eb7f02c57baafba5f610a2201ed7685ba830 192.168.119.146:6379@16379 slave e64712307f538b452e7b191433aeabe3e826293e 0 1660134010661 5 connected
vars currentEpoch 6 lastVoteEpoch 0

2.3 验证集群

# 配置连接,显示槽位不在本节点上
[root@node1 ~]# redis-cli -a wuhaolam -h 192.168.119.128 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.119.129:6379
# 连接到正确槽位节点,发现能够正常写入数据
[root@node1 ~]# redis-cli -a wuhaolam -h 192.168.119.129 set key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@node1 ~]# redis-cli -a wuhaolam -h 192.168.119.129 get key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"values1"

# 使用-c选项以集群模式连接,当槽位不在本节点时,自动将数据写入到正确的节点
[root@node1 ~]# redis-cli -a wuhaolam -c  -h 192.168.119.128 --no-auth-warning
192.168.119.128:6379> CLUSTER KEYSLOT linux
(integer) 12299
192.168.119.128:6379> SET linux love
-> Redirected to slot [12299] located at 192.168.119.138:6379
OK
192.168.119.138:6379> get linux
"love"
192.168.119.138:6379> exit
[root@node1 ~]# redis-cli -a wuhaolam -h 192.168.119.138 get linux
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"love"
posted @ 2022-08-10 20:39  wuhaolam  阅读(42)  评论(0编辑  收藏  举报