redis cluster集群
Redis集群工作原理:
Redis 集群沒有使用一致性hash, 而是引入了 哈希槽的概念。 Redis 集群有16384個(gè)哈希槽,每個(gè)key通過CRC16校驗(yàn)后對(duì)16384取模來(lái)決定放置哪個(gè)槽。集群的每個(gè) 節(jié)點(diǎn)負(fù)責(zé)一部分hash槽,舉個(gè)例子,比如當(dāng)前集群有3個(gè)節(jié)點(diǎn),那么: 節(jié)點(diǎn) A 包含 0 到 5460號(hào)哈希槽. 節(jié)點(diǎn) B 包含5461到10922號(hào)哈希槽. 節(jié)點(diǎn) C 包含10923到16383號(hào)哈希槽. 這種結(jié)構(gòu)很容易添加或者刪除節(jié)點(diǎn),比如如果我想新添加個(gè)節(jié)點(diǎn)D,我需要從節(jié)點(diǎn) A, B, C中得部分槽到 D上。如果我想移除節(jié)點(diǎn)A,需要將A中的槽移到B和C節(jié)點(diǎn)上,然后將沒有任何槽的A節(jié)點(diǎn)從集群中移除 即可。由于從一個(gè)節(jié)點(diǎn)將哈希槽移動(dòng)到另一個(gè)節(jié)點(diǎn)并不會(huì)停止服務(wù),所以無(wú)論添加刪除或者改變某個(gè)節(jié) 點(diǎn)的哈希槽的數(shù)量都不會(huì)造成集群不可用的狀態(tài)。
Redis 集群的主從復(fù)制模型:
為了使在部分節(jié)點(diǎn)失敗或者大部分節(jié)點(diǎn)無(wú)法通信的情況下集群仍然可用,所以集群使用了主從復(fù)制模型, 每個(gè)節(jié)點(diǎn)都會(huì)有N-1個(gè)復(fù)制品。 在上面具有A,B,C三個(gè)節(jié)點(diǎn)的集群,在沒有復(fù)制模型的情況下,如果節(jié)點(diǎn)B失敗了,那么整個(gè)集群就 會(huì)以為缺少5461到10922這個(gè)范圍的槽而不可用。 然而如果在集群創(chuàng)建的時(shí)候(或者過一段時(shí)間)我們?yōu)槊總€(gè)節(jié)點(diǎn)添加一個(gè)從節(jié)點(diǎn)A1,B1,C1,那么整個(gè) 集群便有三個(gè)master節(jié)點(diǎn)和三個(gè)slave節(jié)點(diǎn)組成,這樣在節(jié)點(diǎn)B失敗后,集群便會(huì)選舉B1為新的主節(jié)點(diǎn) 繼續(xù)服務(wù),整個(gè)集群便不會(huì)因?yàn)椴壅也坏蕉豢捎昧耍贿^當(dāng)B和B1 都失敗后,集群是不可用的。
redis cluster集群部署:
環(huán)境:
centos 7.X
redis 5.0.7
創(chuàng)建redis實(shí)例:
#創(chuàng)建redis-cluster 存放目錄: mkdir -p /usr/local/redis-cluster #用初始化腳本方式,快速創(chuàng)建實(shí)例配置文件,日志目錄: cd /usr/src/redis-5.0.7 root@localhost redis-5.0.7]# ./utils/install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] 7001 Please select the redis config file name [/etc/redis/7001.conf] /usr/local/redis-cluster/7001.conf Please select the redis log file name [/var/log/redis_7001.log] /usr/local/redis-cluster/7001.log Please select the data directory for this instance [/var/lib/redis/7001] /usr/local/redis-cluster/7001 Please select the redis executable path [/usr/local/redis/bin/redis-server] /usr/local/redis/bin/redis-server Selected config: Port : 7001 Config file : /usr/local/redis-cluster/7001.conf Log file : /usr/local/redis-cluster/7001.log Data dir : /usr/local/redis-cluster/7001 Executable : /usr/local/redis/bin/redis-server Cli Executable : /usr/local/redis/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/7001.conf => /etc/init.d/redis_7001 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful! #以上是創(chuàng)建7001實(shí)例: 用相同的方式創(chuàng)建剩余的5個(gè)redis cluster實(shí)例;
開啟Redis集群模式:
#在每個(gè)redis 實(shí)例的配置文件中,開啟如下參數(shù); #將 cluster-enabled yes 前面的#去掉,意味著開啟 redis-cluster cluster-enabled yes #將前面的#去掉,并且將配置文件改為 7001.conf,Redis Cluster 記錄的啟動(dòng)信息文件, 文件由 cluster 自動(dòng)生成,不需要用戶編輯 cluster-config-file nodes-7001.conf #Redis 群集節(jié)點(diǎn)可以不可用的最長(zhǎng)時(shí)間,而不會(huì)將其視為失敗。 cluster-node-timeout 5000 #開啟 AOF 持久化 appendonly yes
啟動(dòng)Redis實(shí)例:
#啟動(dòng)實(shí)例服務(wù): /etc/init.d/redis_7001 restart /etc/init.d/redis_7002 restart /etc/init.d/redis_7003 restart /etc/init.d/redis_7004 restart /etc/init.d/redis_7005 restart /etc/init.d/redis_7006 restart #批量啟動(dòng)實(shí)例服務(wù): for i in `seq 1 6`;do /etc/init.d/redis_700$i start;sleep 2;done #查看服務(wù)進(jìn)程: netstat -nutlp ps -ef|grep redis-server
搭建集群:
現(xiàn)在已經(jīng)有了6個(gè)redis實(shí)例了,接下來(lái)可以直接用這些實(shí)例創(chuàng)建集群,并為每個(gè)節(jié)點(diǎn)編寫配置文件, redis5版本可以直接使用redis-cli命令創(chuàng)建集群,檢查或重新新硬化現(xiàn)有集群等等。
#創(chuàng)建集群: redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1 --cluster-replicas 1 表示為集群中的每個(gè)主節(jié)點(diǎn)創(chuàng)建一個(gè)從節(jié)點(diǎn)。 其中指定了6個(gè)redis的ip:port,3個(gè)master3個(gè)slave。 以上命令回車后,當(dāng)我們輸入yes,redis-cli就會(huì)將這份配置應(yīng)用到集群當(dāng)中,讓各個(gè)節(jié)點(diǎn)開始互相通訊, 最 后可以得到如下信息: ... [OK] All 16384 slots covered
系統(tǒng)就開始執(zhí)行 cluster 的相關(guān)配置,最終 哈希槽均被配置完 畢,且配置好每個(gè)主節(jié)點(diǎn)和和從節(jié)點(diǎn)的對(duì)應(yīng)關(guān)系。
執(zhí)行完畢后,所有主機(jī)的 redis 實(shí)例數(shù)據(jù)目錄下,都會(huì)自動(dòng)生成 dump.rdb 和 nodes7000.conf 文件
#查看集群信息或狀態(tài): [root@localhost redis-5.0.7]# redis-cli -p 7001 cluster info
#查看集群節(jié)點(diǎn): [root@localhost redis-5.0.7]# redis-cli -p 7001 cluster nodes
添加新的節(jié)點(diǎn):
# 重新初始化一個(gè)節(jié)點(diǎn):
[root@localhost redis-5.0.7]# ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 7007
Please select the redis config file name [/etc/redis/7007.conf] /usr/local/redis-cluster/7007.conf
Please select the redis log file name [/var/log/redis_7007.log] /usr/local/redis-cluster/7007.log
Please select the data directory for this instance [/var/lib/redis/7007] /usr/local/redis/7007
Please select the redis executable path [/usr/local/redis/bin/redis-server] /usr/local/redis/bin/redis-server
Selected config:
Port : 7000
Config file : /usr/local/redis-cluster/7000.conf
Log file : /usr/local/redis-cluster/7000.log
Data dir : /usr/local/redis/7000
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/7000.conf => /etc/init.d/redis_7007
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost redis-5.0.7]#
#修改配置文件添加如下或開啟:
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly ye
#啟動(dòng)服務(wù):
/etc/init.d/redis_7000 restart
#添加新節(jié)點(diǎn): redis-cli --cluster add-node 127.0.0.1:7000 127.0.0.1:7001
重新分片:
雖然節(jié)點(diǎn)添加成功,但是還沒有分配散列槽,需要重新分片,就是將其他節(jié)點(diǎn)上的部分散列槽移動(dòng)到該 節(jié)點(diǎn)上。
# 可以看到雖然7000新節(jié)點(diǎn)添加成功,但是沒有分配散列槽: [root@localhost redis-5.0.7]# redis-cli --cluster check 127.0.0.1:7001 127.0.0.1:7001 (e8cf1e59...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:7000 (1186fbcb...) -> 0 keys | 0 slots | 0 slaves. 127.0.0.1:7002 (b02a78af...) -> 0 keys | 5462 slots | 1 slaves. 127.0.0.1:7003 (dd351704...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 4 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 127.0.0.1:7001) M: e8cf1e5955caba283e843a57b01f3d5bf04a3f77 127.0.0.1:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 1186fbcb65016b5ce42fa95c77de872b4e9c7e6b 127.0.0.1:7000 slots: (0 slots) master M: b02a78af5e9106cb1100e5feff1d56146c911bb1 127.0.0.1:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: db67bfb7580ee7f6067f381e8ddd92ebb27044bc 127.0.0.1:7004 slots: (0 slots) slave replicates dd3517043af0ae6f936ae820036079101b61b2b4 S: a1e53727be8fdb39485427d5054c70e9964cddbc 127.0.0.1:7006 slots: (0 slots) slave replicates b02a78af5e9106cb1100e5feff1d56146c911bb1 M: dd3517043af0ae6f936ae820036079101b61b2b4 127.0.0.1:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: fd471711e0d771b22e63dad5e4312aff0144b066 127.0.0.1:7005 slots: (0 slots) slave replicates e8cf1e5955caba283e843a57b01f3d5bf04a3f77 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
重新分配,只需指定一個(gè)節(jié)點(diǎn),redis-cli將自動(dòng)找到其他節(jié)點(diǎn):
#重新分配 redis-cli --cluster reshard 127.0.0.1:7001 首先指定分配多少散列槽: # 重新分配多少個(gè)散列槽,這里嘗試重新設(shè)置2048個(gè)散列槽: How many slots do you want to move (from 1 to 16384)? 2048 指定接收哈希槽的節(jié)點(diǎn)ID: # 指定接受的redis節(jié)點(diǎn)ID,注意不是指定ip地址: (7000實(shí)例的ID) What is the receiving node ID? 1186fbcb65016b5ce42fa95c77de872b4e9c7e6b 指定從哪些節(jié)點(diǎn)獲取散列槽: 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 # all: 表示自動(dòng)在所有節(jié)點(diǎn)進(jìn)行分配。 # done: 表示指定節(jié)點(diǎn)結(jié)束,一般用在移除節(jié)點(diǎn)時(shí),指定完Source node #1: 節(jié)點(diǎn)ID,再輸入done, 表示輸入結(jié)束
是否確定分配:yes
在最終確認(rèn)之后,redis-cli開始執(zhí)行重新分配:
重新分片正在進(jìn)行中時(shí),程序不受影響地運(yùn)行。
檢查集群的運(yùn)行狀態(tài):
# 可以看到散列槽已經(jīng)分配了 [root@node5 ~]# redis-cli --cluster check 127.0.0.1:7000 127.0.0.1:7001 [root@localhost redis-5.0.7]# redis-cli -h 127.0.0.1 -p 7000 cluster nodes
免交互重新分片腳本:
可以自動(dòng)執(zhí)行重新分片,而無(wú)需以交互方式手動(dòng)輸入?yún)?shù)。這可以使用如下命令行:
redis-cli reshard <host>:<port> --cluster-from <node-id> --cluster-to <node-id> --cluster-slots <number of slots> --cluster-yes
刪除節(jié)點(diǎn):
刪除節(jié)點(diǎn)與添加節(jié)點(diǎn)類似,就是一定要注意,刪除節(jié)點(diǎn)之前一定要先將數(shù)據(jù)移到其他節(jié)點(diǎn),不能直接刪 除,不過也不用擔(dān)心,即使有數(shù)據(jù),不小心執(zhí)行了刪除節(jié)點(diǎn)指令,也會(huì)報(bào)有數(shù)據(jù)存在,不可以刪除的錯(cuò) 誤。
# 通過重新分片,將散列槽移到其他節(jié)點(diǎn): redis-cli --cluster reshard 127.0.0.1:7000 How many slots do you want to move (from 1 to 16384)? 2047 # 用來(lái)接受散列槽的節(jié)點(diǎn),可以任意指定: What is the receiving node ID? b02a78af5e9106cb1100e5feff1d56146c911bb1 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. # 7000節(jié)點(diǎn)的id(意思是把該節(jié)點(diǎn)的散列槽分配給其他節(jié)點(diǎn)): Source node #1: 1186fbcb65016b5ce42fa95c77de872b4e9c7e6b Source node #2: done # 查看狀態(tài): [root@node5 ~]# redis-cli --cluster check 127.0.0.1:7000 127.0.0.1:7001 (aa04c256...) -> 0 keys | 4779 slots | 1 slaves. 127.0.0.1:7002 (f6838a0e...) -> 0 keys | 4779 slots | 1 slaves. # 已經(jīng)沒有了散列槽: 127.0.0.1:7006 (44124bd9...) -> 0 keys | 0 slots | 0 slaves. 127.0.0.1:7004 (e4f85202...) -> 1 keys | 6826 slots | 1 slaves.
以下刪除節(jié)點(diǎn):
# 確定沒問題了,執(zhí)行下面命令,可以實(shí)現(xiàn)刪除節(jié)點(diǎn): 127.0.0.1:7000 [root@node5 ~]# redis-cli --cluster del-node 127.0.0.1:7000 1186fbcb65016b5ce42fa95c77de872b4e9c7e6b >>> Removing node 1186fbcb65016b5ce42fa95c77de872b4e9c7e6b from cluster 127.0.0.1:7000 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node
總結(jié)
以上是生活随笔為你收集整理的redis cluster集群的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bluetooth LMP介绍
- 下一篇: <基于欧几里德聚类的激光雷达点云分