您的位置:首页 > 数据库 > Redis

Redis Cluster集群

2018-08-24 15:18 176 查看

Redis Cluster集群

  • Redis-cluster设计

Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持Redis-cluster集群,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。其redis-cluster架构图如下:

其结构特点:

   1、所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
   2、节点的fail是通过集群中超过半数的节点检测失效时才生效。
   3、客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
    4、redis-cluster把所有的物理节点映射到[0-16383]slot上(不一定是平均分配),cluster 负责维护node<->slot<->value。

    5、Redis集群预分好16384个slot,当需要在 Redis 集群中放置一个 key-value 时,根据 CRC16(key) mod 16384的值,决定将一个key放到哪个slot中。

  1. Redis cluster节点分配

   现在我们是三个主节点分别是:A, B, C 三个节点,它们可以是一台机器上的三个端口,也可以是三台不同的服务器。那么,采用哈希槽 (hash slot)的方式来分配16384个slot 的话,它们三个节点分别承担的slot 区间是:

      节点A覆盖0-5460;

      节点B覆盖5461-10922;

      节点C覆盖10923-16383

     获取数据:

      如果存入一个值,按照redis cluster哈希槽的算法: CRC16('key')384 = 6782。 那么就会把这个key 的存储分配到 B 上了。同样,当我连接(A,B,C)任何一个节点想获取'key'这个key时,也会这样的算法,然后内部跳转到B节点上获取数据

     新增一个主节点:

     新增一个节点D,redis cluster的这种做法是从各个节点的前面各拿取一部分slot到D上

    节点A覆盖1365-5460

    节点B覆盖6827-10922

    节点C覆盖12288-16383

    节点D覆盖0-1364,5461-6826,10923-12287

     同样删除一个节点也是类似,移动完成后就可以删除这个节点了

  1. Redis Cluster主从模式

    Redis cluster 为了保证数据的高可用性,加入了主从模式,一个主节点对应一个或多个从节点,主节点提供数据存取,从节点则是从主节点拉取数据备份,当这个主节点挂掉后,就会有这个从节点选取一个来充当主节点,从而保证集群不会挂掉。

    上面那个例子里, 集群有ABC三个主节点, 如果这3个节点都没有加入从节点,如果B挂掉了,我们就无法访问整个集群了。A和C的slot也无法访问。

     所以我们在集群建立的时候,一定要为每个主节点都添加了从节点, 比如像这样, 集群包含主节点A、B、C, 以及从节点A1、B1、C1, 那么即使B挂掉系统也可以继续正确工作。

     B1节点替代了B节点,所以Redis集群将会选择B1节点作为新的主节点,集群将会继续正确地提供服务。 当B重新开启后,它就会变成B1的从节点。

    不过需要注意,如果节点B和B1同时挂了,Redis集群就无法继续正确地提供服务了

  • Redis-cluster集群安装

第一步:安装Redis

root@dfshi-virtual-machine:~# sudo apt-get install redis 

Redis安装包里有个集群工具,要复制到/usr/local/bin里去

如果找不到redis-trib.rb 文件,可以执行全系统搜索,

root@dfshi-virtual-machine:/# ls -R | grep redis       # -R 参数递归显示文件

最后在/usr/share/doc/redis-tools/examples# 文件中找到了redis-trib.rb 文件,

root@dfshi-virtual-machine:/usr/share/doc/redis-tools/examples# ls

lru  redis-trib.rb

拷贝文件到usr/local/bin

root@dfshi-virtual-machine:/usr/share/doc/redis-tools/examples# cp redis-trib.rb /usr/local/bin

第二步:修改配置,创建配置文件 

在/usr/local中创建cluster/6380、cluster/6381、cluster/6382、cluster/6383、cluster/6384、cluster/6385,然后将/etc/redis/redis.conf文件依次复制到新建的文件夹中,用vim编辑器修改一下参数:

port 6379     # 节点使用的端口

daemonize yes            # redis 后台运行

pidfile /var/run/redis_6379.pid    # pidfile文件对相应6380-6385

cluster-enabled yes             # 开启集群

cluster-config-file nodes-6379.conf   #保存节点配置,自动创建,自动更新对应6380-6385

cluster-node-timeout 5000       #集群超时时间,节点超过这个时间没反应就断定是宕机

appendonly yes              # 存储方式,aof,将写操作记录保存到日志中

注意,port为对应的6380、6381、6382、6383、6384、6385,cluster-config-file也要对应,注释一定一定要去掉

 

下面是创建及修改好的配置文件:

root@dfshi-virtual-machine:/usr/local/cluster# ls -R

.:

6380  6381  6382  6383  6384  6385

 

./6380:

redis.conf

 

./6381:

redis.conf

 

./6382:

redis.conf

 

./6383:

redis.conf

 

./6384:

redis.conf

 

./6385:

redis.conf

 

第三步:启动配置文件,开启6个节点

root@dfshi-virtual-machine:/etc/redis# redis-server /usr/local/cluster/6380/redis.conf

root@dfshi-virtual-machine:/etc/redis# redis-server /usr/local/cluster/6381/redis.conf

root@dfshi-virtual-machine:/etc/redis# redis-server /usr/local/cluster/6382/redis.conf

root@dfshi-virtual-machine:/etc/redis# redis-server /usr/local/cluster/6383/redis.conf

root@dfshi-virtual-machine:/etc/redis# redis-server /usr/local/cluster/6384/redis.conf

root@dfshi-virtual-machine:/etc/redis# redis-server /usr/local/cluster/6385/redis.conf

查看开启效果

root@dfshi-virtual-machine:/etc/redis# ps -aux | grep redis

redis   8457  0.1  0.0  47204   316 ?    Ssl  5月18   2:10 /usr/bin/redis-server *:6379

root   20676  0.0  0.3  65564  3728 pts/4    T    09:25   0:00 vim redis.conf

root   20859  0.0  0.3  47204  3184 ?     Ssl  10:20   0:08 redis-server *:6380 [cluster]

root   20864  0.0  0.3  47204  3368 ?     Ssl  10:21   0:08 redis-server *:6381 [cluster]

root   20868  0.0  0.3  47204  3336 ?     Ssl  10:21   0:08 redis-server *:6382 [cluster]

root   20872  0.0  0.3  47204  3136 ?    Ssl  10:21   0:08 redis-server *:6383 [cluster]

root    20878  0.0  0.3  47204  3172 ?    Ssl  10:21   0:08 redis-server *:6384 [cluster]

root    20882  0.0  0.3  47204  3244 ?   Ssl  10:21   0:08 redis-server *:6385 [cluster]

root    22062 28.0  0.0  21312   964 pts/4  S+   13:06   0:00 grep --color=auto redis

说明都启动成功了。

第四步:创建集群

在创建集群之前 需要安装ruby,以及redis和ruby连接

 

root@dfshi-virtual-machine:/usr/local/bin# redis-trib.rb create --replicas 1 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 127.0.0.1:6385

/usr/bin/env: "ruby": 没有那个文件或目录

缺少“ruby”软件,需要安装ruby

root@dfshi-virtual-machine:/usr/local/bin#  sudo apt-get install ruby

正在读取软件包列表... 完成

正在分析软件包的依赖关系树。。。。。

 

如果redis和ruby没有连接,会报错:

root@dfshi-virtual-machine:/usr/local/bin# redis-trib.rb create --replicas 1 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 127.0.0.1:6385

/usr/local/bin/redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573

/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)

        from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

        from /usr/local/bin/redis-trib.rb:25:in `<main>'

 

Ruby的redis接口没有安装,需要安装Redis接口,输入命令 " gem install redis " 进行安装,

root@dfshi-virtual-machine:/usr/local/bin# gem install redis

Fetching: redis-4.0.1.gem (100%)

Successfully installed redis-4.0.1

Parsing documentation for redis-4.0.1

Installing ri documentation for redis-4.0.1

Done installing documentation for redis after 2 seconds

1 gem installed

 

再执行命令就会创建成功。

root@dfshi-virtual-machine:/usr/local/bin# redis-trib.rb create --replicas 1 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 127.0.0.1:6385

/usr/local/bin/redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:6380

127.0.0.1:6381

127.0.0.1:6382

Adding replica 127.0.0.1:6383 to 127.0.0.1:6380

Adding replica 127.0.0.1:6384 to 127.0.0.1:6381

Adding replica 127.0.0.1:6385 to 127.0.0.1:6382

M: f17b480feadb2932a63ec3374acfb5b975859557 127.0.0.1:6380

   slots:0-5460 (5461 slots) master

M: 7d41e4a33cfd0914e28de8fa16978b2d93013495 127.0.0.1:6381

   slots:5461-10922 (5462 slots) master

M: 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7 127.0.0.1:6382

   slots:10923-16383 (5461 slots) master

S: f7b39b887b18eb536f86dae6fe9cd717ba7ed802 127.0.0.1:6383

   replicates f17b480feadb2932a63ec3374acfb5b975859557

S: ac4e75b32450e4112ec7b3ad422d7a2781e21648 127.0.0.1:6384

   replicates 7d41e4a33cfd0914e28de8fa16978b2d93013495

S: 7bc3df85c62d7aa1ee761c0005eb7d771ff1f701 127.0.0.1:6385

   replicates 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7

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:6380)

M: f17b480feadb2932a63ec3374acfb5b975859557 127.0.0.1:6380

   slots:0-5460 (5461 slots) master

M: 7d41e4a33cfd0914e28de8fa16978b2d93013495 127.0.0.1:6381

   slots:5461-10922 (5462 slots) master

M: 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7 127.0.0.1:6382

   slots:10923-16383 (5461 slots) master

M: f7b39b887b18eb536f86dae6fe9cd717ba7ed802 127.0.0.1:6383

   slots: (0 slots) master

   replicates f17b480feadb2932a63ec3374acfb5b975859557

M: ac4e75b32450e4112ec7b3ad422d7a2781e21648 127.0.0.1:6384

   slots: (0 slots) master

   replicates 7d41e4a33cfd0914e28de8fa16978b2d93013495

M: 7bc3df85c62d7aa1ee761c0005eb7d771ff1f701 127.0.0.1:6385

   slots: (0 slots) master

   replicates 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

从运行结果看 主节点就是6380 6381 6382 从节点分别是6383 6384 6385 

6380分配到的哈希槽是 0-5460

6381分配到的哈希槽是 5461-10922

6382分配到的哈希槽是 10923-16383

 

目前来说,集群已搭建好了。

第五步:集群数据测试

查看redis-cli -c -p 端口号,进入某一个redis服务

root@dfshi-virtual-machine:/etc/redis# redis-cli -c -p 6381

127.0.0.1:6381> ping

PONG

127.0.0.1:6381> set name redis6381              # 添加数据

OK

127.0.0.1:6381> keys *

1) "name"

127.0.0.1:6381>exit

 

前面说过Redis Cluster值分配规则,所以分配key的时候,它会使用CRC16(‘my_name’)%16384算法,来计算,将这个key 放到哪个节点,这里分配到了5798 slot ,就分配到port:6381的哈希槽(5461-10922)范围内.

所以我们从其他集群节点 ,都可以获取到数据

 

root@dfshi-virtual-machine:/etc/redis# redis-cli -c -p 6385

127.0.0.1:6385> ping

PONG

127.0.0.1:6385> get name

-> Redirected to slot [5798] located at 127.0.0.1:6381       # key:name 所方的节点

"redis6381"

127.0.0.1:6381>exit

 

第六步:集群宕机测试

root@dfshi-virtual-machine:/etc/redis# ps -aux | grep redis       # 当前运行的redis程序

redis   8457  0.1  0.0  47204   316 ?    Ssl  5月18   2:14 /usr/bin/redis-server *:6379

root   20859  0.0  0.3  47204  3184 ?    Ssl  10:20   0:13 redis-server *:6380 [cluster]

root  20864  0.0  0.3  47204  3392 ?     Ssl  10:21   0:13 redis-server *:6381 [cluster]

root   20868  0.0  0.3  47204  3336 ?    Ssl  10:21   0:13 redis-server *:6382 [cluster]

root  20872  0.0  0.3  47204  3136 ?     Ssl  10:21   0:13 redis-server *:6383 [cluster]

root  20878  0.0  0.3  47204  3180 ?     Ssl  10:21   0:13 redis-server *:6384 [cluster]

root  20882  0.0  0.3  47204  3244 ?     Ssl  10:21   0:13 redis-server *:6385 [cluster]

root   22278  0.0  0.0  21312   956 pts/4    R+   14:23   0:00 grep --color=auto redis

假如我们干掉一个节点,比如6381 这个主节点

root@dfshi-virtual-machine:/etc/redis# kill -9 20864

root@dfshi-virtual-machine:/etc/redis# ps -aux | grep redis

redis   8457  0.1  0.0  47204   316 ?    Ssl  5月18   2:19 /usr/bin/redis-server *:6379

root  20859  0.0  0.3  47204  3228 ?     Ssl  10:20   0:20 redis-server *:6380 [cluster]

root  20868  0.0  0.3  47204  3336 ?     Ssl  10:21   0:20 redis-server *:6382 [cluster]

root  20872  0.0  0.3  47204  3136 ?     Ssl  10:21   0:20 redis-server *:6383 [cluster]

root  20878  0.1  0.3  47204  3228 ?     Ssl  10:21   0:20 redis-server *:6384 [cluster]

root  20882  0.1  0.3  47204  3244 ?     Ssl  10:21   0:20 redis-server *:6385 [cluster]

root   22537 10.0  0.0  21312   956 pts/4  S+   16:04   0:00 grep --color=auto redis

 

然后再来看下集群的情况:

root@dfshi-virtual-machine:/etc/redis# redis-trib.rb check 127.0.0.1:6380

/usr/local/bin/redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573

>>> Performing Cluster Check (using node 127.0.0.1:6380)

M: f17b480feadb2932a63ec3374acfb5b975859557 127.0.0.1:6380

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7 127.0.0.1:6382

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

M: ac4e75b32450e4112ec7b3ad422d7a2781e21648 127.0.0.1:6384

   slots:5461-10922 (5462 slots) master

   0 additional replica(s)

S: 7bc3df85c62d7aa1ee761c0005eb7d771ff1f701 127.0.0.1:6385

   slots: (0 slots) slave

   replicates 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7

S: f7b39b887b18eb536f86dae6fe9cd717ba7ed802 127.0.0.1:6383

   slots: (0 slots) slave

   replicates f17b480feadb2932a63ec3374acfb5b975859557

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

我们发现 6384本来是从节点,由于他对应的主节点6381被闭塞了,就自动变成主节点master,最后说明 All 16384 slots covered. 所有哈希槽都被覆盖; 集群可以正常使用

如果把6384也毙掉,就会发现,哈希槽没有完全分配,会报错:[ERR] Not all 16384 slots are covered by nodes.,不能正常使用集群。

所以配对主从节点,最少要一个是正常的,才能使用集群。

 

 

root@dfshi-virtual-machine:/etc/redis# kill -9 20878

root@dfshi-virtual-machine:/etc/redis# redis-trib.rb check 127.0.0.1:6380

/usr/local/bin/redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573

[ERR] Sorry, can't connect to node 127.0.0.1:6384

>>> Performing Cluster Check (using node 127.0.0.1:6380)

M: f17b480feadb2932a63ec3374acfb5b975859557 127.0.0.1:6380

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7 127.0.0.1:6382

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 7bc3df85c62d7aa1ee761c0005eb7d771ff1f701 127.0.0.1:6385

   slots: (0 slots) slave

   replicates 44c978a9336f45ee0df5bc06cf91a1e5ef89a4e7

S: f7b39b887b18eb536f86dae6fe9cd717ba7ed802 127.0.0.1:6383

   slots: (0 slots) slave

   replicates f17b480feadb2932a63ec3374acfb5b975859557

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[ERR] Not all 16384 slots are covered by nodes.

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: