Redis集群中節(jié)點(diǎn)更換IP后實(shí)現(xiàn)恢復(fù)集群且保留數(shù)據(jù)
前段時(shí)間在生產(chǎn)中遇到一個(gè)問題,即系統(tǒng)需要從一個(gè)網(wǎng)段遷移到另一個(gè)網(wǎng)段。我們知道redis集群在創(chuàng)建時(shí)是指定了節(jié)點(diǎn)的ip:port,因此在節(jié)點(diǎn)IP變更后,集群自然就失效了。如果需要恢復(fù)集群怎么辦?
當(dāng)然在大部分情況下,我們可以選擇刪除所有節(jié)點(diǎn)的數(shù)據(jù)文件dbfilename、持久化文件appendfilename、集群配置文件cluster-config-file,然后重建集群。但是如果需要保留數(shù)據(jù),又該怎么操作呢?
以一個(gè)三主三從的單副本集群來演示恢復(fù)過程
[root@test1 bin]# ./redis-cli -a password --cluster check 192.168.66.101:7000 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.66.101:7000 (d1ddeaa7...) -> 334 keys | 5461 slots | 1 slaves. 192.168.66.102:7003 (d21ce248...) -> 341 keys | 5462 slots | 1 slaves. 192.168.66.101:7001 (bb5c5e76...) -> 325 keys | 5461 slots | 1 slaves. [OK] 1000 keys in 3 masters. 0.06 keys per slot on average. >>> Performing Cluster Check (using node 192.168.66.101:7000) M: d1ddeaa7c77e35b3df50953fc09834b662cbac8b 192.168.66.101:7000 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: d21ce2482179af3b76a9f29d870848bae18a3214 192.168.66.102:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 089b2e16dff1f68c399a1efc73580e7cbbbfa71b 192.168.66.101:7002 slots: (0 slots) slave replicates d21ce2482179af3b76a9f29d870848bae18a3214 S: 92d8208b582c6111bd383b6fdfc2d80a86f47350 192.168.66.102:7005 slots: (0 slots) slave replicates d1ddeaa7c77e35b3df50953fc09834b662cbac8b S: ea68bec54e3deb0bd209f151151098ae6d8cf0b4 192.168.66.102:7004 slots: (0 slots) slave replicates bb5c5e768ab4aff9c92d7fd3f2d55007e2736c65 M: bb5c5e768ab4aff9c92d7fd3f2d55007e2736c65 192.168.66.101:7001 slots:[10923-16383] (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
將集群中所有節(jié)點(diǎn)的IP由192.168.66.*更換為192.168.77.*,此時(shí)如果嘗試檢查集群狀態(tài),可以看到集群仍然嘗試連接192.168.66.*網(wǎng)段的節(jié)點(diǎn):
[root@test1 bin]# ./redis-cli -a password --cluster check 192.168.77.101:7000 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Could not connect to Redis at 192.168.66.102:7003: Connection timed out ......
shutdown所有節(jié)點(diǎn)。
找到節(jié)點(diǎn)配置文件中cluster-config-file,此字段配置集群配置文件的保存位置(本示例中為/data/redis/cluster/7000/nodes_7000.conf),查看該文件內(nèi)容:
[root@test1 ~] cat /data/redis/cluster/7000/nodes_7000.conf d1ddeaa7c77e35b3df50953fc09834b662cbac8b 192.168.66.101:7000@17000 myself,master - 0 1626244031000 1 connected 0-5460 ea68bec54e3deb0bd209f151151098ae6d8cf0b4 192.168.66.102:7004@17004 slave bb5c5e768ab4aff9c92d7fd3f2d55007e2736c65 0 1626 244034813 5 connected d21ce2482179af3b76a9f29d870848bae18a3214 192.168.66.102:7003@17003 master - 0 1626244033803 4 connected 5461-10922 089b2e16dff1f68c399a1efc73580e7cbbbfa71b 192.168.66.101:7002@17002 slave d21ce2482179af3b76a9f29d870848bae18a3214 0 1626 244032793 4 connected bb5c5e768ab4aff9c92d7fd3f2d55007e2736c65 192.168.66.101:7001@17001 master - 0 1626244030770 2 connected 10923-16383 92d8208b582c6111bd383b6fdfc2d80a86f47350 192.168.66.102:7005@17005 slave d1ddeaa7c77e35b3df50953fc09834b662cbac8b 0 1626 244031782 6 connected vars currentEpoch 6 lastVoteEpoch 0
將所有節(jié)點(diǎn)的cluster-config-file文件中的IP地址均由192.168.66.*改為192.168.77.*:
# 192.168.66.101 執(zhí)行 sed -i 's/192.168.66/192.168.77/g' /data/redis/cluster/7000/nodes_7000.conf /data/redis/cluster/7001/nodes_7001.conf /data/redis/cluster/7002/nodes_7002.conf # 192.168.66.102 執(zhí)行 sed -i 's/192.168.66/192.168.77/g' /data/redis/cluster/7003/nodes_7003.conf /data/redis/cluster/7004/nodes_7004.conf /data/redis/cluster/7005/nodes_7005.conf
啟動所有節(jié)點(diǎn)。
再次檢查集群狀態(tài)
[root@test1 bin]# ./redis-cli -a password --cluster check 192.168.77.101:7000 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.77.101:7000 (d1ddeaa7...) -> 334 keys | 5461 slots | 1 slaves. 192.168.77.102:7003 (d21ce248...) -> 341 keys | 5462 slots | 1 slaves. 192.168.77.101:7001 (bb5c5e76...) -> 325 keys | 5461 slots | 1 slaves. [OK] 1000 keys in 3 masters. 0.06 keys per slot on average. >>> Performing Cluster Check (using node 192.168.77.101:7000) M: d1ddeaa7c77e35b3df50953fc09834b662cbac8b 192.168.77.101:7000 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 92d8208b582c6111bd383b6fdfc2d80a86f47350 192.168.77.102:7005 slots: (0 slots) slave replicates d1ddeaa7c77e35b3df50953fc09834b662cbac8b M: d21ce2482179af3b76a9f29d870848bae18a3214 192.168.77.102:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: bb5c5e768ab4aff9c92d7fd3f2d55007e2736c65 192.168.77.101:7001 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 089b2e16dff1f68c399a1efc73580e7cbbbfa71b 192.168.77.101:7002 slots: (0 slots) slave replicates d21ce2482179af3b76a9f29d870848bae18a3214 S: ea68bec54e3deb0bd209f151151098ae6d8cf0b4 192.168.77.102:7004 slots: (0 slots) slave replicates bb5c5e768ab4aff9c92d7fd3f2d55007e2736c65 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@test1 bin]# ./redis-cli -a password --cluster info 192.168.77.101:7000 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.77.101:7000 (d1ddeaa7...) -> 334 keys | 5461 slots | 1 slaves. 192.168.77.102:7003 (d21ce248...) -> 341 keys | 5462 slots | 1 slaves. 192.168.77.101:7001 (bb5c5e76...) -> 325 keys | 5461 slots | 1 slaves. [OK] 1000 keys in 3 masters. 0.06 keys per slot on average.
可以看到集群狀態(tài)已經(jīng)恢復(fù),key數(shù)量與IP變更前一致。
測試一下集群的數(shù)據(jù)寫入和讀取
[root@test1 bin]# ./redis-cli -a password -c -h 192.168.77.101 -p 7000 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.77.101:7000> keys * 1) "name725" 2) "name359" ...... 192.168.77.101:7000> get name7 "hello\n" 192.168.77.101:7000> get name400 -> Redirected to slot [11448] located at 192.168.77.101:7001 "hello\n" 192.168.77.101:7001> set testkey 'testvalue' -> Redirected to slot [4757] located at 192.168.77.101:7000 OK 192.168.77.101:7000> get testkey "testvalue"
原有數(shù)據(jù)讀取正常,新數(shù)據(jù)寫入讀取正常,集群恢復(fù)。
總結(jié)
redis集群節(jié)點(diǎn)更換IP后,只需要修改所有節(jié)點(diǎn) cluster-config-file 中的IP地址為新地址,并重啟所有節(jié)點(diǎn),集群即可自動恢復(fù)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于redis實(shí)現(xiàn)token驗(yàn)證用戶是否登陸
這篇文章主要為大家詳細(xì)介紹了基于redis實(shí)現(xiàn)token驗(yàn)證用戶是否登陸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

