Redis集群官方推荐方案 Redis-Cluster
Redis-Cluster
redis使用中遇到的瓶頸
我們?nèi)粘T趯τ趓edis的使用中,經(jīng)常會遇到一些問題
1、高可用問題,如何保證redis的持續(xù)高可用性。
2、容量問題,單實例redis內(nèi)存無法無限擴充,達到32G后就進入了64位世界,性能下降。
3、并發(fā)性能問題,redis號稱單實例10萬并發(fā),但也是有盡頭的。
?
redis-cluster的優(yōu)勢
1、官方推薦,毋庸置疑。
2、去中心化,集群最大可增加1000個節(jié)點,性能隨節(jié)點增加而線性擴展。
3、管理方便,后續(xù)可自行增加或摘除節(jié)點,移動分槽等等。
4、簡單,易上手。
?
redis-cluster名詞介紹
1、master 主節(jié)點、
2、slave 從節(jié)點
3、slot 槽,一共有16384數(shù)據(jù)分槽,分布在集群的所有主節(jié)點中。
?
redis-cluster簡介
?
?
?
圖中描述的是六個redis實例構(gòu)成的集群
6379端口為客戶端通訊端口
16379端口為集群總線端口
集群內(nèi)部劃分為16384個數(shù)據(jù)分槽,分布在三個主redis中。
從redis中沒有分槽,不會參與集群投票,也不會幫忙加快讀取數(shù)據(jù),僅僅作為主機的備份。
三個主節(jié)點中平均分布著16384數(shù)據(jù)分槽的三分之一,每個節(jié)點中不會存有有重復(fù)數(shù)據(jù),僅僅有自己的從機幫忙冗余。
?
集群部署
測試部署方式,一臺測試機多實例啟動部署。
安裝redis
$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz $ tar xzf redis-3.2.8.tar.gz $ cd redis-3.2.8 $ make
修改配置文件 redis.conf
#redis.conf默認(rèn)配置 daemonize yes pidfile /var/run/redis/redis.pid #多實例情況下需修改,例如redis_6380.pid port 6379 #多實例情況下需要修改,例如6380 tcp-backlog 511 bind 0.0.0.0 timeout 0 tcp-keepalive 0 loglevel notice logfile /var/log/redis/redis.log #多實例情況下需要修改,例如6380.log databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb #多實例情況下需要修改,例如dump.6380.rdb slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes appendfilename "appendonly.aof" #多實例情況下需要修改,例如 appendonly_6380.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10#################自定義配置 #系統(tǒng)配置 #vim /etc/sysctl.conf #vm.overcommit_memory = 1aof-rewrite-incremental-fsync yes maxmemory 4096mb maxmemory-policy allkeys-lru dir /opt/redis/data #多實例情況下需要修改,例如/data/6380#集群配置 cluster-enabled yes cluster-config-file /opt/redis/6380/nodes.conf #多實例情況下需要修改,例如/6380/ cluster-node-timeout 5000#從ping主間隔默認(rèn)10秒 #復(fù)制超時時間 #repl-timeout 60#遠距離主從 #config set client-output-buffer-limit "slave 536870912 536870912 0" #config set repl-backlog-size 209715200
啟動六個實例:
/編譯安裝目錄/src/redis-server redis.conf
注意,redis.conf應(yīng)為6個不同的修改過的多實例配置文件。?
注意,配置文件復(fù)制六分后,有許多需要你修改的地方。
?
?
創(chuàng)建redis-cluster
redis-trib.rb命令與redis-cli命令放置在同一個目錄中,可全路徑執(zhí)行或者創(chuàng)建別名。
redis-trib.rb create --replicas 0 127.0.0.1:6310 127.0.0.1:6320 127.0.0.1:6330 127.0.0.1:6340 127.0.0.1:6350 127.0.0.1:6360 只要缺失了任意一部分的槽,redis-cluster便無法讀取。 測試強行停機一臺,既顯示: 127.0.0.1:6310> get key (error) CLUSTERDOWN The cluster is down 注:這里分片設(shè)置為了0 啟動丟失的那一臺后既恢復(fù)。數(shù)據(jù)不會丟失。移動槽
redis-trib.rb reshard 127.0.0.1:6360 執(zhí)行集群reshard操作,通過集群中127.0.0.1:6360這一臺機器 [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 2731 輸入需要移動的槽數(shù)量 What is the receiving node ID? 21c93aa709e10f7a9064faa04539b3ecd 輸入接收的節(jié)點的ID How many slots do you want to move (from 1 to 16384)? 2731 What is the receiving node ID? 0abf4ca21c93aa709e10f7a9064faa04539b3ecd 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:0ddb4e430dda8778ac873dd169951c7d71b8235e Source node #2:done 輸入所有被移動的節(jié)點ID,確認(rèn)后輸入done ??? Moving slot 5460 from 0ddb4e430dda8778ac873dd169951c7d71b8235e ??? Moving slot 13653 from 0ddb4e430dda8778ac873dd169951c7d71b8235e Do you want to proceed with the proposed reshard plan (yes/no)? 檢查后輸入yes進行移動分槽 至此,分槽移動完畢。刪除節(jié)點
redis-trib.rb del-node 127.0.0.1:6360 'f24c0c1ecf629b5413cbca632d389efcad7c8346' 最后跟著的是這個節(jié)點的ID,可在redis-cli終端中使用CLUSTER NODES查看 必要條件,此節(jié)點所有分槽均已移除。添加master節(jié)點
redis-trib.rb add-node 127.0.0.1:6360 127.0.0.1:6350 新節(jié)點必須是空的,不能包含任何數(shù)據(jù)。請把之前aof和dump文件刪掉,并且若有nodes.conf也需要刪除。 add-node? 將一個節(jié)點添加到集群里面, 第一個是新節(jié)點ip:port, 第二個是任意一個已存在節(jié)點ip:port node:新節(jié)點沒有包含任何數(shù)據(jù), 因為它沒有包含任何slot。新加入的加點是一個主節(jié)點, 當(dāng)集群需要將某個從節(jié)點升級為新的主節(jié)點時, 這個新節(jié)點不會被選中,同時新的主節(jié)點因為沒有包含任何slot,不參加選舉和failover。添加一個從節(jié)點
前三步操作同添加master一樣 第四步:redis-cli連接上新節(jié)點shell,輸入命令:cluster replicate 對應(yīng)master的node-id?
注:
安裝部署部分不是無腦復(fù)制即可,請結(jié)合你的主機情況進行操作,若有問題可以聯(lián)系我 ?QQ:2169866431
謝土豪
?
轉(zhuǎn)載于:https://www.cnblogs.com/kerwinC/p/6611634.html
總結(jié)
以上是生活随笔為你收集整理的Redis集群官方推荐方案 Redis-Cluster的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java面试题】54 去掉一个Ve
- 下一篇: css 层叠式样式表(2)