Redis——Redis 集群cluster

Redis 集群cluster

  • 集群简介
    • 集群作用
  • Redis集群结构设计
    • 数据存储设计
    • 集群内部通讯设计
  • cluster集群结构搭建
    • 主从下线和主从切换

集群简介

  • 集群就是使用网络将若干台计算机联通起来,并提供统一的管理方式,使其对外呈现单机的服务效果

集群作用

  • 分散单台服务器的访问压力,实现负载均衡
  • 分散单台服务器的存储压力,实现可扩展性
  • 降低单台服务器宕机带来的业务灾难

Redis集群结构设计

数据存储设计

  • 通过算法设计,计算出key应该保存的位置
  • 将所有的存储空间计划切割成16384份,每台主机保存一部分每份代表的是一个存储空间,不是一个key的保存空间
  • 将key按照计算出的结果放到对应的存储空间
  1. key通过hash算法计算出一个值,然后拿这个值%16384

  2. 得到一个数(假如是37)为key的保存位置,然后再存入相应的存储空间位置
    Redis——Redis 集群cluster_第1张图片

  3. 假如又增加了一个节点,之前每个节点都会拿出部分槽给新的节点
    Redis——Redis 集群cluster_第2张图片

  4. 如果是去除节点,则把被去除节点的槽加入到存在的节点当作

集群内部通讯设计

  • 各个数据库互相通信,保存各个库中槽的编号数据
  1. 客户端发出一个key访问A,通过算法计算出key的存储位置
  2. 如果一次命中,直接返回
  3. 一次未命中,告知具体位置
  4. 一次命中或者两次命中提高数据访问效率
    Redis——Redis 集群cluster_第3张图片

cluster集群结构搭建

  • 原生安装(单条命令)
    • 配置服务器(3主3从)
    • 建立通信(Meet)
    • 分槽(Slot)
    • 搭建主从(master-slave)
  • 工具安装(批处理)

Cluster配置

  • 添加节点

    cluster-enabled yes|no

  • cluster配置文件名,该文件属于自动生成,仅用于快速查找文件并查询文件内容

    cluster-config-file

  • 节点服务响应超时时间,用于判定该节点是否下线或切换为从节点

    cluster-node-timeout

  • master连接的slave最小数量

    cluster-migration-barrier

Cluster节点操作命令

  • 查看集群节点信息

    cluster nodes
    
  • 进入一个从节点 redis,切换其主节点

    cluster replicate <master-id>
    
  • 发现一个新节点,新增主节点

    cluster meet ip:port
    
  • 忽略一个没有solt的节点

    cluster forget <id>
    
  • 手动故障转移

    cluster failover
    
首先清空之前的数据
[root@maomao ~]# cd /usr/local/redis/data/
[root@maomao data]# ls
appendonly-6379.aof  dump-6379.rdb  dump.rdb
[root@maomao data]# rm -rf * 

配置文件添加
cluster-enabled yes		# 开启cluster
cluster-config-file nodes-6379.conf		# 配置文件和名字
cluster-node-timeout 10000		# cluster下线时间,超时时间为10秒


复制六个配置文件
sed 's/6379/6380/g' redis-6379.conf > redis-6380.conf
sed 's/6379/6381/g' redis-6379.conf > redis-6381.conf
sed 's/6379/6382/g' redis-6379.conf > redis-6382.conf
sed 's/6379/6383/g' redis-6379.conf > redis-6383.conf
sed 's/6379/6384/g' redis-6379.conf > redis-6384.conf
sed 's/6379/6385/g' redis-6379.conf > redis-6385.conf

检查
cat redis-6385.conf |grep -v '#' |grep -v '^$'

开启服务
redis-server redis_config/redis-6379.conf
...
redis-server redis_config/redis-6384.conf

[root@maomao bin]# ps aux |grep redis-
root       2116  0.2  0.5 165020 10824 pts/0    Sl+  02:23   0:00 redis-server 127.0.0.1:6379 [cluster]
root       2121  0.2  0.5 165020 10664 pts/7    Sl+  02:23   0:00 redis-server 127.0.0.1:6380 [cluster]
root       2126  0.2  0.5 165020 10720 pts/6    Sl+  02:23   0:00 redis-server 127.0.0.1:6381 [cluster]
root       2131  0.2  0.6 171164 12768 pts/2    Sl+  02:23   0:00 redis-server 127.0.0.1:6382 [cluster]
root       2136  0.2  0.5 165020 10760 pts/5    Sl+  02:24   0:00 redis-server 127.0.0.1:6383 [cluster]
root       2141  0.1  0.5 165020 10788 pts/1    Sl+  02:24   0:00 redis-server 127.0.0.1:6384 [cluster]

redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 56aa54f614a2fc8f99e30a109d5e6a91d723193e 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
M: 083eb7940fb5d5444563e3a48d36a1609b8d5af4 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
M: f515c69256533ebb17d2aabefe81d6e630f7eee5 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
S: d25e2d50c3b6136cafc3cdd0a5fef0e86fbf66fb 127.0.0.1:6382
   replicates 56aa54f614a2fc8f99e30a109d5e6a91d723193e
S: 86f0f88285ca648df41e8f348e41cf5ef520bdae 127.0.0.1:6383
   replicates 083eb7940fb5d5444563e3a48d36a1609b8d5af4
S: 738d205c923600d6f7c8ea8df8313527b8267b95 127.0.0.1:6384
   replicates f515c69256533ebb17d2aabefe81d6e630f7eee5
Can I set the above configuration? (type 'yes' to accept): 

6379 6380 6381 是master
6382 6383 6384 是slave
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 127.0.0.1:6379)
M: 56aa54f614a2fc8f99e30a109d5e6a91d723193e 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d25e2d50c3b6136cafc3cdd0a5fef0e86fbf66fb 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 56aa54f614a2fc8f99e30a109d5e6a91d723193e
S: 86f0f88285ca648df41e8f348e41cf5ef520bdae 127.0.0.1:6383
   slots: (0 slots) slave
   replicates 083eb7940fb5d5444563e3a48d36a1609b8d5af4
M: 083eb7940fb5d5444563e3a48d36a1609b8d5af4 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: f515c69256533ebb17d2aabefe81d6e630f7eee5 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 738d205c923600d6f7c8ea8df8313527b8267b95 127.0.0.1:6384
   slots: (0 slots) slave
   replicates f515c69256533ebb17d2aabefe81d6e630f7eee5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以查看日志
[root@maomao bin]# cat /usr/local/redis/data/nodes-6379.conf 

集群搭建完成
测试
[root@maomao bin]# redis-cli
127.0.0.1:6379> set name maomao
(error) MOVED 5798 127.0.0.1:6380	# 叫我们去这个槽操作

[root@maomao bin]# redis-cli -c		# 操控集群使用 redis-cli -c
127.0.0.1:6379> set name maomao
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK

我们在6382获取key 失败
[root@maomao bin]# redis -cli -c -p 6382
-bash: redis: command not found
[root@maomao bin]# redis-cli -c -p 6382
127.0.0.1:6382> get name
-> Redirected to slot [5798] located at 
127.0.0.1:6380
"maomao"
127.0.0.1:6380> exit

连接到6380就能够获取key
[root@maomao bin]# redis-cli -c -p 6380
127.0.0.1:6380> get name
"maomao"

主从下线和主从切换

我们将一个主节点下线,然后查看信息

127.0.0.1:6380> cluster nodes
86f0f88285ca648df41e8f348e41cf5ef520bdae 127.0.0.1:6383@16383 slave 083eb7940fb5d5444563e3a48d36a1609b8d5af4 0 1618815771240 
2 connected
083eb7940fb5d5444563e3a48d36a1609b8d5af4 127.0.0.1:6380@16380 myself,master - 0 1618815770000 2 connected 5461-10922
56aa54f614a2fc8f99e30a109d5e6a91d723193e 127.0.0.1:6379@16379 master,fail - 1618815743184 1618815740000 1 disconnected
d25e2d50c3b6136cafc3cdd0a5fef0e86fbf66fb 127.0.0.1:6382@16382 master - 0 1618815768232 7 connected 0-5460
f515c69256533ebb17d2aabefe81d6e630f7eee5 127.0.0.1:6381@16381 master - 0 1618815770000 3 connected 10923-16383
738d205c923600d6f7c8ea8df8313527b8267b95 127.0.0.1:6384@16384 slave f515c69256533ebb17d2aabefe81d6e630f7eee5 0 1618815770238 
3 connected

master,fail意思是master会回来

你可能感兴趣的:(Redis,redis,linux,数据库,cluster)