Docker 搭建redis集群

分布式集群
1、redis分布式集群(每个节点存储一部分桶数据)

redis.conf配置文件内容如下(这里有些配置不生效的就在启动创建容器上加)

bash 复制代码
daemonize no
# 不限制访问
bind 0.0.0.0
# redis 端口号
port 6379
# 设置密码
requirepass "123456"
# 持久化
appendonly yes
#运行外部访问ip
protected-mode no
#开启集群模式
cluster-enabled yes
#集群配置文件
cluster-config-file nodes.conf
#链接超时时间
cluster-node-timeout 5000
#链接的主机IP
cluster-announce-ip 192.168.1.10
#集群的端口
cluster-announce-port 6379
#总线端口
cluster-announce-bus-port 16379
2、主节点192.168.1.10
bash 复制代码
#主节点创建命令
docker run --name redis -p 6379:6379 -p 16379:16379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --daemonize "no" --protected "no" --appendonly "yes" --bind "0.0.0.0" --cluster-enabled "yes" --cluster-config-file "nodes6379.conf" --cluster-node-timeout "5000" --cluster-announce-ip "192.168.1.10" --cluster-announce-port "6379" --cluster-announce-bus-port "6379"

192.168.1.11从节点需要的执行下面命令

bash 复制代码
docker run --name redisSlave -p 6379:6379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --replicaof 192.168.1.10 6379 --masterauth "123456" --protected-mode "no" --appendonly "yes" --bind "0.0.0.0"

使用 info replication执行发现要是没有挂上主节点(下图为参考),那就需要登录从节点后执行

slaveof 192.168.1.10 去关联上主节点(这样是一主一从)

bash 复制代码
#登录从节点命令
docker exec -it redisSlave redis-cli -p 6379 -a 123456
3、3主3从

其他主节点也是这样创建后,3主节点进行关联,执行命令

--cluster-replicas 1 :为每一个主节点创建一个副本所以这儿 3主3从

bash 复制代码
#这个是3主3从
redis-cli -a 123456 --cluster create 192.168.1.10:6379 192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379 192.168.1.14:6379 192.168.1.15:6379 --cluster-replicas 1

#这个是3主节点 没有从节点
redis-cli -a 123456 --cluster create 192.168.1.10:6379 192.168.1.11:6379 192.168.1.12:6379 --cluster-replicas 0
redis哨兵模式
1、哨兵模式需要先按照主从模式(192.168.1.10),从节点(192.168.1.11)要是没关联上就登录后执行 slaveof 192.168.1.10 去关联上主节点(这样是一主一从)
bash 复制代码
#主节点创建命令
docker run --name redis -p 6379:6379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --daemonize "no" --daemonize "no" --bind "0.0.0.0" --appendonly "no"
bash 复制代码
#从节点创建命令
docker run --name redis -p 6379:6379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --protected-mode "no" --daemonize "no" --bind "0.0.0.0" --appendonly "yes"  --replicaof 192.168.1.10 6379 --masterauth "123456"
2、哨兵创建,配置文件sentinel.conf如下
bash 复制代码
port 26379
daemonize no
protected-mode no
#哨兵监控服务 当有1台宕机 既算宕机 监控主节点的配置 mymaster名字随便起;2因为是三台哨兵,所以大于等于半数票数2
sentinel monitor mymaster 192.168.1.10
# 配置主节点密码
sentinel auth-pass mymaster 123456
3、创建哨兵模式的主节点
bash 复制代码
# 主节点sentinel  192.168.1.12
docker run --net=host -it  --name redisSentinel -p 26379:26379 -v /home/redis/sentinel.conf:/usr/local/bin/sentinel.conf  redis:v1.0 redis-sentinel /usr/local/bin/sentinel.conf

# 从节点01 sentinel 192.168.1.13 从节点的sentinel.conf和主节点一样
docker run --net=host -it  --name redisSentinel -p 26379:26379 -v /home/redis/sentinel.conf:/usr/local/bin/sentinel.conf  redis:v1.0 redis-sentinel /usr/local/bin/sentinel.conf

进入sentinel 验证查看

bash 复制代码
#进入命令
docker exec -it redisSentinel redis-cli -p 26379 -a 123456

# 查看信息 哨兵集群
info sentinel

# 展示如下信息
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.10:6379,slaves=1,sentinels=2


# 其他命令
sentinel master mymaster
sentinel sentinels mymaster
sentinel slaves mymaster
相关推荐
梦想与想象-广州大智汇1 小时前
自建docker加速镜像,使用 Cloudflare Workers/Pages 部署加速教程
运维·docker·容器
AI服务老曹3 小时前
架构师视角:如何构建支持GB28181/RTSP的异构AI视频平台?从Docker部署到源码交付的深度实践
人工智能·docker·音视频
薪火铺子4 小时前
Redis 缓存三大问题与解决方案
redis·spring·缓存
用户78937733908535 小时前
Docker 部署踩坑记录:从“构建失败”到“服务跑通”,以及为什么数据被清空了
python·docker
Slow菜鸟6 小时前
Docker 学习篇(三)| Docker安装指南(Linux版)
linux·学习·docker
人道领域8 小时前
【黑马点评日记】RedisGEO实战:黑马点评附近商铺功能
java·数据库·redis·adb
炸炸鱼.10 小时前
容器技术入门与 Docker 环境部署
docker
宁静@星空10 小时前
007-Docker构建 jar 包成镜像
docker·容器·jar
薪火铺子10 小时前
Redis 分布式锁与 Redisson 原理深度解析
java·redis·分布式·后端
摇滚侠10 小时前
基于 Redis 实现验证码登录
javascript·redis·bootstrap