一、原始集群(6節(jié)點(diǎn) 3主3從):
(1)啟動(dòng)集群:
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7003/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7004/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7005/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7006/redis.conf
(2)查看服務(wù)器運(yùn)行狀態(tài):
[root@bhz004 ~]# ps -el | grep redis
5 S 0 1999 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2003 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2007 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2011 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2017 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2023 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
(3)查看集群狀態(tài)
二、新集群操作(添加兩個(gè)節(jié)點(diǎn))
(4)我們新建倆個(gè)服務(wù),按照之前搭建的集群方式新增倆個(gè)節(jié)點(diǎn):(一主一從 master、slave)
Master:7007,Slave:7008
步驟一:創(chuàng)建7007/7008文件夾。拷貝redis.conf文件到對(duì)于的7007,7008目錄下 要 進(jìn)行修改配置文件。
[root@bhz004 redis-cluster]# mkdir 7007
[root@bhz004 redis-cluster]# mkdir 7008
[root@bhz004 redis-cluster]# cd 7001
[root@bhz004 7001]# cp redis.conf /usr/local/redis-cluster/7007/
[root@bhz004 7001]# cp redis.conf /usr/local/redis-cluster/7008/
[root@bhz004 7001]# vim /usr/local/redis-cluster/7007/redis.conf
修改內(nèi)容如下:
port:7007
dir /usr/local/redis-cluster/7007/
cluster-config-file nodes7007.conf
[root@bhz004 7001]# vim /usr/local/redis-cluster/7008/redis.conf
修改內(nèi)容如下:
port:7008
dir /usr/local/redis-cluster/7008/
cluster-config-file nodes7008.conf
步驟二:?jiǎn)?dòng)7007和7008倆個(gè)服務(wù)并查看服務(wù)狀態(tài)。
[root@bhz004 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7007/redis.conf
[root@bhz004 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf
[root@bhz004 7001]# ps -el | grep redis
(5)學(xué)習(xí)redis-trib命令使用:
[root@bhz004 local]# cd /usr/local/redis3.0/src
[root@bhz004 src]# redis-trib.rb
1 create:創(chuàng)建一個(gè)集群環(huán)境host1:port1 ... hostN:portN(集群中的主從節(jié)點(diǎn)比例)
2 call:可以執(zhí)行redis命令
3 add-node:將一個(gè)節(jié)點(diǎn)添加到集群里,第一個(gè)參數(shù)為新節(jié)點(diǎn)的ip:port,第二個(gè)參數(shù)為集群中任意一個(gè)已經(jīng)存在的節(jié)點(diǎn)的ip:port
4 del-node:移除一個(gè)節(jié)點(diǎn)
5 reshard:重新分片
6 check:檢查集群狀態(tài)
(6)新增一個(gè)主節(jié)點(diǎn)7007(master)
步驟一:使用add-node命令:綠色為新增節(jié)點(diǎn),紅色為已知存在節(jié)點(diǎn)
[root@bhz004 7001]#
/usr/local/redis-3.2.6/src/redis-trib.rb add-node 192.168.1.171:7007 192.168.1.171:7001
輸出如下:
>>> Adding node 192.168.1.171:7007 to cluster 192.168.1.171:7001
Connecting to node 192.168.1.171:7001: OK
Connecting to node 192.168.1.171:7006: OK
Connecting to node 192.168.1.171:7005: OK
Connecting to node 192.168.1.171:7004: OK
Connecting to node 192.168.1.171:7002: OK
Connecting to node 192.168.1.171:7003: OK
>>> Performing Cluster Check (using node 192.168.1.171:7001)
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171: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.1.171:7007: OK
>>> Send CLUSTER MEET to node 192.168.1.171:7007 to make it join the cluster.
[OK] New node added correctly.
步驟二:查看集群狀態(tài):
[root@bhz004 src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.171 -p 7001
192.168.1.171:7001> cluster nodes
注意:當(dāng)添加節(jié)點(diǎn)成功以后,新增的節(jié)點(diǎn)不會(huì)有任何數(shù)據(jù),因?yàn)樗鼪]有分配任何的slot(hash槽)。我們需要為新節(jié)點(diǎn)手工分配slot。
(7)為7007分配slot槽。
步驟一:使用redis-trib命令,找到集群中的任意一個(gè)主節(jié)點(diǎn)(紅色位置表現(xiàn)集群中的任意一個(gè)主節(jié)點(diǎn)),對(duì)其進(jìn)行重新分片工作。
[root@bhz004 7001]# /usr/local/redis-3.2.6/src/redis-trib.rb reshard 192.168.1.115:7001
輸出如下:
>>> Performing Cluster Check (using node 192.168.1.171:7001)
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots: (0 slots) master
0 additional replica(s)
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171: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 you want to move (from 1 to 16384)? 200
(提示二)
What is the receiving node ID? 382634a4025778c040b7213453fd42a709f79e28
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:all
Ready to move 200 slots.
Source nodes:
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots: (0 slots) master
0 additional replica(s)
Resharding plan:(分片執(zhí)行計(jì)劃日志)
Moving slot 5461 from 8aac82b63d42a1989528cd3906579863a5774e77
...
Moving slot 0 from 614d0def75663f2620b6402a017014b57c912dad
...
Moving slot 10923 from 83df08875c7707878756364039df0a4c8658f272
...
(提示三)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 65 from 192.168.1.171:7001 to 192.168.1.171:7007:
...
Moving slot 10923 from 192.168.1.171:7003 to 192.168.1.171:7007:
...
Moving slot 5527 from 192.168.1.171:7002 to 192.168.1.171:7007:
...
1提示一:是希望你需要多少個(gè)槽移動(dòng)到新的節(jié)點(diǎn)上,可以自己設(shè)置,比如200個(gè)槽。
2提示二:是你需要把這200個(gè)slot槽移動(dòng)到那個(gè)節(jié)點(diǎn)上去(需要指定節(jié)點(diǎn)id),并且下個(gè) 提示是輸入all為從所有主節(jié)點(diǎn)(7001 7002 7003)中分別抽取響應(yīng)的槽數(shù)(一共為200個(gè)槽到指定的新節(jié)點(diǎn)中!,并且會(huì)打印執(zhí)行分片的計(jì)劃。)
3提示三:輸入yes確認(rèn)開始執(zhí)行分片任務(wù)。在最后我們?cè)俅慰匆幌录籂顟B(tài):
如上圖所示,現(xiàn)在我們的7007已經(jīng)有slot槽了,也就是說(shuō)可以在7007上進(jìn)行讀寫數(shù)據(jù)啦!到此為止我們的7007已經(jīng)加入到集群中啦,并且是主節(jié)點(diǎn)(Master)
(8)添加從節(jié)點(diǎn)(7008)到集群中去。
步驟一:還是需要執(zhí)行add-node命令:
[root@bhz004 7001]#
/usr/local/redis-3.2.6/src/redis-trib.rb add-node 192.168.1.115:7008 192.168.1.115:7001
提示添加成功后我們繼續(xù)看一下集群的狀態(tài):
如圖所示,還是一個(gè)master節(jié)點(diǎn),沒有被分配任何的slot槽。
步驟二:我們需要執(zhí)行replicate命令來(lái)指定當(dāng)前節(jié)點(diǎn)(從節(jié)點(diǎn))的主節(jié)點(diǎn)id為哪個(gè)。
首先需要登錄新加的7008節(jié)點(diǎn)的客戶端,然后使用集群命令進(jìn)行操作,把當(dāng)前的7008(slave)節(jié)點(diǎn)指定到一個(gè)主節(jié)點(diǎn)下(這里使用之前創(chuàng)建的7007主節(jié)點(diǎn),紅色表示節(jié)點(diǎn)id)
[root@bhz004 ~]# /usr/local/bin/redis-cli -c -h 192.168.1.115 -p 7008
192.168.1.171:7008> cluster replicate 382634a4025778c040b7213453fd42a709f79e28
192.168.1.171:7008> OK(提示OK則操作成功)
我們繼續(xù)看一下當(dāng)前集群的狀態(tài),如下圖:我們已經(jīng)成功的把7008放到7007這個(gè)主節(jié)點(diǎn)下面了,到此為止我們已經(jīng)成功的添加完一個(gè)從節(jié)點(diǎn)了。
(9)我們可以對(duì)集群進(jìn)行操作,來(lái)驗(yàn)證下是否可以進(jìn)行讀寫(當(dāng)然可以)。
(10)我們現(xiàn)在嘗試刪除一個(gè)節(jié)點(diǎn)(7008 slave)
步驟一:刪除從節(jié)點(diǎn)7008,輸入del-node命令,指定刪除節(jié)點(diǎn)ip和端口,以及節(jié)點(diǎn)id(紅色為7008節(jié)點(diǎn)id)
[root@bhz004 7001]# /usr/local/redis-3.2.6/src/redis-trib.rb
del-node 192.168.1.115:7008 97b0e0115326833724eb0ffe1d0574ee34618e9f
輸出如下:
>>> Removing node 97b0e0115326833724eb0ffe1d0574ee34618e9f from cluster 192.168.1.171:7008
Connecting to node 192.168.1.171:7008: OK
Connecting to node 192.168.1.171:7003: OK
Connecting to node 192.168.1.171:7006: OK
Connecting to node 192.168.1.171:7002: OK
Connecting to node 192.168.1.171:7005: OK
Connecting to node 192.168.1.171:7001: OK
Connecting to node 192.168.1.171:7004: OK
Connecting to node 192.168.1.171:7007: OK
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
步驟二:再次查看一下集群狀態(tài),如下圖所示,我們已經(jīng)成功的移除了7008 slave節(jié)點(diǎn),另外我們發(fā)現(xiàn)移除一個(gè)節(jié)點(diǎn)以后,當(dāng)前節(jié)點(diǎn)的服務(wù)進(jìn)程也會(huì)隨之銷毀。可以使用ps命令查看當(dāng)前的服務(wù)(ps -el | grep redis),發(fā)現(xiàn)少了一個(gè)運(yùn)行的server,也就是剛移除的7008從節(jié)點(diǎn)。
(11)最后,我們嘗試刪除之前加入的主節(jié)點(diǎn)7007,這個(gè)步驟會(huì)相對(duì)比較麻煩一些,因?yàn)橹鞴?jié)點(diǎn)的里面是有分配了slot槽的,所以我們這里必須先把7007里的slot槽放入到其他的可用主節(jié)點(diǎn)中去,然后再進(jìn)行移除節(jié)點(diǎn)操作才行,不然會(huì)出現(xiàn)數(shù)據(jù)丟失問(wèn)題。
步驟一:刪除7007(master)節(jié)點(diǎn)之前,我們需要先把其全部的數(shù)據(jù)(slot槽)移動(dòng)到其他節(jié)點(diǎn)上去(目前只能把master的數(shù)據(jù)遷移到一個(gè)節(jié)點(diǎn)上,暫時(shí)做不了平均分配功能)。
[root@bhz004 7001]# /usr/local/redis-3.2.6/src/redis-trib.rb reshard 192.168.1.115:7007
輸出如下:
>>> Performing Cluster Check (using node 192.168.1.171:7007)
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots:0-65,5461-5527,10923-10988 (199 slots) master
0 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:66-5460 (5395 slots) master
1 additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5528-10922 (5395 slots) master
1 additional replica(s)
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10989-16383 (5395 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 you want to move (from 1 to 16384)? 199
(注釋:這里不會(huì)是正好200個(gè)槽)
What is the receiving node ID? 614d0def75663f2620b6402a017014b57c912dad
(注釋:這里是需要把數(shù)據(jù)移動(dòng)到哪?7001的主節(jié)點(diǎn)id)
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:382634a4025778c040b7213453fd42a709f79e28
(注釋:這里是需要數(shù)據(jù)源,也就是我們的7007節(jié)點(diǎn)id)
Source node #2:done
(注釋:這里直接輸入done 開始生成遷移計(jì)劃)
Ready to move 199 slots.
Source nodes:
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots:0-65,5461-5527,10923-10988 (199 slots) master
0 additional replica(s)
Destination node:
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:66-5460 (5395 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 0 from 382634a4025778c040b7213453fd42a709f79e28
...
Do you want to proceed with the proposed reshard plan (yes/no)? Yes
(注釋:這里輸入yes開始遷移)
Moving slot 0 from 192.168.1.171:7007 to 192.168.1.171:7001:
...
到此為止我們已經(jīng)成功的把7007主節(jié)點(diǎn)的數(shù)據(jù)遷移到7001上去了,我們可以看一下現(xiàn)在的集群狀態(tài)如下圖,你會(huì)發(fā)現(xiàn)7007下面已經(jīng)沒有任何數(shù)據(jù)(slot)槽了,證明遷移成功!
步驟二:最后我們直接使用del-node命令刪除7007主節(jié)點(diǎn)即可(紅色表示7007的節(jié)點(diǎn)id)。
[root@bhz004 7001]# /usr/local/redis-3.2.6/src/redis-trib.rb del-node
192.168.1.115:7007 382634a4025778c040b7213453fd42a709f79e28
輸出如下:
>>> Removing node 382634a4025778c040b7213453fd42a709f79e28 from cluster 192.168.1.171:7007
Connecting to node 192.168.1.171:7007: OK
Connecting to node 192.168.1.171:7006: OK
Connecting to node 192.168.1.171:7004: OK
Connecting to node 192.168.1.171:7001: OK
Connecting to node 192.168.1.171:7002: OK
Connecting to node 192.168.1.171:7005: OK
Connecting to node 192.168.1.171:7003: OK
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node。
最后:我們查看集群狀態(tài),一切還原為最初始狀態(tài)啦!OK 結(jié)束!