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

      Redis-Cluster

redis使用中遇到的瓶頸

  咱們平常在對於redis的使用中,常常會遇到一些問題node

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

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

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

 

redis-cluster的優點  

  一、官方推薦,毋庸置疑。併發

  二、去中心化,集羣最大可增長1000個節點,性能隨節點增長而線性擴展。app

  三、管理方便,後續可自行增長或摘除節點,移動分槽等等。less

  四、簡單,易上手。dom

 

redis-cluster名詞介紹

  一、master  主節點、tcp

  二、slave   從節點性能

  三、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

謝土豪

相關文章
相關標籤/搜索