由于redis集群需要使用ruby命令,所以我們需要安裝ruby(所有的操作都是在單機(jī)部署的偽集群模式下進(jìn)行)
(1)yum?install?ruby
(2)yum?install?rubygems
(3)gem?install?redis?(安裝redis和ruby的接口)
一贰谣、原始集群(6節(jié)點(diǎn) 3主3從):
(1)啟動集群:
[root@centos~]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
[root@ centos ~]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
[root@ centos ~]# /usr/local/redis/bin/redis-server/usr/local/redis-cluster/7003/redis.conf
[root@ centos ~]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7004/redis.conf
[root@ centos ~]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7005/redis.conf
[root@ centos ~]# /usr/local/redis/bin/redis-server/usr/local/redis-cluster/7006/redis.conf
(2)查看服務(wù)器運(yùn)行狀態(tài):
[root@centos~]# 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)
二倍啥、新集群操作
(1)我們新建倆個服務(wù)牌捷,按照之前搭建的集群方式新增倆個節(jié)點(diǎn):(一主一從 master乌助、slave)
????Master:7007??????????????? Slave:7008
步驟一:創(chuàng)建7007/7008文件夾萌京⊙媲幔拷貝redis.conf文件到對于的7007,7008目錄下要? ? ? ? ? ? ? ? ? ? ? ??
進(jìn)行修改配置文件数苫。
????[root@centos redis-cluster]# mkdir 7007
????[root@centos redis-cluster]# mkdir 7008
????[root@centos redis-cluster]# cd 7001
????[root@centos 7001]# cp redis.conf /usr/local/redis-cluster/7007/
????[root@centos 7001]# cp redis.conf /usr/local/redis-cluster/7008/
? ? ?[root@centos 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@centos 7001]# vim/usr/local/redis-cluster/7008/redis.conf
修改內(nèi)容如下:
??????? ?port:7008
?????dir /usr/local/redis-cluster/7008/
?????cluster-config-file nodes7008.conf
步驟二:啟動7007和7008倆個服務(wù)并查看服務(wù)狀態(tài)狭归。
[root@centos 7001]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7007/redis.conf
[root@centos 7001]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf
[root@centos 7001]# ps -el | grep redis
(2)學(xué)習(xí)redis-trib命令使用:
[root@centos local]# cd/usr/local/redis3.0/src
[root@centos src]# redis-trib.rb
1 create:創(chuàng)建一個集群環(huán)境host1:port1 ... hostN:portN(集群中的主從節(jié)點(diǎn)比例)
2 call:可以執(zhí)行redis命令
3 add-node:將一個節(jié)點(diǎn)添加到集群里,第一個參數(shù)為新節(jié)點(diǎn)的ip:port文判,第二個參數(shù)為集群中任意一個已經(jīng)存在的節(jié)點(diǎn)的ip:port
4 del-node:移除一個節(jié)點(diǎn)
5 reshard:重新分片
6 check:檢查集群狀態(tài)
(3)新增一個主節(jié)點(diǎn)7007(master)
步驟一:使用add-node命令:綠色為新增節(jié)點(diǎn)过椎,紅色為已知存在節(jié)點(diǎn)
[root@centos 7001]#
/usr/local/redis3.0/src/redis-trib.rbadd-node192.168.1.171:7007192.168.1.171:7001
輸出如下:
>>> Addingnode 192.168.1.171:7007 to cluster 192.168.1.171:7001
Connecting to node192.168.1.171:7001: OK
Connecting to node192.168.1.171:7006: OK
Connecting to node192.168.1.171:7005: OK
Connecting to node192.168.1.171:7004: OK
Connecting to node192.168.1.171:7002: OK
Connecting to node192.168.1.171:7003: OK
>>> PerformingCluster 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
?? replicates83df08875c7707878756364039df0a4c8658f272
S:adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
?? slots: (0 slots) slave
?? replicates8aac82b63d42a1989528cd3906579863a5774e77
S:a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
?? slots: (0 slots) slave
?? replicates614d0def75663f2620b6402a017014b57c912dad
M: 8aac82b63d42a1989528cd3906579863a5774e77192.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 agreeabout slots configuration.
>>> Check foropen slots...
>>> Checkslots coverage...
[OK] All 16384 slotscovered.
Connecting to node192.168.1.171:7007: OK
>>> SendCLUSTER MEET to node 192.168.1.171:7007 to make it join the cluster.
[OK] New node added correctly.
步驟二:查看集群狀態(tài):
[root@centos 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)不會有任何數(shù)據(jù)戏仓,因為它沒有分配任何的slot(hash槽)疚宇。我們需要為新節(jié)點(diǎn)手工分配slot。
為7007分配slot槽赏殃。
步驟一:使用redis-trib命令敷待,找到集群中的任意一個主節(jié)點(diǎn)(紅色位置表現(xiàn)集群中的任意一個主節(jié)點(diǎn)),對其進(jìn)行重新分片工作仁热。
[root@centos 7001]#/usr/local/redis3.0/src/redis-trib.rb reshard192.168.1.171:7001
輸出如下:
>>> PerformingCluster 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
?? replicates83df08875c7707878756364039df0a4c8658f272
S:adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
?? slots: (0 slots) slave
?? replicates8aac82b63d42a1989528cd3906579863a5774e77
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
?? replicates614d0def75663f2620b6402a017014b57c912dad
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 agreeabout slots configuration.
>>> Check foropen slots...
>>> Checkslots coverage...
[OK] All 16384 slotscovered.
(提示一)
How many slots do you
want to move (from 1 to 16384)? 200
(提示二)
What is the receiving
node 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.
? Source
nodes:
??? M: 614d0def75663f2620b6402a017014b57c912dad192.168.1.171:7001
?? slots:0-5460 (5461 slots) master
?? 1 additional replica(s)
??? M: 8aac82b63d42a1989528cd3906579863a5774e77192.168.1.171:7002
?? slots:5461-10922 (5462 slots) master
?? 1 additional replica(s)
??? M: 83df08875c7707878756364039df0a4c8658f272192.168.1.171:7003
?? slots:10923-16383 (5461 slots) master
?? 1 additional replica(s)
? Destination
node:
??? M: 382634a4025778c040b7213453fd42a709f79e28192.168.1.171:7007
?? slots: (0 slots) master
?? 0 additional replica(s)
? Resharding plan:(分片執(zhí)行計劃日志)
Moving slot 5461 from8aac82b63d42a1989528cd3906579863a5774e77
...
Moving slot 0 from 614d0def75663f2620b6402a017014b57c912dad
...
??? Moving slot 10923 from83df08875c7707878756364039df0a4c8658f272
...
(提示三)
Do you want to
proceed with the proposed reshard plan (yes/no)? yes
Moving slot 65 from192.168.1.171:7001 to 192.168.1.171:7007:
...
Moving slot 10923 from 192.168.1.171:7003to 192.168.1.171:7007:
...
Moving slot 5527 from192.168.1.171:7002 to 192.168.1.171:7007:
...
1提示一:是希望你需要多少個槽移動到新的節(jié)點(diǎn)上榜揖,可以自己設(shè)置,比如200個槽抗蠢。
2提示二:是你需要把這200個slot槽移動到那個節(jié)點(diǎn)上去(需要指定節(jié)點(diǎn)id)举哟,并且下個????????????? ? 提示是輸入all為從所有主節(jié)點(diǎn)(7001 7002 7003)中分別抽取響應(yīng)的槽數(shù)(一共為200個槽到指定的新節(jié)點(diǎn)中!迅矛,并且會打印執(zhí)行分片的計劃妨猩。)
3提示三:輸入yes確認(rèn)開始執(zhí)行分片任務(wù)。在最后我們再次看一下集群狀態(tài):
如上圖所示秽褒,現(xiàn)在我們的7007已經(jīng)有slot槽了壶硅,也就是說可以在7007上進(jìn)行讀寫數(shù)據(jù)啦!到此為止我們的7007已經(jīng)加入到集群中啦销斟,并且是主節(jié)點(diǎn)(Master)
添加從節(jié)點(diǎn)(7008)到集群中去庐椒。
步驟一:還是需要執(zhí)行add-node命令:
[root@centos 7001]#/usr/local/redis3.0/src/redis-trib.rbadd-node 192.168.1.171:7008 192.168.1.171:7001
提示添加成功后我們繼續(xù)看一下集群的狀態(tài):
如圖所示,還是一個master節(jié)點(diǎn)蚂踊,沒有被分配任何的slot槽约谈。
步驟二:我們需要執(zhí)行replicate命令來指定當(dāng)前節(jié)點(diǎn)(從節(jié)點(diǎn))的主節(jié)點(diǎn)id為哪個。
首先需要登錄新加的7008節(jié)點(diǎn)的客戶端,然后使用集群命令進(jìn)行操作窗宇,把當(dāng)前的7008(slave)節(jié)點(diǎn)指定到一個主節(jié)點(diǎn)下(這里使用之前創(chuàng)建的7007主節(jié)點(diǎn),紅色表示節(jié)點(diǎn)id)
[root@centos ~]# /usr/local/redis/bin/redis-cli-c -h 192.168.1.171 -p 7008
192.168.1.171:7008> cluster replicate382634a4025778c040b7213453fd42a709f79e28
192.168.1.171:7008> OK(提示OK則操作成功)
我們繼續(xù)看一下當(dāng)前集群的狀態(tài)特纤,如下圖:我們已經(jīng)成功的把7008放到7007這個主節(jié)點(diǎn)下面了军俊,到此為止我們已經(jīng)成功的添加完一個從節(jié)點(diǎn)了。
我們可以對集群進(jìn)行操作捧存,來驗證下是否可以進(jìn)行讀寫(當(dāng)然可以)粪躬。
我們現(xiàn)在嘗試刪除一個節(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@centos 7001]#/usr/local/redis3.0/src/redis-trib.rb
del-node 192.168.1.171:700897b0e0115326833724eb0ffe1d0574ee34618e9f
輸出如下:
>>> Removing node 97b0e0115326833724eb0ffe1d0574ee34618e9ffrom 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 FORGETmessages to the cluster...
>>> SHUTDOWN the node.
步驟二:再次查看一下集群狀態(tài),如下圖所示吗货,我們已經(jīng)成功的移除了7008 slave節(jié)點(diǎn)泳唠,另外我們發(fā)現(xiàn)移除一個節(jié)點(diǎn)以后,當(dāng)前節(jié)點(diǎn)的服務(wù)進(jìn)程也會隨之銷毀宙搬”啃龋可以使用ps命令查看當(dāng)前的服務(wù)(ps -el | grep
redis),發(fā)現(xiàn)少了一個運(yùn)行的server勇垛,也就是剛移除的7008從節(jié)點(diǎn)脖母。
最后,我們嘗試刪除之前加入的主節(jié)點(diǎn)7007
這個步驟會相對比較麻煩一些闲孤,因為主節(jié)點(diǎn)的里面是有分配了slot槽的谆级,所以我們這里必須先把7007里的slot槽放入到其他的可用主節(jié)點(diǎn)中去,然后再進(jìn)行移除節(jié)點(diǎn)操作才行讼积,不然會出現(xiàn)數(shù)據(jù)丟失問題肥照。
步驟一:刪除7007(master)節(jié)點(diǎn)之前,我們需要先把其全部的數(shù)據(jù)(slot槽)移動到其他節(jié)點(diǎn)上去(目前只能把master的數(shù)據(jù)遷移到一個節(jié)點(diǎn)上勤众,暫時做不了平均分配功能)建峭。
[root@centos 7001]#/usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.171: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: fa299e41c173fa807ba04684c2f5e5e185d5f7d0192.168.1.171:7006
??slots: (0 slots) slave
??replicates 83df08875c7707878756364039df0a4c8658f272
S: a69b98937844c6050ee5885266ccccb185a3f36a192.168.1.171:7004
??slots: (0 slots) slave
??replicates 614d0def75663f2620b6402a017014b57c912dad
M: 614d0def75663f2620b6402a017014b57c912dad192.168.1.171:7001
??slots:66-5460 (5395 slots) master
?? 1additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77192.168.1.171:7002
??slots:5528-10922 (5395 slots) master
?? 1additional replica(s)
S: adb99506ddccad332e09258565f2e5f4f456a150192.168.1.171:7005
??slots: (0 slots) slave
??replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 83df08875c7707878756364039df0a4c8658f272192.168.1.171: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 you
want to move (from 1 to 16384)? 199
(注釋:這里不會是正好200個槽)
What is the receiving
node ID? 614d0def75663f2620b6402a017014b57c912dad
(注釋:這里是需要把數(shù)據(jù)移動到哪?7001的主節(jié)點(diǎn)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
(注釋:這里是需要數(shù)據(jù)源决摧,也就是我們的7007節(jié)點(diǎn)id)
Source node #2:done
(注釋:這里直接輸入done 開始生成遷移計劃)
Ready to move 199 slots.
??Source nodes:
??? M: 382634a4025778c040b7213453fd42a709f79e28192.168.1.171:7007
?? slots:0-65,5461-5527,10923-10988 (199 slots)master
?? 0 additional replica(s)
? Destination
node:
??? M: 614d0def75663f2620b6402a017014b57c912dad192.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 to192.168.1.171:7001:
...
到此為止我們已經(jīng)成功的把7007主節(jié)點(diǎn)的數(shù)據(jù)遷移到7001上去了亿蒸,我們可以看一下現(xiàn)在的集群狀態(tài)如下圖,你會發(fā)現(xiàn)7007下面已經(jīng)沒有任何數(shù)據(jù)(slot)槽了掌桩,證明遷移成功边锁!
步驟二:最后我們直接使用del-node命令刪除7007主節(jié)點(diǎn)即可(紅色表示7007的節(jié)點(diǎn)id)。
[root@centos 7001]#/usr/local/redis3.0/src/redis-trib.rb del-node
192.168.1.171:7007382634a4025778c040b7213453fd42a709f79e28
輸出如下:
>>> Removing node382634a4025778c040b7213453fd42a709f79e28 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 FORGETmessages to the cluster...
>>> SHUTDOWN the node波岛。
最后:我們查看集群狀態(tài)茅坛,一切還原為最初始狀態(tài)啦!OK 結(jié)束!