redis3.0集羣搭建

Redis集羣搭建

redis cluster介紹

節點自動發現、集羣容錯slave選舉、Cluster管理、集羣配置管理。

集羣中的每個Redis節點需要2個TCP連接端口,如6379端口用於Client連接,16379端口用於集羣數據通信

集羣採用Hash Slot方案,而不是一致性哈希,共16384個Hashslot。如果有3臺機器,那麼NodeA在0-5500,NodeB 在5501-11000,NodeC在11001-16384.這種設計下,添加,刪除新Node比較方便。

由於HashSlot在節點間的遷移無需停止操作,集羣新增或者刪除節點,改變集羣內部節點佔用的Slot比例等都可在線完成。

工作方式:

       內部使用二進制協議優化傳輸速度和帶寬,所有的redis節點彼此互聯(PING-PONG機制)

       集羣中超過半數的節點檢測失效時集羣進入fail狀態。

       客戶端不需要連接集羣所有節點,連接集羣中任何一個可用節點即可

狀態和選舉:

    (1)領着選舉過程是集羣中所有master參與,如果半數以上master節點與master節點通信超過(cluster-node-timeout),認爲當前master節點掛掉.

    (2):什麼時候整個集羣不可用(cluster_state:fail),當集羣不可用時,所有對集羣的操作做都不可用,收到((error)CLUSTERDOWN The cluster is down)錯誤

如果集羣任意master掛掉,且當前master沒有slave.集羣進入fail狀態,也可以理解成進羣的slot映射[0-16383]不完成時進入fail狀態.

如果進羣超過半數以上master掛掉,無論是否有slave集羣進入fail狀態.

安裝

安裝依賴 ruby

yum install ruby rubygems

安裝redis cluster

tar zxvf redis-3.2.3.tar.gz

cd redis-3.2.3

make

cp redis-3.0.5/src/redis-trib.rb /bin/

cp redis-3.0.5/src/redis-server /bin/

cp redis-3.0.5/src/redis-cli    /bin/


配置文件

vim  /etc/redis_6380.conf
daemonize yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
maxmemory 10gb
databases 16
dir /data/redis/6380
slave-serve-stale-data yes
loglevel notice
logfile "/data/redis/6380/redis_6380.log"
#slave只讀
slave-read-only yes
#not use default
repl-disable-tcp-nodelay yes
slave-priority 100
appendonly yes
#打開aof持久化
appendfsync everysec
#每秒一次aof寫
no-appendfsync-on-rewrite yes
#關閉在aof rewrite的時候對新的寫操作進行fsync
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
cluster-enabled yes
#打開redis集羣
cluster-config-file /data/redis/6380/nodes-6380.conf
cluster-node-timeout 15000
#節點互連超時的閥值(單位毫秒)
cluster-migration-barrier 1
#一個主節點在擁有多少個好的從節點的時候就要割讓一個從節點出來給其他沒有從節點或者從節點掛掉的主節點
cluster-require-full-coverage no
#如果某一些key space沒有被集羣中任何節點覆蓋,最常見的就是一個node掛掉,集羣將停止接受寫入
auto-aof-rewrite-percentage 80-100
#部署在同一機器的redis實例,把auto-aof-rewrite搓開,防止瞬間fork所有redis進程做rewrite,佔用大量內存
slowlog-log-slower-than 10000
slowlog-max-len 128
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
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
aof-rewrite-incremental-fsync yes


啓動服務

redis-server /etc/redis_6380.conf

redis-server /etc/redis_6381.conf

集羣配置參數

所有的集羣配置參數都存在於redis.conf中,主要幾個如下:

Cluster-enabled    :是否開啓集羣模式

Cluster-config-file  :集羣配置變更後會自動寫入改文件

Cluster-node-timeout :節點超時時間,超過該時間無法連接主要Master節點後,會停止接受查詢服務

Cluster-slave-validity-factor   :控制從節點FailOver相關的設置

設爲0,從節點會一直嘗試啓動FailOver.

設爲正數,失聯大於一定時間(factor*節點TimeOut),不再進行FailOver

Cluster-migration-barrier   :最小從節點連接數

Cluster-require-full-coverage  :默認爲Yes,丟失一定比例Key後(可能Node無法連接或者掛掉),集羣停止接受寫操作

設置爲No,集羣丟失Key的情況下仍提供查詢服務


創建集羣

/usr/local/redis-3.2.3/bin/redis-trib.rbcreate --replicas 1 192.168.0.131:6380 192.168.0.132:6380 192.168.0.154:6380192.168.0.154:6381 192.168.0.132:6381 192.168.0.131:6381

wKioL1h8dVqzBJ4kAABcni4_4X8508.png-wh_50

輸入yes


查看集羣狀態

redis-trib.rb check192.168.0.131:6380      //集羣任一節點

wKiom1h8dXegX2haAABYPf6yGh8528.png-wh_50

其他

添加節點

redis-trib.rb add-node 新節點   舊節點(集羣任意節點)

添加節點爲指點節點的從節點

redis-trib.rb add-node --slave--master-id '304f069a63299b……(master節點id)'  新節點    127.0.0.1:6380(集羣任意節點)

重新分配slot

redis-trib.rb reshard192.168.10.219:6378 //下面是主要過程

How many slots do you want to move(from 1 to 16384)? 1000 //設置slot1000  

What is the receiving node ID?03ccad2ba5dd1e062464bc7590400441fafb63f2 //新節點

node id Please enter all the sourcenode IDs.  

Type 'all' to use all the nodes assource nodes for the hash slots.

Type 'done' once you entered all thesource nodes IDs.  

Source node #1:all //表示全部節點重新洗牌

Do you want to proceed with the proposedreshard plan (yes/no)? yes //確認重新分

刪除節點

1,刪除從節點  
# redis-trib.rb del-node 192.168.0.131:6381 '304f069a63299bf4b20d2f018a3b2c3bba650a53'  
2,刪除主節點
如果主節點有從節點,將從節點轉移到其他主節點
如果主節點有slot,去掉分配的slot,然後在刪除主節點
redis-trib.rb reshard192.168.0.131:6381 //取消分配的slot,下面是主要過程  


How many slots do you want to move (from 1to 16384)? 1000 //被刪除master的所有slot數量  
What is the receiving node ID?304f069a63299bf4b20d2f018a3b2c3bba650a53 //接收6381節點slot的master  
Please enter all the source node IDs.  
Type 'all' to use all the nodes as sourcenodes for the hash slots.  
Type 'done' once you entered all the sourcenodes IDs.  
Source node#1:304f069a63299bf4b20d2f018a3b2c3bba650a53 //被刪除master的node-id  
Source node #2:done   
  Doyou want to proceed with the proposed reshard plan (yes/no)? yes //取消slot後,reshard 
新增master節點後,也進行了這一步操作,當時是分配,現在去掉。反着的。
# redis-trib.rb del-node192.168.0.131:6381 '304f069a63299bf4b20d2f018a3b2c3bba650a53'


參考鏈接:redis知識庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章