redis-cluster安装比较简单,主要的过程为
下载redis源码,编译,编译如果缺少gcc环境,yum install gcc,如果还出错可以指定下make MOLLOC = libc
修改redis.conf,需要几台机器就新建几个文件夹,我这起了6个
配置如下:
port 6379
bind 192.168.245.128
daemonize yes
pidfile /var/run/redis_6379.pid
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 10100
appendonly yes
3.启动节点,配置了几个节点就启动几个
redis-5.0.4/src/redis-server ~/nodes/6379/redis.conf
redis-5.0.4/src/redis-server ~/nodes/6380/redis.conf
redis-5.0.4/src/redis-server ~/nodes/6381/redis.conf
redis-5.0.4/src/redis-server ~/nodes/6382/redis.conf
redis-5.0.4/src/redis-server ~/nodes/6383/redis.conf
redis-5.0.4/src/redis-server ~/nodes/6384/redis.conf
redis-5.0.4/src/redis-server ~/nodes/6385/redis.conf
4.启动集群,启动集群有点麻烦,主要redis-cluster用ruby写的需要2.3.0以上版本,yum源安装的ruby在2.0.0,需要先安装ruby,可以先安装ruby版本管理工具rvm,可以参考这篇安装rvm,安装完ruby后执行
gem install redis
安装完后就可以启动集群了,然后启动redis-cli cluster
,老版本应该是redis-trib.rb
命令,启动集群
redis-5.0.4/src/redis-cli --cluster create 192.168.245.128:6379 192.168.245.128:6380 192.168.245.128:6381 192.168.245.128:6382 192.168.245.128:6383 192.168.245.128:6384
接着redis会显示分配槽信息和集群初始化的信息:
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 2730
Master[1] -> Slots 2731 - 5460
Master[2] -> Slots 5461 - 8191
Master[3] -> Slots 8192 - 10922
Master[4] -> Slots 10923 - 13652
Master[5] -> Slots 13653 - 16383
M: 1986ebba976f77b4dab84a44bac17623dc430ab0 192.168.245.128:6379
slots:[0-2730] (2731 slots) master
M: 97241dfe9f5ffb78398b4936268f034821ff1b4f 192.168.245.128:6380
slots:[2731-5460] (2730 slots) master
M: aed493eb5e4cdf1a2e71896b67d2d3df4688002c 192.168.245.128:6381
slots:[5461-8191] (2731 slots) master
M: f1e5c142e04be48ee0b2788ad832d6ef2c6ec939 192.168.245.128:6382
slots:[8192-10922] (2731 slots) master
M: da9c3ad1769082c7c47d16731784de6026e46de0 192.168.245.128:6383
slots:[10923-13652] (2730 slots) master
M: 9a4552a184647f080e9d280e74bceafd974bd1a5 192.168.245.128:6384
slots:[13653-16383] (2731 slots) master
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.245.128:6379)
M: 1986ebba976f77b4dab84a44bac17623dc430ab0 192.168.245.128:6379
slots:[0-2730] (2731 slots) master
M: 9a4552a184647f080e9d280e74bceafd974bd1a5 192.168.245.128:6384
slots:[13653-16383] (2731 slots) master
M: 97241dfe9f5ffb78398b4936268f034821ff1b4f 192.168.245.128:6380
slots:[2731-5460] (2730 slots) master
M: f1e5c142e04be48ee0b2788ad832d6ef2c6ec939 192.168.245.128:6382
slots:[8192-10922] (2731 slots) master
M: aed493eb5e4cdf1a2e71896b67d2d3df4688002c 192.168.245.128:6381
slots:[5461-8191] (2731 slots) master
M: da9c3ad1769082c7c47d16731784de6026e46de0 192.168.245.128:6383
slots:[10923-13652] (2730 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
可以看到总共6台机器,都是主,16384个槽分到了6台机器上.
二.连接redis
客户端随便输入连接到哪台机器上
redis-5.0.4/src/redis-cli -h 192.168.245.128 -p 6379
随便设一个值
set pmj 'redis'
redis的返回
(error) MOVED 10386 192.168.245.128:6382
可以看到redis的去中心化是需要客户端协助完成的,客户端随便连一台机器,设置值后,当前服务器会这个key是不是本机器,如果不是本机器再告诉客户端应该去哪台机器上设置,如果我们换到连6382这个节点可以看到值设置成功.