Docker搭建Redis集群

Docker搭建Redis集群

创建一个专属redis的网络

shell 复制代码
docker network create redis --subnet 172.38.0.0/16

通过shell脚本创建并启动6个redis服务

shell 复制代码
#通过脚本一次创建6个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/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
# 通过脚本一次启动6个redis容器
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done

进入某个redis中

shell 复制代码
docker exec -it redis-1 /bin/sh

创建集群配置

shell 复制代码
 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

进入集群

shell 复制代码
/data # redis-cli -c
127.0.0.1:6379> 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:399
cluster_stats_messages_pong_sent:424
cluster_stats_messages_sent:823
cluster_stats_messages_ping_received:419
cluster_stats_messages_pong_received:399
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:823
127.0.0.1:6379> cluster nodes
0a6f57dd64c4fa6199bb8b50134301f9e444eb1b 172.38.0.13:6379@16379 master - 0 1700491426631 3 connected 10923-16383
870383b25a12474bcf5cc63464bdd29d5a4dbf58 172.38.0.14:6379@16379 slave 0a6f57dd64c4fa6199bb8b50134301f9e444eb1b 0 1700491426132 4 connected
3b3ecd8426c8314b9a0ffc4be2f4bc0e5adafa31 172.38.0.12:6379@16379 master - 0 1700491427533 2 connected 5461-10922
de3f56bd83ad82dcd9dc091f29a8c99e662fb7df 172.38.0.11:6379@16379 myself,master - 0 1700491426000 1 connected 0-5460
46d53036f6f3c411f2878da5d5e0f3a8f577bb88 172.38.0.16:6379@16379 slave 3b3ecd8426c8314b9a0ffc4be2f4bc0e5adafa31 0 1700491426532 6 connected
aca624d266ba3c0683e889e7491df4ac6a813909 172.38.0.15:6379@16379 slave de3f56bd83ad82dcd9dc091f29a8c99e662fb7df 0 1700491425630 5 connected

127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
172.38.0.13:6379> get a
"b"
相关推荐
跃ZHD1 分钟前
前后端分离,Jackson,Long精度丢失
java
blammmp23 分钟前
Java:数据结构-枚举
java·开发语言·数据结构
暗黑起源喵41 分钟前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong1 小时前
Java反射
java·开发语言·反射
ketil271 小时前
Ubuntu 安装 redis
redis
全能全知者1 小时前
docker快速安装与配置mongoDB
mongodb·docker·容器
九圣残炎1 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge1 小时前
Netty篇(入门编程)
java·linux·服务器
Re.不晚2 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
雷神乐乐2 小时前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven