Redis集群水平擴(kuò)展、集群中添加以及刪除節(jié)點(diǎn)的操作
在Redis集群搭建中,我們搭建了下圖所示的Redis集群,接下來(lái)我們就來(lái)看看如何為我們的Redis集群進(jìn)行水平擴(kuò)容。
[root@localhost redis-5.0.3]# src/redis-server redis-cluster/8001/redis.conf [root@localhost redis-5.0.3]# src/redis-server redis-cluster/8004/redis.conf [root@localhost redis-5.0.3]# src/redis-server redis-cluster/8002/redis.conf [root@localhost redis-5.0.3]# src/redis-server redis-cluster/8005/redis.conf [root@localhost redis-5.0.3]# src/redis-server redis-cluster/8003/redis.conf [root@localhost redis-5.0.3]# src/redis-server redis-cluster/8006/redis.conf
首先,先啟動(dòng)我們的集群(在上一篇博客中,我們已經(jīng)將集群環(huán)境搭好了,只需要重啟啟動(dòng)redis服務(wù)即可)
查看集群中的信息:
為集群水平擴(kuò)容
接下來(lái),我們將在集群得基礎(chǔ)上再添加一主一從,增加后集群如下圖所示:
1. 增加8007和8008倆個(gè)redis實(shí)例
在/usr/local/software/redis-5.0.3/redis-cluster下創(chuàng)建8007和8008文件夾,并拷貝8001文件夾下的redis.conf文件到8007和8008這兩個(gè)文件夾下,進(jìn)行修改
mkdir 8007 8008 cd 8001 cp redis.conf /usr/local/software/redis-5.0.3/redis-cluster/8007/ cp redis.conf /usr/local/software/redis-5.0.3/redis-cluster/8008/ # 修改8007文件夾下的redis.conf配置文件 vim /usr/local/software/redis-5.0.3/redis-cluster/8007/redis.conf # 修改如下內(nèi)容: port 8007 dir /usr/local/software/redis-5.0.3/redis-cluster/8007 cluster-config-file nodes-8007.conf # 修改8008文件夾下的redis.conf配置文件 vim /usr/local/software/redis-5.0.3/redis-cluster/8008/redis.conf 修改內(nèi)容如下: port 8008 dir /usr/local/software/redis-5.0.3/redis-cluster/8008 cluster-config-file nodes-8008.conf # 啟動(dòng)8007和8008倆個(gè)服務(wù)并查看服務(wù)狀態(tài) src/redis-server /usr/local/software/redis-5.0.3/redis-cluster/8007/redis.conf src/redis-server /usr/local/software/redis-5.0.3/redis-cluster/8008/redis.conf ps -el | grep redis
2. 將8007和8008加入集群中
查看redis集群得幫助命令:
1.create:創(chuàng)建一個(gè)集群環(huán)境host1:port1 … hostN:portN
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)
添加8007和8008節(jié)點(diǎn)到集群中
[root@localhost redis-5.0.3]# src/redis-cli --cluster add-node 192.168.243.133:8007 192.168.243.133:8001 [root@localhost redis-5.0.3]# src/redis-cli --cluster add-node 192.168.243.133:8008 192.168.243.133:8001
3. 查看集群狀態(tài)
我們發(fā)現(xiàn)對(duì)于新加入的節(jié)點(diǎn)默認(rèn)都是master節(jié)點(diǎn),最重要的是,他們都沒(méi)有分配slots槽位,所以根據(jù)Redis集群分片的原理,這些剛加入集群的節(jié)點(diǎn)實(shí)際上是不能讀寫(xiě)數(shù)據(jù)的。
4. 為新加入的節(jié)點(diǎn)分配槽位
使用集群中的任意一個(gè)主節(jié)點(diǎn),對(duì)其進(jìn)行重新分片工作
[root@localhost redis-5.0.3]# src/redis-cli --cluster reshard 192.168.243.133:8001
輸入命令后,會(huì)進(jìn)入一個(gè)交互式的操作:
1. How many slots do you want to move (from 1 to 16384)? 600
你想移動(dòng)多少個(gè)槽位? 這里移動(dòng)600個(gè)
2. What is the receiving node ID? 7cbcddaea49560b2847327b1465e8db7082655d2
要將槽位分配給拿給節(jié)點(diǎn)? 指定節(jié)點(diǎn)的ID
3. 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
要從哪幾個(gè)節(jié)點(diǎn)抽取槽位,all:從所有的節(jié)點(diǎn)中,各自抽取一部分槽位給8007。輸入all后會(huì)有一個(gè)抽取槽位的計(jì)劃
4.Do you want to proceed with the proposed reshard plan (yes/no)? yes開(kāi)始執(zhí)行
是否執(zhí)行該reshard計(jì)劃。
查看集群的最新?tīng)顟B(tài):
我們可以看到8007已經(jīng)有hash槽位了,此時(shí)我們就能往8007進(jìn)行讀寫(xiě)操作了
5. 將8008配置為8007的從節(jié)點(diǎn)
我們可以通過(guò)replicate命令指定當(dāng)前節(jié)點(diǎn)成為哪一個(gè)節(jié)點(diǎn)的從節(jié)點(diǎn)。
192.168.243.133:8008> cluster replicate 7cbcddaea49560b2847327b1465e8db7082655d2 OK
查看集群的狀態(tài):
發(fā)現(xiàn)8008已經(jīng)成為8007的從節(jié)點(diǎn)了,至此,Redis水平擴(kuò)容成功。
刪除集群中的節(jié)點(diǎn)
刪除8008從節(jié)點(diǎn)
用del-node刪除從節(jié)點(diǎn)8008,指定刪除節(jié)點(diǎn)ip和端口,以及節(jié)點(diǎn)id
[root@localhost redis-5.0.3]# src/redis-cli --cluster del-node 192.168.243.133:8008 840340ce7a2dabdfc6ad40fde17e9e0c803b386c
發(fā)現(xiàn)8008已經(jīng)被移除了。
刪除8007主節(jié)點(diǎn)
刪除8007節(jié)點(diǎn)比刪除8008節(jié)點(diǎn)麻煩一點(diǎn),因?yàn)?007節(jié)點(diǎn)管理一部分slots,在刪除它之前,需要將slots分配給其他可用的master節(jié)點(diǎn)上,否則就會(huì)出現(xiàn)數(shù)據(jù)丟失問(wèn)題。
1、重新分配8007上的slots
[root@localhost redis-5.0.3]# src/redis-cli --cluster reshard 192.168.243.133:8007
交互流程:
1. How many slots do you want to move (from 1 to 16384)? 600
2. What is the receiving node ID? ec0001bd4282f790017d1e68259c67f2d7037a3c
接收slots的節(jié)點(diǎn)ID(這里是8001的主節(jié)點(diǎn)ID)
3. 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: 7cbcddaea49560b2847327b1465e8db7082655d2 Source node #2: done
數(shù)據(jù)源ID,這里輸入8007節(jié)點(diǎn)的ID,表示600個(gè)slots都由8007提供(8007總共就600個(gè)slots)
4. Do you want to proceed with the proposed reshard plan (yes/no)? yes
查看集群節(jié)點(diǎn)狀態(tài):
2、使用del-node命令刪除8007節(jié)點(diǎn)
[root@localhost redis-5.0.3]# src/redis-cli --cluster del-node 192.168.243.133:8007 7cbcddaea49560b2847327b1465e8db7082655d2
再次查看集群狀態(tài):
8007已經(jīng)被移除
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
用Redis實(shí)現(xiàn)微博關(guān)注關(guān)系
在微博中,每一個(gè)用戶都會(huì)有一個(gè)關(guān)注列表,一個(gè)粉絲列表。用戶可以查看自己的關(guān)注,粉絲列表,也可以查看別人的關(guān)注,粉絲列表。并且,要展示列表里每個(gè)人與當(dāng)前查看者的關(guān)注狀態(tài)。2015-09-09Redis緩存過(guò)期的實(shí)現(xiàn)示例
Redis緩存的過(guò)期策略是保證緩存可靠性和性能的關(guān)鍵之一,本文主要介紹了Redis緩存過(guò)期的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Redis實(shí)現(xiàn)分布式鎖的幾種方法總結(jié)
這篇文章主要介紹了Redis實(shí)現(xiàn)分布式鎖的幾種方法總結(jié)的相關(guān)資料, Redis實(shí)現(xiàn)與Zookeeper實(shí)現(xiàn)和數(shù)據(jù)庫(kù)實(shí)現(xiàn),需要的朋友可以參考下2017-07-07Redis分布式鎖與Redlock算法實(shí)現(xiàn)
在Redis中,可以使用多種方式實(shí)現(xiàn)分布式鎖,如使用SETNX命令或RedLock算法,本文就來(lái)介紹一下Redis分布式鎖與Redlock算法實(shí)現(xiàn),感興趣的可以了解一下2023-12-12Redis數(shù)據(jù)庫(kù)的應(yīng)用場(chǎng)景介紹
這篇文章主要介紹了Redis數(shù)據(jù)庫(kù)的應(yīng)用場(chǎng)景介紹,本文講解了MySql+Memcached架構(gòu)的問(wèn)題、Redis常用數(shù)據(jù)類型、Redis數(shù)據(jù)類型應(yīng)用和實(shí)現(xiàn)方式、Redis實(shí)際應(yīng)用場(chǎng)景等內(nèi)容,需要的朋友可以參考下2015-06-06Redis數(shù)據(jù)結(jié)構(gòu)之intset整數(shù)集合使用學(xué)習(xí)
這篇文章主要為大家介紹了Redis數(shù)據(jù)結(jié)構(gòu)之整數(shù)集合使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Quarkus集成redis操作Redisson實(shí)現(xiàn)數(shù)據(jù)互通
這篇文章主要為大家介紹了Quarkus集成redis操作Redisson實(shí)現(xiàn)數(shù)據(jù)互通的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Redis做預(yù)定庫(kù)存緩存功能設(shè)計(jì)使用
這篇文章主要為大家介紹了Redis做預(yù)定庫(kù)存緩存功能設(shè)計(jì)使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04