配置 redis 叢集
- 三主三從叢集設定
Master1(6381) Master2(6382) Master3(6383)
Slave1(6384) Slave2(6385) Slave3(6386)
- 拉取映象啟動容器
docker pull redis:6.0.8
docker run -d --name redis-node-1 --net host --privileged=true -v /data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
docker ps
CONTAINER ID IMAGE NAMES
ded0061df2ce redis:6.0.8 redis-node-6
402b0073a401 redis:6.0.8 redis-node-5
78f3028f13be redis:6.0.8 redis-node-4
5d96881f585d redis:6.0.8 redis-node-3
d087d97a7507 redis:6.0.8 redis-node-2
beb8f99d6645 redis:6.0.8 redis-node-1
- 主從配置
# --cluster-replicas 1 為每個master分配一個slave
docker exec -it redis-node-1 /bin/bash
redis-cli --cluster create 192.168.2.241:6381 192.168.2.241:6382 192.168.2.241:6383 192.168.2.241:6384 192.168.2.241:6385 192.168.2.241:6386 --cluster-replicas 1
- 檢視叢集
# 以 6381 為切入點進入
root@test:/data# redis-cli -p 6381(預設6379)
127.0.0.1:6381> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:314
cluster_stats_messages_pong_sent:309
cluster_stats_messages_sent:623
cluster_stats_messages_ping_received:304
cluster_stats_messages_pong_received:314
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:623
127.0.0.1:6381> CLUSTER NODES
661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678706342394 3 connected 10923-16383
c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 myself,master - 0 1678706341000 1 connected 0-5460
15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678706343396 3 connected
140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 master - 0 1678706342000 2 connected 5461-10922
9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 slave c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 0 1678706341392 1 connected
27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678706341000 2 connected
127.0.0.1:6381>
- 資料讀寫儲存
docker exec -it redis-node-1 /bin/bash
redis-cli -p 6381 -c(最佳化路由, 以叢集而不是單機模式)
127.0.0.1:6381> KEYS *
127.0.0.1:6381> FLUSHALL
127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.2.241:6383
OK
192.168.2.241:6383> set k2 v2
-> Redirected to slot [449] located at 192.168.2.241:6381
OK
192.168.2.241:6381> set k3 v3
OK
192.168.2.241:6381> set k4 v4
-> Redirected to slot [8455] located at 192.168.2.241:6382
OK
# 檢視叢集資訊
redis-cli --cluster check 192.168.2.241:6381
192.168.2.241:6381 (c0c7c1a9...) -> 2 keys | 5461 slots | 1 slaves.
192.168.2.241:6383 (661ef7b3...) -> 1 keys | 5461 slots | 1 slaves.
192.168.2.241:6382 (140d2119...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.2.241:6381)
M: c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386
slots: (0 slots) slave
replicates 661ef7b3f4730b71f2d76d87e6baad2229e01399
M: 140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384
slots: (0 slots) slave
replicates c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f
S: 27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385
slots: (0 slots) slave
replicates 140d21198470f2e066ac376f560c0641c98327ae
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 主從切換檢視
# 停止一個主節點
docker stop redis-node-1
docker exec -it redis-node-2 /bin/bash
redis-cli -p 6382 -c
127.0.0.1:6382> CLUSTER NODES
661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678708057000 3 connected 10923-16383
140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 myself,master - 0 1678708053000 2 connected 5461-10922
9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 master - 0 1678708057686 7 connected 0-5460
27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678708056684 2 connected
c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 master,fail - 1678707913388 1678707909000 1 disconnected
15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678708054681 3 connected
192.168.2.241:6383> get k1
"v1"
192.168.2.241:6383> get k2
-> Redirected to slot [449] located at 192.168.2.241:6384
"v2"
192.168.2.241:6384> get k3
"v3"
192.168.2.241:6384> get k4
-> Redirected to slot [8455] located at 192.168.2.241:6382
"v4"
# 啟動停止的主節點
docker start redis-node-1
docker exec -it redis-node-1 /bin/bash
redis-cli -p 6381 -c
127.0.0.1:6381> CLUSTER NODES # 還是slave, 要想6381還是master, 可以停掉6384再啟動
27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678708466000 2 connected
661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678708467908 3 connected 10923-16383
c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 myself,slave 9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 0 1678708467000 7 connected
140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 master - 0 1678708464000 2 connected 5461-10922
9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 master - 0 1678708465905 7 connected 0-5460
15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678708466906 3 connected
- 擴容到 4 主 4 從
docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
docker exec -it redis-node-7 /bin/bash
# 新增一個master節點
redis-cli --cluster add-node 192.168.2.241:6387 192.168.2.241:6381(相當於通知叢集的老大6381)
# 檢視
redis-cli --cluster check 192.168.2.241:6381
# 重新分配槽位
redis-cli --cluster reshard 192.168.2.241:6381
...How many slots do you want to move (from 1 to 16384)? 4096 (16384/Master的個數)
...What is the receiving node ID? f23674b12839887c47761c40017fd1e44a2a3c45 (6387的id)
...Source node #1: all
# 檢視
redis-cli --cluster check 192.168.2.241:6381
# 為什麼6387是3個新的區間, 以前的還是連續?
# 重新分配成本太高, 所以前3家各自勻出來一部分, 從6381/6382/6383三個舊節點分別勻出1364個坑位給新節點6387
# M: f23674b12839887c47761c40017fd1e44a2a3c45 192.168.2.241:6387
# slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
# 新增從節點
redis-cli --cluster add-node 192.168.2.241:6388 192.168.2.241:6387 --cluster-slave --cluster-master-id f23674b12839887c47761c40017fd1e44a2a3c45
# 檢視
redis-cli --cluster check 192.168.2.241:6381
- 縮容 6387 6388 下線
# 移除節點
redis-cli --cluster del-node 192.168.2.241:6388 6811637e126da89ec173afe67d2b2f14b4a27b1e(6388的id)
# 將6387的槽號清空, 重新分配, 本例將清出來的槽號都給6381
redis-cli --cluster reshard 192.168.2.241:6381
...How many slots do you want to move (from 1 to 16384)? 4096
...What is the receiving node ID? c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f(6381的id)
...Source node #1: f23674b12839887c47761c40017fd1e44a2a3c45(6387的id, 告知刪除哪個)
...Source node #2: done
# 檢視
redis-cli --cluster check 192.168.2.241:6381
# M: c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381
# slots:[0-6826],[10923-12287] (8192 slots) master
# 1 additional replica(s)
...
# M: f23674b12839887c47761c40017fd1e44a2a3c45 192.168.2.241:6387
# slots: (0 slots) master
# 刪除6387節點
redis-cli --cluster del-node 192.168.2.241:6387 f23674b12839887c47761c40017fd1e44a2a3c45
# 檢視
redis-cli --cluster check 192.168.2.241:6381