公司的很多项目在使用redis主从。由于coder的各种毁灭性操作,迫切需要一个能带故障恢复的架构。因此新版的cluster,开始了测试。
Redis集群是一个提供在多个Redis间节点间共享数据的程序集。
Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误。
Redis 集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令。
Redis 集群的优势:
Redis 并不能保证数据的强一致性. 这意味这在实际中集群在特定的条件下可能会丢失写操作.
第一个原因是因为集群是用了异步复制. 写操作过程:
客户端向主节点B写入一条命令。
主节点B向客户端回复命令状态。
主节点将写操作复制给他得从节点 B1, B2 和 B3。
主节点对命令的复制工作发生在返回命令回复之后, 因为如果每次处理命令请求都需要等待复制操作完成的话, 那么主节点处理命令请求的速度将极大地降低 —— 我们必须在性能和一致性之间做出权衡。
Redis 集群另外一种可能会丢失命令的情况是集群出现了网络分区, 并且一个客户端与至少包括一个主节点在内的少数实例被孤立。.
举个例子 假设集群包含 A 、 B 、 C 、 A1 、 B1 、 C1 六个节点, 其中 A 、B 、C 为主节点, A1 、B1 、C1 为A,B,C的从节点, 还有一个客户端 Z1
假设集群中发生网络分区,那么集群可能会分为两方,大部分的一方包含节点 A 、C 、A1 、B1 和 C1 ,小部分的一方则包含节点 B 和客户端 Z1 。
Z1仍然能够向主节点B中写入, 如果网络分区发生时间较短,那么集群将会继续正常运作,如果分区的时间足够让大部分的一方将B1选举为新的master,那么Z1写入B中得数据便丢失了。
1、redis-cluster架构图
架构细节:
2、redis-cluster选举:容错
所需软件:
redis-3.0.6.tar.gz
tcl8.6.1-src.tar.gz
rubygems-2.4.2.tgz
redis-3.0.0.gem
注意,由于官网,建议6个台服务器,本人笔记本就启动三个VM凑合。
192.168.11.12:6379/6380
192.168.11.13:6379/6380
192.168.11.14:6379/6380
1.安装redis(只演示一台)
mkdir -pv /usr/local/redis6379/{etc,log,var,data}
cd redis-3.0.6
make
make PREFIX=/usr/local/redis6379 install
2.配置文件(仅测试,为最少选项):
cat /usr/local/redis6379/etc/redis6379.conf
daemonize yes
port 6379
appendonly yes
appendfilename “appendonly-6379.aof”
cluster-enabled yes
cluster-config-file /opt/nodes-6379.conf
cluster-node-timeout 5000
cluster-enabled 选项用于开实例的集群模式
cluster-config-file 定了保存节点配置文件的路径, 默认值为 nodes.conf.节点配置文件无须人为修改, 它由 Redis 集群在启动时创建, 并在有需要时自动进行更新。
3.启动redis
/usr/local/redis6379/bin/redis-server /usr/local/redis6379/etc/redis6379.conf
# ps aux | grep redis
root 14968 0.1 0.9 137444 9616 ? Ssl 20:23 0:11 /usr/local/redis6379/bin/redis-server *:6379 [cluster]
root 15002 0.1 0.7 137444 7520 ? Ssl 20:23 0:11 /usr/local/redis6380/bin/redis-server *:6380 [cluster]
4.启动集群
[root@12 10.19.166.212 /usr/local/src/redis-3.0.6/src ]
#/usr/local/src/redis-3.0.6/src/redis-trib.rb create —replicas 1 192.168.11.12:6379 192.168.11.12:6380 192.168.11.13:6379 192.168.11.13:6380 192.168.11.14:6379 192.168.11.14:6380
/usr/bin/env: ruby: 没有那个文件或目录
报错找不到ruby
因为我们还没有安装
5.安装ruby以及ruby依赖
yum -y install ruby ruby-rdoc
注意这时候还缺少依赖,直接开始构建集群的话还会报错:
#/usr/local/src/redis-3.0.6/src/redis-trib.rb create —replicas 1 192.168.11.12:6379 192.168.11.12:6380 192.168.11.13:6379 192.168.11.13:6380 192.168.11.14:6379 192.168.11.14:6380
/usr/local/src/redis-3.0.6/src/redis-trib.rb:24:in `require’: no such file to load — rubygems (LoadError)
from /usr/local/src/redis-3.0.6/src/redis-trib.rb:24
继续安装rubygem
tar zxmf rubygems-2.4.2.tgz
ruby setup.rb
cp bin/gem /usr/local/bin/
gem install -l redis-3.0.0.gem
6.构建集群
/usr/local/src/redis-3.0.6/src/redis-trib.rb create —replicas 1 192.168.11.12:6379 192.168.11.12:6380 192.168.11.13:6379 192.168.11.13:6380 192.168.11.14:6379 192.168.11.14:6380
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.11.14:6379
192.168.11.13:6379
192.168.11.12:6379
Adding replica 192.168.11.13:6380 to 192.168.11.14:6379
Adding replica 192.168.11.14:6380 to 192.168.11.13:6379
Adding replica 192.168.11.12:6380 to 192.168.11.12:6379
M: c776fbe75505f6cc5c452cea363626804d675433 192.168.11.12:6379
slots:10923-16383 (5461 slots) master
S: 4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380
replicates c776fbe75505f6cc5c452cea363626804d675433
M: 945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379
slots:5461-10922 (5462 slots) master
S: 00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380
replicates ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8
M: ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379
slots:0-5460 (5461 slots) master
S: a8334f26be6b35a3e75f037fa5f44779cf970b12 192.168.11.14:6380
replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0
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.11.12:6379)
M: c776fbe75505f6cc5c452cea363626804d675433 192.168.11.12:6379
slots:10923-16383 (5461 slots) master
M: 4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380
slots: (0 slots) master
replicates c776fbe75505f6cc5c452cea363626804d675433
M: 945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379
slots:5461-10922 (5462 slots) master
M: 00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380
slots: (0 slots) master
replicates ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8
M: ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379
slots:0-5460 (5461 slots) master
M: a8334f26be6b35a3e75f037fa5f44779cf970b12 192.168.11.14:6380
slots: (0 slots) master
replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
好了安装完成了
注:
redis-trib.rb 这个命令在这里用于创建一个新的集群, 选项—replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是这个集群实例的地址列表,3个master3个slave。
在这里可以 :/usr/local/src/redis-3.0.6/src/redis-trib.rb create —replicas 0 192.168.11.12:6379 192.168.11.13:6379 192.168.11.14:6379
表示不做slave之后在redis-trib.rb add-node 192.168.11.12:6380 192.168.11.13:6380 添加节点
下面检查一下节点:
/usr/local/src/redis-3.0.6/src/redis-trib.rb check 192.168.11.12:6379
>>> Performing Cluster Check (using node 192.168.11.12:6379)
M: c776fbe75505f6cc5c452cea363626804d675433 192.168.11.12:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: a8334f26be6b35a3e75f037fa5f44779cf970b12 192.168.11.14:6380
slots: (0 slots) slave
replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0
M: 945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380
slots: (0 slots) slave
replicates ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8
M: ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380
slots: (0 slots) slave
replicates c776fbe75505f6cc5c452cea363626804d675433
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
7.测试写入
/usr/local/redis6379/bin/redis-cli -c -h 192.168.11.12 -p 6379
192.168.11.12:6379> set aa 123
-> Redirected to slot [1180] located at 192.168.11.14:6379
OK
192.168.11.14:6379>
[root@13 ~]# /usr/local/redis6379/bin/redis-cli -c -h 192.168.11.12 -p 6380
192.168.11.12:6380> get aa
-> Redirected to slot [1180] located at 192.168.11.14:6379
"123"
192.168.11.14:6379>
测试删除:
[root@12 10.19.166.212 /usr/local/src ]
/usr/local/redis6379/bin/redis-cli -c -h 192.168.11.12 -p 6379 shutdown
[root@12 10.19.166.212 /usr/local/src ]
/usr/local/src/redis-3.0.6/src/redis-trib.rb check 192.168.11.13:6379
>>> Performing Cluster Check (using node 192.168.11.13:6379)
M: 945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380
slots:10923-16383 (5461 slots) master
0 additional replica(s)
S: 00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380
slots: (0 slots) slave
replicates ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8
S: a8334f26be6b35a3e75f037fa5f44779cf970b12 192.168.11.14:6380
slots: (0 slots) slave
replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
/usr/local/redis6379/bin/redis-cli -c -h 192.168.11.12 -p 6380 shutdown
/usr/local/redis6379/bin/redis-cli -c -h 192.168.11.14 -p 6379
192.168.11.14:6379> get aa
(error) CLUSTERDOWN The cluster is down
192.168.11.14:6379> get bb
(error) CLUSTERDOWN The cluster is down
192.168.11.14:6379> quit
/usr/local/redis6379/bin/redis-cli -c -h 192.168.11.14 -p 6379
192.168.11.14:6379> get aa
“123”
192.168.11.14:6379> get bb
-> Redirected to slot [8620] located at 192.168.11.13:6379
“234”
安装到此结束
原文:http://www.cnblogs.com/iteemo/p/5103410.html