Redis集群官方推薦方案 Redis-Cluster


      Redis-Cluster

redis使用中遇到的瓶頸

  我們日常在對於redis的使用中,經常會遇到一些問題

  1、高可用問題,如何保證redis的持續高可用性。

  2、容量問題,單實例redis內存無法無限擴充,達到32G后就進入了64位世界,性能下降。

  3、並發性能問題,redis號稱單實例10萬並發,但也是有盡頭的。

 

redis-cluster的優勢  

  1、官方推薦,毋庸置疑。

  2、去中心化,集群最大可增加1000個節點,性能隨節點增加而線性擴展。

  3、管理方便,后續可自行增加或摘除節點,移動分槽等等。

  4、簡單,易上手。

 

redis-cluster名詞介紹

  1、master  主節點、

  2、slave   從節點

  3、slot    槽,一共有16384數據分槽,分布在集群的所有主節點中。

 

redis-cluster簡介

 

redis-cluster

 

 

圖中描述的是六個redis實例構成的集群

6379端口為客戶端通訊端口

16379端口為集群總線端口

集群內部划分為16384個數據分槽,分布在三個主redis中。

從redis中沒有分槽,不會參與集群投票,也不會幫忙加快讀取數據,僅僅作為主機的備份。

三個主節點中平均分布着16384數據分槽的三分之一,每個節點中不會存有有重復數據,僅僅有自己的從機幫忙冗余。

 

集群部署

測試部署方式,一台測試機多實例啟動部署。

安裝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默認配置
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

#################自定義配置
#系統配置
#vim /etc/sysctl.conf
#vm.overcommit_memory = 1

aof-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主間隔默認10秒
#復制超時時間
#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應為6個不同的修改過的多實例配置文件。 

注意,配置文件復制六分后,有許多需要你修改的地方。

 

 

創建redis-cluster

redis-trib.rb命令與redis-cli命令放置在同一個目錄中,可全路徑執行或者創建別名。

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
注:這里分片設置為了0
啟動丟失的那一台后既恢復。數據不會丟失。
 
 
 
 

移動槽

redis-trib.rb reshard 127.0.0.1:6360
執行集群reshard操作,通過集群中127.0.0.1:6360這一台機器
 
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 2731
輸入需要移動的槽數量
 
What is the receiving node ID? 21c93aa709e10f7a9064faa04539b3ecd
輸入接收的節點的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
輸入所有被移動的節點ID,確認后輸入done
 
    Moving slot 5460 from 0ddb4e430dda8778ac873dd169951c7d71b8235e
    Moving slot 13653 from 0ddb4e430dda8778ac873dd169951c7d71b8235e
Do you want to proceed with the proposed reshard plan (yes/no)?
檢查后輸入yes進行移動分槽
至此,分槽移動完畢。
 
 

刪除節點

 
redis-trib.rb del-node 127.0.0.1:6360 'f24c0c1ecf629b5413cbca632d389efcad7c8346'
最后跟着的是這個節點的ID,可在redis-cli終端中使用CLUSTER NODES查看
必要條件,此節點所有分槽均已移除。
 
 

添加master節點

redis-trib.rb add-node 127.0.0.1:6360 127.0.0.1:6350
新節點必須是空的,不能包含任何數據。請把之前aof和dump文件刪掉,並且若有nodes.conf也需要刪除。
add-node  將一個節點添加到集群里面, 第一個是新節點ip:port, 第二個是任意一個已存在節點ip:port
node:新節點沒有包含任何數據, 因為它沒有包含任何slot。新加入的加點是一個主節點, 當集群需要將某個從節點升級為新的主節點時, 這個新節點不會被選中,同時新的主節點因為沒有包含任何slot,不參加選舉和failover。
 
 

添加一個從節點

前三步操作同添加master一樣
第四步:redis-cli連接上新節點shell,輸入命令:cluster replicate 對應master的node-id

 

注:

安裝部署部分不是無腦復制即可,請結合你的主機情況進行操作,若有問題可以聯系我  QQ:2169866431

謝土豪

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM