redis分布式集群搭建

今天搭建了一个redis分布式集群,踩了很多的坑,今天来跟大家分享一下。

1:首先在网上下载一版3.0.0版本以上的redis解压包,可以去redis官网上下载。

2:解压到自己定义的redis包下。

3:然后将解压出来的文件下的redis.conf拷贝出来。

4:在usr/local/目录下新建redis-cluster文件夹,并在该文件夹下新建6个文件夹。分别为7000,7001,7002,7003,7004,7005

 (因为最少是需要6个一起组建为集群)

5:将拷贝的redis.conf分别放入到这六个文件夹下。

6*:分别更改这6个redis.conf文件如下: 

                                                              port 7000

                                                              daemonize yes

                                                              cluster-enabled yes //改完前面的#号注释去掉

                                                              cluster-config-file nodes-7000.conf  //改完前面的#号注释去掉

                                                              cluster-node-timeout 5000

                                                               appendonly yes

修改完成后,进入redis安装目录下的src层,输入redis-server /usr/local/redis-cluster/7000/redis.conf,分别启动该6项

然后输入ps -ef|grep redis,查看启动项有哪些。

7.搭建redis集群还需要依赖ruby环境,因此还需要安装ruby:

   安装ruby:yum install ruby
   安装rubygems:yum install rubygems
   安装ruby的redis驱动:gem install redis

8.进入/redis/redis-3.2.12/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 //用于创建该6个节点的子节点数

9:然后出现这样程序就对了:

>>> Creating cluster
>>> 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: 9538f0de78900781335a5225b643e293a4d23381 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: e3ac1a15481bf708e4867b542d1e7f60706a56c9 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 644a42b9e9e1464d87b1961b27ba6a22024bc49e 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 5466cb9241c1602ca253b849c65bac054184ab7d 127.0.0.1:7003
   replicates 9538f0de78900781335a5225b643e293a4d23381
S: 500b95311fd013ec2376c067d760d388fa36b448 127.0.0.1:7004
   replicates e3ac1a15481bf708e4867b542d1e7f60706a56c9
S: 95ac6d6c7e14ad1ba8b6499a786f22271950e816 127.0.0.1:7005
   replicates 644a42b9e9e1464d87b1961b27ba6a22024bc49e
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: 9538f0de78900781335a5225b643e293a4d23381 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5466cb9241c1602ca253b849c65bac054184ab7d 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 9538f0de78900781335a5225b643e293a4d23381
M: 644a42b9e9e1464d87b1961b27ba6a22024bc49e 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: e3ac1a15481bf708e4867b542d1e7f60706a56c9 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 95ac6d6c7e14ad1ba8b6499a786f22271950e816 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 644a42b9e9e1464d87b1961b27ba6a22024bc49e
S: 500b95311fd013ec2376c067d760d388fa36b448 127.0.0.1:7004
   slots: (0 slots) slave
   replicates e3ac1a15481bf708e4867b542d1e7f60706a56c9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
10.这时候可以进入测试了:

[root@iz2zeg7d2xorzi8slwc0kgz src]# ./redis-cli -c -p 7000
127.0.0.1:7000> set name zdj
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001>  
[root@iz2zeg7d2xorzi8slwc0kgz src]# ./redis-cli -c -p 7001
127.0.0.1:7001> get name
"zdj"
 

报改错:

[ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
说明7001的redis缓存没有清空,只需先进入该7001客户端(./redis-cli -c -p 7001) 再运行:flushdb即可

 

转载自:https://yq.aliyun.com/php/23862

          

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值