您好,登錄后才能下訂單哦!
-- 主節點 --- 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、主從節點建立復制關系
相關鏈接:
用redis-trib.rb搭建redis集群
redis集群【報錯總結】
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。