CentOS完美搭建Redis3.0集群并附测试(2)

[root@localhost cluster]# cd /usr/local/redis-3.0.6/src
[root@localhost src]# ./redis-trib.rb  create --replicas 1 192.168.1.198:7000 192.168.1.198:7001 192.168.1.198:7002 192.168.1.199:7003 192.168.1.199:7004 192.168.1.199:70056.1到这一步因为前面第1步装了依赖包,未提示ruby和rubygems的错误,但还是会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装
错误内容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from ./redis-trib.rb:25
解决:gem install redis
6.2 再次执行第6步的命令,正常执行,提示是否允许修改配置文件,输入yes,然后整个集群配置完成!
[root@localhost src]# ./redis-trib.rb  create --replicas 1 192.168.1.198:7000 192.168.1.198:7001 192.168.1.198:7002 192.168.1.199:7003 192.168.1.199:7004 192.168.1.199:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.199:7003
192.168.1.198:7000
192.168.1.199:7004
Adding replica 192.168.1.198:7001 to 192.168.1.199:7003
Adding replica 192.168.1.199:7005 to 192.168.1.198:7000
Adding replica 192.168.1.198:7002 to 192.168.1.199:7004
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
  slots:5461-10922 (5462 slots) master
S: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
  replicates 02f1958bd5032caca2fd47a56362c8d562d7e621
S: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
  replicates 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299
M: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
  slots:0-5460 (5461 slots) master
M: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
  slots:10923-16383 (5461 slots) master
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
  replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
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 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
  slots:5461-10922 (5462 slots) master
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
  slots: (0 slots) master
  replicates 02f1958bd5032caca2fd47a56362c8d562d7e621
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
  slots: (0 slots) master
  replicates 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299
M: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
  slots:0-5460 (5461 slots) master
M: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
  slots:10923-16383 (5461 slots) master
M: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
  slots: (0 slots) master
  replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


7、测试集群

server1上登录redis客户端并执行

[root@localhost src]# redis-cli -c -p 7000
127.0.0.1:7000> get key
-> Redirected to slot [12539] located at 192.168.1.199:7004
"val"
192.168.1.199:7004> set name test
-> Redirected to slot [5798] located at 192.168.1.198:7000
OK
192.168.1.198:7000> set adress shanghai
-> Redirected to slot [1562] located at 192.168.1.199:7003
OK
192.168.1.199:7003>server2上登录redis客户端并执行


[root@localhost src]# redis-cli -c -p 7003
127.0.0.1:7003> set key val
-> Redirected to slot [12539] located at 192.168.1.199:7004
OK
192.168.1.199:7004> get keyv
"val"
192.168.1.199:7004> set key2 val2
-> Redirected to slot [4998] located at 192.168.1.199:7003
OK
192.168.1.199:7003> get key2
"val2"
192.168.1.199:7003>


从中可以发现存时是分布式存储,取时也是从集群中取,测试成功


8、redis cluster 架构

1)redis-cluster架构图

CentOS完美搭建Redis3.0集群并附测试

架构细节:

(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.

(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.

(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可

(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value

2) redis-cluster选举:容错

CentOS完美搭建Redis3.0集群并附测试

(1)领着选举过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.

(2):什么时候整个集群不可用(cluster_state:fail),当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误

a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成进群的slot映射[0-16383]不完成时进入fail状态.

b:如果进群超过半数以上master挂掉,无论是否有slave集群进入fail状态.

下面关于Redis的文章您也可能喜欢,不妨参考下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7006a22e3b5a7ccecf7f6255dfac39a6.html