当前位置:网站首页>docker部署redis一主二从三哨兵模式
docker部署redis一主二从三哨兵模式
2022-07-30 06:12:00 【weixin_44953227】
说明:当前安装部署指引只在一台宿主机服务器上启动三个(一主二从)redis容器节点和三个哨兵容器节点
1、docker搭建主从节点
#从镜像仓库拉取redis镜像(此指引拉取的是官方最新的redis镜像,根据实际情况而定,可拉取私仓自定义稳定的镜像)
docker pull redis
#切换到目录
cd /home/soft
#宿主机上创建redis配置及数据目录
mkdir -p redis/config
mkdir -p redis/data/data6379 redis/data/data6380 redis/data/data6381
#进入配置目录创建配置文件且添加配置信息
cd /home/soft/config
#创建并编辑redis6379.conf,最后wq保存退出(后文涉及到的配置文件其他需个性化配置的具体配置项根据实际情况配置)
vi redis6379.conf
port 6379
#创建并编辑redis6380.conf配置文件,最后wq保存退出(当java程序需要连接redis,从节点则需配置slave-announce-ip和slave-announce-port)
vi redis6380.conf
#节点容器端口
port 6380
#宿主机ip、master端口
replicaof 192.168.10.15 6379
#宿主机ip
slave-announce-ip 192.168.10.15
#节点宿主机端口
slave-announce-port 6380
#创建并编辑redis6381.conf配置文件,最后wq保存退出
vi redis6381.conf
port 6381
replicaof 192.168.10.15 6379
slave-announce-ip 192.168.10.15
slave-announce-port 6381
#启动master-redis节点
docker run \
-p 6379:6379 \
--name master-redis \
--restart=always /
-v /home/soft/redis/data/data6379:/data \
-v /home/soft/redis/config/redis6379.conf:/etc/redis/redis.conf \
-v /etc/localtime:/etc/localtime \
-d redis redis-server /etc/redis/redis.conf
#启动salve1-redis节点
docker run \
-p 6380:6380 \
--name salve1-redis \
--restart=always /
-v /home/soft/redis/data/data6380:/data \
-v /home/soft/redis/config/redis6380.conf:/etc/redis/redis.conf \
-v /etc/localtime:/etc/localtime \
-d redis redis-server /etc/redis/redis.conf
#启动salve2-redis节点
docker run \
-p 6381:6381 \
--name salve2-redis \
--restart=always /
-v /home/soft/redis/data/data6381:/data \
-v /home/soft/redis/config/redis6381.conf:/etc/redis/redis.conf \
-v /etc/localtime:/etc/localtime \
-d redis redis-server /etc/redis/redis.conf
#进入redis容器查看
docker exec -it master-redis redis-cli -p 6379
#查看容器节点信息
info replication

2、docker搭建哨兵模式
#进入宿主机redis哨兵配置文件目录
cd /home/soft/redis/config
#创建并编辑哨兵模式配置文件sentinel-26379.conf、sentinel-26380.conf、sentinel-26381.conf
vi sentinel-26379.conf
port 26379
sentinel monitor mymaster 192.168.10.15 6379 2
vi sentinel-26380.conf
port 26380
sentinel monitor mymaster 192.168.10.15 6379 2
vi sentinel-26381.conf
port 26381
sentinel monitor mymaster 192.168.10.15 6379 2
#启动哨兵容器及哨兵服务sentinel-26379、sentinel-26380、sentinel-26381
docker run \
--name sentinel-26379 \
-p 26379:26379 \
-v /home/soft/redis/config/sentinel-26379.conf:/etc/redis/sentinel.conf \
-v /etc/localtime:/etc/localtime \
-d redis redis-sentinel /etc/redis/sentinel.conf
docker run \
--name sentinel-26380 \
-p 26380:26380 \
-v /home/soft/redis/config/sentinel-26380.conf:/etc/redis/sentinel.conf \
-v /etc/localtime:/etc/localtime \
-d redis redis-sentinel /etc/redis/sentinel.conf
docker run \
--name sentinel-26381 \
-p 26381:26381 \
-v /home/soft/redis/config/sentinel-26381.conf:/etc/redis/sentinel.conf \
-v /etc/localtime:/etc/localtime \
-d redis redis-sentinel /etc/redis/sentinel.conf
3、模拟master节点宕机,哨兵模式重新选举新的master节点

查看redis从节点连接日志,发现由一开始连接不上主节点到连上选出的新master节点
docker logs -f salve2-redis

查看redis主从节点关系变化,可发现如下:
#salve1-redis节点已经变为master节点,且该节点下有两个从节点

#master-redis节点信息

#salve2-redis节点信息

注意:当redis的master节点down掉,当重新启动down掉的redis容器节点时,也应重启对应的sentinel容器节点,否则当重新选举的redis的master节点再次down掉,就不会进行选举了。
边栏推荐
- From catching up to surpassing, domestic software shows its talents
- Upload file -- file type, picture type, document type, video type, compressed package type
- C# 使用RestSharp 实现Get,Post 请求(2)
- Headline 2: there are several kinds of common SQL errors in MySQL usage?
- UDP和TCP使用同一个端口,可行吗?
- 这个终端连接工具,碾压Xshell
- ArrayList
- New material under the plastic restriction order - polylactic acid (PLA)
- Table with tens of millions of data, how to query the fastest?
- Equation Derivation Proof of Vector Triple Product
猜你喜欢

UDP和TCP使用同一个端口,可行吗?

Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)

How to calculate the daily cumulative capital flow one by one in real time

Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)

这个终端连接工具,碾压Xshell

Go uses the mencached cache

MySQL master-slave replication configuration construction, one step in place
Get all interface paths and names in the controller

【MySQL】MySQL中如何实现分页操作

DP5340国产替代CM5340立体声音频A/D转换器芯片
随机推荐
头条二面:MySQL中有几种常见的 SQL 错误用法?
Hex conversion...
A magical no main method of code
从追赶到超越,国产软件大显身手
go : go-redis list操作
The calculation and source code of the straight line intersecting the space plane
k8s 部署mysql8(PV和PVC 版本)
sizeof
go : go gin返回JSON数据
Go: go - redis based operation
千万级数据量的表,怎样最快速度查询?
申请内存,std::transform和AVX256指令集用例和执行速度比较
Go 使用mencached缓存
What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles
golang: Gorm配置Mysql多数据源
The newcomer deleted data by mistake, and the team leader skillfully used MySQL master-slave replication to delay the recovery of losses
Electron中设置菜单(Menu),主进程向渲染进程共享数据
Electron之初出茅庐——搭建环境并运行第一个程序
ArrayList
Develop common tool software