目錄
- 一、集群簡介
- 1、現狀問題
- 2、集群作用
- 二、集群結構設計
- 1、集群存儲設
- 2、消息通信設計
- 三、Cluster集群三主三從結構搭建
- 1、redis.conf配置文件可配置項
- 2、配置集群
- 3、鏈接集群
- 4、命令客戶端連接集群并使用
- 四、集群擴容
- 1、添加節點
- 2、槽位分配
- 3、添加從節點
- 五、集群縮容
- 1、刪除從節點
- 2、刪除主節點
一、集群簡介
1、現狀問題
- redis提供的服務OPS可以達到10萬/秒,當前業務OPS已經達到20萬/秒
- 內存單機容量達到256G,當前業務需求內存容量1T
2、集群作用
- 分散單臺服務器的訪問壓力,實現負載均衡
- 分散單臺服務器的存儲壓力,實現可擴展性
- 降低單臺服務器宕機帶來的業務災難
二、集群結構設計
1、集群存儲設
- 讓key通過方法CRC161計算一個hash值,再繼續取余%16384,得出在Redis中存儲的位置。
- 將所有的存儲空間計算切割16384個槽,每臺主機保存一部分。
- 這個槽代表一個存儲空間,不是一個key的保存空間。
- 將key按照計算出的計算放到對應的存儲空間。
當增加一臺計算機,內部就會優化,其余redis把自身一些槽,分給新加的節點。如果去機器把原來的槽再返回給原來的節點。這樣可擴展性就得到增強。
2、消息通信設計
- 各個數據庫互相通信,保存各個庫中槽的編號數據
- 一次命中,直接返回。
- 一次未命中,告知具體位置,直接去下一個庫中尋找,命中返回。
這樣最多兩次命中,就可以找到數據。
三、Cluster集群三主三從結構搭建
1、redis.conf配置文件可配置項
- 打開 cluster 集群配置
cluster-enabled yes|no
- cluster配置文件名,該文件屬于自動生成,僅用于快速查找文件并查詢文件內容
cluster-config-file <filename>
- 節點服務響應超時時間,用于判定該節點是否下線或切換為從節點
cluster-node-timeout <milliseconds>
- master連接的slave最小數量
cluster-migration-barrier <count>
2、配置集群
注意redis5.0.5以后,就不需要安裝ruby了
- 清除redis中data目錄所有數據
rm -rf ./data
- 新建
redis-端口號.conf
文件
port 端口號
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
# 打開 cluster集群配置
cluster-enabled yes# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
# cluster集群配置文件
cluster-config-file nodes-端口號.conf# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
# 當前這個節點超時時,多少秒后反饋信息
cluster-node-timeout 10000
復制六份這樣的文件,每個配置文件的參數改成端口號
-
啟動六個cluster節點
/usr/local/bin/redis-server /opt/module/redis-5.0.5/conf/redis-端口號.conf
3、鏈接集群
進入到一個redis,bin目錄下執行命令
./redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
如果集群有密碼:./redis-cli 后面跟 -a password
–cluster-replicas 1 表示 一個master后有幾個slave,redis集群自行分配
注意 ip一定不能用127.0.0.1 不然外部無法重定向訪問集群 cluster-replicas后面的1表示一個主機有幾個從機 因為現在只有一個因此是1 工作中是2
4、命令客戶端連接集群并使用
- 注意:-c 表示是以redis集群方式進行連接
./redis-cli -a 123456 -h 127.0.0.1 -p 6379 -c
- 查看集群狀態
cluster info
- 查看集群中的節點
cluster nodes
- 查看幫助
cluster help
- 新增master節點
cluster meet ip:port
- 忽略一個沒有solt的節點
cluster forget id
- 手動故障轉移
cluster failover
- 進入從節點redis,切換其主節點
cluster replicate masterID
- 當存放數據存,放的key:001根據crc16算法算出值然后對16384取余算出的值恰好落在分配到7001的槽中,所以就存放到7001中。我們去7002中進行獲取的時候。會重定向到7001中進行獲取。
四、集群擴容
新加入的節點都是master,并且不會分配任何slot槽位,我們要手動為新節點分配hash槽
1、添加節點
- 啟動6385節點 進入bin目錄進行啟動
./redis-server redis-6385.conf
- 申請加入集群
./redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6379
- 命令解釋:添加節點 6385 去meet6379申請加入集群
- 啟動客戶端并查看節點信息:
redis-cli -a 123456 -h 127.0.0.1 -p 6379 -c
cluster nodes
2、槽位分配
使用redis-cli --cluster reshard
命令為新加入的節點分配槽位,需要使用集群中任意一個master節點對其進行重新分片
redis-cli -a 123456 --cluster reshard 127.0.0.1:6385
How many slots do you want to move (from 1 to 16384)? 600
(ps:需要多少個槽移動到新的節點上,自己設置,比如600個hash槽)
What is the receiving node ID? 2728a594a0498e98e4b83a537e19f9a0a3790f38
(ps:把這600個hash槽移動到哪個節點上去,需要指定節點id,可通過 cluster nodes 查看當前節點id)
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(ps:輸入all為從所有主節點(8001,8002,8003)中分別抽取相應的槽數指定到新節點中,抽取的總槽數為600個)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:輸入yes確認開始執行分片任務)
3、添加從節點
-
添加節點(這時候的節點是master節點,并不會分配槽位)
redis-cli -a 123456 --cluster add-node 192.168.0.61:6386 192.168.0.61:6379
-
先登錄從節點,然后在replicate命令中指定主節點的id
redis-cli -a 123456-c -h 192.168.0.61 -p 6379
-
將當前節點分配給指定的master節點作為slave節點
cluster replicate 2728a594a0498e98e4b83a537e19f9a0a3790f38 *#后面這串id為要添加slave節點的master的節點id*
五、集群縮容
接下來將上面新增加的兩個節點刪除
1、刪除從節點
- 用
redis-cli --cluster del-node
刪除從節點6386,指定刪除節點ip和端口,以及節點id- redis-cli -a hs --cluster del-node 127.0.0.1:6386 a1cfe35722d151cf70585cee21275565393c0956
2、刪除主節點
主節點的里面是有分配了hash槽的,所以我們這里必須先把6385里的hash槽放入到其他的可用主節點中去,然后再進行移除節點操作,不然會出現數據丟失問題
只能把master的數據遷移到一個節點上,暫時做不了平均分配功能
- 任選一個主節點進行重新分片*
redis-cli -a 123456 --cluster reshard 127.0.0.1:6385
-
How many slots do you want to move (from 1 to 16384)? 600 (ps:需要多少個槽移動到新的節點上) What is the receiving node ID? 2728a594a0498e98e4b83a537e19f9a0a3790f38 (ps:把這600個hash槽移動到哪個節點上去,這里使用6379的主節點id) 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:2728a594a0498e98e4b83a537e19f9a0a3790f38 (ps:這里是需要數據源,也就是我們的6385節點id。這里這次就不寫all了) Source node 2:done (ps:這里直接輸入done 開始生成遷移計劃) Do you want to proceed with the proposed reshard plan (yes/no)? yes (ps:這里輸入yes開始遷移)
- 查看節點信息,發現沒有插槽數了
cluster info
- 開始刪除節點
這次就不寫all了)
Source node 2:done
(ps:這里直接輸入done 開始生成遷移計劃)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
(ps:這里輸入yes開始遷移) - 查看節點信息,發現沒有插槽數了
cluster info
- 開始刪除節點
redis-cli -a 123456 --cluster del-node 192.168.0.61:6385 2728a594a0498e98e4b83a537e19f9a0a3790f38