其他分享
首页 > 其他分享> > 第二十一周练习题

第二十一周练习题

作者:互联网

第二十一周

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

为了解决单机性能的瓶颈,提高Redis 性能,可以使用分布式集群的解决方案
1. 所有Redis节点使用(PING机制)互联
2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
4. redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担16384/N个槽位
5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。

二、基于redis5的redis cluster部署

1.创建 redis cluster集群的环境准备
1.1.#每个redis 节点采用相同的相同的redis版本、相同的密码、硬件配置;所有redis服务器必须没有任何数据;准备六台主机,地址如下:
10.0.0.150
10.0.0.160
10.0.0.170
10.0.0.180
10.0.0.190
10.0.0.200

2.启用redis cluster配置
2.1.#所有6台主机都要执行以下配置
[root@centos8 ~]#dnf -y install redis

2.2.#每个节点修改redis配置,必须开启cluster功能的参数
2.2.1.#手动修改配置文件
[root@redis-node1 ~]vim /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不可用

2.2.2.#或者执行下面命令,批量修改
[root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 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' /etc/redis.conf

2.2.3.#启动redis服务
[root@redis-node1 ~]#systemctl enable --now redis

2.2.4.#验证当前Redis服务状态:
#开启了16379的cluster的端口,实际的端口=redis port + 10000
[root@redis-node1 ~]# ss -ntl
State      Recv-Q     Send-Q           Local Address:Port            Peer Address:Port
LISTEN     0          128                    0.0.0.0:6379                 0.0.0.0:*
LISTEN     0          128                    0.0.0.0:111                  0.0.0.0:*
LISTEN     0          32               192.168.122.1:53                   0.0.0.0:*
LISTEN     0          128                    0.0.0.0:22                   0.0.0.0:*
LISTEN     0          5                    127.0.0.1:631                  0.0.0.0:*
LISTEN     0          100                  127.0.0.1:25                   0.0.0.0:*
LISTEN     0          128                  127.0.0.1:6010                 0.0.0.0:*
LISTEN     0          128                    0.0.0.0:16379                0.0.0.0:*

#注意进程有[cluster]状态
[root@redis-node1 ~]# ps -ef|grep redis
redis      31887       1  0 12:56 ?        00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root       31966   31920  0 12:59 pts/0    00:00:00 grep --color=auto redis


3.创建集群
# redis-cli --cluster-replicas 1 表示每个master对应一个slave节点
[root@redis-node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.150:6379  10.0.0.160:6379  10.0.0.170:6379  10.0.0.180:6379  10.0.0.190:6379  10.0.0.200: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 10.0.0.180:6379 to 10.0.0.150:6379
Adding replica 10.0.0.190:6379 to 10.0.0.160:6379
Adding replica 10.0.0.200:6379 to 10.0.0.170:6379
M: f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379
   slots:[0-5460] (5461 slots) master
M: b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379
   slots:[5461-10922] (5462 slots) master
M: c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379
   slots:[10923-16383] (5461 slots) master
S: 9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379
   replicates f1e7ed79ab91541f362771c6b9db2543cd1e3044
S: b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379
   replicates b6ff266b891e766ee844bbeda1f069a3077d10b8
S: 644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379
   replicates c4a31f125e3cfceeb80325a890e9af89216009a0
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 10.0.0.150:6379)
M: f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379
   slots: (0 slots) slave
   replicates f1e7ed79ab91541f362771c6b9db2543cd1e3044
S: 644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379
   slots: (0 slots) slave
   replicates c4a31f125e3cfceeb80325a890e9af89216009a0
M: c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379
   slots: (0 slots) slave
   replicates b6ff266b891e766ee844bbeda1f069a3077d10b8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.



4.查看主从状态
[root@redis-node1 ~]# 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=10.0.0.180,port=6379,state=online,offset=154,lag=1
master_replid:14c9201de50d54a91599d3eb188e5a94910d3111
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154

[root@redis-node4 ~]# 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:10.0.0.150
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:252
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:14c9201de50d54a91599d3eb188e5a94910d3111
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252


5.验证集群状态
[root@redis-node1 ~]# 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:250
cluster_stats_messages_pong_sent:273
cluster_stats_messages_sent:523
cluster_stats_messages_ping_received:268
cluster_stats_messages_pong_received:250
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:523


#查看任意节点的集群状态
[root@redis-node2 ~]# redis-cli -a 123456 --cluster info 10.0.0.150:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.150:6379 (f1e7ed79...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.170:6379 (c4a31f12...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.160:6379 (b6ff266b...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.


6.查看集群node对应关系
[root@redis-node1 ~]# redis-cli -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379@16379 slave f1e7ed79ab91541f362771c6b9db2543cd1e3044 0 1643260442777 4 connected
644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379@16379 slave c4a31f125e3cfceeb80325a890e9af89216009a0 0 1643260440742 6 connected
f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379@16379 myself,master - 0 1643260439000 1 connected 0-5460
c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379@16379 master - 0 1643260441000 3 connected 10923-16383
b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379@16379 master - 0 1643260441760 2 connected 5461-10922
b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379@16379 slave b6ff266b891e766ee844bbeda1f069a3077d10b8 0 1643260440000 5 connected

7.验证集群写入key
7.1.#redis cluster 写入key
#经过算法计算,当前key的槽位需要写入指定的node
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.150 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 10.0.0.160:6379


#指定槽位对应node可写入
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.160 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK


#对应的slave节点可以KEYS *,但GET key1失败,可以到master上执行GET key1
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.190 KEYS "*"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "key1"
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.190 get key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 10.0.0.160:6379
[root@redis-node1 ~]#  redis-cli -a 123456 -h 10.0.0.160 get key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"values1"



7.2.#redis cluster 计算key所属的slot
[root@redis-node1 ~]# redis-cli -h 10.0.0.150 -a 123456 --no-auth-warning cluster nodes
9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379@16379 slave f1e7ed79ab91541f362771c6b9db2543cd1e3044 0 1643260685207 4 connected
644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379@16379 slave c4a31f125e3cfceeb80325a890e9af89216009a0 0 1643260688274 6 connected
f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379@16379 myself,master - 0 1643260686000 1 connected 0-5460
c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379@16379 master - 0 1643260687249 3 connected 10923-16383
b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379@16379 master - 0 1643260687000 2 connected 5461-10922
b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379@16379 slave b6ff266b891e766ee844bbeda1f069a3077d10b8 0 1643260689295 5 connected

#计算得到hello对应的slot
[root@redis-node1 ~]# redis-cli -h 10.0.0.150 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866


#使用选项-c 以集群模式连接
[root@redis-node1 ~]# redis-cli -h 10.0.0.150 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866
[root@redis-node1 ~]# redis-cli -c -h 10.0.0.150 -a 123456 --no-auth-warning
10.0.0.150:6379> cluster keyslot linux
(integer) 12299
10.0.0.150:6379> set linux love
-> Redirected to slot [12299] located at 10.0.0.170:6379
OK
10.0.0.170:6379> get linux
"love"
10.0.0.170:6379> exit


[root@redis-node1 ~]# redis-cli -h 10.0.0.170 -a 123456 --no-auth-warning get linux
"love"

标签:练习题,10.0,cluster,0.0,redis,第二十一,6379,slots
来源: https://www.cnblogs.com/tanll/p/15849517.html