博文目录
创新互联建站成立以来不断整合自身及行业资源、不断突破观念以使企业策略得到完善和成熟,建立了一套“以技术为基点,以客户需求中心、市场为导向”的快速反应体系。对公司的主营项目,如中高端企业网站企划 / 设计、行业 / 企业门户设计推广、行业门户平台运营、手机APP定制开发、手机网站制作、微信网站制作、软件开发、简阳服务器托管等实行标准化操作,让客户可以直观的预知到从创新互联建站可以获得的服务效果。
一、Redis群集原理
1、架构细节
2、Redis选举
二、搭建Redis群集
1、安装第一台Redis并修改配置文件
2、安装第二台Redis并修改配置文件
3、使用脚本创建群集
4、测试群集
关于Redis非关系型数据库工作原理详述请参考博文:聊一聊Centos 7中的Redis(非关系型数据库)
Redis Cluster是一个无中心的结构,每个节点都保存数据和整个群集的状态。每个节点都会保存其他节点的信息,知道其他节点锁负责的槽,并且会与其他节点定时发送心跳信息,能够及时感知群集中异常的节点。如下图所示:
当客户端向群集中任一节点发送与数据库键有关的命令时,接受命令的节点会计算出命令要处理的数据库键属于哪个槽,并检查这个槽是否指派给了自己。如果键所在的槽正好指派给了当前节点,那么节点直接执行这个命令;如果键所在的槽并没有指派给当前节点,那么节点会向客户端返回一个MOVED错误,指引客户端转向(redirect)正确的节点,并再次发送之前想要执行的命令。
群集角色有Master和Slave。Master之间分配slots,一共有16384个slot。Slave向它指定的Master同步数据,实现备份。当其中一个Master无法提供服务时,该Master的Slave将提升为Master。以保证群集键slot的完整性。当其中的某一个Master和它的Slave都失效,就会导致slot不完整,群集失效,这是就需要人工去处理了。
群集搭建好后,群集中的每个节点都会定期地想其他节点发送PING消息,如果接收PING消息的节点没有在规定的时间内返回PONG消息,那么发送PING消息的节点就会将其标记为疑似下线(PFAIL)。各个节点会通过互相发送消息的方式来交换群集中各个节点的状态信息。如果在一个群集里面,半数以上的主节点都将某个节点 X 报告为疑似下线,那么这个主节点 X 将被标记为已下线(FAIL),同时会向群集广播一条关于主节点 X 的FAIL消息,所有收到这条FAIL消息的节点都会立即将主节点 X 标记为已下线。
当需要减少或增加群集中的机器时,我们需要将已经指派给某个节点(源节点)的槽改为指派给另一个节点(目标节点),并且将相关槽所属的键值对从源节点移动到目标节点。
Redis群集的重新分片操作是由Redis的群集管理软件redis-trib负责执行的,不支持自动的分片,而且需要自己计算从哪些节点上迁移多少Slot。在重新分片的过程中,群集不需要下线,并且源节点和目标节点都可以继续处理命令请求。
所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽;
节点的失效(fail)在群集中超过半数的主(master)节点检测失效才失效;
客户端与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
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/
[root@centos01 ~]# mv /usr/src/redis-3.2.9/ /usr/src/redis/
[root@centos01 ~]# cd /usr/src/redis/
[root@centos01 redis]# cd utils/
[root@centos01 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]
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!
[root@centos01 ~]# vim /etc/redis/6379.conf
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
Starting Redis server...
[root@centos01 ~]# netstat -anptu | grep redis
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
[root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.20:/root
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
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
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
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
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
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
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
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
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
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
[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
[root@centos02 ~]# mv redis-3.2.9 /usr/src/redis/
[root@centos02 ~]# cd /usr/src/redis/
[root@centos02 redis]# make && make install
[root@centos02 redis]# cd utils/
[root@centos02 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]
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!
[root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.20:/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/
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/
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/
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/
root@192.168.100.60's password:
6379.conf 100% 46KB 44.7MB/s 00:00
[root@centos02 ~]# vim /etc/redis/6379.conf
bind 192.168.100.20
[root@centos02 ~]# redis-server /etc/redis/6379.conf
[root@centos02 ~]# netstat -anptu | grep 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
>>> 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
>>> 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
>>> 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 ~]# 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
-> 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
———————— 本文至此结束,感谢阅读 ————————
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。