标签:redis 集群
-- 主节点 --- redis-7000.conf redis-7100.conf redis-7200.conf -- 从节点 --- redis-7001.conf redis-7101.conf redis-7201.conf
Redis集群一般有多个节点组成,节点数量至少为6个才能保证组成完整的高可用集群;
$ vim redis-7000.conf daemonize yes ## 开启守护进程 port 7000 # 节点端口 logfile "/soft/redis/cluster/7000/redis.log" # 指定日志输入位置 cluster-enabled yes # 开启集群模式 cluster-node-timeout 15000 # 节点超时时间,单位毫秒 cluster-config-file "nodes-7000.conf" # 集群内部配置文件【第一次启动时如果没有该文件,则会自动创建】
其他节点配置和单机模式一致即可,配置文件命名规则:redis-{prot}.conf,准备好后启动所有节点;
-- 主节点 --- redis-server /soft/redis/cluster/redis-7000.conf & redis-server /soft/redis/cluster/redis-7100.conf & redis-server /soft/redis/cluster/redis-7200.conf & -- 从节点 --- redis-server /soft/redis/cluster/redis-7001.conf & redis-server /soft/redis/cluster/redis-7101.conf & redis-server /soft/redis/cluster/redis-7201.conf &
检查当前节点日志是否正确,内容如下:
$ cat /cluster/redis-7000.log 77893:M 16 Apr 00:38:38.924 * Increased maximum number of open files to 10032 (it was originally set to 1024). 77893:M 16 Apr 00:38:38.925 * No cluster configuration found, I‘m 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924‘ ... 77893:M 16 Apr 00:38:38.935 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 77893:M 16 Apr 00:38:38.935 # Server started, Redis version 3.2.0 ... 77893:M 16 Apr 00:38:38.935 * The server is now ready to accept connections on port 7000
节点首次启动后生成集群配置文件,内容为:
-- 通过查看日志方式 -- $ cat nodes-7000.conf 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 :0 myself,master - 0 0 0 connected vars currentEpoch 0 lastVoteEpoch 0
文件内存记录了集群初始状态,这里最重要的是节点ID【这是一个40位16进制字符串
该节点ID只创建一次【不同于运行ID】,节点重启时会加载该配置文件进行重用;
-- 通过命令交互形式回显 -- [root@test cluster]# redis-cli -p 7000 127.0.0.1:7000> cluster nodes 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 :7000 myself,master - 0 0 0 connected
节点握手是指一批运行在集群模式下的节点通过Gossip协议彼此通信,以达到感知对方的过程; 节点握手是建立集群通信的第一步,由客户端发起命令:cluster meet {IP} {port}
步骤如下:
1. 节点7000 发送meet消息给 节点7001 2. 节点7001 接受到meet消息后,保存节点7000的信息并回复pong消息 3. 之后,节点7000 和 7001 彼此定期通过ping/pong消息进行正常的节点通信; -- 发送节点握手指令 -- 127.0.0.1:7000> cluster meet 127.0.0.1 7001 127.0.0.1:7000> cluster meet 127.0.0.1 7101 127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7100 127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7200 127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7300 -- 查看握手是否正常 -- 127.0.0.1:7000> cluster nodes c0ffcbe12a16d2457786b542707dca0529bb1610 127.0.0.1:7300 master - 0 1492278433843 0 connected b40c70eb1c32b4af2450e94deed01dec14ca26a0 127.0.0.1:7200 master - 0 1492278432333 0 connected 34d2f50d036d7b95e5f3ae43571404c735b2e3ee 127.0.0.1:7100 master - 0 1492278430825 3 connected 367ec3a52e8faedc37f3d919865016f50f689893 127.0.0.1:7001 master - 0 1492278432835 2 connected 8c8b9679cd7c52d28fc58aa86d6b0e78efa249c0 127.0.0.1:7101 master - 0 1492278429818 4 connected 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 127.0.0.1:7000 myself,master - 0 0 1 connected
此时,节点握手建立完成;但还不能正常工作:
127.0.0.1:7000> CLUSTER info cluster_state:fail cluster_slots_assigned:0 ## slots【槽的意思】,此时为0 表示目前所有槽没有被分配到节点上 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:0 cluster_current_epoch:5 cluster_my_epoch:1 cluster_stats_messages_sent:8725 cluster_stats_messages_received:8725
Redis集群把所有的数据映射到16384个槽中,每个key会映射为一个固定的槽,只有当节点分配了槽时,才能响应和这些槽关联的键命令;
这里通过 cluster addslots 命令为节点分配槽:
--- 通过 bash 特性批量设置槽(lots) --- redis-cli -h 127.0.0.1 -p 7000 cluster addslots {0..5461} redis-cli -h 127.0.0.1 -p 7000 cluster addslots {5462..10922} redis-cli -h 127.0.0.1 -p 7000 cluster addslots {10923..16383} --- 通过交互命令分配槽(lots) --- 127.0.0.1:7000> cluster addslots {0..5461} 127.0.0.1:7000> cluster addslots {5462..10922} 127.0.0.1:7000> cluster addslots {10923..16383} --- 查看集群状态 --- 127.0.0.1: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:1 cluster_current_epoch:5 cluster_my_epoch:1 cluster_stats_messages_sent:8725 cluster_stats_messages_received:8725 127.0.0.1:7000> CLUSTER NODES c0ffcbe12a16d2457786b542707dca0529bb1610 127.0.0.1:7201 master - 0 1492280674461 0 connected b40c70eb1c32b4af2450e94deed01dec14ca26a0 127.0.0.1:7201 master - 0 1492280674965 5 connected 34d2f50d036d7b95e5f3ae43571404c735b2e3ee 127.0.0.1:7100 master - 0 1492280673454 3 connected 367ec3a52e8faedc37f3d919865016f50f689893 127.0.0.1:7001 master - 0 1492280675467 2 connected 8c8b9679cd7c52d28fc58aa86d6b0e78efa249c0 127.0.0.1:7101 master - 0 1492280672448 4 connected 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 127.0.0.1:7000 myself,master - 0 0 1 connected 0-16383
目前还有3个节点没有使用,作为一个完整的集群;
每个负责处理槽的节点都应该具有从节点,保证当它出现故障时可自动进行故障转移;
集群模式下,redis节点角色分配为主节点和从节点;
首次启动的节点和被分配槽的节点都是主节点,从节点负责负责主节点槽信息和相关数据;
--- 使用命令方式 --- $ redis-cli -h 127.0.0.1 -p 7000 cluster nodes c0ffcbe12a16d2457786b542707dca0529bb1610 127.0.0.1:7201 master - 0 1492288202486 0 connected b40c70eb1c32b4af2450e94deed01dec14ca26a0 127.0.0.1:7200 master - 0 1492288205502 5 connected 34d2f50d036d7b95e5f3ae43571404c735b2e3ee 127.0.0.1:7100 master - 0 1492288204498 3 connected 367ec3a52e8faedc37f3d919865016f50f689893 127.0.0.1:7001 master - 0 1492288201480 2 connected 8c8b9679cd7c52d28fc58aa86d6b0e78efa249c0 127.0.0.1:7101 master - 0 1492288203490 4 connected 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 127.0.0.1:7000 myself,master - 0 0 1 connected 0-16383 --- 建立主从节点 --- 127.0.0.1:7001> CLUSTER REPLICATE 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 127.0.0.1:7101> CLUSTER REPLICATE 367ec3a52e8faedc37f3d919865016f50f689893 127.0.0.1:7201> CLUSTER REPLICATE 8c8b9679cd7c52d28fc58aa86d6b0e78efa249c0 --- 查看复制状态信息 --- 127.0.0.1:7101> CLUSTER NODES c0ffcbe12a16d2457786b542707dca0529bb1610 127.0.0.1:7101 myself,slave 8c8b9679cd7c52d28fc58aa86d6b0e78efa249c0 0 0 0 connected 34d2f50d036d7b95e5f3ae43571404c735b2e3ee 127.0.0.1:7001 slave 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 0 1492288883018 3 connected b40c70eb1c32b4af2450e94deed01dec14ca26a0 127.0.0.1:7201 slave 367ec3a52e8faedc37f3d919865016f50f689893 0 1492288884023 5 connected 8c8b9679cd7c52d28fc58aa86d6b0e78efa249c0 127.0.0.1:7100 master - 0 1492288885033 4 connected 367ec3a52e8faedc37f3d919865016f50f689893 127.0.0.1:7200 master - 0 1492288881005 2 connected 0c0bea9746cfe04dfcbb6ec1db35be5756dd7924 127.0.0.1:7000 master - 0 1492288882012 1 connected 0-16383
127.0.0.1:7000> CLUSTER SLOTS 1) 1) (integer) 0 2) (integer) 16383 3) 1) "127.0.0.1" 2) (integer) 7000 # 主节点 3) "0c0bea9746cfe04dfcbb6ec1db35be5756dd7924" 4) 1) "127.0.0.1" 2) (integer) 7001 # 从节点 3) "34d2f50d036d7b95e5f3ae43571404c735b2e3ee"
127.0.0.1:7000> CLUSTER DELSLOTS 1 2 3 或者: redis-cli -h 127.0.0.1 -p 7000 cluster delslots {10923..16383}
1、手动创建集群,重启redis服务后;集群断开,节点握手也断开 2、创建集群分为3步骤: 1、节点握手 2、分配槽 3、主从节点建立复制关系
相关链接:
本文出自 “靠谱儿” 博客,请务必保留此出处http://liubao0312.blog.51cto.com/2213529/1939298
标签:redis 集群
原文地址:http://liubao0312.blog.51cto.com/2213529/1939298