本文档基于如下原始文档(CentOS)创建:
修改了一些路径的错误,补全了一些命令执行结果信息,确认在Mac下创建集群成功
同时参考
(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下)
- 127.0.0.1:7000
- 127.0.0.1:7001
- 127.0.0.1:7002
- 127.0.0.1:7003
- 127.0.0.1:7004
- 127.0.0.1:7005
1:官网下载3.0.1 Stable (http://redis.io/)
2:上传服务器,解压,编译
#将下载的文件放到 /usr/local/ tar -zxvf redis-3.0.1.tar.gz #生成 redis-3.0.1文件夹 cd /usr/local/redis-3.0.1 make make install #执行一下make test查看 make的结果全部都是OK #最终结果显示如下 o/ All tests passed without errors!
3:创建集群需要的目录
mkdir -p /usr/local/cluster cd /usr/local/cluster mkdir 7000 mkdir 7001 mkdir 7002 mkdir 7003 mkdir 7004 mkdir 7005
4:修改配置文件redis.conf
cp /usr/local/redis-3.0.1/redis.conf /usr/local/cluster vi redis.conf ##修改配置文件中的下面选项 port 7000 daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ##修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到7000/7001/7002/7003/7004/7005目录下面 cp /usr/local/cluster/redis.conf /usr/local/cluster/7000 cp /usr/local/cluster/redis.conf /usr/local/cluster/7001 cp /usr/local/cluster/redis.conf /usr/local/cluster/7002 cp /usr/local/cluster/redis.conf /usr/local/cluster/7003 cp /usr/local/cluster/redis.conf /usr/local/cluster/7004 cp /usr/local/cluster/redis.conf /usr/local/cluster/7005 ##注意:拷贝完成之后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称
5:分别启动这6个redis实例(实践证明,必须要cd到700X文件夹下执行redis-server命令)
cd /usr/local/cluster/7000 redis-server redis.conf cd /usr/local/cluster/7001 redis-server redis.conf cd /usr/local/cluster/7002 redis-server redis.conf cd /usr/local/cluster/7003 redis-server redis.conf cd /usr/local/cluster/7004 redis-server redis.conf cd /usr/local/cluster/7005 redis-server redis.conf ##启动之后使用命令查看redis的启动情况ps -ef|grep redis 如下显示则说明启动成功 X0544:7005 wei.li$ ps -ef | grep redis 909302175 5547 1 0 11:31上午 ?? 0:01.07 redis-server *:7000 [cluster] 909302175 5736 1 0 11:44上午 ?? 0:00.29 redis-server *:7001 [cluster] 909302175 5761 1 0 11:48上午 ?? 0:00.05 redis-server *:7002 [cluster] 909302175 5773 1 0 11:49上午 ?? 0:00.03 redis-server *:7003 [cluster] 909302175 5780 1 0 11:49上午 ?? 0:00.02 redis-server *:7004 [cluster] 909302175 5787 1 0 11:49上午 ?? 0:00.01 redis-server *:7005 [cluster] 909302175 5790 952 0 11:49上午 ttys000 0:00.00 grep redis
6:执行redis的创建集群命令创建集群
cd /usr/local/redis-3.0.1/src ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
请注意:
本机已经安装rvm和rubygems,只需要执行
X0544:cluster wei.li$ gem install redis -V
即可安装完毕redis-rb
正常执行结果如下
X0544:src wei.li$ ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 >>> Creating cluster Connecting to node 127.0.0.1:7000: OK Connecting to node 127.0.0.1:7001: OK Connecting to node 127.0.0.1:7002: OK Connecting to node 127.0.0.1:7003: OK Connecting to node 127.0.0.1:7004: OK Connecting to node 127.0.0.1:7005: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 Adding replica 127.0.0.1:7003 to 127.0.0.1:7000 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 M: 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cf 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: 432d74a91b5f1b6358cf245a153160fcf4e93447 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: ecfbdc1268990a42968f1bd887341f02e679f01f 127.0.0.1:7002 slots:10923-16383 (5461 slots) master S: cfacbc4067cbfffab2caa83858f0eebcb572c31d 127.0.0.1:7003 replicates 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cf S: 69d3240c55fb8209e5f248f76c7557d023c4420f 127.0.0.1:7004 replicates 432d74a91b5f1b6358cf245a153160fcf4e93447 S: ff8c288967f944d463cf0a4d8712eb460e715c0f 127.0.0.1:7005 replicates ecfbdc1268990a42968f1bd887341f02e679f01f Can I set the above configuration? (type 'yes' to accept):
输入yes,然后配置完成。
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 127.0.0.1:7000) M: 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cf 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: 432d74a91b5f1b6358cf245a153160fcf4e93447 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: ecfbdc1268990a42968f1bd887341f02e679f01f 127.0.0.1:7002 slots:10923-16383 (5461 slots) master M: cfacbc4067cbfffab2caa83858f0eebcb572c31d 127.0.0.1:7003 slots: (0 slots) master replicates 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cf M: 69d3240c55fb8209e5f248f76c7557d023c4420f 127.0.0.1:7004 slots: (0 slots) master replicates 432d74a91b5f1b6358cf245a153160fcf4e93447 M: ff8c288967f944d463cf0a4d8712eb460e715c0f 127.0.0.1:7005 slots: (0 slots) master replicates ecfbdc1268990a42968f1bd887341f02e679f01f [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. X0544:src wei.li$
至此redis集群即搭建成功!
7:使用redis-cli命令进入集群环境
X0544:src wei.li$ redis-cli -c -p 7000 127.0.0.1:7000>
8: 测试集群状态
X0544:src wei.li$ redis-cli -c -p 7000 127.0.0.1:7000> 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:99452 cluster_stats_messages_received:99452 127.0.0.1:7000> set key1 value1 -> Redirected to slot [9189] located at 127.0.0.1:7001 OK 127.0.0.1:7001> set key2 value2 -> Redirected to slot [4998] located at 127.0.0.1:7000 OK 127.0.0.1:7000> set key3 value3 OK 127.0.0.1:7000> get key1 -> Redirected to slot [9189] located at 127.0.0.1:7001 "value1" 127.0.0.1:7001> get key2 -> Redirected to slot [4998] located at 127.0.0.1:7000 "value2" 127.0.0.1:7000> get key3 "value3" 127.0.0.1:7000>