Redis4集群搭建

2018-02-27 10:54:08来源:oschina作者:PengTdy人点击

分享

1.安装单机版redis

redis单机版安装请参考:http://blog.csdn.net/yougoule/article/details/78233583


2.redis集群介绍

redis集群中至少应该有三个节点,要保证集群的高可用,需要每个节点有一个备份机,redis集群至少需要6台服务器;这里介绍的搭建方式是伪分布式,可以使用一台服务器运行6个redis实例,需要修改端口号7001~7006;


3.安装ruby环境
1.1安装ruby
[root@MiWiFi-R3-srv local]# yum install -y ruby
[root@MiWiFi-R3-srv local]# yum install -y rubygems
1.2安装ruby脚本运行使用的包
首先上传一个redis-3.0.0.gem到local目录下
[root@MiWiFi-R3-srv local]# gem install redis-3.0.0.gem
4.redis集群搭建
1.1初始配置
[root@MiWiFi-R3-srv local]# mkdir redis-cluster
[root@MiWiFi-R3-srv local]# cp -r redis/bin/ redis-cluster/redis01
[root@MiWiFi-R3-srv local]# cd redis-cluster/redis01/
[root@MiWiFi-R3-srv redis01]# rm -rf dump.rdb
[root@MiWiFi-R3-srv redis01]# vim redis.conf

修改conf文件中以下内容:


#设置端口号
port 7001
#打开集群模式
cluster-enabled yes
1.2创建6个redis实例
[root@MiWiFi-R3-srv redis01]# cd .. [root@MiWiFi-R3-srv redis-cluster]# cp -r redis01/ redis02 [root@MiWiFi-R3-srv redis-cluster]# cp -r redis01/ redis03 [root@MiWiFi-R3-srv redis-cluster]# cp -r redis01/ redis04 [root@MiWiFi-R3-srv redis-cluster]# cp -r redis01/ redis05 [root@MiWiFi-R3-srv redis-cluster]# cp -r redis01/ redis06
1.3修改各个实例的端口号
[root@MiWiFi-R3-srv redis-cluster]# vim redis02/redis.conf
port 7002 #修改端口号为7002
[root@MiWiFi-R3-srv redis-cluster]# vim redis03/redis.conf
port 7003 #修改端口号为7003
[root@MiWiFi-R3-srv redis-cluster]# vim redis04/redis.conf
port 7004 #修改端口号为7004
[root@MiWiFi-R3-srv redis-cluster]# vim redis05/redis.conf
port 7005 #修改端口号为7005
[root@MiWiFi-R3-srv redis-cluster]# vim redis06/redis.conf
port 7006 #修改端口号为7006
1.4创建批处理运行文件
[root@MiWiFi-R3-srv redis-cluster]# vim start-all.sh

start-all.sh文件中写入以下内容:


cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf

添加可执行权限:


[root@MiWiFi-R3-srv redis-cluster]# chmod u+x start-all.sh
5.运行redis集群
[root@MiWiFi-R3-srv redis-cluster]# ./start-all.sh
4238:C 15 Oct 03:29:23.257 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4238:C 15 Oct 03:29:23.257 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=4238, just started
4238:C 15 Oct 03:29:23.257 # Configuration loaded
4240:C 15 Oct 03:29:23.266 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4240:C 15 Oct 03:29:23.266 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=4240, just started
4240:C 15 Oct 03:29:23.266 # Configuration loaded
4242:C 15 Oct 03:29:23.275 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4242:C 15 Oct 03:29:23.275 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=4242, just started
4242:C 15 Oct 03:29:23.275 # Configuration loaded
4244:C 15 Oct 03:29:23.283 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4244:C 15 Oct 03:29:23.283 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=4244, just started
4244:C 15 Oct 03:29:23.283 # Configuration loaded
4246:C 15 Oct 03:29:23.292 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4246:C 15 Oct 03:29:23.292 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=4246, just started
4246:C 15 Oct 03:29:23.292 # Configuration loaded
4248:C 15 Oct 03:29:23.300 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4248:C 15 Oct 03:29:23.300 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=4248, just started
4248:C 15 Oct 03:29:23.301 # Configuration loaded

ps命令查看redis:


[root@MiWiFi-R3-srv redis-cluster]# ps aux|grep redis
root42390.10.5 1473089592 ? Ssl03:29 0:00 ./redis-server 0.0.0.0:7001 [cluster]
root42410.00.5 1473089592 ? Ssl03:29 0:00 ./redis-server 0.0.0.0:7002 [cluster]
root42430.00.5 1473089592 ? Ssl03:29 0:00 ./redis-server 0.0.0.0:7003 [cluster]
root42450.00.5 1473089596 ? Ssl03:29 0:00 ./redis-server 0.0.0.0:7004 [cluster]
root42470.00.5 1473089592 ? Ssl03:29 0:00 ./redis-server 0.0.0.0:7005 [cluster]
root42490.00.5 1473089592 ? Ssl03:29 0:00 ./redis-server 0.0.0.0:7006 [cluster]
root42690.00.0 112664 968 pts/0S+ 03:29 0:00 grep --color=auto redis

拷贝ruby脚本:


[root@MiWiFi-R3-srv redis-cluster]# cp ../redis-4.0.2/src/redis-trib.rb .

使用ruby脚本搭建集群:


[root@MiWiFi-R3-srv redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.31.241:7001 192.168.31.241:7002 192.168.31.241:7003 192.168.31.241:7004 192.168.31.241:7005 192.168.31.241:7006

这里输入:yes


Can I set the above configuration? (type 'yes' to accept): yes
6.测试

使用redis-cli客户端连接集群


[root@MiWiFi-R3-srv redis-cluster]# redis01/redis-cli -p 7001 -c

使用set和get测试


127.0.0.1:7001> set str 123
-> Redirected to slot [6928] located at 192.168.31.241:7002
OK
192.168.31.241:7002> get str
"123"
192.168.31.241:7002>

当前集群状态:


192.168.31.241:7002> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:468
cluster_stats_messages_pong_sent:450
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:922
cluster_stats_messages_ping_received:447
cluster_stats_messages_pong_received:472
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:922
192.168.31.241:7002>

查询当前节点:


192.168.31.241:7002> cluster nodes
1287195e99eb770187ef35b50cf564ebbaa77c9a 192.168.31.241:7002@17002 myself,master - 0 1508010819000 2 connected 5461-10922
e370d523e92609d96edbc61cf93bf5551752d74c 192.168.31.241:7006@17006 slave 4a7335af725d38263589dcbe63243486568131c2 0 1508010819607 6 connected
4a7335af725d38263589dcbe63243486568131c2 192.168.31.241:7003@17003 master - 0 1508010821000 3 connected 10923-16383
9dc894458b45d67c04e08de14b5bc483fa208e0e 192.168.31.241:7005@17005 slave 1287195e99eb770187ef35b50cf564ebbaa77c9a 0 1508010818606 5 connected
6fbe4d50fc350955762467226006a4a062e973ef 192.168.31.241:7001@17001 master - 0 1508010820608 1 connected 0-5460
4813ee384848ed5e9debb92e081ed9133faee806 192.168.31.241:7004@17004 slave 6fbe4d50fc350955762467226006a4a062e973ef 0 1508010821610 4 connected
192.168.31.241:7002>

至此,Redis集群就搭建完成了!!!

7.redis-cluster的架构(附知识点!)
1.1 redis-cluster架构图

这里写图片描述


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


1.2 redis-cluster选举:容错

这里写图片描述


(1)领着选举过程是集群中所有master参与,如果半数以上master节点与故障节点通信超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操作.(2):什么时候整个集群不可用(cluster_state:fail)?a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态. ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.b:如果集群超过半数以上master挂掉,无论是否有slave集群进入fail状态.ps:当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误;

如果您喜欢这篇文章就请关注我的博客和朋友一起分享吧!关注我: ☛ http://blog.csdn.net/yougoule

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台