目录
redis集群机制
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
(2)节点的fail是通过集群中超过半数的节点检测失效时才生效。
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value。
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点
安装ruby环境
redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:
安装ruby
yum install ruby
yum install rubygems
安装ruby和redis的接口程序
拷贝redis-3.0.0.gem至/usr/local下
执行:
gem install /usr/local/redis-3.0.0.gem
创建集群:
集群结点规划
Redis集群中至少应该有三个节点。要保证集群的高可用,需要每个节点有一个备份机。
Redis集群至少需要6台服务器。
搭建伪分布式。可以使用一台虚拟机运行6个redis实例。需要修改redis的端口号7001-7006
如果节点没有满足至少三个,启动会报错
>>> Creating cluster
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.
这里在同一台服务器用不同的端口表示不同的redis服务器,如下:
主节点:192.168.101.3:7001 192.168.101.3:7002 192.168.101.3:7003
从节点:192.168.101.3:7004 192.168.101.3:7005 192.168.101.3:7006
在/usr/local下创建redis-cluster目录,其下创建7001、7002...7006目录,如下:
将redis安装目录bin下的文件拷贝到每个700X目录内,同时将redis源码目录src下的redis-trib.rb拷贝到redis-cluster目录下。
修改每个700X目录下的redis.conf配置文件:
port XXXX
bind 192.168.101.3
cluster-enabled yes
启动每个结点redis服务
分别进入7001、7002、...7006目录,执行:
./redis-server ./redis.conf
查看redis进程:
执行创建集群命令
执行redis-trib.rb,此脚本是ruby脚本,它依赖ruby环境。
./redis-trib.rb create --replicas 1 192.168.101.3:7001 192.168.101.3:7002 192.168.101.3:7003 192.168.101.3:7004 192.168.101.3:7005 192.168.101.3:7006
说明:
redis集群至少需要3个主节点,每个主节点有一个从节点总共6个节点
replicas指定为1表示每个主节点有一个从节点
注意:
如果执行时报如下错误:
[ERR] Node XXXXXX is not empty.
Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解决方法是删除生成的配置文件nodes.conf,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis的持久化文件后再重启redis,比如:appendonly.aof、dump.rdb
创建集群输出如下:
>>> Creating cluster Connecting to node 192.168.101.3:7001: OK Connecting to node 192.168.101.3:7002: OK Connecting to node 192.168.101.3:7003: OK Connecting to node 192.168.101.3:7004: OK Connecting to node 192.168.101.3:7005: OK Connecting to node 192.168.101.3:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.101.3:7001 192.168.101.3:7002 192.168.101.3:7003 Adding replica 192.168.101.3:7004 to 192.168.101.3:7001 Adding replica 192.168.101.3:7005 to 192.168.101.3:7002 Adding replica 192.168.101.3:7006 to 192.168.101.3:7003 M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7001 slots:0-5460 (5461 slots) master M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 192.168.101.3:7002 slots:5461-10922 (5462 slots) master M: 1a8420896c3ff60b70c716e8480de8e50749ee65 192.168.101.3:7003 slots:10923-16383 (5461 slots) master S: 69d94b4963fd94f315fba2b9f12fae1278184fe8 192.168.101.3:7004 replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 S: d2421a820cc23e17a01b597866fd0f750b698ac5 192.168.101.3:7005 replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 S: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 192.168.101.3:7006 replicates 1a8420896c3ff60b70c716e8480de8e50749ee65 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.101.3:7001) M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7001 slots:0-5460 (5461 slots) master M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 192.168.101.3:7002 slots:5461-10922 (5462 slots) master M: 1a8420896c3ff60b70c716e8480de8e50749ee65 192.168.101.3:7003 slots:10923-16383 (5461 slots) master M: 69d94b4963fd94f315fba2b9f12fae1278184fe8 192.168.101.3:7004 slots: (0 slots) master replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 M: d2421a820cc23e17a01b597866fd0f750b698ac5 192.168.101.3:7005 slots: (0 slots) master replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 M: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 192.168.101.3:7006 slots: (0 slots) master replicates 1a8420896c3ff60b70c716e8480de8e50749ee65 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
创建关闭集群的脚本:
创建关闭集群的脚本: [root@localhost redis-cluster]# vim shutdow-all.sh redis01/redis-cli -p 7001 shutdown redis01/redis-cli -p 7002 shutdown redis01/redis-cli -p 7003 shutdown redis01/redis-cli -p 7004 shutdown redis01/redis-cli -p 7005 shutdown redis01/redis-cli -p 7006 shutdown [root@localhost redis-cluster]# chmod u+x shutdow-all.sh
集群的使用方法
Redis-cli连接集群。
[root@localhost redis-cluster]# redis01/redis-cli -p 7002 -c
-c:代表连接的是redis集群
查询集群信息
集群创建成功登陆任意redis节点查询集群中的节点情况。
客户端以集群方式登陆:
说明:
./redis-cli -c -h 192.168.101.3 -p 7001
其中-c表示以集群方式连接redis,-h指定ip地址,-p指定端口号
cluster nodes 查询集群结点信息
cluster info 查询集群状态信息
参考链接:https://redis.io/topics/cluster-tutorial