博文目录
1、Redis群集原理
一、架构细节
二、Redis选举
2、搭建Redis群集
一、安装第一台Redis并修改配置文件
二、安装第二台Redis并修改配置文件
三、使用脚本建立群集
四、测试群集node
关于Redis非关系型数据库工做原理详述请参考博文:聊一聊Centos 7中的Redis(非关系型数据库)redis
Redis Cluster是一个无中心的结构,每一个节点都保存数据和整个群集的状态。每一个节点都会保存其余节点的信息,知道其余节点锁负责的槽,而且会与其余节点定时发送心跳信息,可以及时感知群集中异常的节点。以下图所示:数据库
当客户端向群集中任一节点发送与数据库键有关的命令时,接受命令的节点会计算出命令要处理的数据库键属于哪一个槽,并检查这个槽是否指派给了本身。若是键所在的槽正好指派给了当前节点,那么节点直接执行这个命令;若是键所在的槽并无指派给当前节点,那么节点会向客户端返回一个MOVED错误,指引客户端转向(redirect)正确的节点,并再次发送以前想要执行的命令。vim
群集角色有Master和Slave。Master之间分配slots,一共有16384个slot。Slave向它指定的Master同步数据,实现备份。当其中一个Master没法提供服务时,该Master的Slave将提高为Master。以保证群集键slot的完整性。当其中的某一个Master和它的Slave都失效,就会致使slot不完整,群集失效,这是就须要人工去处理了。centos
群集搭建好后,群集中的每一个节点都会按期地想其余节点发送PING消息,若是接收PING消息的节点没有在规定的时间内返回PONG消息,那么发送PING消息的节点就会将其标记为疑似下线(PFAIL)。各个节点会经过互相发送消息的方式来交换群集中各个节点的状态信息。若是在一个群集里面,半数以上的主节点都将某个节点 X 报告为疑似下线,那么这个主节点 X 将被标记为已下线(FAIL),同时会向群集广播一条关于主节点 X 的FAIL消息,全部收到这条FAIL消息的节点都会当即将主节点 X 标记为已下线。ruby
当须要减小或增长群集中的机器时,咱们须要将已经指派给某个节点(源节点)的槽改成指派给另外一个节点(目标节点),而且将相关槽所属的键值对从源节点移动到目标节点。服务器
Redis群集的从新分片操做是由Redis的群集管理软件redis-trib负责执行的,不支持自动的分片,并且须要本身计算从哪些节点上迁移多少Slot。在从新分片的过程当中,群集不须要下线,而且源节点和目标节点均可以继续处理命令请求。网络
全部的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽;架构
节点的失效(fail)在群集中超过半数的主(master)节点检测失效才失效;tcp
客户端与redis节点直连,不须要中间代理(proxy)层,客户端不须要链接群集全部节点,链接群集中任何一个可用节点便可;
- redis-cluster把全部的物理节点映射到【0-16383】slot上,cluster 负责维护node<->slot<->key;
以下图所示:选举过程是群集中全部master参与,若是半数以上master节点与当前master节点通讯超时(cluster-node-timeout),认为当前master节点挂掉。
如下两种状况为整个群集不可用(cluster_state:fail),当群集不可用时,全部对群集的操做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误。
若是群集任意master挂掉,且当前master没有slave,则群集进入fail状态,也能够理解为群集的slot映射[0-16383]不完整时进入的fail状态;
- 若是群集中出现半数的master挂掉,不管是否有slave,群集都进入fail状态;默认状况下,每一个群集的节点都使用两个TCP端口,一个是6379,一个16379;6379服务用于客户端的链接,16379用于群集总线,即便用二进制协议的节点到节点通讯通道。节点使用群集总线进行故障检测、配置更新、故障转移受权等。若是开启了防火墙,须要开放这两个端口。
环境描述:
此案例的环境采用6台Centos 7服务器搭建Redis群集,其中3台为master,3台为salve;
6台服务器的IP地址为192.168.100.10/24——192.168.100.60/24;
自行准备所需源码包即工具,也能够访问:https://pan.baidu.com/s/126siXcoxrqBWmp9SWhEVmQ
提取码:a45i ;
自行配置网络及防火墙,放行TCP的6379和16379这两个端口,我这里直接关闭了防火墙;
自行经过rz命令将redis压缩包和redis的gem文件上传到服务器;
废话少说,大点干早点散,开始干活...
[root@centos01 ~]# ls <!--查看redis压缩包和redis的gem文件是否上传成功--> anaconda-ks.cfg initial-setup-ks.cfg redis-3.2.0.gem redis-3.2.9.tar.gz [root@centos01 ~]# tar zxvf redis-3.2.9.tar.gz -C /usr/src/ <!--redis解压缩到/usr/src/目录--> [root@centos01 ~]# mv /usr/src/redis-3.2.9/ /usr/src/redis/ <!--将redis全部配置文件剪切到/usr/src/redis/目录 --> [root@centos01 ~]# cd /usr/src/redis/ <!--进入redis目录-> [root@centos01 redis]# ls 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests BUGS deps MANIFESTO runtest sentinel.conf utils CONTRIBUTING INSTALL README.md runtest-cluster src [root@centos01 redis]# make && make install <!--编辑及安装redis--> [root@centos01 redis]# cd utils/ <!--进入utils目录--> [root@centos01 utils]# ./install_server.sh <!--初始化redis--> 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] <!--回车键便可--> Selected config: Port : 6379 <!--端口号,默认容许6379--> Config file : /etc/redis/6379.conf <!--redis主配置文件--> Log file : /var/log/redis_6379.log <!--redis日志文件--> 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! [root@centos01 ~]# vim /etc/redis/6379.conf <!--修改redis主配置文件启动群集功能--> 62 bind 192.168.100.10 <!--监听的主机地址--> 85 port 6379 <!--监听端口号--> 129 daemonize yes <!--启动守护进程--> 164 logfile /var/log/redis_6379.log <!--指定日志文件--> 722 cluster-enabled yes <!--启动群集--> 730 cluster-config-file nodes-6379.conf <!--群集配置文件--> 736 cluster-node-timeout 15000 813 cluster-require-full-coverage no [root@centos01 ~]# /etc/init.d/redis_6379 start <!--启动redis服务--> Starting Redis server... [root@centos01 ~]# netstat -anptu | grep redis <!--查看6379和16379端口是否已经正常启动--> tcp 0 0 192.168.100.10:6379 0.0.0.0:* LISTEN 4662/redis-server 1 tcp 0 0 192.168.100.10:16379 0.0.0.0:* LISTEN 4662/redis-server 1 [root@centos01 ~]# yum -y install ruby rubygems <!--安装群集所需依赖程序--> [root@centos01 ~]# gem install redis --version 3.2.0 <!--安装gem工具--> <!--j接下来将redis压缩包远程复制到100.20、30、40、50、60服务器的根目录--> [root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.20:/root <!--将redis压缩包复制到100.20服务器的根目录--> The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts. root@192.168.100.20's password: <!--输入密码--> redis-3.2.9.tar.gz 100% 1511KB 44.5MB/s 00:00 [root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.30:/root <!--将redis压缩包复制到100.30服务器的根目录--> The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts. root@192.168.100.30's password: <!--输入密码--> redis-3.2.9.tar.gz 100% 1511KB 48.7MB/s 00:00 [root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.40:/root <!--将redis压缩包复制到100.40服务器的根目录--> The authenticity of host '192.168.100.40 (192.168.100.40)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.40' (ECDSA) to the list of known hosts. root@192.168.100.40's password: <!--输入密码--> redis-3.2.9.tar.gz 100% 1511KB 72.2MB/s 00:00 [root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.50:/root <!--将redis压缩包复制到100.50服务器的根目录--> The authenticity of host '192.168.100.50 (192.168.100.50)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.50' (ECDSA) to the list of known hosts. root@192.168.100.50's password: <!--输入密码--> redis-3.2.9.tar.gz 100% 1511KB 47.3MB/s 00:00 [root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.60:/root <!--将redis压缩包复制到100.60服务器的根目录--> The authenticity of host '192.168.100.60 (192.168.100.60)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.60' (ECDSA) to the list of known hosts. root@192.168.100.60's password: <!--输入密码--> redis-3.2.9.tar.gz 100% 1511KB 3.5MB/s 00:00
[root@centos02 ~]# yum -y install ruby rubygems <!--安装redis群集所需依赖程序--> [root@centos02 ~]# ls anaconda-ks.cfg initial-setup-ks.cfg redis-3.2.9.tar.gz [root@centos02 ~]# tar zxvf redis-3.2.9.tar.gz <!--解压缩redis程序包--> [root@centos02 ~]# mv redis-3.2.9 /usr/src/redis/ <!--剪切到/usr/src/redis/目录--> [root@centos02 ~]# cd /usr/src/redis/ <!--进入redis目录--> [root@centos02 redis]# make && make install <!--编辑及安装redis--> [root@centos02 redis]# cd utils/ <!--进入utils目录--> [root@centos02 utils]# ./install_server.sh <!--初始化redis--> 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] 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! <!--在第一台redis服务器上将redis主配置文件远程复制到剩下五台服务器上--> [root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.20:/etc/redis/ <!--将第一台redis服务器上的主配置文件远程复制到第二台redis服务器的/etc/redis/目录下--> root@192.168.100.20's password: <!--输入密码--> 6379.conf 100% 46KB 37.4MB/s 00:00 [root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.30:/etc/redis/ <!--将第一台redis服务器上的主配置文件远程复制到第三台redis服务器的/etc/redis/目录下--> root@192.168.100.30's password: <!--输入密码--> 6379.conf 100% 46KB 27.9MB/s 00:00 [root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.40:/etc/redis/ <!--将第一台redis服务器上的主配置文件远程复制到第四台redis服务器的/etc/redis/目录下--> root@192.168.100.40's password: <!--输入密码--> 6379.conf 100% 46KB 13.5MB/s 00:00 [root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.50:/etc/redis/ <!--将第一台redis服务器上的主配置文件远程复制到第五台redis服务器的/etc/redis/目录下--> root@192.168.100.50's password: <!--输入密码--> 6379.conf 100% 46KB 35.4MB/s 00:00 [root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.60:/etc/redis/ <!--将第一台redis服务器上的主配置文件远程复制到第六台redis服务器的/etc/redis/目录下--> root@192.168.100.60's password: <!--输入密码--> 6379.conf 100% 46KB 44.7MB/s 00:00 [root@centos02 ~]# vim /etc/redis/6379.conf <!--第二台redis服务器修改主配置文件监听的IP地址--> bind 192.168.100.20 <!--将IP地址改成本服务器自身的IP--> [root@centos02 ~]# redis-server /etc/redis/6379.conf <!--启动redis服务--> [root@centos02 ~]# netstat -anptu | grep redis <!--监听redis服务是否正常启动--> tcp 0 0 192.168.100.20:6379 0.0.0.0:* LISTEN 6224/redis-server 1 tcp 0 0 192.168.100.20:16379 0.0.0.0:* LISTEN 6224/redis-server 1
剩下四台服务器的操做步骤按照第二台的操做步骤作一样的配置便可,修改主配置文件监听的IP地址设置成服务器本身的IP地址便可
建立群集要用到ruby的一个脚本,在建立群集以前,须要先安装ruby的运行环境和ruby的Redis客户端,该操做在其中一台服务器进行便可。gem命令是提早下载的redis-3.2.0 gem软件包提供的,直接上传服务器便可使用。
[root@centos01 ~]# /usr/src/redis/src/redis-trib.rb create --replicas 1 192.168.100.10:6379 192.168.100.20:6379 192.192.168.100.30:6379 192.168.100.40:6379 192. 192.168.100.50:6379 192.168.100.60:6379 <!--在第一台redis服务器上使用脚本建立群集--> >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.100.10:6379 192.168.100.20:6379 192.168.100.30:6379 Adding replica 192.168.100.40:6379 to 192.168.100.10:6379 Adding replica 192.168.100.50:6379 to 192.168.100.20:6379 Adding replica 192.168.100.60:6379 to 192.168.100.30:6379 M: 53a7082afe52d1216a447bd505f1828e8c04c7b6 192.168.100.10:6379 slots:0-5460 (5461 slots) master M: 7023c518c9bde44e137db167abcaf3ef6302ef5c 192.168.100.20:6379 slots:5461-10922 (5462 slots) master M: 82196443876dd7a7dba2cbda237064577e6996e5 192.168.100.30:6379 slots:10923-16383 (5461 slots) master S: b86a7228dc45da696a9e95f6593cf28e9d350643 192.168.100.40:6379 replicates 53a7082afe52d1216a447bd505f1828e8c04c7b6 S: 2ef78b8d7e4174c7cb8ff6c9c7834e8e0e97e6fc 192.168.100.50:6379 replicates 7023c518c9bde44e137db167abcaf3ef6302ef5c S: 161c231d36b342103ff1524d027e131e66da06ef 192.168.100.60:6379 replicates 82196443876dd7a7dba2cbda237064577e6996e5 Can I set the above configuration? (type 'yes' to accept): yes <!--输入“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 192.168.100.10:6379) M: 53a7082afe52d1216a447bd505f1828e8c04c7b6 192.168.100.10:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 7023c518c9bde44e137db167abcaf3ef6302ef5c 192.168.100.20:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: b86a7228dc45da696a9e95f6593cf28e9d350643 192.168.100.40:6379 slots: (0 slots) slave replicates 53a7082afe52d1216a447bd505f1828e8c04c7b6 M: 82196443876dd7a7dba2cbda237064577e6996e5 192.168.100.30:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 161c231d36b342103ff1524d027e131e66da06ef 192.168.100.60:6379 slots: (0 slots) slave replicates 82196443876dd7a7dba2cbda237064577e6996e5 S: 2ef78b8d7e4174c7cb8ff6c9c7834e8e0e97e6fc 192.168.100.50:6379 slots: (0 slots) slave replicates 7023c518c9bde44e137db167abcaf3ef6302ef5c [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@centos01 ~]# cd /usr/src/redis/src/ [root@centos01 src]# ./redis-trib.rb check 192.168.100.10:6379 <!--查看群集状态 (能够清楚的看到哪台主机是master、哪台主机是salve)--> >>> Performing Cluster Check (using node 192.168.100.10:6379) M: 53a7082afe52d1216a447bd505f1828e8c04c7b6 192.168.100.10:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 7023c518c9bde44e137db167abcaf3ef6302ef5c 192.168.100.20:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: b86a7228dc45da696a9e95f6593cf28e9d350643 192.168.100.40:6379 slots: (0 slots) slave replicates 53a7082afe52d1216a447bd505f1828e8c04c7b6 M: 82196443876dd7a7dba2cbda237064577e6996e5 192.168.100.30:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 161c231d36b342103ff1524d027e131e66da06ef 192.168.100.60:6379 slots: (0 slots) slave replicates 82196443876dd7a7dba2cbda237064577e6996e5 S: 2ef78b8d7e4174c7cb8ff6c9c7834e8e0e97e6fc 192.168.100.50:6379 slots: (0 slots) slave replicates 7023c518c9bde44e137db167abcaf3ef6302ef5c [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
<!--登陆redis群集,设置键值测试。这里须要跟“-c”参数来激活群集模式--> [root@centos01 ~]# redis-cli -h 192.168.100.10 -p 6379 -c <!--登陆到群集--> 192.168.100.10:6379> set centos 7.4 <!--插入数据--> OK 192.168.100.10:6379> get centos <!--查看--> "7.4" 192.168.100.10:6379> quit <!--退出群集--> [root@centos01 ~]# redis-cli -h 192.168.100.40 -p 6379 -c 192.168.100.40:6379> get centos <!--查看在100.10上插入的数据,一样能够看到;数据自动同步其redis服务器--> -> Redirected to slot [467] located at 192.168.100.10:6379 "7.4" 192.168.100.10:6379> quit [root@centos01 ~]# redis-cli -h 192.168.100.60 -p 6379 -c 192.168.100.60:6379> get centos -> Redirected to slot [467] located at 192.168.100.10:6379 "7.4" 192.168.100.10:6379> quit
———————— 本文至此结束,感谢阅读 ————————