Linux环境下Redis集群实践
环境:centos 7
一、编译及安装redis源码
源码地址:redis版本发布列表
cd redis-3.2.8 sudo make && make install
二、创建节点
创建Redis-Cluster 文件夹,并创建7000-7005共6个文件夹
复制源码中的redis.conf文件并修改以下内容后分别放入7000-7005文件夹
【7000】示例
bind 192.168.1.105 port 7000 cluster-enabled yes cluster-config-file /home/jabben/Redis-Cluster/7000/nodes-7000.conf
pidfile /home/jabben/Redis-Cluster/7000/redis_7000.pid cluster-node-timeout 5000 daemonize yes
分别启动6个Redis节点:
redis-server 7000/redis.conf redis-server 7001/redis.conf redis-server 7002/redis.conf redis-server 7003/redis.conf redis-server 7004/redis.conf redis-server 7005/redis.conf
查看redis进程是否已经启动:
ps -ef | grep redis
三、创建集群
复制redis-trib.rb文件到bin目录下
cd /home/jabben/Redis-Cluster/Source/redis-3.2.8/src sudo cp redis-trib.rb /usr/local/bin/
安装ruby环境
yum install ruby
安装rubygems组件
yum install rubygems
安装gem-redis
gem install redis
创建集群
redis-trib.rb create --replicas 1 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 192.168.1.105:7003 192.168.1.105:7004 192.168.1.105:7005
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 Adding replica 192.168.1.105:7003 to 192.168.1.105:7000 Adding replica 192.168.1.105:7004 to 192.168.1.105:7001 Adding replica 192.168.1.105:7005 to 192.168.1.105:7002 M: 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd 192.168.1.105:7000 slots:0-5460 (5461 slots) master M: 7bd0b20971d5fc929f8454c6ff388f780cc70e77 192.168.1.105:7001 slots:5461-10922 (5462 slots) master M: aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 192.168.1.105:7002 slots:10923-16383 (5461 slots) master S: e731c1918fad61f972fbca4ab6523da1d9f827af 192.168.1.105:7003 replicates 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd S: c16b520a5075b0fee0840cc26cf1f7f27bad8e45 192.168.1.105:7004 replicates 7bd0b20971d5fc929f8454c6ff388f780cc70e77 S: 990a4e7a2831d8d9ba8f9835557be6a394d393a4 192.168.1.105:7005 replicates aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 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.105:7000) M: 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd 192.168.1.105:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 990a4e7a2831d8d9ba8f9835557be6a394d393a4 192.168.1.105:7005 slots: (0 slots) slave replicates aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c S: e731c1918fad61f972fbca4ab6523da1d9f827af 192.168.1.105:7003 slots: (0 slots) slave replicates 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd M: aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 192.168.1.105:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c16b520a5075b0fee0840cc26cf1f7f27bad8e45 192.168.1.105:7004 slots: (0 slots) slave replicates 7bd0b20971d5fc929f8454c6ff388f780cc70e77 M: 7bd0b20971d5fc929f8454c6ff388f780cc70e77 192.168.1.105:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
至此集群搭建完毕
四、测试
使用redis-cli客户端连接到刚创建的集群
redis-cli -c -h 192.168.1.105 -p 7000
查询集群相关信息
192.168.1.105:7000> CLUSTER INFO cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_sent:2645 cluster_stats_messages_received:2645
【数据写入测试】
192.168.1.105:7000> set name zhangsan -> Redirected to slot [5798] located at 192.168.1.105:7001 OK 192.168.1.105:7001> get name "zhangsan"
【集群线性扩展】
待完善
【失效转移】
待完善
五、集群相关常用命令
链接:https://redis.io/commands
CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 //节点 CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。 CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。 CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。 CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。 CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 //键 CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。 CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。 //新增 CLUSTER SLAVES node-id 返回一个master节点的slaves 列表