文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Docker搭建Redis Cluster集群

2024-11-30 01:26

关注

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"


来源:微技术之家内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯