redis集群部署

来源:互联网 发布:serpentza黑中国知乎 编辑:程序博客网 时间:2024/06/07 08:35


1.安装redis,并把src目录中对应的可执行文件建立软连接到/bin目录下


2.新建目录:
mkdir redis_master_6379 redis_master_2001 redis_master_2002 redis_master_2003
3.修改配置:
将redis.conf文件每个目录拷贝一份,修改文件内容为:
daemonize yes
port 6379 #端口配置为对应目录的端口
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

4.启动多个实例:
进入到对应的目录执行:redis-server redis.conf即可


5.创建集群:
redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:2001 127.0.0.1:2002 127.0.0.1:2003
redis-trib.rb命令依赖于ruby,因此需要先安装ruby:
yum install ruby ruby-devel rubygems rpm-build
gem install redis
若执行成功,可看到打印:
[root@localhost redis_slave_2003]# redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:2001 127.0.0.1:2002 127.0.0.1:2003
>>> Creating cluster
Connecting to node 127.0.0.1:6379: OK
Connecting to node 127.0.0.1:2001: OK
Connecting to node 127.0.0.1:2002: OK
Connecting to node 127.0.0.1:2003: OK
>>> Performing hash slots allocation on 4 nodes...
Using 4 masters:
127.0.0.1:6379
127.0.0.1:2001
127.0.0.1:2002
127.0.0.1:2003
M: fd621fc8ba5a6c06b656baac2997865fffddf45f 127.0.0.1:6379
  slots:0-4095 (4096 slots) master
M: 985da03fab7a72d9b448a03c82e5552bb3fe1c8b 127.0.0.1:2001
  slots:4096-8191 (4096 slots) master
M: 1668bcd81c890a74de5de053f642b7930e9bec24 127.0.0.1:2002
  slots:8192-12287 (4096 slots) master
M: 6fb1f9175eb6550344fcaba3bd56344945ea36be 127.0.0.1:2003
  slots:12288-16383 (4096 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 127.0.0.1:6379)
M: fd621fc8ba5a6c06b656baac2997865fffddf45f 127.0.0.1:6379
  slots:0-4095 (4096 slots) master
M: 985da03fab7a72d9b448a03c82e5552bb3fe1c8b 127.0.0.1:2001
  slots:4096-8191 (4096 slots) master
M: 1668bcd81c890a74de5de053f642b7930e9bec24 127.0.0.1:2002
  slots:8192-12287 (4096 slots) master
M: 6fb1f9175eb6550344fcaba3bd56344945ea36be 127.0.0.1:2003
  slots:12288-16383 (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

6.测试集群:
[root@localhost redis_master]# redis-cli -c -p 6379
127.0.0.1:6379> get 001
-> Redirected to slot [9794] located at 127.0.0.1:2002
(nil)
127.0.0.1:2002> set 001 value001
OK
127.0.0.1:2002> get 001
"value001"
127.0.0.1:2002> set 002 value002
-> Redirected to slot [5665] located at 127.0.0.1:2001
OK
127.0.0.1:2001> get 002
"value002"
127.0.0.1:2001> sadd s001 svalue001
-> Redirected to slot [16363] located at 127.0.0.1:2003
(integer) 1
127.0.0.1:2003> scard s001
(integer) 1
127.0.0.1:2003> 

重上面的结果可以看出,集群已经部署成功,每次获取或设置对应的键值会通过一致性hash重定向到对应的redis-server中


ps:文章为本人一步一步亲测,可能存在错误,欢迎指正,转载请注明出处,关于redis集群内部实现细节请参考:http://www.redis.cn/topics/cluster-spec.html

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0