本文档基于如下原始文档(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 ( )
下载地址:
2:上传服务器,解压,编译
#将下载的文件放到 /usr/local/ tar -zxvf redis-3.0.1.tar.gz#生成 redis-3.0.1文件夹 cd /usr/local/redis-3.0.1makemake install #执行一下make test查看 make的结果全部都是OK#最终结果显示如下\o/ All tests passed without errors!
3:创建集群需要的目录
mkdir -p /usr/local/cluster cd /usr/local/clustermkdir 7000mkdir 7001mkdir 7002mkdir 7003mkdir 7004mkdir 7005
4:修改配置文件redis.conf
cp /usr/local/redis-3.0.1/redis.conf /usr/local/clustervi redis.conf##修改配置文件中的下面选项port 7000daemonize yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 5000appendonly yes##修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到7000/7001/7002/7003/7004/7005目录下面cp /usr/local/cluster/redis.conf /usr/local/cluster/7000cp /usr/local/cluster/redis.conf /usr/local/cluster/7001cp /usr/local/cluster/redis.conf /usr/local/cluster/7002cp /usr/local/cluster/redis.conf /usr/local/cluster/7003cp /usr/local/cluster/redis.conf /usr/local/cluster/7004cp /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/7000redis-server redis.confcd /usr/local/cluster/7001redis-server redis.confcd /usr/local/cluster/7002redis-server redis.confcd /usr/local/cluster/7003redis-server redis.confcd /usr/local/cluster/7004redis-server redis.confcd /usr/local/cluster/7005redis-server redis.conf ##启动之后使用命令查看redis的启动情况ps -ef|grep redis如下显示则说明启动成功 X0544:7005 wei.li$ ps -ef | grep redis909302175 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 clusterConnecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7005: OK>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:7000127.0.0.1:7001127.0.0.1:7002Adding replica 127.0.0.1:7003 to 127.0.0.1:7000Adding replica 127.0.0.1:7004 to 127.0.0.1:7001Adding replica 127.0.0.1:7005 to 127.0.0.1:7002M: 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cf 127.0.0.1:7000 slots:0-5460 (5461 slots) masterM: 432d74a91b5f1b6358cf245a153160fcf4e93447 127.0.0.1:7001 slots:5461-10922 (5462 slots) masterM: ecfbdc1268990a42968f1bd887341f02e679f01f 127.0.0.1:7002 slots:10923-16383 (5461 slots) masterS: cfacbc4067cbfffab2caa83858f0eebcb572c31d 127.0.0.1:7003 replicates 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cfS: 69d3240c55fb8209e5f248f76c7557d023c4420f 127.0.0.1:7004 replicates 432d74a91b5f1b6358cf245a153160fcf4e93447S: ff8c288967f944d463cf0a4d8712eb460e715c0f 127.0.0.1:7005 replicates ecfbdc1268990a42968f1bd887341f02e679f01fCan 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 clusterWaiting 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) masterM: 432d74a91b5f1b6358cf245a153160fcf4e93447 127.0.0.1:7001 slots:5461-10922 (5462 slots) masterM: ecfbdc1268990a42968f1bd887341f02e679f01f 127.0.0.1:7002 slots:10923-16383 (5461 slots) masterM: cfacbc4067cbfffab2caa83858f0eebcb572c31d 127.0.0.1:7003 slots: (0 slots) master replicates 2cd16d3e6eb9f29ba578fbcf0627f3b3bb4653cfM: 69d3240c55fb8209e5f248f76c7557d023c4420f 127.0.0.1:7004 slots: (0 slots) master replicates 432d74a91b5f1b6358cf245a153160fcf4e93447M: 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 7000127.0.0.1:7000>
8: 测试集群状态
X0544:src wei.li$ redis-cli -c -p 7000127.0.0.1:7000> cluster infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_sent:99452cluster_stats_messages_received:99452127.0.0.1:7000> set key1 value1-> Redirected to slot [9189] located at 127.0.0.1:7001OK127.0.0.1:7001> set key2 value2-> Redirected to slot [4998] located at 127.0.0.1:7000OK127.0.0.1:7000> set key3 value3OK127.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>