最近在linux安裝了一下redis,特作一些筆記!node
本文先單節點啓動redis,而後再進行持久化配置,在次基礎上,再分享搭建主從模式的配置以及Sentinel 哨兵模式及集羣的搭建linux
請先確保linux操做系統安裝了gccc++
安裝gcc、gcc-c++redis
yum -y install gcc+ gcc-c++vim
檢查gcc環境是否安裝好緩存
rpm -qa|grep gcc安全
若安裝好,執行rpm -qa|grep gcc後:服務器
libgcc-4.8.5-39.el7.x86_64 gcc-4.8.5-39.el7.x86_64架構
下載安裝redisapp
請在官網https://redis.io下載對應的redis,本例使用的是redis-5.0.5
在對應的下載目錄解壓redis,並進入文件夾
tar -zxvf redis-5.0.5.tar.gz
cd redis-5.0.5
編譯安裝
make && make install(make命令或者make install命令)
自此,redis已經安裝成功
啓動redis
./src/redis-server redis.conf
啓動成功後可看到相似日誌
10371:C 09 Oct 2019 16:39:17.783 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 10371:C 09 Oct 2019 16:39:17.783 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=10371, just started 10371:C 09 Oct 2019 16:39:17.783 # Configuration loaded ```
守護線程肯定redis
修改redis.conf,將daemonize no 修改成 daemonize yes,從新啓動便可!
查看redis服務
ps -ef |grep redis
啓動客戶端鏈接
./src/redis-cli
輸入info,查看當前redis信息
自此,redis成功安裝!
./src/redis-cli
也可進行命令號操做了!
Redis提供了將數據按期自動持久化至硬盤的能力,包括RDB和AOF兩種方案,兩種方案分別有其長處和短板,能夠配合起來同時運行,確保數據的穩定性。
必須使用數據持久化嗎?
Redis的數據持久化機制是能夠關閉的。若是你只把Redis做爲緩存服務使用,Redis中存儲的全部數據都不是該數據的主體而僅僅是同步過來的備份,那麼能夠關閉Redis的數據持久化機制。
但一般來講,仍然建議至少開啓RDB方式的數據持久化,由於:
RDB方式的持久化幾乎不損耗Redis自己的性能,在進行RDB持久化時,Redis主進程惟一須要作的事情就是fork出一個子進程,全部持久化工做都由子進程完成
Redis不管由於什麼緣由crash掉以後,重啓時可以自動恢復到上一次RDB快照中記錄的數據。這省去了手工從其餘數據源(如DB)同步數據的過程,並且要比其餘任何的數據恢復方式都要快
如今硬盤那麼大,真的不缺那一點地方
RDB
redis.conf默認配置是開啓了RBD的,默認配置
save 900 1 save 300 10 save 60 10000
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
AOF
採用AOF持久方式時,Redis會把每個寫請求都記錄在一個日誌文件裏。在Redis重啓時,會把AOF文件中記錄的全部寫操做順序執行一遍,確保數據恢復到最新。
AOF默認是關閉的,如要開啓,進行以下配置:
appendonly yes
AOF提供了三種fsync配置,always/everysec/no,經過配置項[appendfsync]指定:
appendfsync no:不進行fsync,將flush文件的時機交給OS決定,速度最快
appendfsync always:每寫入一條日誌就進行一次fsync操做,數據安全性最高,但速度最慢
appendfsync everysec:折中的作法,交由後臺線程每秒fsync一次:開啓後默認
隨着AOF不斷地記錄寫操做日誌,一定會出現一些無用的日誌,例如某個時間點執行了命令SET key1 「abc」,在以後某個時間點又執行了SET key1 「bcd」,那麼第一條命令很顯然是沒有用的。大量的無用日誌會讓AOF文件過大,也會讓數據恢復的時間過長。
因此Redis提供了AOF rewrite功能,能夠重寫AOF文件,只保留可以把數據恢復到最新狀態的最小寫操做集。
AOF rewrite能夠經過BGREWRITEAOF命令觸發,也能夠配置Redis按期自動進行:
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
上面兩行配置的含義是,Redis在每次AOF rewrite時,會記錄完成rewrite後的AOF日誌大小,當AOF日誌大小在該基礎上增加了100%後,自動進行AOF rewrite。同時若是增加的大小沒有達到64mb,則不會進行rewrite。
修改以上配置後,重啓redis,則redis持久化完成
當你set一個值到redis中,而後kill該進程,在重啓改redis,此值並不會消息
以一主多重模式搭建主從模式,計劃使用638一、638二、6383三個端口啓動redis
新建目錄
cd redis-5.0.5 mkdir config cd config mkdir 6381 mkdir 6382 mkdir 6383
複製redis.conf到主從目錄
cp redis.conf ./config/6831 cp redis.conf ./config/6832 cp redis.conf ./config/6833
修改如下配置文件
1) 主節點master
vim ./config/6381/redis.conf
bind 0.0.0.0 #任意ip均可以鏈接 protected-mode no #關閉保護,容許非本地鏈接 port 6381 #端口號 daemonize yes #後臺運行 pidfile /var/run/redis_6381.pid #進程守護文件,就是存放該進程號相關信息的地方 dir /root/worksoft/redis-5.0.5/config/6381/ #db等相關目錄位置 appendonly yes #開啓日誌形式,AOF持久化
vim ./config/6382/redis.conf
protected-mode no #關閉保護,容許非本地鏈接 port 6382 #端口號 daemonize yes #後臺運行 pidfile /var/run/redis_6382.pid #進程守護文件,就是存放該進程號相關信息的地方 dir /root/worksoft/redis-5.0.5/config/6382/ #db等相關目錄位置 appendonly yes #開啓日誌形式,AOF持久化 slaveof 10.0.20.122 6381 #主信息
vim ./config/6383/redis.conf
protected-mode no #關閉保護,容許非本地鏈接 port 6383 #端口號 daemonize yes #後臺運行 pidfile /var/run/redis_6383.pid #進程守護文件,就是存放該進程號相關信息的地方 dir /root/worksoft/redis-5.0.5/config/6383/ #db等相關目錄位置 appendonly yes #開啓日誌形式,AOF持久化 slaveof 10.0.20.122 6381 #主信息
啓動三臺redis
./src/redis-server ./config/6381/redis.conf ./src/redis-server ./config/6382/redis.conf ./src/redis-server ./config/6383/redis.conf
至此,redis主從已經搭建完畢
驗證
查看進程:ps -ef|grep redis
root 10360 1 0 Oct09 ? 00:00:36 ./src/redis-server 0.0.0.0:6381 root 10365 1 0 Oct09 ? 00:00:38 ./src/redis-server 0.0.0.0:6382 root 10372 1 0 Oct09 ? 00:00:38 ./src/redis-server 0.0.0.0:6383
查看信息:./src/redis-cli -p 6381
role:master connected_slaves:2 slave0:ip=10.0.20.122,port=6382,state=online,offset=99216,lag=1 slave1:ip=10.0.20.122,port=6383,state=online,offset=99230,lag=0
而後你還能夠在主節點寫入數據,看是否同步,或者重啓redis,看持久化效果!
編寫啓動/中止腳本:
mkdir bin
cd bin
vim master-slave-start.sh(啓動腳本)
#!/bin/sh /root/worksoft/redis-5.0.5/src/redis-server /root/worksoft/redis-5.0.5/config/6381/redis.conf /root/worksoft/redis-5.0.5/src/redis-server /root/worksoft/redis-5.0.5/config/6382/redis.conf /root/worksoft/redis-5.0.5/src/redis-server /root/worksoft/redis-5.0.5/config/6383/redis.conf```
vim master-slave-stop.sh(中止腳本)
#!/bin/sh /root/worksoft/redis-5.0.5/src/redis-cli -p 6381 shutdown /root/worksoft/redis-5.0.5/src/redis-cli -p 6382 shutdown /root/worksoft/redis-5.0.5/src/redis-cli -p 6383 shutdown
哨兵是redis集羣架構中很是重要的一個組件,主要功能以下:
(1)集羣監控,負責監控redis master 和slave進程是否正常工做。 (2)消息通知,若是某個redis實例有故障,那麼哨兵負責發送消息做爲報警通知給管理員。 (3)故障轉移,若是master node掛掉了,會自動轉移到slave node上。 (4)配置中心,若是故障轉移發生了,通知client客戶端新的master地址。
哨兵自己也是分佈式的,做爲一個哨兵集羣去運行的,相互協同工做
(1)故障轉移時,判斷一個master node宕機了,須要大部分哨兵都贊成才行,涉及到分佈式選舉問題。 (2)及時部分哨兵節點掛掉了,哨兵集羣仍是能正常工做的,由於若是一個做爲高可用機制重要組成部分的故障轉移系統自己就是單點,那麼就不靠譜。
哨兵的核心知識
哨兵至少須要3個實例,來保證本身的健壯性。
哨兵+redis主從的部署架構,是不會保證數據零丟失的,只能保證redis集羣的高可用性
對於哨兵+redis主從這種複雜的部署架構,儘可能在測試環境和生產環境,都進行充分的測試和演練。
因爲redis版本已是5.X,自帶集羣功能以及很完善,此處略過哨兵機制,直接進去redis cluster搭建!
2018年十月 Redis 發佈了穩定版本的 5.0 版本,推出了各類新特性,其中一點是放棄 Ruby的集羣方式,改成 使用 C語言編寫的 redis-cli的方式,是集羣的構建方式複雜度大大下降。關於集羣的更新能夠在 Redis5 的版本說明中看到,以下:
The cluster manager was ported from Ruby (redis-trib.rb) to C code inside redis-cli. check
redis-cli --cluster help
for more info.
能夠查看Redis官網查看集羣搭建方式,鏈接以下
https://redis.io/topics/cluster-tutorial
如下步驟是在一臺 Linux 服務器上搭建有6個節點的 Redis集羣。
新建目錄:/home/redis_cluster/
將以前下載的redis-5.0.5.tar.gz複製到/home/redis_cluster/下,並更名爲redis_all
在redis_all下建立集羣目錄,咱們以官網示例爲例,建立留個目錄,3主3從(redis集羣最少要有3個master)
mkdir cluster_7000 mkdir cluster_7001 mkdir cluster_7002 mkdir cluster_7003 mkdir cluster_7004 mkdir cluster_7005
將配置文件redis.conf複製到集羣目錄下(當前目錄在redis_all下)
cp redis.conf ./cluster_7000/ cp redis.conf ./cluster_7001/ cp redis.conf ./cluster_7002/ cp redis.conf ./cluster_7003/ cp redis.conf ./cluster_7004/ cp redis.conf ./cluster_7005/
修改redis如下配置:
port 7000 #端口 cluster-enabled yes #啓用集羣模式 cluster-config-file nodes-7000.conf cluster-node-timeout 5000 #超時時間 appendonly yes daemonize yes #後臺運行 pidfile /var/run/redis_7000.pid
其中 port 、 pidfile 、 cluster-config-file 須要隨着不一樣的節點調整端口號
個人啓動腳本以下:
#!/bin/sh /home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7000/redis.conf /home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7001/redis.conf /home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7002/redis.conf /home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7003/redis.conf /home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7004/redis.conf /home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7005/redis.conf
ps -ef|grep redis
root 5352 1 0 Oct12 ? 00:03:30 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7000 [cluster] root 5357 1 0 Oct12 ? 00:03:29 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7001 [cluster] root 5362 1 0 Oct12 ? 00:03:07 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7002 [cluster] root 5367 1 0 Oct12 ? 00:03:39 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7003 [cluster] root 5372 1 0 Oct12 ? 00:03:32 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7004 [cluster] root 5377 1 0 Oct12 ? 00:03:10 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7005 [cluster]
我使用的是redis5,在src目錄下使用redis-cli便可將6個節點串聯成集羣
/src/redis-cli --cluster create 10.0.14.81:7000 10.0.14.81:7001 10.0.14.81:7002 10.0.14.81:7003 10.0.14.81:7004 10.0.14.81:7005 --cluster-replicas 1
若你使用過的是redis3或者redis4,則使用redis-trib.rb,語法很是類似
/src/redis-trib.rb create --replicas 1 10.0.14.81:7000 10.0.14.81:7001 10.0.14.81:7002 10.0.14.81:7003 10.0.14.81:7004 10.0.14.81:7005
以上是一主一從的配置,Redis-cli將詢問你的配置,一路輸入yes接受建議的配置。最後,祝您一切順利,您會看待這樣的信息:
[OK] All 16384 slots covered
./src/redis-cli --cluster check 10.0.14.81:7000
結果以下:
10.0.14.81:7003 (b2c9485d...) -> 40 keys | 5462 slots | 1 slaves. 10.0.14.81:7005 (a0d86a09...) -> 34 keys | 5461 slots | 1 slaves. 10.0.14.81:7002 (01af0266...) -> 38 keys | 5461 slots | 1 slaves. [OK] 112 keys in 3 masters. 0.01 keys per slot on average.
> Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
隨着業務的增加,可能以前規劃的redis集羣已經不能知足業務增加的需求,須要對Redis進行擴容!
Redis cluster提供動態擴容的功能
首先新建文件夾cluster_7006
mkdir cluster_7006
複製cluster_7000下的redis.conf文件到cluster_7006下
cp ./cluster_7000/redis.conf ./cluster_7000/
修改如下配置文件:
port 7006 #端口 cluster-enabled yes #啓用集羣模式 cluster-config-file nodes-7006.conf cluster-node-timeout 5000 #超時時間 appendonly yes daemonize yes #後臺運行 pidfile /var/run/redis_7006.pid
啓動該節點
/home/redis_cluster/redis_all/src/redis-server /home/redis_cluster/redis_all/cluster_7006/redis.conf
ok,7006 端口已經啓動好了,而且進程也存在了,下面就是加入到集羣中
同理redis5
./src/redis-cli --cluster add-node 10.0.14.81:7006 10.0.14.81:7000
redis3或者redis4
./src/redis-trib.rb add-node 10.0.14.81:7006 10.0.14.81:7000
日誌以下
> Adding node 10.0.14.81:7006 to cluster 10.0.14.81:7000 > Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered. > Send CLUSTER MEET to node 10.0.14.81:7006 to make it join the cluster. [OK] New node added correctly.
以上,表示新的節點鏈接成功了,並且也已經加入到集羣了,咱們再來檢查一下:
咱們再來檢查一下節點:
./src/redis-cli --cluster check 10.0.14.81:7000
10.0.14.81:7003 (b2c9485d...) -> 40 keys | 5462 slots | 1 slaves. 10.0.14.81:7005 (a0d86a09...) -> 34 keys | 5461 slots | 1 slaves. 10.0.14.81:7002 (01af0266...) -> 38 keys | 5461 slots | 1 slaves. 10.0.14.81:7006 (7e2bad49...) -> 0 keys | 0 slots | 0 slaves. [OK] 112 keys in 4 masters. 0.01 keys per slot on average.
> Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 7e2bad498c38709068babf5b2b6fcdd26199e070 10.0.14.81:7006 slots: (0 slots) master S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
能夠看到 有7個節點。7006 也做爲了master節點,可是,可是,你看看後面的:
10.0.14.81:7006 (7e2bad49...) -> 0 keys | 0 slots | 0 slaves.
什麼狀況,意思就是雖然它如今是主節點,可是,缺沒有分配任何節點給它,也就是它如今還不負責數據存取!
那加上去有錘子用啊!!!!
莫急莫急,咱們先再加入一個從節點,組成4主4從的集羣模式
同上,咱們先啓動一個7007的redis
ps -ef|grep redis
root 5352 1 0 Oct12 ? 00:03:37 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7000 [cluster] root 5357 1 0 Oct12 ? 00:03:36 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7001 [cluster] root 5362 1 0 Oct12 ? 00:03:13 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7002 [cluster] root 5367 1 0 Oct12 ? 00:03:46 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7003 [cluster] root 5372 1 0 Oct12 ? 00:03:39 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7004 [cluster] root 5377 1 0 Oct12 ? 00:03:16 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7005 [cluster] root 14923 1 0 16:08 ? 00:00:01 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7006 [cluster] root 16329 1 0 16:36 ? 00:00:00 /home/redis_cluster/redis_all/src/redis-server 10.0.14.81:7007 [cluster]
將此節點加入集羣:
同理redis5
./src/redis-cli --cluster add-node 10.0.14.81:7007 10.0.14.81:7000 --cluster-slave
redis3或者redis4
./src/redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
再來檢查一下集羣信息
./src/redis-cli --cluster check 10.0.14.81:7000
10.0.14.81:7003 (b2c9485d...) -> 40 keys | 5462 slots | 1 slaves. 10.0.14.81:7005 (a0d86a09...) -> 34 keys | 5461 slots | 1 slaves. 10.0.14.81:7002 (01af0266...) -> 38 keys | 5461 slots | 1 slaves. 10.0.14.81:7006 (7e2bad49...) -> 0 keys | 0 slots | 1 slaves. [OK] 112 keys in 4 masters. 0.01 keys per slot on average.
> Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 669279c3e10fd9ba2526c6fe0a4c5eba788118d4 10.0.14.81:7007 slots: (0 slots) slave replicates 7e2bad498c38709068babf5b2b6fcdd26199e070 M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 7e2bad498c38709068babf5b2b6fcdd26199e070 10.0.14.81:7006 slots: (0 slots) master 1 additional replica(s) S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
OK,4主4從模式搭建成功!下面再來看7006做爲master, (0 slots)是0的問題。
新加入的7006節點做爲master,集羣並無自動將數據從新分片,致使加入後並無任何數據,
這是,須要咱們手動對集羣進行從新分片遷移,也是這個命令:
同理redis5
./src/redis-cli --cluster reshard 10.0.14.81:7000
redis3或redis4
./src/redis-trib.rb reshard 127.0.0.1:7000
這是,redis會詢問你
How many slots do you want to move (from 1 to 16384)?
它提示咱們須要遷移多少slot到7006
上,咱們能夠算一下:16384/4 = 4096
,也就是說,爲了平衡分配起見,咱們須要移動4096
個槽點到7006
上。
好,那輸入4096
How many slots do you want to move (from 1 to 16384)? 4096 What is the receiving node ID?
下一個問題:接受的node ID是多少,7006
的id 咱們經過上面就能夠看到是7e2bad498c38709068babf5b2b6fcdd26199e070 :
那好,輸入7e2bad498c38709068babf5b2b6fcdd26199e070
What is the receiving node ID? 7e2bad498c38709068babf5b2b6fcdd26199e070 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:
接着,又詢問從新分片的源節點(source node), 也便是, 要從哪一個節點中取出 4096 個哈希槽, 並將這些槽移動到7006節點上面。
若是咱們不打算從特定的節點上取出指定數量的哈希槽, 那麼能夠向 redis-trib 輸入 all , 這樣的話, 集羣中的全部主節點都會成爲源節點, redis-trib 將從各個源節點中各取出一部分哈希槽, 湊夠 4096 個, 而後移動到7006節點上:
Source node #1: all
接下來就開始遷移了,而且會詢問你是否確認:
Moving slot 12267 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12268 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12269 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12270 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12271 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12272 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12273 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12274 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12275 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12276 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12277 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12278 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12279 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12280 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12281 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12282 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12283 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12284 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12285 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12286 from 01af026615e4d5ca206b51faa65fca158ac52b3d Moving slot 12287 from 01af026615e4d5ca206b51faa65fca158ac52b3d Do you want to proceed with the proposed reshard plan (yes/no)?
輸入 yes 並使用按下回車以後, redis 就會正式開始執行從新分片操做, 將指定的哈希槽從源節點一個個地移動到7006節點上面!
再次檢查集羣:
./src/redis-cli --cluster check 10.0.14.81:7000
10.0.14.81:7003 (b2c9485d...) -> 26 keys | 4096 slots | 1 slaves. 10.0.14.81:7005 (a0d86a09...) -> 28 keys | 4096 slots | 1 slaves. 10.0.14.81:7002 (01af0266...) -> 28 keys | 4096 slots | 1 slaves. 10.0.14.81:7006 (7e2bad49...) -> 30 keys | 4096 slots | 1 slaves. [OK] 112 keys in 4 masters. 0.01 keys per slot on average.
> Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[1365-5460] (4096 slots) master 1 additional replica(s) S: 669279c3e10fd9ba2526c6fe0a4c5eba788118d4 10.0.14.81:7007 slots: (0 slots) slave replicates 7e2bad498c38709068babf5b2b6fcdd26199e070 M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: 7e2bad498c38709068babf5b2b6fcdd26199e070 10.0.14.81:7006 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master 1 additional replica(s) S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
咱們看到7006 slots爲4096,進入7006查看數據
./src/redis-cli -h 10.0.14.81 -p 7006 keys *
發現新節點已經有數據,再次查看以前老節點,發現數據已經被遷移!
redis cluster一樣支持移除節點功能
同理redis5
./src/redis-cli --cluster del-node 10.0.14.81:7000 <node-id>
node-id爲節點id,咱們如今要移出7007的節點,node-id爲669279c3e10fd9ba2526c6fe0a4c5eba788118d4
Redis3 或 redis4
./src/redis-trib del-node 127.0.0.1:7000 <node-id>
可是想刪除一個節點前,他必須沒有數據,且槽位必須分配到其餘的節點
You can remove a master node in the same way as well, however in order to remove a master node it must be empty
固然,若是改節點有從節點,能夠直接刪除,如咱們刪除7007節點,因爲他有7006位從節點,則能夠直接刪除:
./src/redis-cli --cluster del-node 10.0.14.81:7000 669279c3e10fd9ba2526c6fe0a4c5eba788118d4
> Removing node 669279c3e10fd9ba2526c6fe0a4c5eba788118d4 from cluster 10.0.14.81:7000 > Sending CLUSTER FORGET messages to the cluster... > SHUTDOWN the node.
查看集羣信息: ./src/redis-cli --cluster check 10.0.14.81:7000
10.0.14.81:7003 (b2c9485d...) -> 26 keys | 4096 slots | 1 slaves. 10.0.14.81:7005 (a0d86a09...) -> 28 keys | 4096 slots | 1 slaves. 10.0.14.81:7002 (01af0266...) -> 28 keys | 4096 slots | 1 slaves. 10.0.14.81:7006 (7e2bad49...) -> 30 keys | 4096 slots | 0 slaves. [OK] 112 keys in 4 masters. 0.01 keys per slot on average.
> Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[1365-5460] (4096 slots) master 1 additional replica(s) M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: 7e2bad498c38709068babf5b2b6fcdd26199e070 10.0.14.81:7006 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
能夠看到,7007被刪除,7006右從變主,並接管了7007的數據
咱們繼續刪除7006,讓集羣變成3主3從
./src/redis-cli --cluster del-node 10.0.14.81:7000 7e2bad498c38709068babf5b2b6fcdd26199e070
能夠看到,因爲7006沒從節點,且數據不爲空,刪除失敗
> Removing node 7e2bad498c38709068babf5b2b6fcdd26199e070 from cluster 10.0.14.81:7000 [ERR] Node 10.0.14.81:7006 is not empty! Reshard data away and try again.
因此,咱們如今要對數據從新分片:
同上redis5
./src/redis-cli --cluster reshard 10.0.14.81:7000
redis3或redis4
./src/redis-trib.rb reshard 127.0.0.1:7000
第一個詢問,咱們要分多少個槽點,因爲7007上有4096
個槽點,因此這裏填寫4096:
> Check for open slots... > Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)?
下一步,提示咱們,須要移動到哪一個id上,那就填7003的吧(鎖容以後,槽位可能分配不均勻):
What is the receiving node ID? b2c9485d8e2b3ae42444f2f56715086c8682afdb 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:
下一步,詢問咱們從哪一個節點去遷移數據,由於咱們是要刪除7006的,因此,咱們就得7006的id了
Source node #1:7e2bad498c38709068babf5b2b6fcdd26199e070 Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes
ok,這樣就遷移好了。咱們看看7006是否爲空了
[root@localhost redis_all]# ./src/redis-cli -h 10.0.14.81 -p 7006 10.0.14.81:7006> keys * (empty list or set)
OK,再次來移出集羣
./src/redis-cli --cluster del-node 10.0.14.81:7000 7e2bad498c38709068babf5b2b6fcdd26199e070
成功:
> Removing node 7e2bad498c38709068babf5b2b6fcdd26199e070 from cluster 10.0.14.81:7000 > Sending CLUSTER FORGET messages to the cluster... > SHUTDOWN the node.
檢查集羣 ./src/redis-cli --cluster check 10.0.14.81:7000
10.0.14.81:7003 (b2c9485d...) -> 56 keys | 8192 slots | 1 slaves. 10.0.14.81:7005 (a0d86a09...) -> 28 keys | 4096 slots | 1 slaves. 10.0.14.81:7002 (01af0266...) -> 28 keys | 4096 slots | 1 slaves. [OK] 112 keys in 3 masters. 0.01 keys per slot on average.
> Performing Cluster Check (using node 10.0.14.81:7000) S: 3d8af4254be23d4548daf361053d40f06a17e785 10.0.14.81:7000 slots: (0 slots) slave replicates a0d86a09aafc2c4ae83fbecb1130919e5461d81e M: b2c9485d8e2b3ae42444f2f56715086c8682afdb 10.0.14.81:7003 slots:[0-1364],[5461-12287] (8192 slots) master 1 additional replica(s) S: b181b7b9aa37bf0a11f6b0d1866124db3f84fc11 10.0.14.81:7001 slots: (0 slots) slave replicates b2c9485d8e2b3ae42444f2f56715086c8682afdb M: a0d86a09aafc2c4ae83fbecb1130919e5461d81e 10.0.14.81:7005 slots:[1365-5460] (4096 slots) master 1 additional replica(s) M: 01af026615e4d5ca206b51faa65fca158ac52b3d 10.0.14.81:7002 slots:[12288-16383] (4096 slots) master 1 additional replica(s) S: 582e7dcebca9a9701a07bbd7799dec6df9e59b22 10.0.14.81:7004 slots: (0 slots) slave replicates 01af026615e4d5ca206b51faa65fca158ac52b3d [OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
能夠看到已移出,可是有個問題,由於剛給咱們把7006的槽位移到了7003,因此能夠看到7003的槽位爲8192個,會形成分配不均勻,若須要均勻,須要你平均的分配槽位信息,相信有了上面的命令,這一點也沒有什麼難度了!