手把手教你搭建redis集群环境

搭建redis集群环境

我研究技术,一般喜欢先看看这个技术有哪些应用场景,然后在学会怎么去搭建,怎么去使用
看阅读本文之前,建议先阅读下:
手把手教你搭建Redis主从,从编译到安装,主机,docker搭建主从复制
手把手教你redis sentinel哨兵高可用故障转移搭建

redis集群使用场景:

1.单机模式内存瓶颈
2.主从复制哨兵集群内存瓶颈

  • 什么是内存瓶颈?
    我想想学习redis的人,都知道redis是一款基于内存的noSQL数据库,数据存储在redis服务上,就是存储在内存中的,但是每台服务器的内存是有限的,如果
    是主从复制,一主多从模式,无论是主还是从,内存中的数据都是一样的,这样未免有点浪费内存,如果遇到存储了大量的数据或者大数据,很容易把服务器内存
    撑爆,这也就是我们说的内存瓶颈

  • 内存瓶颈会发生什么事?
    如果redis存储数据量过大,就会导致服务器内存上限,从而导致整个redis无法对外提供服务,客户端连接不上,到处报错

如何解决redis内存瓶颈?

这个很简单,将单机或者主从复制改为集群模式,就能解决单机或者主从复制中单机的内存瓶颈问题。为什么集群就可以解决redis内存瓶颈问题呢?
原因是这样,主从复制情况下,主从所存储的数据是一模一样的,即使你有100个从redis,这100个slave存储的数据都是一样,我们假设每台redis服务器
的内存都是一样的,这样就有点浪费了。
但是如果是集群的话,能把数据分散存储到集群redis上,比如有6台redis服务,原来主从模式是每台都存储一样的数据,现在集群情况下,可以将数据平均分散
到三台redis上,这样,redis将能多存储三倍的数据,将来如果三台服务都存储满了,还可以加入新的redis到集群中,对redis服务进行横向拓展,就能存储海量的数据
用大白话说,就是将redis进行分片存储

怎么将redis分片存储?

redis集群环境中,数据都是存储在slots哈希槽中的,看看官方的解释:

Redis Cluster data sharding
Redis Cluster does not use consistent hashing, but a different form of sharding where every key is conceptually part of what we call a hash slot.

There are 16384 hash slots in Redis Cluster, and to compute what is the hash slot of a given key, we simply take the CRC16 of the key modulo 16384.

Every node in a Redis Cluster is responsible for a subset of the hash slots, so for example you may have a cluster with 3 nodes, where:

Node A contains hash slots from 0 to 5500.
Node B contains hash slots from 5501 to 11000.
Node C contains hash slots from 11001 to 16383.
This allows to add and remove nodes in the cluster easily. For example if I want to add a new node D, I need to move some hash slots from nodes A, B, C to D. Similarly if I want to remove node A from the cluster I can just move the hash slots served by A to B and C. When the node A will be empty I can remove it from the cluster completely.

Because moving hash slots from a node to another does not require stopping operations, adding and removing nodes, or changing the percentage of hash slots hold by nodes, does not require any downtime.

Redis Cluster supports multiple key operations as long as all the keys involved into a single command execution (or whole transaction, or Lua script execution) all belong to the same hash slot. The user can force multiple keys to be part of the same hash slot by using a concept called hash tags.

Hash tags are documented in the Redis Cluster specification, but the gist is that if there is a substring between {} brackets in a key, only what is inside the string is hashed, so for example this{foo}key and another{foo}key are guaranteed to be in the same hash slot, and can be used together in a command with multiple keys as arguments.

我这里解析一下,大概意思就是说,slots一共可以分为16384 个哈希槽,这些哈希槽可以指定分配到对应的redis集群服务商,数据存储是根据CRC16 算法计算出到底应该存储在哪个槽上的,或者说存储在哈希槽对应的redis 节点上

集群搭建

步骤:
1.创建节点目录
2.修改配置
3.创建集群关联

1.创建节点目录

我这里因为没有多余的机器,所以只用一台服务器来搭建redis集群,分配不同的端口就可以了,也就是伪集群,为了方便演示,我这里搭建6台redis服务以作为集群演示
创建cluster目录,并进入该目录下,创建节点目录

mkdir cluster
cd cluster 
mkdir 7000 7001 7002 7003 7004 7005

2.修改配置

将redis.conf修改端口和cluster相关配置,关键配置如下:

daemonize yes      #开启后台启动
port 7000          #指定redis服务端口号
cluster-enabled yes      #启用cluster
cluster-config-file nodes.conf   #默认节点配置,自动生成
cluster-node-timeout 5000
appendonly yes

将redis.conf 分别拷贝到目录7000 7001 7002 7003 7004 7005 ,并修改端口号为对应目录的端口号

3.创建集群关联

  • 启动redis
    进入到每个节点目录下,然后启动redis服务。如

cd 7000

redis-server redis.conf #启动redis服务

注意:这样启动之前,需要先配置好redis的环境变量

  • 创建集群
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
--cluster-replicas 1

这里解析一下,参数:–cluster-replicas 1 表示创建一个副本,即1个从节点。上述命令创建了6个redis服务,这样,redis会自动创建3个master节点
和3个slave节点,并且会提示你是否需要自动生成slots哈希槽。

我们输入yes,这样,redis集群就搭建好了

验证集群

随便登录到一台redis服务

$ redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7002> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

说明已经成功搭建好集群。这里记得要带上命令参数 -c,表示是否支持重定向。因为如果运气不好,你在7000端口对应服务上存储数据,但是数据分配的slot却
不在7000上,就会存储失败,所以加上-c参数之后,会自动帮你重定向到对应节点上

猜你喜欢

转载自blog.csdn.net/huangxuanheng/article/details/123645185