Docker搭建Redis Cluster集群

发布日期: 2021-02-05 16:15:14 作者: Stephen 评论: 1

Redis Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。

Redis Cluster提供了一种运行Redis安装的方法,在该安装中,数据会 在多个Redis节点之间自动分片。Redis Cluster 在分区期间还提供了一定程度的可用性,这实际上是在某些节点出现故障或无法通信时继续操作的能力。但是,如果发生较大故障(例如,大多数主服务器不可用时),集群将停止运行。Redis Cluster自动分割在多个节点之间的数据集,一部分节点出现故障或无法与集群的其余部分通信时,继续运行的能力。

创建网卡

docker network create redis --subnet 172.28.0.0/16

Redis配置

通过脚本创建6个 redis 配置

for port in $(seq 1 6); \
do \
mkdir -p ~/redis/node-${port}/conf
touch ~/redis/node-${port}/conf/redis.conf
cat << EOF > ~/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.28.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

运行容器

# 容器1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v ~/redis/node-1/data:/data \
-v ~/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# 容器2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v ~/redis/node-2/data:/data \
-v ~/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# 容器3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v ~/redis/node-3/data:/data \
-v ~/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# 容器4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v ~/redis/node-4/data:/data \
-v ~/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# 容器5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v ~/redis/node-5/data:/data \
-v ~/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# 容器6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v ~/redis/node-6/data:/data \
-v ~/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.28.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

创建集群

# 进入容器
> docker exec -it redis-1 /bin/sh

# 进入容器后,在容器中创建集群
> redis-cli --cluster create 172.28.0.11:6379 172.28.0.12:6379 172.28.0.13:6379 172.28.0.14:6379 172.28.0.15:6379 172.28.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.28.0.15:6379 to 172.28.0.11:6379
Adding replica 172.28.0.16:6379 to 172.28.0.12:6379
Adding replica 172.28.0.14:6379 to 172.28.0.13:6379
M: cfb09d104f563d5dea870437cf73fac266b69a30 172.28.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 47d28c2cc92ce7fafbe09b14d521a35e56a2c02c 172.28.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: be4034133574260765165beac5fd1edacb63a2bd 172.28.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: 74685c90c5aebdc8a035d288c962e22f98e994e8 172.28.0.14:6379
   replicates be4034133574260765165beac5fd1edacb63a2bd
S: 96b4185cc40b532b144b2ac3e2fe7213e7c13a50 172.28.0.15:6379
   replicates cfb09d104f563d5dea870437cf73fac266b69a30
S: edd2e12510d921be2193df5b461033fc0f465144 172.28.0.16:6379
   replicates 47d28c2cc92ce7fafbe09b14d521a35e56a2c02c
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.28.0.11:6379)
M: cfb09d104f563d5dea870437cf73fac266b69a30 172.28.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 96b4185cc40b532b144b2ac3e2fe7213e7c13a50 172.28.0.15:6379
   slots: (0 slots) slave
   replicates cfb09d104f563d5dea870437cf73fac266b69a30
S: edd2e12510d921be2193df5b461033fc0f465144 172.28.0.16:6379
   slots: (0 slots) slave
   replicates 47d28c2cc92ce7fafbe09b14d521a35e56a2c02c
M: be4034133574260765165beac5fd1edacb63a2bd 172.28.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 47d28c2cc92ce7fafbe09b14d521a35e56a2c02c 172.28.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 74685c90c5aebdc8a035d288c962e22f98e994e8 172.28.0.14:6379
   slots: (0 slots) slave
   replicates be4034133574260765165beac5fd1edacb63a2bd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 启动redis集群客户端(-c表示集群)
> redis-cli -c

# 查看集群信息
> 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:235
cluster_stats_messages_pong_sent:225
cluster_stats_messages_sent:460
cluster_stats_messages_ping_received:220
cluster_stats_messages_pong_received:235
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:460

# 集群创建好之后,11,12,13为主节点,其余为从节点
> cluster nodes

96b4185cc40b532b144b2ac3e2fe7213e7c13a50 172.28.0.15:6379@16379 slave cfb09d104f563d5dea870437cf73fac266b69a30 0 1596856234249 5 connected
edd2e12510d921be2193df5b461033fc0f465144 172.28.0.16:6379@16379 slave 47d28c2cc92ce7fafbe09b14d521a35e56a2c02c 0 1596856234146 6 connected
cfb09d104f563d5dea870437cf73fac266b69a30 172.28.0.11:6379@16379 myself,master - 0 1596856232000 1 connected 0-5460
be4034133574260765165beac5fd1edacb63a2bd 172.28.0.13:6379@16379 master - 0 1596856233000 3 connected 10923-16383
47d28c2cc92ce7fafbe09b14d521a35e56a2c02c 172.28.0.12:6379@16379 master - 0 1596856233000 2 connected 5461-10922
74685c90c5aebdc8a035d288c962e22f98e994e8 172.28.0.14:6379@16379 slave be4034133574260765165beac5fd1edacb63a2bd 0 1596856233230 4 connected

运行测试

添加Redis缓存

会返回所添加的对应的节点,如这里返回的是12的节点

set name Stephen

-> Redirected to slot [5798] located at 172.28.0.12:6379
OK

停止容器

停止刚添加缓存的对应的redis节点

docker stop redis-2

当主节点断开之后,会自动将从节点切换为主节点,比如这里将16切换为了主节点

cluster nodes

96b4185cc40b532b144b2ac3e2fe7213e7c13a50 172.28.0.15:6379@16379 slave cfb09d104f563d5dea870437cf73fac266b69a30 0 1596856365279 5 connected
edd2e12510d921be2193df5b461033fc0f465144 172.28.0.16:6379@16379 master - 0 1596856367324 7 connected 5461-10922
cfb09d104f563d5dea870437cf73fac266b69a30 172.28.0.11:6379@16379 myself,master - 0 1596856365000 1 connected 0-5460
be4034133574260765165beac5fd1edacb63a2bd 172.28.0.13:6379@16379 master - 0 1596856366814 3 connected 10923-16383
47d28c2cc92ce7fafbe09b14d521a35e56a2c02c 172.28.0.12:6379@16379 master,fail - 1596856348392 1596856346000 2 connected
74685c90c5aebdc8a035d288c962e22f98e994e8 172.28.0.14:6379@16379 slave be4034133574260765165beac5fd1edacb63a2bd 0 1596856366301 4 connected

获取缓存

如果能取到值,表示集群搭建成功,如下,从16这个节点中取到了值

get name

-> Redirected to slot [5798] located at 172.28.0.16:6379
"stephen"

相关链接

搭建Redis Cluster集群

杜老师说 3年前
新年快乐,流量猛进!