1. Redis集群提供了以下两个好处
(1) 将数据自动切分(split)到多个节点
(2) 当集群中的某一个节点故障时,redis还可以继续处理客户端的请求。
2. 集群的方案:
redis-cluster集群,采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接,主要通过节点的配置,辅以redis的主从来完成集群。
方法比较简单. 建议使用两台虚拟机或是docker.
咱们以a机器 和b机器为例子
这两台机器,存在以下目录 /data/config/redis/cluster7001
a机器配置如下(开3个实例)
a机器目录需要存在/data/config/redis/cluster7001
/data/config/redis/cluster7002
/data/config/redis/cluster7003
b机器需要目录 存在
/data/config/redis/cluster7004
/data/config/redis/cluster7005
/data/config/redis/cluster7006
/data/conf/redis/cluster7001/redis.conf
1 daemonize yes
2 port 7001
3 bind 0.0.0.0
4 timeout 0
5 loglevel notice
6 pidfile /data/conf/redis/cluster7001/cluster-7001.pid
7 cluster-enabled yes
8 cluster-config-file /data/conf/redis/cluster7001/config.conf
9 cluster-node-timeout 1500
10 appendonly yes
/data/conf/redis/cluster7002/redis.conf
/data/conf/redis/cluster7003/redis.conf
以上两个文件按上面的配置完全一样.但把关键词替换一下. 7001换成7002 7003
b机器配置,也一样开三个
/data/conf/redis/cluster7004/redis.conf
/data/conf/redis/cluster7005/redis.conf
/data/conf/redis/cluster7006/redis.conf
a启动redis
redis-server /data/conf/redis/cluster7001/redis.conf
redis-server /data/conf/redis/cluster7002/redis.conf
redis-server /data/conf/redis/cluster7003/redis.conf
b启动redis
redis-server /data/conf/redis/cluster7004/redis.conf
redis-server /data/conf/redis/cluster7005/redis.conf
redis-server /data/conf/redis/cluster7006/redis.conf
到此. 集群基本配置完成
接一下把它们联系起来.
下载源码,找到src目录 里的 redis-trib.rb 拷到 /usr/bin/下.
然后安装这个程序最基本的运行环境 ruby
1 yum install -y ruby ruby-devel rubygems rpm-build ;
2 gem install redis;
再然后 执行以下命令(a的ip 是0.2 b是0.3 ,redis-trib.rb在哪台机器执行都可以,以下命令哪台执行都可以)
redis-trib.rb create --replicas 1 172.17.0.2:7001 172.17.0.2:7002 172.17.0.2:7003 172.17.0.3:7004 172.17.0.3:7005 172.17.0.3:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.17.0.3:7004
172.17.0.2:7001
172.17.0.3:7005
Adding replica 172.17.0.2:7002 to 172.17.0.3:7004
Adding replica 172.17.0.3:7006 to 172.17.0.2:7001
Adding replica 172.17.0.2:7003 to 172.17.0.3:7005
M: 0f0f6a4a458830fac67d71d532da8ea92ff016cb 172.17.0.2:7001
slots:5461-10922 (5462 slots) master
S: a2979f1e90d2148e7ae3355c6f61dd1dc2d510bc 172.17.0.2:7002
replicates 333f379acab6c22525330c3a08bfc3442597ec0c
S: 4d273f96aebb61adbf9b9b8c96458358e5b6c57f 172.17.0.2:7003
replicates 2b57d94794fa802a1df302e54068d4d7912e1550
M: 333f379acab6c22525330c3a08bfc3442597ec0c 172.17.0.3:7004
slots:0-5460 (5461 slots) master
M: 2b57d94794fa802a1df302e54068d4d7912e1550 172.17.0.3:7005
slots:10923-16383 (5461 slots) master
S: 1089b5b933ca452fbb99878fd1e5fe49c003c6ff 172.17.0.3:7006
replicates 0f0f6a4a458830fac67d71d532da8ea92ff016cb
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 172.17.0.2:7001)
M: 0f0f6a4a458830fac67d71d532da8ea92ff016cb 172.17.0.2:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 333f379acab6c22525330c3a08bfc3442597ec0c 172.17.0.3:7004
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 2b57d94794fa802a1df302e54068d4d7912e1550 172.17.0.3:7005
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 4d273f96aebb61adbf9b9b8c96458358e5b6c57f 172.17.0.2:7003
slots: (0 slots) slave
replicates 2b57d94794fa802a1df302e54068d4d7912e1550
S: a2979f1e90d2148e7ae3355c6f61dd1dc2d510bc 172.17.0.2:7002
slots: (0 slots) slave
replicates 333f379acab6c22525330c3a08bfc3442597ec0c
S: 1089b5b933ca452fbb99878fd1e5fe49c003c6ff 172.17.0.3:7006
slots: (0 slots) slave
replicates 0f0f6a4a458830fac67d71d532da8ea92ff016cb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到此.就完成了集群的配置了. 超级简单吧 以下是测试代码(b机器进行的测试)
[root@c6077bc08be1 src]# redis-cli -c -h 127.0.0.1 -p 7004
127.0.0.1:7004> set test test
-> Redirected to slot [6918] located at 172.17.0.2:7001
OK
172.17.0.2:7001> get test
"test"
172.17.0.2:7001> exit
[root@c6077bc08be1 src]# redis-cli -c -h 127.0.0.1 -p 7005
127.0.0.1:7005> get test
-> Redirected to slot [6918] located at 172.17.0.2:7001
"test"
172.17.0.2:7001> exit
[root@c6077bc08be1 src]# redis-cli -c -h 172.17.0.2 -p 7002
172.17.0.2:7002> get test
-> Redirected to slot [6918] located at 172.17.0.2:7001
"test"