1. 程式人生 > >Redis集群的操作

Redis集群的操作

位置 using ids min div options 集群 inf slot

1、原始集群

1.1 啟動Redis集群

[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf 
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf 
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7003/redis.conf 
[[email protected]
/* */ ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7004/redis.conf [[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7005/redis.conf [[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7006/redis.conf

1.2 查看server執行狀態

[[email protected]
/* */ ~]# ps -ef | grep redis root 2322 1 0 04:48 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7001 [cluster] root 2326 1 0 04:48 ?

00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7002 [cluster] root 2330 1 0 04:48 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7003 [cluster] root 2334 1 0 04:48 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7004 [cluster] root 2339 1 0 04:48 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7005 [cluster] root 2344 1 0 04:48 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7006 [cluster] root 2349 2299 0 04:49 pts/0 00:00:00 grep redis

1.3 查看集群狀態

# -c 表示集群模式

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.2.105 -p 7001
192.168.2.105:7001> cluster nodes
aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001 myself,slave 1b362004cf174d9ecb72c0650117781618c52a70 0 0 1 connected
1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004 master - 0 1477568990462 8 connected 0-5460
987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005 slave f9a0fbd03254b0010bca6782867dfa65f21a2448 0 1477568991974 5 connected
f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002 master - 0 1477568991471 2 connected 5461-10922
cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003 master - 0 1477568990967 3 connected 10923-16383
286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006 slave cabff9be14080ae12bfad0b199ae737639ec57bd 0 1477568990967 6 connected
192.168.2.105:7001> 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:8
cluster_my_epoch:8
cluster_stats_messages_sent:998
cluster_stats_messages_received:351


2、新集群操作

2.1 我們新建倆個服務,依照之前搭建的集群方式新增倆個節點:(一主一從 master、slave) Master:7007 Slave:7008

步驟一:創建7007/7008文件夾。

拷貝redis.conf文件到對於的7007,7008文件夾下要進行改動配置文件。

             [[email protected] redis-cluster]# mkdir 7007
             [[email protected] redis-cluster]# mkdir 7008
             [[email protected] redis-cluster]# cd 7001
             [[email protected] 7001]# cp redis.conf /usr/local/redis-cluster/7007/
             [[email protected] 7001]# cp redis.conf /usr/local/redis-cluster/7008/
             [[email protected] 7001]# vim/usr/local/redis-cluster/7007/redis.conf
                改動內容例如以下:
                port:7007
                dir /usr/local/redis-cluster/7007/
                cluster-config-file nodes-7007.conf
             [[email protected] 7001]# vim/usr/local/redis-cluster/7008/redis.conf
                改動內容例如以下:
                port:7008
                dir /usr/local/redis-cluster/7008/
                cluster-config-file nodes-7008.conf

步驟二:啟動7007和7008倆個服務並查看服務狀態。

[[email protected] 7001]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7007/redis.conf
[[email protected] 7001]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf
[[email protected] 7001]# ps -el | grep redis

2.2 學習redis-trib命令使用

[[email protected] ~]# cd /usr/local/redis-3.0.0-rc2/src/
[[email protected] src]# ./redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>

  call            host:port command arg arg .. arg
  set-timeout     host:port milliseconds
  del-node        host:port node_id
  fix             host:port
  reshard         host:port
                  --slots <arg>
                  --yes
                  --from <arg>
                  --to <arg>
  help            (show this help)
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  check           host:port
  import          host:port
                  --from <arg>
  create          host1:port1 ... hostN:portN
                  --replicas <arg>

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.


      

1. create:創建一個集群環境host1:port1 ... hostN:portN(集群中的主從節點比例)

2. call:能夠運行redis命令

3. add-node:將一個節點加入到集群裏。第一個參數為新節點的ip:port,第二個參數為集群中隨意一個已經存在的節點的ip:port

4. del-node:移除一個節點

5. reshard:又一次分片

6. check:檢查集群狀態

3、新增一個主節點7007(master)

步驟一:使用add-node命令:綠色為新增節點。紅色為已知存在節點

/usr/local/redis-3.0.0-rc2/src/redis-trib.rb add-node 192.168.2.105:7007 192.168.2.105:7001

>>> Adding node 192.168.2.105:7007 to cluster 192.168.2.105:7004
Connecting to node 192.168.2.105:7004: OK
Connecting to node 192.168.2.105:7005: OK
Connecting to node 192.168.2.105:7002: OK
Connecting to node 192.168.2.105:7006: OK
Connecting to node 192.168.2.105:7001: OK
Connecting to node 192.168.2.105:7003: OK
>>> Performing Cluster Check (using node 192.168.2.105:7004)
M: 1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005
   slots: (0 slots) slave
   replicates f9a0fbd03254b0010bca6782867dfa65f21a2448
M: f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006
   slots: (0 slots) slave
   replicates cabff9be14080ae12bfad0b199ae737639ec57bd
S: aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001
   slots: (0 slots) slave
   replicates 1b362004cf174d9ecb72c0650117781618c52a70
M: cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003
   slots:10923-16383 (5461 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.
Connecting to node 192.168.2.105:7007: OK
>>> Send CLUSTER MEET to node 192.168.2.105:7007 to make it join the cluster.
[OK] New node added correctly.

步驟二:查看集群狀態:

/usr/local/redis/bin/redis-cli -c -h 192.168.2.105 -p 7001
192.168.2.105:7001> cluster  nodes
192.168.2.105:7001> cluster nodes
aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001 myself,slave 1b362004cf174d9ecb72c0650117781618c52a70 0 0 1 connected
c25aca95364e13b58e57d831126ed77656ba1d76 192.168.2.105:7007 master - 0 1477571648229 9 connected 0-65 5461-5527 10923-10988
1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004 master - 0 1477571648734 8 connected 66-5460
987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005 slave f9a0fbd03254b0010bca6782867dfa65f21a2448 0 1477571649238 5 connected
f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002 master - 0 1477571649238 2 connected 5528-10922
cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003 master - 0 1477571648229 3 connected 10989-16383
286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006 slave cabff9be14080ae12bfad0b199ae737639ec57bd 0 1477571647225 6 connected

註意:當加入節點成功以後,新增的節點不會有不論什麽數據。由於它沒有分配不論什麽的slothash槽)。我們須要為新節點手工分配slot

4、為7007分配slot槽

步驟一:使用redis-trib 命令。找到集群中的隨意一個主節點(紅色位置表現集群中的隨意一個主節點),對其進行又一次分片工作。

[[email protected] 7001]#/usr/local/redis-3.0.0-rc2/src/redis-trib.rb reshard 192.168.2.105:7004

Connecting to node 192.168.2.105:7004: OK
Connecting to node 192.168.2.105:7007: OK
Connecting to node 192.168.2.105:7005: OK
Connecting to node 192.168.2.105:7002: OK
Connecting to node 192.168.2.105:7006: OK
Connecting to node 192.168.2.105:7001: OK
Connecting to node 192.168.2.105:7003: OK
>>> Performing Cluster Check (using node 192.168.2.105:7004)
M: 1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: c25aca95364e13b58e57d831126ed77656ba1d76 192.168.2.105:7007
   slots: (0 slots) master
   0 additional replica(s)
S: 987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005
   slots: (0 slots) slave
   replicates f9a0fbd03254b0010bca6782867dfa65f21a2448
M: f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006
   slots: (0 slots) slave
   replicates cabff9be14080ae12bfad0b199ae737639ec57bd
S: aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001
   slots: (0 slots) slave
   replicates 1b362004cf174d9ecb72c0650117781618c52a70
M: cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003
   slots:10923-16383 (5461 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.

(提示一)

How many slots do youwant to move (from 1 to 16384)?200

(提示二)

What is the receivingnode ID? 382634a4025778c040b7213453fd42a709f79e28

Please enter all thesource node IDs.

Type ‘all‘ to use all the nodes as sourcenodes for the hash slots.

Type ‘done‘ once you entered all the sourcenodes IDs.

Source node #1:all

Ready to move 200slots.

Sourcenodes:

M: 614d0def75663f2620b6402a017014b57c912dad192.168.2.105:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 8aac82b63d42a1989528cd3906579863a5774e77192.168.2.105:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

M: 83df08875c7707878756364039df0a4c8658f272192.168.2.105:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

Destinationnode:

M: 382634a4025778c040b7213453fd42a709f79e28192.168.2.105:7007

slots: (0 slots) master

0 additional replica(s)

Resharding plan:(分片運行計劃日誌)

Moving slot 5461 from8aac82b63d42a1989528cd3906579863a5774e77

...

Moving slot 0 from 614d0def75663f2620b6402a017014b57c912dad

...

Moving slot 10923 from83df08875c7707878756364039df0a4c8658f272

...

(提示三)

Do you want toproceed with the proposed reshard plan (yes/no)?yes

Moving slot 65 from 192.168.2.105:7001to 192.168.2.105:7007:

...

Moving slot 10923 from 192.168.2.105:7003to 192.168.2.105:7007:

...

Moving slot 5527 from 192.168.2.105:7002to 192.168.2.105:7007:

...

提示一:是希望你須要多少個槽移動到新的節點上,能夠自己設置,比方200個槽。

提示二:是你須要把這200個slot槽移動到那個節點上去(須要指定節點id),而且下個提示是輸入all為從全部主節點(7001 7002 7003)中分別抽取響應的槽數(一共為200個槽到指定的新節點中。,而且會打印運行分片的計劃。

提示三:輸入yes確認開始運行分片任務。


技術分享

如上圖所看到的。如今我們的7007已經有slot槽了,也就是說能夠在7007上進行讀寫數據啦。到此為止我們的7007已經增加到集群中啦,而且是主節點(Master

5、加入從節點(7008)到集群中去

步驟一:還是須要運行add-node命令:

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rbadd-node 192.168.2.105:7008 192.168.2.105:7004

步驟二:我們須要運行replicate命令來指定當前節點(從節點)的主節點id為哪個。

首先須要登錄新加的7008節點的client。然後使用集群命令進行操作。把當前的7008(slave)節點指定到一個主節點下(這裏使用之前創建的7007主節點,紅色表示節點id)

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.2.105 -p 7008
192.168.2.105:7008> cluster replicate <strong><span style="color:#ff0000;">c25aca95364e13b58e57d831126ed77656ba1d76</span></strong>
192.168.2.105:7008> OK(提示OK則操作成功)

技術分享

我們繼續看一下當前集群的狀態,例如以下圖:我們已經成功的把7008放到7007這個主節點以下了,到此為止我們已經成功的加入完一個從節點了。


6、嘗試刪除一個節點(7008 slave)

步驟一:刪除從節點7008,輸入del-node命令,指定刪除節點ip和port,以及節點id(紅色為7008節點id)

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rb del-node 192.168.2.105:7008bd83784a3eb65264fcdfc38e6c2a8f6f5dc6dd0c

步驟二:再次查看一下集群狀態,例如以下圖所看到的,我們已經成功的移除了7008 slave節點。另外我們發現移除一個節點以後,當前節點的服務進程也會隨之銷毀。

能夠使用ps命令查看當前的服務(ps -ef | grep redis),發現少了一個執行的server,也就是剛移除的7008從節點。


7、刪除之前增加的主節點7007。這個步驟會相對照較麻煩一些,由於主節點的裏面是有分配了slot槽的,所以我們這裏必須先把7007裏的slot槽放入到其它的可用主節點中去。然後再進行移除節點操作才行,不然會出現數據丟失問題。

步驟一:刪除7007(master)節點之前,我們須要先把其所有的數據(slot槽)移動到其它節點上去(眼下僅僅能把master的數據遷移到一個節點上,臨時做不了平均分配功能)。

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rb reshard 192.168.2.105:7007

輸出例如以下:

>>> Performing Cluster Check(using node 192.168.2.105:7007)

M: 382634a4025778c040b7213453fd42a709f79e28192.168.2.105:7007

slots:0-65,5461-5527,10923-10988 (199 slots) master

0 additional replica(s)

S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0192.168.2.105:7006

slots: (0 slots) slave

replicates 83df08875c7707878756364039df0a4c8658f272

S: a69b98937844c6050ee5885266ccccb185a3f36a192.168.2.105:7004

slots: (0 slots) slave

replicates 614d0def75663f2620b6402a017014b57c912dad

M: 614d0def75663f2620b6402a017014b57c912dad192.168.2.105:7001

slots:66-5460 (5395 slots) master

1additional replica(s)

M: 8aac82b63d42a1989528cd3906579863a5774e77192.168.2.105:7002

slots:5528-10922 (5395 slots) master

1additional replica(s)

S: adb99506ddccad332e09258565f2e5f4f456a150192.168.2.105:7005

slots: (0 slots) slave

replicates8aac82b63d42a1989528cd3906579863a5774e77

M: 83df08875c7707878756364039df0a4c8658f272192.168.2.105:7003

slots:10989-16383 (5395 slots) master

1additional replica(s)

[OK] All nodes agree about slotsconfiguration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

How many slots do youwant to move (from 1 to 16384)?199

(凝視:這裏不會是正好200個槽)

What is the receivingnode ID? 614d0def75663f2620b6402a017014b57c912dad
(凝視:這裏是須要把數據移動到哪?7001的主節點id

Please enter all thesource node IDs.

Type ‘all‘ to use all the nodes as sourcenodes for the hash slots.

Type ‘done‘ once you entered all the sourcenodes IDs.

Source node #1:382634a4025778c040b7213453fd42a709f79e28

(凝視:這裏是須要數據源,也就是我們的7007節點id

Source node #2:done

(凝視:這裏直接輸入done 開始生成遷移計劃)

Ready to move 199 slots.

Source nodes:

M: 382634a4025778c040b7213453fd42a709f79e28192.168.2.105:7007

slots:0-65,5461-5527,10923-10988 (199 slots)master

0 additional replica(s)

Destinationnode:

M: 614d0def75663f2620b6402a017014b57c912dad192.168.2.105:7001

slots:66-5460 (5395 slots) master

1 additional replica(s)

Resharding plan:

Moving slot 0 from 382634a4025778c040b7213453fd42a709f79e28

...

Do you want toproceed with the proposed reshard plan (yes/no)? Yes

(凝視:這裏輸入yes開始遷移)

Moving slot 0 from 192.168.2.105:7007 to 192.168.2.105:7001:

到此為止我們已經成功的把7007主節點的數據遷移到7001上去了,我們能夠看一下如今的集群狀態例如以下圖。你會發現7007以下已經沒有不論什麽數據(slot)槽了,證明遷移成功!

技術分享

步驟二:最後我們直接使用del-node命令刪除7007主節點就可以(紅色表示7007的節點id)。

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rbdel-node 192.168.2.105:7007 c25aca95364e13b58e57d831126ed77656ba1d76


最後:我們查看集群狀態,一切還原為最初始狀態啦!OK結束!

192.168.2.105:7001> cluster nodes
aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001 myself,slave 1b362004cf174d9ecb72c0650117781618c52a70 0 0 1 connected
1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004 master - 0 1477574617571 10 connected 0-5527 10923-10988
987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005 slave f9a0fbd03254b0010bca6782867dfa65f21a2448 0 1477574618076 5 connected
f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002 master - 0 1477574618579 2 connected 5528-10922
cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003 master - 0 1477574617066 3 connected 10989-16383
286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006 slave cabff9be14080ae12bfad0b199ae737639ec57bd 0 1477574617066 6 connected


Redis集群的操作