I. Overview
Cluster is supported after Redis 3.0.
1.1, the status quo of redis cluster
The cluster features currently supported by redis:
1): Node automatic discovery
2): slave->master election, cluster fault tolerance
3):Hot resharding: Online sharding
4): Group management: cluster xxx
5): Cluster management based on configuration (nodes-port.conf)
6):ASK steering/MOVED steering mechanism.
1.2, redis cluster architecture
1) redis-cluster architecture diagram
Architecture details:
(1) All redis nodes are interconnected with each other (PING-PONG mechanism), and the binary protocol is used internally to optimize the transmission speed and bandwidth.
(2) The fail of a node takes effect only when more than half of the nodes in the cluster detect the failure.
(3) The client is directly connected to the redis node, no intermediate proxy layer is required. The client does not need to connect to all nodes in the cluster, just connect to any available node in the cluster
(4) redis-cluster maps all physical nodes to [0-16383] slot, cluster is responsible for maintaining node<->slot<->value
2) redis-cluster election: fault tolerance
(1) Leading the election process is the participation of all masters in the cluster. If the communication between more than half of the master nodes and the master node exceeds (cluster-node-timeout), the current master node is considered to be hung up.
(2): When the entire cluster is unavailable (cluster_state: fail), when the cluster is unavailable, all operations on the cluster are unavailable, and the ((error) CLUSTERDOWN The cluster is down) error is received
a: If any master of the cluster hangs, and the current master has no slave. The cluster enters the fail state, it can also be understood as entering the fail state when the slot mapping [0-16383] of the cluster is not completed.
b:如果进群超过半数以上master挂掉,无论是否有slave集群进入fail状态.
二、redis cluster安装
1、下载和解包
1
2
3
|
cd /usr/local/
wget http:
//download.redis.io/releases/redis-3.2.1.tar.gz
tar -zxvf /redis-
3.2
.
1
.tar.gz
|
2、 编译安装
cd redis-3.2.1 make && make install
3、创建redis节点
测试我们选择2台服务器,分别为:192.168.1.237,192.168.1.238.每分服务器有3个节点。
我先在192.168.1.237创建3个节点:
cd /usr/local/ mkdir redis_cluster //创建集群目录 mkdir 7000 7001 7002 //分别代表三个节点 其对应端口 7000 7001 7002 //创建7000节点为例,拷贝到7000目录 cp /usr/local/redis-3.2.1/redis.conf ./redis_cluster/7000/ //拷贝到7001目录 cp /usr/local/redis-3.2.1/redis.conf ./redis_cluster/7001/ //拷贝到7002目录 cp /usr/local/redis-3.2.1/redis.conf ./redis_cluster/7002/
分别对7001,7002、7003文件夹中的3个文件修改对应的配置
daemonize yes //redis后台运行 pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7002,7003 port 7000 //端口7000,7002,7003 cluster-enabled yes //开启集群 把注释#去掉 cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002 cluster-node-timeout 5000 //请求超时 设置5秒够了 appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
在192.168.1.238创建3个节点:对应的端口改为7003,7004,7005.配置对应的改一下就可以了。
4、两台机启动各节点(两台服务器方式一样)
cd /usr/local redis-server redis_cluster/7000/redis.conf redis-server redis_cluster/7001/redis.conf redis-server redis_cluster/7002/redis.conf redis-server redis_cluster/7003/redis.conf redis-server redis_cluster/7004/redis.conf redis-server redis_cluster/7005/redis.conf
5、查看服务
ps -ef | grep redis #查看是否启动成功
netstat -tnlp | grep redis #可以看到redis监听端口
三、创建集群
前面已经准备好了搭建集群的redis节点,接下来我们要把这些节点都串连起来搭建集群。官方提供了一个工具:redis-trib.rb(/usr/local/redis-3.2.1/src/redis-trib.rb) 看后缀就知道这鸟东西不能直接执行,它是用ruby写的一个程序,所以我们还得安装ruby.
yum -y install ruby ruby-devel rubygems rpm-build
再用 gem 这个命令来安装 redis接口 gem是ruby的一个工具包.
gem install redis //等一会儿就好了 当然,方便操作,两台Server都要安装。
上面的步骤完事了,接下来运行一下redis-trib.rb
/usr/local/redis-3.2.1/src/redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
reshard host:port
--to <arg>
--yes
--slots <arg>
--from <arg>
check host:port
call host:port command arg arg .. arg
set-timeout host:port milliseconds
add-node new_host:new_port existing_host:existing_port
--master-id <arg>
--slave
del-node host:port node_id
fix host:port
import host:port
--from <arg>
help (show this help)
create host1:port1 ... hostN:portN
--replicas <arg>
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
看到这,应该明白了吧, 就是靠上面这些操作 完成redis集群搭建的.
确认所有的节点都启动,接下来使用参数create 创建 (在192.168.1.237中来创建)
/usr/local/redis-3.2.1/src/redis-trib.rb create --replicas 1 192.168.1.237:7000 192.168.1.237:7001 192.168.1.237:7003 192.168.1.238:7003 192.168.1.238:7004 192.168.1.238:7005
解释下, --replicas 1 表示 自动为每一个master节点分配一个slave节点 上面有6个节点,程序会按照一定规则生成 3个master(主)3个slave(从)
前面已经提醒过的 防火墙一定要开放监听的端口,否则会创建失败。
运行中,提示Can I set the above configuration? (type 'yes' to accept): yes //输入yes
接下来 提示 Waiting for the cluster to join.......... 安装的时候在这里就一直等等等,没反应,傻傻等半天,看这句提示上面一句,Sending Cluster Meet Message to join the Cluster.
这下明白了,我刚开始在一台Server上去配,也是不需要等的,这里还需要跑到Server2上做一些这样的操作。
在192.168.1.238, redis-cli -c -p 700* 分别进入redis各节点的客户端命令窗口, 依次输入 cluster meet 192.168.1.238 7000……
回到Server1,已经创建完毕了。
查看一下 /usr/local/redis/src/redis-trib.rb check 192.168.1.237:7000
到这里集群已经初步搭建好了。
四、测试
1)get 和 set数据
redis-cli -c -p 7000
进入命令窗口,直接 set hello howareyou
直接根据hash匹配切换到相应的slot的节点上。
还是要说明一下,redis集群有16383个slot组成,通过分片分布到多个节点上,读写都发生在master节点。
2)假设测试
果断先把192.168.1.238服务Down掉,(192.168.1.238有1个Master, 2个Slave) , 跑回192.168.1.238, 查看一下 发生了什么事,192.168.1.237的3个节点全部都是Master,其他几个Server2的不见了
测试一下,依然没有问题,集群依然能继续工作。
原因: redis集群 通过选举方式进行容错,保证一台Server挂了还能跑,这个选举是全部集群超过半数以上的Master发现其他Master挂了后,会将其他对应的Slave节点升级成Master.
疑问: 要是挂的是192.168.1.237怎么办? 哥试了,cluster is down!! 没办法,超过半数挂了那救不了了,整个集群就无法工作了。 要是有三台Server,每台两Master,切记对应的主从节点
不要放在一台Server,别问我为什么自己用脑子想想看,互相交叉配置主从,挂哪台也没事,你要说同时两台crash了,呵呵哒......
3)关于一致性
我还没有这么大胆拿redis来做数据库持久化哥网站数据,只是拿来做cache,官网说的很清楚,Redis Cluster is not able to guarantee strong consistency.
五、安装遇到的问题
1、
CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/usr/local/redis-3.2.1/src
make: *** [all] Error 2
解决办法:GCC没有安装或版本不对,安装一下
yum install gcc
2、
zmalloc.h:50:31:
error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error:
#error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error
1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src
make: *** [all]
Error 2
解决办法:原因是没有安装jemalloc内存分配器,可以安装jemalloc 或 直接
输入make MALLOC=libc && make install