redis集羣演變過程
單機版
核心技術:持久化
持久化是最簡單的高可用方法,主要作用是數據備份,即將數據存儲在硬盤,保證數據不會因進程退出而丟失。
主從複製
複製是高可用redis的基礎,哨兵和集羣都是在複製基礎上實現高可用,複製主要實現了數據的多機備份,以及對於讀操作的負載均衡和簡單的故障恢復,缺陷是故障恢復無法自動化;寫操作無法負載均衡;存儲能力受到單機的限制;
哨兵
在複製的基礎上,哨兵實現了自動化的故障恢復,缺陷是寫操作無法負載均衡;存儲能力受到單機的限制。
集羣
通過集羣,redis解決了寫操作無法負載均衡,以及存儲能力受到單機限制的問題,實現了較爲完善的高可用方案。
什麼是redis cluster集羣
redis cluster是一個由多個朱從節點羣組成的分佈式服務器羣,具有複製、高可用和分片特性。redis cluster不需要sentinel哨兵也能完成節點移除和故障轉移的功能。需要將每個節點設置成集羣模式,這種集羣模式沒有中心節點,可以水平擴展。redis cluster集羣的性能和高可用性均優於哨兵模式,而且集羣配置非常簡單。
redis cluster集羣搭建
下載安裝
# 下redis5
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar -zxvf redis-4.0.8.tar.gz
mv redis-4.0.8 redis
cd redis/
make
cd src
make install prefix=/usr/local/redis
配置
cd /usr/local/etc
# 集羣搭建六個節點
mkdir redis7000 redis7001 redis7002 redis7003 redis7004 redis7005
cp /root/Desktop/installpack/redis/redis.conf /usr/local/etc/redis7000
要修改的配置
#綁定本機ip
69 bind 本機ip
#指定該節點端口號
92 port 7000
#以後臺方式啓動
137 daemonize yes
159 pidfile /var/run/redis_7000.pid
172 logfile "/usr/local/etc/redis7000/redis.log"
264 dir /usr/local/etc/redis7000
#設置集羣連接主節點的密碼
289 masterauth 123456
501 requirepass 123456
# 打開集羣開關,以集羣方式運行
815 cluster-enabled yes
823 cluster-config-file nodes-7000.conf
# 當主節點宕機,在沒有從節點接替的情況下,整個集羣是否可以正常運行,如果配置成no 則當沒有相應從節點進行故障恢復時仍然可用
906 cluster-require-full-coverage yes
批量替換複製
[root@localhost etc]# sed 's/7000/7001/g' redis7000/redis.conf > redis7001/redis.conf
[root@localhost etc]# sed 's/7000/7002/g' redis7000/redis.conf > redis7002/redis.conf
[root@localhost etc]# sed 's/7000/7003/g' redis7000/redis.conf > redis7003/redis.conf
[root@localhost etc]# sed 's/7000/7004/g' redis7000/redis.conf > redis7004/redis.conf
[root@localhost etc]# sed 's/7000/7005/g' redis7000/redis.conf > redis7005/redis.conf
redis配置文件中文註釋:
鏈接:https://pan.baidu.com/s/19OdEKKlF7zrbmzvlH62ODg
提取碼:gh24
啓動集羣
/usr/local/bin/redis-server /usr/local/etc/redis7005/redis.conf
客戶端連接
插入一條數據,報錯,沒有分配集羣槽位。
分配槽位,指定主從節點
meet
讓各個節點之間能夠互相通信;
cluster meet ip port
指派槽
# 查看crc16算法算出的key的槽位命令
cluster keyslot key
cluster addslots slot 槽位下標
分配主從
> cluster nodes
6e5bfe8f68cd4a543283f5a94bc206e061c66d9a :7000@17000 myself,master - 0 0 0 connected
> cluster replicate node-id
也可以直接用一條命令完成指定:
# redis3
./redis-trib.rb create --replicas 1 192.168.1.8:7000 192.168.1.8:7001 192.168.1.8:7002 192.168.1.8:7003 192.168.1.8:7004 192.168.1.8:7005
/usr/local/bin/redis-cli --cluster create 192.168.1.8:7000 192.168.1.8:7001 192.168.1.8:7002 192.168.1.8:7003 192.168.1.8:7004 192.168.1.8:7005 --cluster-replicas 1
--replicas 1
表示將六個節點按1:1劃分,即三個主節點,三個從節點。
驗證集羣
客戶端連接集羣
/usr/local/bin/redis-cli -c -h 192.168.1.8 -p 7000 -a 123456
查看集羣信息
查看節點列表
查看幫助手冊
/usr/local/bin/redis-cli -c -h 192.168.1.8 -p 7000 -a 123456 --cluster help
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
集羣擴容
添加主節點
# 192.168.1.8:7006 新節點
# 192.168.1.8:7003 已經存在的節點
/usr/local/bin/redis-cli --cluster add-node 192.168.1.8:7006 192.168.1.8:7003 -a 123456
添加從節點
/usr/local/bin/redis-cli --cluster add-node 192.168.1.8:7007 192.168.1.8:7001 --cluster-slave --cluster-master-id 主節點node_id -a 123456
給新添加的主節點7006分配槽位
/usr/local/bin/redis-cli --cluster reshard 192.168.1.8:7006 -a 123456
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 分配多少個槽位
How many slots do you want to move (from 1 to 16384)? 300
# 分配給哪個主節點
What is the receiving node ID? e7876ab13bde2ea87be3ef3bf2ed9bb89328044f
# all表示在已經存在的主節點中平均分配
# 如果想從指定主節點中分配,則輸入已經存在節點的node_id
# 指定完成後輸入done表示完成
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:
查看分配結果
集羣縮容
去除槽位
redis-cli --cluster reshard 已經存在節點ip:port --cluster-from 遷出節點id --cluster-to 遷入節點id --cluster-slot 300 -a 123456
刪除節點
先刪除從節點,再刪除主節點
redis-cli --cluster del-node host:port node_id
關閉集羣
# 需要一個一個執行
/usr/local/redis/bin/redis-cli -c -h 127.0.0.1 -p 7000 shutdown