【Redis学習ノート】--マスターコピー


前回の記事では、Redisクラスタの高可用性(sentinelメカニズム)を理解しました.この記事と次の記事では、redisクラスタの高可用性を検証してみましょう.現在のredisクラスタの状態を見ると、6379は単機版で、7001-7006はクラスタ版で、6379はクラスタに重点を置くことは無視できます.
[root@localhost ~]# 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ノードを削除します.
[root@localhost ~]# kill 4974

プロセスを表示し、7001が保留されていることを確認します.
[root@localhost 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バックアップノードが70047001で停止した後、7004が代替され、クラスタ全体のステータスも良好であるため、クラスタステータスと各ノードステータスが表示されます.
[root@localhost 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をチェックします.接続できません.
[root@localhost 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

クラスタで7002ポートのredisをチェックすると、7004が7001の代わりにmasterになっていることがわかる.
[root@localhost 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ノードに初期に存在する).
 [root@localhost redis-cluster]# redis02/redis-cli -p 7002 -c
192.168.178.12:7002> keys *
1) "a"
2) "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したredis 01ノード(7001)が追加され、7001ノードが7004のスレーブノードとなり、主従関係が逆転していることがわかります.
[root@localhost redis-cluster]# cd redis01 
[root@localhost redis01]# ./redis-server redis.conf
[root@localhost 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.

次の記事では、redisクラスタのノードの追加と削除を検証し、転送ゲートを送ります.