Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。集群的每个节点负责一部分hash槽。这种结构很容易添加或者删除节点,并且无论是添加删除或者修改某一个节点,都不会造成集群不可用的状态。使用哈希槽的好处就在于可以方便的添加或移除节点。当需要增加节点时,只需要把其他节点的某些哈希槽挪到新节点就可以了;当需要移除节点时,只需要把移除节点上的哈希槽挪到其他节点就行了;在这一点上,我们以后新增或移除节点的时候不用先停掉所有的 redis 服务。
1、集群至少要有3个节点,每个节点都有主从结构
mkdir /usr/data/redis/cluster
mkdir 6001 6002 6003 6004 6005 6006
cp redis.conf ../cluster/6001
改 1.端口2.pidfile 3.log 4.aof方式 5.redis cluster里注释去掉(cluster-enabled=yes …)
******
连接 redis-cli -c –h 192.168.1.1 –p 6001
cluster info 查看状态 没有成功
redis-cli -c -h 192.168.1.1 -p 6001
192.168.1.1:6001> CLUSTER info
cluster_state:fail
2、安装ruby,必须2.0以上,2.0以上自带rubygems包管理器 yum install ruby
ruby的安装首先查看你的Linux系统是否安装了ruby可以用下面两个命令查询
rpm -qa | grep ruby
yum list | grep ruby
查看是否成功
ruby -v
输出 ruby 2.0.0p598 (2014-11-13) [x86_64-linux]
3、gem命令安装redis包,增加redis-trib.rb调用redis的接口包
gem install redis
Fetching: redis-3.3.2.gem (100%)
Successfully installed redis-3.3.2
Parsing documentation for redis-3.3.2
Installing ri documentation for redis-3.3.2
1 gem installed
[root@iZ23pv5rps8Z cluster]# gem list
*** LOCAL GEMS ***
bigdecimal (1.2.0)
io-console (0.4.2)
json (1.7.7)
psych (2.0.0)
rdoc (4.0.0)
redis (3.3.2)
4、用redis-trib.rb集群工具管理redis集群
./redis-trib.rb create --replicas 1 192.168.1.1:6001 192.168.1.1:6003 192.168.1.1:6005 192.168.1.1:6002 192.168.1.1:6004 192.168.1.1:6006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.1:6001
192.168.1.1:6003
192.168.1.1:6005
Adding replica 192.168.1.1:6002 to 192.168.1.1:6001
Adding replica 192.168.1.1:6004 to 192.168.1.1:6003
Adding replica 192.168.1.1:6006 to 192.168.1.1:6005
M: 7cb7a99292c9bee9c09651d18744841af89f0857 192.168.1.1:6001
slots:0-5460 (5461 slots) master
M: 21b83ea73203786d9057acfb06606594dbbd7e1b 192.168.1.1:6003
slots:5461-10922 (5462 slots) master
M: 85d44c9015fd3901e31bee4cd98ac423ce62d943 192.168.1.1:6005
slots:10923-16383 (5461 slots) master
S: d10d3aeee290382aca5c69476b17f9e3d2d3e73b 192.168.1.1:6002
replicates 7cb7a99292c9bee9c09651d18744841af89f0857
S: a1d8a58b9bc5adea852c229656fa6eba3d63c706 192.168.1.1:6004
replicates 21b83ea73203786d9057acfb06606594dbbd7e1b
S: 9adb885bf4e0cf9c5bf820b0a33e97f29d7396b9 192.168.1.1:6006
replicates 85d44c9015fd3901e31bee4cd98ac423ce62d943
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.1.1:6001)
M: 7cb7a99292c9bee9c09651d18744841af89f0857 192.168.1.1:6001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 85d44c9015fd3901e31bee4cd98ac423ce62d943 192.168.1.1:6005
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 9adb885bf4e0cf9c5bf820b0a33e97f29d7396b9 192.168.1.1:6006
slots: (0 slots) slave
replicates 85d44c9015fd3901e31bee4cd98ac423ce62d943
S: d10d3aeee290382aca5c69476b17f9e3d2d3e73b 192.168.1.1:6002
slots: (0 slots) slave
replicates 7cb7a99292c9bee9c09651d18744841af89f0857
M: 21b83ea73203786d9057acfb06606594dbbd7e1b 192.168.1.1:6003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: a1d8a58b9bc5adea852c229656fa6eba3d63c706 192.168.1.1:6004
slots: (0 slots) slave
replicates 21b83ea73203786d9057acfb06606594dbbd7e1b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
--replicas 1 设置从节点个数
进入集群模式去操作redis命令,-c集群模式
redis-cli -c -h 192.168.1.1 -p 6001
192.168.1.1:6001>cluster info
# Keyspace
192.168.1.1:6001> cluster info
cluster_state:ok
192.168.1.1:6001> cluster nodes
85d44c9015fd3901e31bee4cd98ac423ce62d943 192.168.1.1:6005 master - 0 1479610282377 3 connected 10923-16383
9adb885bf4e0cf9c5bf820b0a33e97f29d7396b9 192.168.1.1:6006 slave 85d44c9015fd3901e31bee4cd98ac423ce62d943 0 1479610279369 6 connected
d10d3aeee290382aca5c69476b17f9e3d2d3e73b 192.168.1.1:6002 slave 7cb7a99292c9bee9c09651d18744841af89f0857 0 1479610282878 4 connected
21b83ea73203786d9057acfb06606594dbbd7e1b 192.168.1.1:6003 master - 0 1479610281373 2 connected 5461-10922
7cb7a99292c9bee9c09651d18744841af89f0857 192.168.1.1:6001 myself,master - 0 0 1 connected 0-5460
a1d8a58b9bc5adea852c229656fa6eba3d63c706 192.168.1.1:6004 slave 21b83ea73203786d9057acfb06606594dbbd7e1b 0 1479610280371 5 connected
搭建已完成
192.168.1.1:6001> set lisi lisi2
-> Redirected to slot [13215] located at 192.168.1.1:6005
OK
192.168.1.1:6005> get lisi
"lisi2"
192.168.1.1:6005> set lisi2 lisi3
-> Redirected to slot [7849] located at 192.168.1.1:6003
OK
192.168.1.1:6003> get lisi2
"lisi3"
Redis 集群添加节点
主要是看现象,slots转移时,对应数据会有什么变化?
增加节点
./redis-trib.rb add-node 192.168.1.1:6007 192.168.1.1:6001
>>> Send CLUSTER MEET to node 192.168.1.1:6007 to make it join the cluster.
[OK] New node added correctly.
查看节点情况
cluster nodes
108a5fbc0a7898e8e2593abbeba0ce52ebd746b9 192.168.1.1:6007 master - 0 1479628890290 0 connected
移动哈希槽,哈希槽重新分配 数据块移动的时候数据也跟着移动到新节点
./redis-trib.rb reshard 192.168.1.1:6001
How many slots do you wan to move(from 1 to 16384)?1000
what is the receving node ID? 108a5fbc0a7898e8e2593abbeba0ce52ebd746b9
Type 'all' to use all...
Type 'done' once you ...?
Source node #1: all
输入all 表示从所有的主节点中随机转移,凑够xx个哈希槽
192.168.1.1:6001> CLUSTER nodes
7cb7a99292c9bee9c09651d18744841af89f0857 192.168.1.1:6001 myself,master - 0 0 1 connected 333-5460
21b83ea73203786d9057acfb06606594dbbd7e1b 192.168.1.1:6003 master - 0 1479629365349 2 connected 5795-10922
d10d3aeee290382aca5c69476b17f9e3d2d3e73b 192.168.1.1:6002 slave 7cb7a99292c9bee9c09651d18744841af89f0857 0 1479629370361 4 connected
a1d8a58b9bc5adea852c229656fa6eba3d63c706 192.168.1.1:6004 slave 21b83ea73203786d9057acfb06606594dbbd7e1b 0 1479629369357 5 connected
85d44c9015fd3901e31bee4cd98ac423ce62d943 192.168.1.1:6005 master - 0 1479629368355 3 connected 11256-16383
108a5fbc0a7898e8e2593abbeba0ce52ebd746b9 192.168.1.1:6007 master - 0 1479629366352 7 connected 0-332 5461-5794 10923-11255
9adb885bf4e0cf9c5bf820b0a33e97f29d7396b9 192.168.1.1:6006 slave 85d44c9015fd3901e31bee4cd98ac423ce62d943 0 1479629367354 6 connected
Redis集群删除节点
把hash slots转出来,转到其他master节点上
./redis-trib.rb reshard 192.168.1.1:6001
然后删除节点
./redis-trib.rb del-node 192.168.1.1:6007 108a5fbc0a7898e8e2593abbeba0ce52ebd746b9
>>> Removing node 108a5fbc0a7898e8e2593abbeba0ce52ebd746b9 from cluster 120.26.230.116:6007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
Redis 监控工具
历史redis运行查询:CPU、内存、命中率、请求量、主从切换等
实时监控曲线
短信报警
使用基于开源Redis Live 修改工具,便于批量实例监控,基础功能都已实现,细节也将逐步完善。
源码地址如下: