1. 程式人生 > 實用技巧 >部署Redis叢集

部署Redis叢集

分片+高可用+負載均衡

其中一個掛了,備用的頂上

redis叢集

啟動6個容器

建立redis叢集網絡卡

docker network create redis --subnet 172.38.0.0/16


檢視redis網絡卡的詳細資訊

docker network inspect redis

配置redis叢集

通過指令碼建立6個redis配置

for port in $(seq 1 6); \
do \
mkdir -p /data/redis/node-${port}/conf
touch /data/redis/node-${port}/conf/redis.conf
cat << EOF >/data/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

檢視建立的檔案

啟動容器

#指令碼
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /data/redis/node-${port}/data:/data \
-v /data/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.10-alpine3.12 redis-server /etc/redis/redis.conf; \


#手動
docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v /data/redis/node-1/data:/data -v /data/redis/node-1/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.11 redis:5.0.10-alpine3.12 redis-server /etc/redis/redis.conf

檢視啟動容器

docker ps

進入redis容器

redis中沒有bash命令,redis進入使用/bin/sh

docker exec -it IDNAME /bin/sh


進入後預設的目錄為/data

建立叢集

redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1


叢集建立完畢

#過程
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 290353fdbf5a7345e66fee97be33fa4e352eadfb 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 2d341c0a2aac0cd99a4cec112292bc7c48e608e2 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: 4d1882d9fa8f86d794bfad393ae982be4350338c 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: 8319c1e759a38114d5208ab329dc241b35289d28 172.38.0.14:6379
   replicates 4d1882d9fa8f86d794bfad393ae982be4350338c
S: 8b81002b5dcef5041c5ca70605c8cc30a53d05e0 172.38.0.15:6379
   replicates 290353fdbf5a7345e66fee97be33fa4e352eadfb
S: 530887eef54e59efe34b2a98f131fdc5cc6e1937 172.38.0.16:6379
   replicates 2d341c0a2aac0cd99a4cec112292bc7c48e608e2
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 172.38.0.11:6379)
M: 290353fdbf5a7345e66fee97be33fa4e352eadfb 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 2d341c0a2aac0cd99a4cec112292bc7c48e608e2 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 4d1882d9fa8f86d794bfad393ae982be4350338c 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 8319c1e759a38114d5208ab329dc241b35289d28 172.38.0.14:6379
   slots: (0 slots) slave
   replicates 4d1882d9fa8f86d794bfad393ae982be4350338c
S: 8b81002b5dcef5041c5ca70605c8cc30a53d05e0 172.38.0.15:6379
   slots: (0 slots) slave
   replicates 290353fdbf5a7345e66fee97be33fa4e352eadfb
S: 530887eef54e59efe34b2a98f131fdc5cc6e1937 172.38.0.16:6379
   slots: (0 slots) slave
   replicates 2d341c0a2aac0cd99a4cec112292bc7c48e608e2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

連線叢集

redis-cli -c

檢視叢集資訊

6379>cluster info


檢視節點資訊

127.0.0.1:6379>cluster nodes


注:3主3從

set a b

主節點在處理

從機應該也有一個這樣的值,主節點掛了,從節點應該替代上來。(高可用)

檢視執行的容器

docker ps

停掉主節點

docker stop ID


再次get值,連線超時

再次連線,get a

master失敗,從172.38.0.14從節點自動升為主節點

Docker搭建redis叢集完成!

使用docker之後,所有的技術慢慢變的簡單起來!