Docker搭建Redis Cluster集群

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

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 weijishu
-> 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
"weijishu"


责任编辑:华轩 来源: 微技术之家
相关推荐

2023-04-07 08:28:14

2021-04-06 06:04:36

Redis 6.X C集群搭建操作系统

2017-07-11 13:30:12

RedisDockerLinux

2023-11-13 09:03:10

2023-06-10 23:09:40

Redis场景内存

2022-09-15 08:31:11

主从复制模式Docker

2021-05-12 09:13:48

MySQL数据库Docker搭建

2021-06-03 18:42:26

Redis集群故障

2021-01-07 10:18:03

Redis数据库环境搭建

2019-12-13 10:50:49

集群Redis存储

2022-01-26 00:06:08

Redis分布式客户端

2022-01-27 20:15:31

集群存储元数据

2024-04-03 00:00:00

Redis集群代码

2020-04-21 22:59:50

Redis搭建选举

2019-09-16 16:05:13

Redis集群模式

2016-01-07 09:36:20

Docker容器

2022-02-11 08:41:19

WindowsRedis集群

2021-03-31 05:57:40

集群搭建哨兵集群Redis

2021-04-22 08:40:42

RedisCluster 集群故障转移

2023-06-12 07:41:16

dockerspark集群
点赞
收藏

51CTO技术栈公众号