目录
- 一、集群简介
- 1、现状问题
- 2、集群作用
- 二、集群结构设计
- 1、集群存储设
- 2、消息通信设计
- 三、Cluster集群三主三从结构搭建
- 1、redis.conf配置文件可配置项
- 2、配置集群
- 3、链接集群
- 4、命令客户端连接集群并使用
- 四、集群扩容
- 1、添加节点
- 2、槽位分配
- 3、添加从节点
- 五、集群缩容
- 1、删除从节点
- 2、删除主节点
一、集群简介
1、现状问题
- redis提供的服务OPS可以达到10万/秒,当前业务OPS已经达到20万/秒
- 内存单机容量达到256G,当前业务需求内存容量1T
2、集群作用
- 分散单台服务器的访问压力,实现负载均衡
- 分散单台服务器的存储压力,实现可扩展性
- 降低单台服务器宕机带来的业务灾难
二、集群结构设计
1、集群存储设
- 让key通过方法CRC161计算一个hash值,再继续取余%16384,得出在Redis中存储的位置。
- 将所有的存储空间计算切割16384个槽,每台主机保存一部分。
- 这个槽代表一个存储空间,不是一个key的保存空间。
- 将key按照计算出的计算放到对应的存储空间。
当增加一台计算机,内部就会优化,其余redis把自身一些槽,分给新加的节点。如果去机器把原来的槽再返回给原来的节点。这样可扩展性就得到增强。
2、消息通信设计
- 各个数据库互相通信,保存各个库中槽的编号数据
- 一次命中,直接返回。
- 一次未命中,告知具体位置,直接去下一个库中寻找,命中返回。
这样最多两次命中,就可以找到数据。
三、Cluster集群三主三从结构搭建
1、redis.conf配置文件可配置项
- 打开 cluster 集群配置
cluster-enabled yes|no
- cluster配置文件名,该文件属于自动生成,仅用于快速查找文件并查询文件内容
cluster-config-file <filename>
- 节点服务响应超时时间,用于判定该节点是否下线或切换为从节点
cluster-node-timeout <milliseconds>
- master连接的slave最小数量
cluster-migration-barrier <count>
2、配置集群
注意redis5.0.5以后,就不需要安装ruby了
- 清除redis中data目录所有数据
rm -rf ./data
- 新建
redis-端口号.conf
文件
port 端口号
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
# 打开 cluster集群配置
cluster-enabled yes# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
# cluster集群配置文件
cluster-config-file nodes-端口号.conf# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
# 当前这个节点超时时,多少秒后反馈信息
cluster-node-timeout 10000
复制六份这样的文件,每个配置文件的参数改成端口号
-
启动六个cluster节点
/usr/local/bin/redis-server /opt/module/redis-5.0.5/conf/redis-端口号.conf
3、链接集群
进入到一个redis,bin目录下执行命令
./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
如果集群有密码:./redis-cli 后面跟 -a password
–cluster-replicas 1 表示 一个master后有几个slave,redis集群自行分配
注意 ip一定不能用127.0.0.1 不然外部无法重定向访问集群 cluster-replicas后面的1表示一个主机有几个从机 因为现在只有一个因此是1 工作中是2
4、命令客户端连接集群并使用
- 注意:-c 表示是以redis集群方式进行连接
./redis-cli -a 123456 -h 127.0.0.1 -p 6379 -c
- 查看集群状态
cluster info
- 查看集群中的节点
cluster nodes
- 查看帮助
cluster help
- 新增master节点
cluster meet ip:port
- 忽略一个没有solt的节点
cluster forget id
- 手动故障转移
cluster failover
- 进入从节点redis,切换其主节点
cluster replicate masterID
- 当存放数据存,放的key:001根据crc16算法算出值然后对16384取余算出的值恰好落在分配到7001的槽中,所以就存放到7001中。我们去7002中进行获取的时候。会重定向到7001中进行获取。
四、集群扩容
新加入的节点都是master,并且不会分配任何slot槽位,我们要手动为新节点分配hash槽
1、添加节点
- 启动6385节点 进入bin目录进行启动
./redis-server redis-6385.conf
- 申请加入集群
./redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6379
- 命令解释:添加节点 6385 去meet6379申请加入集群
- 启动客户端并查看节点信息:
redis-cli -a 123456 -h 127.0.0.1 -p 6379 -c
cluster nodes
2、槽位分配
使用redis-cli --cluster reshard
命令为新加入的节点分配槽位,需要使用集群中任意一个master节点对其进行重新分片
redis-cli -a 123456 --cluster reshard 127.0.0.1:6385
How many slots do you want to move (from 1 to 16384)? 600
(ps:需要多少个槽移动到新的节点上,自己设置,比如600个hash槽)
What is the receiving node ID? 2728a594a0498e98e4b83a537e19f9a0a3790f38
(ps:把这600个hash槽移动到哪个节点上去,需要指定节点id,可通过 cluster nodes 查看当前节点id)
Please enter all the source node IDs.
Type ‘all’ to use all the nodes as source nodes for the hash slots.Type ‘done’ once you entered all the source nodes IDs.
Source node 1:all(ps:输入all为从所有主节点(8001,8002,8003)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为600个)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:输入yes确认开始执行分片任务)
3、添加从节点
-
添加节点(这时候的节点是master节点,并不会分配槽位)
redis-cli -a 123456 --cluster add-node 192.168.0.61:6386 192.168.0.61:6379
-
先登录从节点,然后在replicate命令中指定主节点的id
redis-cli -a 123456-c -h 192.168.0.61 -p 6379
-
将当前节点分配给指定的master节点作为slave节点
cluster replicate 2728a594a0498e98e4b83a537e19f9a0a3790f38 *#后面这串id为要添加slave节点的master的节点id*
五、集群缩容
接下来将上面新增加的两个节点删除
1、删除从节点
- 用
redis-cli --cluster del-node
删除从节点6386,指定删除节点ip和端口,以及节点id- redis-cli -a hs --cluster del-node 127.0.0.1:6386 a1cfe35722d151cf70585cee21275565393c0956
2、删除主节点
主节点的里面是有分配了hash槽的,所以我们这里必须先把6385里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题
只能把master的数据迁移到一个节点上,暂时做不了平均分配功能
- 任选一个主节点进行重新分片*
redis-cli -a 123456 --cluster reshard 127.0.0.1:6385
-
How many slots do you want to move (from 1 to 16384)? 600 (ps:需要多少个槽移动到新的节点上) What is the receiving node ID? 2728a594a0498e98e4b83a537e19f9a0a3790f38 (ps:把这600个hash槽移动到哪个节点上去,这里使用6379的主节点id) Please enter all the source node IDs. Type ‘all’ to use all the nodes as source nodes for the hash slots. Type ‘done’ once you entered all the source nodes IDs. Source node 1:2728a594a0498e98e4b83a537e19f9a0a3790f38 (ps:这里是需要数据源,也就是我们的6385节点id。这里这次就不写all了) Source node 2:done (ps:这里直接输入done 开始生成迁移计划) Do you want to proceed with the proposed reshard plan (yes/no)? yes (ps:这里输入yes开始迁移)
- 查看节点信息,发现没有插槽数了
cluster info
- 开始删除节点
这次就不写all了)
Source node 2:done
(ps:这里直接输入done 开始生成迁移计划)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:这里输入yes开始迁移) - 查看节点信息,发现没有插槽数了
cluster info
- 开始删除节点
redis-cli -a 123456 --cluster del-node 192.168.0.61:6385 2728a594a0498e98e4b83a537e19f9a0a3790f38