redis-Cluster——主從復制
redis詳解
文章目錄
-
redis-Cluster——主從復制 - 1. 單節點Redis服務器帶來的問題
- 1.1 解決方法
- 2. Redis集群介紹
- 3. Redis集群的優勢
- 4. Redis集群的實作方法
- 5. Redis-Cluster資料分片
- 6. Redis-Cluster的主從復制模型
- 7.實驗: redis—Cluster 主從復制
- 7.1 實驗環境
- 7.2實驗描述
- 7.3 實驗步驟
- 7.3.1 所有節點服務器部署redis服務
- 7.3.2 master0(192.168.75.134)節點配置
- 7.3.3 哈希槽的分布
- 7.3.4 三組節點群集情況:
- 7.3.5 查看集群slave、slot、key分布資訊情況
- 7.3.6 redis擴容命令
- 八、 群集查詢陳述句
1. 單節點Redis服務器帶來的問題
- 單點故障,服務不可用
- 無法處理大量的并發資料請求
- 資料丟失—大災難
1.1 解決方法
- 搭建Redis集群
2. Redis集群介紹
- Redis集群是一個提供在多個Redis間節點間共享資料的程式集
- Redis集群并不支持處理多個keys的命令,因為這需要在不同的節點間移動資料,從而達不到像Redis那樣的性能,在高負載的情況下可能會導致不可預料的錯誤
- Redis集群通過磁區來提供一定程度的可用性,在實際環境中當某個節點宕機或者不可達的情況下可繼續處理命令
3. Redis集群的優勢
- 自動分割資料到不同的節點上
- 整個集群的部分節點失敗或者不可達的情況下能夠繼續處理命令
4. Redis集群的實作方法
- 有客戶端分片
- 代理分片
- 服務器端分片
5. Redis-Cluster資料分片
- Redis 集群沒有使用一致性hash, 而是引入了 哈希槽概念
- Redis 集群有16384個哈希槽
- 每個key通過CRC16校驗后對16384取模來決定放置槽
- 集群的每個節點負責一部分哈希槽
- 以3個節點組成的集群為例
- 節點 A 包含 0 到 5500號哈希槽
- 節點 B 包含5501 到 11000 號哈希槽
- 節點 C 包含11001 到 16384號哈希槽
支持添加或者洗掉節點
添加洗掉節點無需停止服務
例如
- 如果想新添加個節點D, 需要移動節點 A, B, C中的部分槽到D上
- 如果想移除節點A,需要將A中的槽移到B和C節點上,再將沒有任何槽的A節點從集群中移除
6. Redis-Cluster的主從復制模型
- 集群中具有A,B,C三個節點,如果節點B失敗了,整個集群就會因缺少5501-11000這個范圍的槽而不可用;
- 為每個節點添加一個從節點A1,B1,C1,整個集群便有三個master節點和三個slave節點組成,在節點B失敗后,集群便會選舉B1為新的主節點繼續服務 當B和B1都失敗后,集群將不可用;
7.實驗: redis—Cluster 主從復制
7.1 實驗環境
- 組件六臺虛擬機:3個master節點3個slave節點
- 主——master1:192.168.75.134 (需要安裝ruby)
- master2:192.168.75.155
- master3:192.168.75.200
- slave1:192.168.75.144
- slave2:192.168.75.131
- slave3:192.168.75.132
7.2實驗描述
- 設定網路引數、關閉防火墻和selinux(所有節點)
- 下載并安裝Redis(所有節點)
- 修改Redis組態檔(所有節點)
- 創建Redis集群(master1節點)
- 匯入key檔案并安裝rvm
- 執行環境變數讓其生效
- 安裝Ruby2.4.1版本
- 安裝redis客戶端
- 創建redis集群
- 所有節點部署Redis服務器
7.3 實驗步驟
1、創建集群:redis-cli --cluster create --cluster-replicas 1 節點IP地址1:埠… 節點IP地址n:埠
2、遠程登錄redis資料庫:redis-cli -h 192.168.233.100 -p 6379 -c
-c:連接集群結點時使用,此選項可防止moved和ask例外
3、查看集群資訊:可登陸redis資料庫中,輸入cluster info查詢集群狀態,也可使用命令 redis-cli --cluster info 節點ip:6379
7.3.1 所有節點服務器部署redis服務
yum -y install gcc gcc-c++ make
tar zxvf redis-5.0.7.tar.gz
cd redis-5.0.7/
make
make PREFIX=/usr/local/redis install
cd /usr/local/redis/
ln -s /usr/local/redis/bin/* /usr/local/bin/
cd /opt/redis-5.0.7/utils/
./install_server.sh
一路回車
Please select the executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server //手動輸入
/etc/init.d/redis_6379 status
/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 start
vim /etc/6379.conf
redis-cli //登錄
修改組態檔所有節點
vim /etc/redis/6379.conf
89/ protected-mode no //關閉保護模式
93/ port 6379
137/ daemonize yes /以獨立行程啟動
833/ cluster-enabled yes //開啟群集功能
841/ cluster-config-file nodes-6379.conf // 群集名稱檔案設定
847/ cluster-node-timeout 15000 //群集超時時間設定
700/ appendonly yes //開啟aof 持久化
#bind 127.0.0.1 //注釋掉,允許所有節點訪問
/etc/init.d/redis_6379 restart
7.3.2 master0(192.168.75.134)節點配置
- ruby服務并且使用 --cluster create建立群集關系
匯入key檔案
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
安裝rvm
curl -sSL https://get.rvm.io | bash -s stable不翻墻無法連接下載解決方向,用下面腳本檔案執行
將rvm-installer.sh 放到虛擬機中
chmod +x rvm-installer.sh
./rvm-installer.sh //執行腳本
source /etc/profile.d/rvm.sh //執行環節變數
rvm list known //列出可執行ruby安裝的版本
rvm install 2.4.10 //安裝rvm
rvm use 2.4.10 //使用rvm
ruby -v //查看版本
gem install redis //再次安裝Redis
所有節點關閉防火墻和核心防護
iptables -F
setenforce 0
建立Redis群集6組,三對,主從
redis-cli --cluster create 192.168.75.134:6379 \
192.168.75.155:6379 \
192.168.75.200:6379 \
192.168.75.144:6379 \
192.168.75.131:6379 \
192.168.75.132:6379 \
--cluster-replicas 1
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.75.131:6379 to 192.168.75.134:6379
Adding replica 192.168.75.132:6379 to 192.168.75.155:6379
Adding replica 192.168.75.144:6379 to 192.168.75.200:6379
M: 2545e996e6a5bc9bd89498eab753195896c6f683 192.168.75.134:6379
slots:[0-5460] (5461 slots) master
M: 0c6b70fd0e8fde5cd9a17234cacdec0f9745f8e5 192.168.75.155:6379
slots:[5461-10922] (5462 slots) master
M: ed9b3be385920f7d1bd20bd0c58ef2b1c7448edf 192.168.75.200:6379
slots:[10923-16383] (5461 slots) master
S: b58e8073edc1e72d15f44a2fcc8f228b64aa383d 192.168.75.144:6379
replicates ed9b3be385920f7d1bd20bd0c58ef2b1c7448edf
S: 3d358490f7bdd68bd876e9dee0d03eb830ef2321 192.168.75.131:6379
replicates 2545e996e6a5bc9bd89498eab753195896c6f683
S: 3b633f51937b54c5786b538b3f69faed5675d8ce 192.168.75.132:6379
replicates 0c6b70fd0e8fde5cd9a17234cacdec0f9745f8e5
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 192.168.75.134:6379)
M: 2545e996e6a5bc9bd89498eab753195896c6f683 192.168.75.134:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 3b633f51937b54c5786b538b3f69faed5675d8ce 192.168.75.132:6379
slots: (0 slots) slave
replicates 0c6b70fd0e8fde5cd9a17234cacdec0f9745f8e5
M: 0c6b70fd0e8fde5cd9a17234cacdec0f9745f8e5 192.168.75.155:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b58e8073edc1e72d15f44a2fcc8f228b64aa383d 192.168.75.144:6379
slots: (0 slots) slave
replicates ed9b3be385920f7d1bd20bd0c58ef2b1c7448edf
M: ed9b3be385920f7d1bd20bd0c58ef2b1c7448edf 192.168.75.200:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 3d358490f7bdd68bd876e9dee0d03eb830ef2321 192.168.75.131:6379
slots: (0 slots) slave
replicates 2545e996e6a5bc9bd89498eab753195896c6f683
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
7.3.3 哈希槽的分布
master0: Slots 0 - 5460 ——192.168.75.134:6379
master1: Slots 5461 - 10922 —— 192.168.75.155:6379
master2: Slots 10923 - 16383 ——192.168.75.200:6379
7.3.4 三組節點群集情況:
master0:192.168.75.134:6379 slave0:192.168.75.131:6379
master1:192.168.75.155:6379 slave1:192.168.75.132:6379
master2:192.168.75.200:6379 slave2:192.168.75.144:6379
7.3.5 查看集群slave、slot、key分布資訊情況
[root@localhost ~]# redis-cli --cluster info 192.168.75.134:6379
192.168.75.134:6379 (004fefa8...) -> 2 keys | 5461 slots | 1 slaves.
192.168.75.200:6379 (0ef7c436...) -> 1 keys | 5461 slots | 1 slaves.
192.168.75.155:6379 (6ecd5793...) -> 2 keys | 5462 slots | 1 slaves.
[OK] 5 keys in 3 masters.
0.00 keys per slot on average.
[root@localhost ~]#
- 在登錄master0:192.168.75.134并且添加資料
[root@localhost 6379]# redis-cli -c -h 192.168.75.134 //登錄
192.168.75.134:6379> use shcool
(error) ERR unknown command `use`, with args beginning with: `shcool`,
192.168.75.134:6379> select 10 //群集模式不能切換庫
(error) ERR SELECT is not allowed in cluster mode
192.168.75.134:6379> set name age //新建資料鍵值name
-> Redirected to slot [5798] located at 192.168.75.155:6379 //哈希槽5798屬于master1 范圍所以set的新資料就切換到節點master1上,
OK
192.168.75.155:6379> get name //其他節點都可以查看
"age"
192.168.75.155:6379> set id 1
OK
192.168.75.155:6379>
7.3.6 redis擴容命令
1、將redis節點添加到集群:進入集群中一個節點添加
redis-cli --cluster add-node 新節點IP:埠號 當前集群中已存在的任意節點IP:埠號’
[root@master1 ~]# redis-cli --cluster add-node 192.168.75.47:6379 192.168.75.166:6379 '//redis-cli --cluster add-node 新節點IP:埠號 當前集群中已存在的任意節點IP:埠號'
2、洗掉節點:redis-cli --cluster del-node ip地址 節點id
在洗掉主節點時需要將資料哈希槽移到其他節點中然后移走或者洗掉節點
redis群集至少有三個master才能構建群集,如果有一組的主從節點都down,群集不存在,
[root@master1 ~] cd /var/lib/redis/6379/
[root@master1 6379] ls
appendonly.aof dump.rdb nodes-6379.conf
[root@master1 6379] rm -rf *
3、遷移redis槽位
[root@master1 ~] redis-cli --cluster reshard 192.168.100.41:6379
How many slots do you want to move (from 1 to 16384)? 4096 '//要遷移多少個槽'
What is the receiving node ID? 99521b7fd126b694bcb9a22ffa5a490f31f66543 '//遷移到哪個節點,資料節點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.
'//要求輸入源節點的id,這里因為原本有三個節點,輸入done表示結束'
Source node 1: 7f112f82bcf28a5d0627ea81b48cb76f4ea8605d
Source node 2: df195a34a91d756157a0fda7c71e99d5bd8fad09
Source node 3: a233a23541f431107fed79908318394d8bb30b51
Source node 4: done
'//最后會有一個遷移方案,輸入yes表示同意,遷移開始,輸入no表示不同意,重新設定遷移方案,'
'//確認是否遷移成功'
[root@master1 ~] redis-cli -h 192.168.100.41 -p 6379
192.168.100.41:6379> cluster nodes
...省略內容
4、遷移后檢測各個節點槽的均衡性
[root@master1 ~] redis-cli --cluster rebalance 192.168.100.41:6379
>>> Performing Cluster Check (using node 192.168.100.41:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
可以看出,節點負責的槽資料差異在2%以內,因此槽分配均衡
5、為擴容的主節點添加從節點
[root@master1 ~] redis-cli -h 192.168.100.48 -p 6379 '//登陸redis資料庫'
192.168.100.48:6379> cluster replicate 99521b7fd126b694bcb9a22ffa5a490f31f66543 '//添加從節點id'
OK
192.168.100.48:6379> cluster nodes '//查看節點資訊'
...省略內容
6、平衡各節點槽數量
[root@master1 ~] redis-cli --cluster rebalance --cluster-threshold 1 192.168.100.41:6379
>>> Performing Cluster Check (using node 192.168.100.41:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 1.00% threshold.
八、 群集查詢陳述句
- cluster nodes:查看群集節點ID
- info replication : 主/從復制資訊
- cluster info:查看群集的資訊
- redis-cli --cluster info 192.168.75.200:6379 //查看群集狀態
- role:查看節點角色和slave
- Redis Client Slots 命令用于當前的集群狀態,以陣列形式展示,
192.168.75.134:6379> cluster slots
1) 1) (integer) 10923
2) (integer) 16383
3) 1) "192.168.75.134"
2) (integer) 6379
3) "004fefa8639346e660c76c6026fb84a115962c61"
4) 1) "192.168.75.144"
2) (integer) 6379
3) "f7f6e088652a935f84d87ff8d878bdfb8913217e"
2) 1) (integer) 5462
2) (integer) 10922
3) 1) "192.168.75.155"
2) (integer) 6379
3) "6ecd57933114d2bff21f495ea5ca8c20663fbaff"
4) 1) "192.168.75.132"
2) (integer) 6379
3) "ea154447d0f7c3f73f303d66d43ecf4dc82b9a57"
3) 1) (integer) 0
2) (integer) 5461
3) 1) "192.168.75.200"
2) (integer) 6379
3) "cb882911b47e91e9b046bd564a1b016697aac9ff"
4) 1) "192.168.75.131"
2) (integer) 6379
3) "edd600d6bdac1a5e20956e930b82ea8cd8f5ff81"
192.168.75.134:6379>
- Redis Flushall 命令用于清空整個 Redis 服務器的資料(洗掉所有資料庫的所有 key ),
Redis Config Resetstat 命令用于重置 INFO 命令中的某些統計資料,包括:
Keyspace hits (鍵空間命中次數)
Keyspace misses (鍵空間不命中次數)
Number of commands processed (執行命令的次數)
Number of connections received (連接服務器的次數)
Number of expired keys (過期key的數量)
Number of rejected connections (被拒絕的連接數量)
Latest fork(2) time(最后執行 fork(2) 的時間)
The aof_delayed_fsync counter(aof_delayed_fsync 計數器的值)
語法
redis Config Resetstat 命令基本語法如下:
redis 127.0.0.1:6379> CONFIG RESETSTAT
- ping 命令
Redis Ping 命令使用客戶端向 Redis 服務器發送一個 PING ,如果服務器運作正常的話,會回傳一個 PONG ,
通常用于測驗與服務器的連接是否仍然生效,或者用于測量延遲值,
語法
redis Ping 命令基本語法如下:
redis 127.0.0.1:6379> PING
可用版本
>= 1.0.0
回傳值
如果連接正常就回傳一個 PONG ,否則回傳一個連接錯誤,
實體
# 客戶端和服務器連接正常
redis 127.0.0.1:6379> PING
PONG
# 客戶端和服務器連接不正常(網路不正常或服務器未能正常運行)
redis 127.0.0.1:6379> PING
Could not connect to Redis at 127.0.0.1:6379: Connection refused
- Redis Client Pause 命令 在指定時間內終止運行來自客戶端的命令
- Redis Debug Object 命令 獲取 key 的除錯資訊
- Redis Flushdb 命令 洗掉當前資料庫的所有key
- Redis Save 命令 異步保存資料到硬碟
- Redis Showlog 命令 管理 redis 的慢日志
- Redis Lastsave 命令 回傳最近一次 Redis 成功將資料保存到磁盤上的時間,以 UNIX 時間戳格式表示
- Redis Config Get 命令 獲取指定配置引數的值
- Redis Command 命令 獲取 Redis 命令詳情陣列
- Redis Slaveof 命令 將當前服務器轉變為指定服務器的從屬服務器(slave server)
- Redis Debug Segfault 命令 讓 Redis 服務崩潰
- Redis Flushall 命令 洗掉所有資料庫的所有key
- Redis Dbsize 命令 回傳當前資料庫的 key 的數量
- Redis Bgrewriteaof 命令 異步執行一個 AOF(AppendOnly File) 檔案重寫操作
- Redis Cluster Slots 命令 獲取集群節點的映射陣列
- Redis Config Set 命令 修改 redis 配置引數,無需重啟
- Redis Command Info 命令 獲取指定 Redis 命令描述的陣列
- Redis Shutdown 命令 異步保存資料到硬碟,并關閉服務器
- Redis Sync 命令 用于復制功能(replication)的內部命令
- Redis Client Kill 命令 關閉客戶端連接
- Redis Role 命令 回傳主從實體所屬的角色
- Redis Monitor 命令 實時列印出 Redis 服務器接收到的命令,除錯用
- Redis Command Getkeys 命令 獲取給定命令的所有鍵
- Redis Client Getname 命令 獲取連接的名稱
- Redis Config Resetstat 命令 重置 INFO 命令中的某些統計資料
- Redis Command Count 命令 獲取 Redis 命令總數
- Redis Time 命令 回傳當前服務器時間
- Redis Info 命令 獲取 Redis 服務器的各種資訊和統計數值
- Redis Config rewrite 命令 對啟動 Redis 服務器時所指定的 redis.conf 組態檔進行改寫
- Redis Client List 命令 獲取連接到服務器的客戶端連接串列
- Redis Client Setname 命令 設定當前連接的名稱
- Redis Bgsave 命令 在后臺異步保存當前資料庫的資料到磁盤
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/41660.html
標籤:其他
