在Redis安装目录下有创建集群的命令create-cluster start,首先进入该目录
cd /opt/redis-3.2.8/utils/create-cluster/
1、执行 ./create-cluster start后创建6个节点如下:
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
2、执行 ./create-cluster create后,键入yes
Using 3 masters:
127.0.0.1:30001
127.0.0.1:30002
127.0.0.1:30003
Adding replica 127.0.0.1:30004 to 127.0.0.1:30001
Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
M: 914b965bb9f4f9058683149ab383c2b868845b51 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: c17b39b369260fca37d9eb125196ac4f91381a03 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: ac9e5b70070fe6c020bb85cd11089871931a1937 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
S: 6100cfd7a6711ec78132e636d9ea4f2dd4561a2b 127.0.0.1:30004
replicates 914b965bb9f4f9058683149ab383c2b868845b51
S: e8885db7349fb6f8b22d32895b6411d4dd95e21d 127.0.0.1:30005
replicates c17b39b369260fca37d9eb125196ac4f91381a03
S: 4490a4492b09757b4b6cb0203b3439c73aaab8c0 127.0.0.1:30006
replicates ac9e5b70070fe6c020bb85cd11089871931a1937
Can I set the above configuration? (type 'yes' to accept):
显示如下表示集群创建成功
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
3、查看集群状态
redis-cli -c -p 30001 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:1229
cluster_stats_messages_received:1229
4、查看集群节点信息
127.0.0.1:30001> cluster nodes
ac9e5b70070fe6c020bb85cd11089871931a1937 127.0.0.1:30003 master - 0 1494133176091 3 connected 10923-16383
c17b39b369260fca37d9eb125196ac4f91381a03 127.0.0.1:30002 master - 0 1494133175688 2 connected 5461-10922
6100cfd7a6711ec78132e636d9ea4f2dd4561a2b 127.0.0.1:30004 slave 914b965bb9f4f9058683149ab383c2b868845b51 0 1494133176091 4 connected
914b965bb9f4f9058683149ab383c2b868845b51 127.0.0.1:30001 myself,master - 0 0 1 connected 0-5460
e8885db7349fb6f8b22d32895b6411d4dd95e21d 127.0.0.1:30005 slave c17b39b369260fca37d9eb125196ac4f91381a03 0 1494133176091 5 connected
4490a4492b09757b4b6cb0203b3439c73aaab8c0 127.0.0.1:30006 slave ac9e5b70070fe6c020bb85cd11089871931a1937 0 1494133176091 6 connected
二、手动指定端口,从6000开始,创建6个节点
1、启动6000节点
redis-server --port 6000 --cluster-enabled yes --cluster-config-file nodes-6000.conf --cluster-node-timeout 2000 --cluster-slave-validity-factor 10 --cluster-migration-barrier 1 --cluster-require-full-coverage yes --dbfilename dump-6000.rdb --daemonize yes
各参数说明:
port:节点端口号
cluster-enabled: 指定Redis运行为集群模式
cluster-config-file:集群配置文件路径,该文件保存集群的变化信息
cluster-node-timeout:节点失效时长,单位毫秒,其他主节点在指定时间内无法访问,则任务该节点失效。如果节点是master则触发salve,如果是salve则停止查询
cluster-slave-validity-factor:故障转移因子,超过cluster-node-timeout*cluster-slave-validity-factor的值,从节点将不能成为主节点
cluster-migration-barrier:最小从节点个数,例如:A--A1,A2,B-B1,C-C1,如果该值设置为2,当C故障时,C1成为主,A还是两个从节点,主C1将是0个从节点,因为该值为2
cluster-require-full-coverage:yes时,当集群中某个主节点故障时,整个集群将不可用,设置为no,hash到该节点的写操作失败,读操作正常
dbfilename:redis保存持久化数据的文件
daemonize:yes表示后台模式运行
可以通过bind参数绑定ip,比如--bind 192.168.174.128,跨物理机构建时可以绑定ip
2、修改以上脚本中的端口,启动6001,6002节点
3、添加节点hash映射
redis-cli -c -p 6000 CLUSTER ADDSLOTS {0..5460} redis-cli -c -p 6001 CLUSTER ADDSLOTS {5461..10922} redis-cli -c -p 6002 CLUSTER ADDSLOTS {10923..16383}
4、添加configuration epoch,设置一个数字,描述集群特定时间点的状态,当节点故障转移或者重新分片时使用该值
redis-cli -c -p 6000 CLUSTER SET-CONFIG-EPOCH 1 redis-cli -c -p 6001 CLUSTER SET-CONFIG-EPOCH 2 redis-cli -c -p 6002 CLUSTER SET-CONFIG-EPOCH 3
5、添加节点间互访
redis-cli -c -p 6000 CLUSTER MEET 127.0.0.1 6001 redis-cli -c -p 6000 CLUSTER MEET 127.0.0.1 6002
CLUSTER MEET在节点间可以自动传播
6、查看集群信息
redis-cli -c -p 6000 cluster info
cluster_state:ok
……
至此集群主节点配置完成
7、添加从节点
创建6003节点,将6003加入集群中
redis-cli -c -p 6003 CLUSTER MEET 127.0.0.1 6000
执行redis-cli -c -p 6000 cluster nodes 查看节点
……
632a1c4ad59406434d312690045ea9a29eb43be4 127.0.0.1:6000 myself,master - 0 0 1 connected 0-5460
8、将6003设置为6000的从节点
redis-cli -c -p 6003 CLUSTER REPLICATE 632a1c4ad59406434d312690045ea9a29eb43be4
再查看节点信息
39217187b8c9f9fea9792330cda5cbb5aa5f5c32 127.0.0.1:6003 slave 632a1c4ad59406434d312690045ea9a29eb43be4 0 1494145699026 1 connected,发现6003变为6000的salve
同理可以设置6004为6001,6005为6002的从节点,方法同上
三、Redis提供了一个Ruby的集群创建脚本
1、执行redis-server命令创建6个节点,
for port in 5000 5001 5002 5003 5004 5005 5006 5007; do redis-server --port ${port} --cluster-enabled yes --cluster-config-file nodes-${port}.conf --cluster-node-timeout 2000 --cluster-slave-validityfactor 10 --cluster-migration-barrier 1 --cluster-require-full-coverage yes --dbfilename dump-${port}.rdb --daemonize yes done
2、创建集群
./src/redis-trib.rb create --replicas 1 127.0.0.1:5000 127.0.0.1:5001 127.0.0.1:5002 127.0.0.1:5003 127.0.0.1:5004 127.0.0.1:5005
Ruby脚本操作起来更简单便捷,省去中间步骤