【redis學習】--叢集增加/刪除節點
-
檢視當前redis叢集狀態,6379是單機版的,7001-7006是叢集版的
[[email protected] ~]# ps -ef | grep redis root 4772 1 0 Oct11 ? 00:00:52 ./redis-server *:6379 root 4974 1 0 Oct11 ? 00:01:03 ./redis-server *:7001 [cluster] root 4976 1 0 Oct11 ? 00:01:09 ./redis-server *:7002 [cluster] root 4980 1 0 Oct11 ? 00:01:02 ./redis-server *:7003 [cluster] root 4984 1 0 Oct11 ? 00:01:07 ./redis-server *:7004 [cluster] root 4988 1 0 Oct11 ? 00:01:00 ./redis-server *:7005 [cluster] root 4992 1 0 Oct11 ? 00:01:00 ./redis-server *:7006 [cluster] root 6152 6132 0 03:44 pts/1 00:00:00 grep --color=auto redis
-
down掉7001埠的redis節點。
[[email protected] ~]# kill 4974
-
檢視程序,確認7001已掛
[[email protected] redis-cluster]# ps -ef | grep redis root 4772 1 0 Oct11 ? 00:00:52 ./redis-server *:6379 root 4976 1 0 Oct11 ? 00:01:10 ./redis-server *:7002 [cluster] root 4980 1 0 Oct11 ? 00:01:03 ./redis-server *:7003 [cluster] root 4984 1 0 Oct11 ? 00:01:07 ./redis-server *:7004 [cluster] root 4988 1 0 Oct11 ? 00:01:01 ./redis-server *:7005 [cluster] root 4992 1 0 Oct11 ? 00:01:01 ./redis-server *:7006 [cluster] root 6292 6132 0 03:46 pts/1 00:00:00 grep --color=auto redis
-
檢視叢集狀態 因為7001有備份節點為7004,7001掛掉之後,7004可以代替,整個叢集狀態也完好。
[[email protected] redis-cluster]# redis02/redis-cli -p 7004 -c 127.0.0.1:7004> cluster nodes 9b953784c34116fa58b0cc973e772654b5b1e0de 192.168.178.12:7005 slave 24425aa5c7a31fee5dfe6742c609bb70b351cc43 0 1539287351805 5 connected 1043a01095d309de69bd4c508299b3fef347bf8e 192.168.178.12:7001 master,fail - 1539287115618 1539287112799 1 disconnected 24425aa5c7a31fee5dfe6742c609bb70b351cc43 192.168.178.12:7002 master - 0 1539287355342 2 connected 5461-10922 56e8ebf36a827b044ed23b7bbab7b2e7e3ca7229 192.168.178.12:7004 myself,master - 0 0 7 connected 0-5460 791626e062c8f676a2f41ea5a4d32ec527c1f07a 192.168.178.12:7003 master - 0 1539287354335 3 connected 10923-16383 0337e5dfecbf29e9c81a6b8a6c429774e4b24c63 192.168.178.12:7006 slave 791626e062c8f676a2f41ea5a4d32ec527c1f07a 0 1539287356351 6 connected 127.0.0.1:7004> 127.0.0.1:7004> 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:7 cluster_my_epoch:7 cluster_stats_messages_sent:58423 cluster_stats_messages_received:56207
-
叢集中檢查7001埠的redis,已經連線不到
[[email protected] redis-cluster]# ./redis-trib.rb check 127.0.0.1:7001 Connecting to node 127.0.0.1:7001: [ERR] Sorry, can’t connect to node 127.0.0.1:7001
[[email protected] redis-cluster]# ./redis-trib.rb check 127.0.0.1:7002 Connecting to node 127.0.0.1:7002: OK Connecting to node 192.168.178.12:7004: OK Connecting to node 192.168.178.12:7006: OK Connecting to node 192.168.178.12:7003: OK Connecting to node 192.168.178.12:7005: OK
Performing Cluster Check (using node 127.0.0.1:7002) M: 24425aa5c7a31fee5dfe6742c609bb70b351cc43 127.0.0.1:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 56e8ebf36a827b044ed23b7bbab7b2e7e3ca7229 192.168.178.12:7004 slots:0-5460 (5461 slots) master 0 additional replica(s) S: 0337e5dfecbf29e9c81a6b8a6c429774e4b24c63 192.168.178.12:7006 slots: (0 slots) slave replicates 791626e062c8f676a2f41ea5a4d32ec527c1f07a M: 791626e062c8f676a2f41ea5a4d32ec527c1f07a 192.168.178.12:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 9b953784c34116fa58b0cc973e772654b5b1e0de 192.168.178.12:7005 slots: (0 slots) slave replicates 24425aa5c7a31fee5dfe6742c609bb70b351cc43 [OK] All nodes agree about slots configuration.
Check for open slots… Check slots coverage… [OK] All 16384 slots covered.
從7002節點連入,可自動跳轉到7004節點,並且獲取b值(ps:b初始存在了7001節點上)。
-
取值測試
[[email protected] redis-cluster]# redis02/redis-cli -p 7002 -c[[email protected] redis-cluster]# redis02/redis-cli -p 7002 -c 192.168.178.12:7002> keys *
- “a”
- “test1” 192.168.178.12:7002> get b -> Redirected to slot [3300] located at 192.168.178.12:7004 “66666” 192.168.178.12:7004>
-
在叢集中加入節點 此時我們加入之前down掉的redis01節點(7001),此時可見7001節點成為了7004的從節點,他們的主從關係倒置了。
[[email protected] redis-cluster]# cd redis01 [[email protected] redis01]# ./redis-server redis.conf [[email protected] redis-cluster]# ./redis-trib.rb check 127.0.0.1:7002 Connecting to node 127.0.0.1:7002: OK Connecting to node 192.168.178.12:7001: OK Connecting to node 192.168.178.12:7004: OK Connecting to node 192.168.178.12:7006: OK Connecting to node 192.168.178.12:7003: OK Connecting to node 192.168.178.12:7005: OK
Performing Cluster Check (using node 127.0.0.1:7002) M: 24425aa5c7a31fee5dfe6742c609bb70b351cc43 127.0.0.1:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 1043a01095d309de69bd4c508299b3fef347bf8e 192.168.178.12:7001 slots: (0 slots) slave replicates 56e8ebf36a827b044ed23b7bbab7b2e7e3ca7229 M: 56e8ebf36a827b044ed23b7bbab7b2e7e3ca7229 192.168.178.12:7004 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 0337e5dfecbf29e9c81a6b8a6c429774e4b24c63 192.168.178.12:7006 slots: (0 slots) slave replicates 791626e062c8f676a2f41ea5a4d32ec527c1f07a M: 791626e062c8f676a2f41ea5a4d32ec527c1f07a 192.168.178.12:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 9b953784c34116fa58b0cc973e772654b5b1e0de 192.168.178.12:7005 slots: (0 slots) slave replicates 24425aa5c7a31fee5dfe6742c609bb70b351cc43 [OK] All nodes agree about slots configuration.
Check for open slots… Check slots coverage… [OK] All 16384 slots covered.