部署redis-cluster集群

一、Redis集群简介

Redis Cluster是一个无中心的结构,每个节点都保存数据和整个群集的状态。每个节点都会保存其他节点的信息,知道其他节点所负责的槽,并且会与其他节点定时发送心跳信息,能够及时感知群集中异常的节点。
Redis没有统一的路口,当客户端向群集中任一节点发送与数据库键有关的命令时,接受命令的节点会计算出命令要处理的数据库键属于哪个槽,并检查这个槽是否指派给了自己。如果键所在的槽正好指派给了当前节点,那么节点直接执行这个命令;如果键所在的槽并没有指派给当前节点,那么节点会向客户端返回一个MOVED错误,指引客户端转向(redirect)正确的节点,并再次发送之前想要执行的命令。

二、Redis集群概述

2.1Redis集群介绍

  • Redis集群是一个提供在多个Redis间节点间共享数据的程序集
  • Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误
  • Redis集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下可继续处理命令

2.2Redis集群的优势

  • 自动分割数据到不同的节点上
  • 整个集群的部分节点失败或者不可达的情况下能够继续处理命令

2.3Redis集群的实现方法

  • 有客户端分片
  • 代理分片
  • 服务器端分片

2.4Redis-Cluster数据分片

  • Redis集群没有使用一致性hash,而是引入了哈希槽概念
  • Redis集群有16384个哈希槽
  • 每个key通过CRC16校验后对16384取余来决定放置槽
  • 集群的每个节点负责一部分哈希槽
    Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value
    时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,
    这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大
    致均等的将哈希槽映射到不同的节点。
  • 数据分片
    以3个节点组成的集群为例
    节点A包含0到5500号哈希槽
    节点B包含5501到11000号哈希槽
    节点C包含11001到16384号哈希槽
    支持添加或者删除节点
    添加/删除节点无需停止服务(支持热状态)
    例如:
    如果想新添加个节点D,需要移动节点A,B,C中的部分槽到D上
    如果想移除节点A,需要将A中的槽移到B和C节点上,再将没有任何槽的A节点从集群中移除

2.5Redis-Cluster的主从复制模型

  • 集群中具有A,B,C三个节点,如果节点B失败了,整个集群就会因缺少5501-11000这个范围的槽而不可用
  • 为每个节点添加一个从节点A1,B1,C1,整个集群便有三个master节点和三个slave节点组成,在节点B失败后,集群便会选举B1为新的主节点继续服务
  • 当B和B1都失败后,集群将不可用

三、搭建Redis集群

3.1案例环境

VMware虚拟机;6台Linux服务器安装Centos 7.6系统

节点类型IP地址
master110.0.0.10
master210.0.0.20
master310.0.0.30
slave110.0.0.40
slave210.0.0.50
slave310.0.0.60

1、环境准备
准备 6台Centos 7.6 的虚拟机,关闭防火墙、关闭核心防护,挂载光盘,搭建本地yum仓库,配置相对应的ip地址

2、安装redis
每个节点服务器都安装redis服务,步骤一样。

[root@localhost opt]# tar xzvf redis-5.0.7.tar.gz -C /opt/
[root@localhost opt]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make        
[root@localhost redis-5.0.7]#  make PREFIX=/usr/local/redis install    
####如果安装过程中,更改安装路径可以用make PRRFIX=安装路径 install

//设置Redis相关配置文件
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/bin/redis-server  ####手动输入
Selected config:
Port           : 6379                                        ####端口号
Config file    : /etc/redis/6379.conf                        ####默认配置文件
Log file       : /var/log/redis_6379.log                     ####日志文件
Data dir       : /var/lib/redis/6379                         ####数据目录
Executable     : /usr/local/bin/redis-server                 ####执行命令
Cli Executable : /usr/local/bin/redis-cli                    ####客户端命令
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

3、修改配置文件

修改各个redis主机的6379.conf配置文件,注意:6个节点都要修改。
[root@localhost utils]# vim /etc/redis/6379.conf
70行:bind 127.0.0.1   #注释掉bind项,选项默认监听所有网卡
89行:protected-mode no  #关闭保护模式
93行:port 6379
137行:daemonize yes   #以独立进程启动
833行:cluster-enabled yes  #开启群集功能
841行:cluster-config-file nodes-6379.conf   #群集名称文件设置
847行:cluster-node-timeout 15000  #群集超时时间设置
700行:appendonly yes   #开启aof持久化

4、启动查看
正常启动后/var/lib/redis/6379/目录下会多出三个文件,第一个是持久化文件appendonly.aof,第二个是RDB持久文件dump.rdb,另外一个是节点首次启动生成的nodes-6379.conf

[root@localhost 6379]# /etc/init.d/redis_6379 restart
[root@localhost utils]# cd /var/lib/redis/6379/
[root@localhost 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf

5、创建集群
仅在一台redis中操作,准备生成集群:

#安装ruby
[root@localhost utils]# yum -y install ruby rubygems 

上传redis-3.2.0.gem 这个包到/opt目录下执行下面命令
[root@localhost utils]# cd /opt
[root@localhost opt]# gem install redis --version 3.2.0
Successfully installed redis-3.2.0
Parsing documentation for redis-3.2.0
Installing ri documentation for redis-3.2.0
1 gem installed

集群创建只在一个节点上敲这条命令即可

先关闭防火墙,否则集群创建报错:Could not connect to Redis at 10.0.0.30:6379: No route to host
[root@server1 opt]# systemctl stop firewalld.service 
s[root@server1 opt]# setenforce 0

创建集群
[root@server1 opt]# cd /opt/redis-5.0.7/src/
[root@server1 src]# redis-cli --cluster create --cluster-replicas 1 10.0.0.10:6379 10.0.0.20:6379 10.0.0.30:6379 10.0.0.40:6379 10.0.0.50:6379 10.0.0.60:6379

在这里插入图片描述
六个实例分为三组,每组一主一备,–replicas 1表示每组一个备,下面交互的时候需要输入yes才可以创建。

[root@server1 src]# redis-cli --cluster create --cluster-replicas 1 10.0.0.10:6379 10.0.0.20:6379 10.0.0.30:6379 10.0.0.40:6379 10.0.0.50:6379 10.0.0.60:6379
>>> 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.50:6379 to 10.0.0.10:6379       #主从对应关系,前面是备,后面是主
Adding replica 10.0.0.60:6379 to 10.0.0.20:6379
Adding replica 10.0.0.40:6379 to 10.0.0.30:6379
M: e3afd8e248aa468101df87b8ce76e6215b470bee 10.0.0.10:6379
   slots:[0-5460] (5461 slots) master
M: cac718da4dfa0557a3f4b88873b7c65a6017064d 10.0.0.20:6379
   slots:[5461-10922] (5462 slots) master
M: 72cdbe261421bfb237dee78c60661509c4696c5c 10.0.0.30:6379
   slots:[10923-16383] (5461 slots) master
S: 8d41a2135bae02c9f7ddb49c370359f1318fa6d7 10.0.0.40:6379
   replicates 72cdbe261421bfb237dee78c60661509c4696c5c
S: c0900054f0b1007ef7c03e9ff26f2c35a5378a5e 10.0.0.50:6379
   replicates e3afd8e248aa468101df87b8ce76e6215b470bee
S: b8f1c0cabc5f2aa79f327a68a3a0a6b323e55578 10.0.0.60:6379
   replicates cac718da4dfa0557a3f4b88873b7c65a6017064d
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 10.0.0.10:6379)
M: e3afd8e248aa468101df87b8ce76e6215b470bee 10.0.0.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: c0900054f0b1007ef7c03e9ff26f2c35a5378a5e 10.0.0.50:6379
   slots: (0 slots) slave
   replicates e3afd8e248aa468101df87b8ce76e6215b470bee
M: cac718da4dfa0557a3f4b88873b7c65a6017064d 10.0.0.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 8d41a2135bae02c9f7ddb49c370359f1318fa6d7 10.0.0.40:6379
   slots: (0 slots) slave
   replicates 72cdbe261421bfb237dee78c60661509c4696c5c
M: 72cdbe261421bfb237dee78c60661509c4696c5c 10.0.0.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: b8f1c0cabc5f2aa79f327a68a3a0a6b323e55578 10.0.0.60:6379
   slots: (0 slots) slave
   replicates cac718da4dfa0557a3f4b88873b7c65a6017064d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

3.2主从数据库验证

[root@server1 src]# redis-cli -c -h 10.0.0.10      #master0写入键值
10.0.0.10:6379> set name lili
-> Redirected to slot [5798] located at 10.0.0.20:6379
OK
10.0.0.20:6379> get name
"lili"

[root@localhost utils]# redis-cli -c -h 10.0.0.40    #其他节点都能查看到数据,都是指向master1
10.0.0.40:6379> get name
-> Redirected to slot [5798] located at 10.0.0.20:6379
"lili"


#master1人为down掉,这时数据都是从slave1中读取到
[root@server2 utils]# /etc/init.d/redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped

[root@localhost utils]# redis-cli -c -h 10.0.0.40
10.0.0.40:6379> get name
-> Redirected to slot [5798] located at 10.0.0.60:6379
"lili"


#master1挂了以后,slave1继承master1的哈希槽进行读写
[root@localhost ~]# redis-cli -c -h 10.0.0.50
10.0.0.50:6379> get name
-> Redirected to slot [5798] located at 10.0.0.60:6379
"lili"

[root@localhost 6379]# redis-cli -c -h 10.0.0.60
10.0.0.60:6379> set liuneng 88
-> Redirected to slot [14125] located at 10.0.0.30:6379
OK
[root@localhost ~]# redis-cli -c -h 10.0.0.60
10.0.0.60:6379> get name
"lili"

#master1和slave1都down掉后,会显示群集已经down掉
[root@localhost ~]# /etc/init.d/redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@localhost ~]# redis-cli -c -h 10.0.0.50
10.0.0.50:6379> get name
(error) CLUSTERDOWN The cluster is down


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值