Redis docker 主从模式与哨兵sentinel
mhr18 2024-12-01 09:14 21 浏览 0 评论
为实现redis的高可用,我们采用主从模式加哨兵的方法。
一主二从三哨兵,共启动6个redis容器。本文示例在同一个服务器上进行操作。
开发环境
- centos 假设ip地址为 x.x.x.1
- docker 1.13.1
- redis 7.0.2
系统为centos
cat /proc/version
Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017
后面的操作将在root权限下进行
docker版本
docker -v
Docker version 1.13.1, build 7d71120/1.13.1
redis版本
我们使用7.0.2版本。用到的服务器上的redis最好统一版本。
docker pull redis:7.0.2
不同版本的redis的配置不一定相同。如果启动容器出现一直在restarting的情况,去看一下log
查看已经启动的redis容器中的redis版本
docker exec -it [容器id] redis-server -v
Redis server v=7.0.2 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=40f017f9608e455e
来官网找对应的安装包 http://download.redis.io/releases/
解压后可以得到redis.conf和sentinel.conf文件
主从结构
一个主redis,2个从redis。它们使用不同的3个端口,注意检查防火墙的设置。
本文假设服务器的ip为x.x.x.1。
启动主redis
主redis,即master。
启动主redis容器
docker run --restart=always -p 6400:6379 --name redis-CNT-MASTER \
-d redis:7.0.2 redis-server --requirepass 778899 --masterauth 778899
- 6400:6379 指定服务器的6400对应redis容器里的6379端口
- --requirepass 778899 设定密码
- --masterauth 778899 从redis连上来需要的密码
进入容器查看状态
docker exec -it redis-CNT-MASTER redis-cli
127.0.0.1:6379> auth 778899
OK
127.0.0.1:6379> info replication
启动1号从redis
从redis使用配置的方式
文件结构 /home/dapp/projects/rustfisher/redis-slave1
├── data
└── redis.conf
1号从redis的redis.conf除掉注释后的部分
#bind 0.0.0.0 # 不用bind
slaveof x.x.x.1 6400 # 记得改成你的服务器ip
replica-announce-ip x.x.x.1 # 记得改成你的服务器ip
replica-announce-port 6401 # 从redis对外的端口,后面启动的时候也要配置的
protected-mode no
port 6379
masterauth 778899
requirepass 778899
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
折叠
启动1号从redis
docker run --restart=always -p 6401:6379 --name redis-CNT-S1 \
-v /home/dapp/projects/rustfisher/redis-slave1/redis.conf:/etc/redis/redis.conf \
-v /home/dapp/projects/rustfisher/redis-slave1/data:/data \
-d redis:7.0.2 redis-server /etc/redis/redis.conf
- 6401:6379 1号从redis对外端口是6401
- redis-CNT-S1 容器名字
启动2号从redis
配置文件可以复制1号的。然后记得修改它的宣称ip和端口
replica-announce-ip x.x.x.1
replica-announce-port 6402 # 2号用的端口
启动2号从redis,需要注意对应的路径
docker run --restart=always -p 6402:6379 --name redis-CNT-S2 \
-v /home/dapp/projects/rustfisher/redis-slave2/redis.conf:/etc/redis/redis.conf \
-v /home/dapp/projects/rustfisher/redis-slave2/data:/data \
-d redis:latest redis-server /etc/redis/redis.conf
查看主从信息info replication
进入主redis容器,输入redis-cli,auth后,检查情况info replication
例如进入主redis容器查看。此时connected_slaves:2。slave的ip和port应该和它们宣称replica-announce的一致。
在主redis中set a 123,在1号和2号从redis里get a可以看到效果。
哨兵 sentinel
我们会启动3个新的redis容器,即3个哨兵。这3个哨兵都监听主redis。
哨兵1号
新建一个哨兵1号用的目录 /home/dapp/projects/rustfisher/sentinel1
├── conf
│ └── sentinel.conf
└── data
先编辑配置文件 sentinel.conf
port 6411
dir "/tmp"
sentinel monitor master001 x.x.x.1 6400 2 # 记得修改成你的主redis的ip和端口
sentinel auth-pass master001 778899 # 密码是前面定的
sentinel down-after-milliseconds master001 30000
sentinel parallel-syncs master001 1
sentinel failover-timeout master001 180000
sentinel deny-scripts-reconfig yes
- port 6411 指定的是哨兵容器里自己的端口
- sentinel monitor 指定了要监听的主master的ip和端口,最后那个2表示需要2个哨兵投票
- master001 是我们给主redis起的名字,后面都用这个
启动1号哨兵
docker run --restart=always -p 6411:6411 --name redis-sentinel-CNT-1 --privileged=true \
-v /home/dapp/projects/rustfisher/sentinel1/conf:/usr/local/etc/redis/conf/ \
-d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
注意:这里需要映射的是目录。-v目录对目录。
进入容器后,可以查看相关信息。redis-cli需要指定端口-p 6411
root@aa8d208546d1:/data# redis-cli -p 6411
127.0.0.1:6411> sentinel master master001
查看服务器上1号哨兵的 sentinel.conf ,发现多了一些内容,是redis哨兵写进来的
# Generated by CONFIG REWRITE
latency-tracking-info-percentiles 50 99 99.9
user default on nopass ~* &* +@all
sentinel myid b43e361ff80b8f9106cb1d4bb59421aa909ac370
sentinel config-epoch master001 0
sentinel leader-epoch master001 0
sentinel current-epoch 0
sentinel known-replica master001 x.x.x.1 6401
sentinel known-replica master001 x.x.x.1 6402
启动2号哨兵
配置2号的路径/home/dapp/projects/rustfisher/sentinel2。
sentinel.conf配置内容和前面一样,启动时候端口用-p 6412:6411
docker run --restart=always -p 6412:6411 --name redis-sentinel-CNT-2 --privileged=true \
-v /home/dapp/projects/rustfisher/sentinel2/conf:/usr/local/etc/redis/conf/ \
-d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
启动3号哨兵
同理,路径用3号自己的 /home/dapp/projects/rustfisher/sentinel3/conf
端口-p 6413:6411
docker run --restart=always -p 6413:6411 --name redis-sentinel-CNT-3 --privileged=true \
-v /home/dapp/projects/rustfisher/sentinel3/conf:/usr/local/etc/redis/conf/ \
-d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
查看哨兵情况
进入哨兵redis容器查看情况
root@5dc0468fb71f:/data# redis-cli -p 6411
127.0.0.1:6411> sentinel master master001
- num-slaves 表示从redis的数量
- num-other-sentinels 表示除自己外的哨兵数量
哨兵测试
停掉主redis容器
docker stop [id]
例如
docker stop redis-CNT-MASTER
过一会等选出新的主redis。然后再启动刚才停掉的容器redis-CNT-MASTER。查看信息,发现它是role:slave
[root@rustfisher_test01 redis-slave1]# docker exec -it redis-CNT-MASTER redis-cli -a 778899 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:x.x.x.1
master_port:6402
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:886622
slave_repl_offset:886622
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:6e8a8cb6c167abeb743e668b654e2f470a537742
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:886622
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:409622
repl_backlog_histlen:477001
从上面看出,现在的主redis变成x.x.x.1:6402了。
哨兵常见问题
配置文件映射
指定配置文件sentinel.conf映射到容器内时,直接了使用文件映射。
这么做有可能导致哨兵没有写入配置文件的权限, 查看log会看到:
WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy.
解决方法是使用目录映射。像上面那样:
-v /home/dapp/projects/rustfisher/sentinel2/conf:/usr/local/etc/redis/conf/
哨兵myid
主从与哨兵redis都启动后,看起来OK了。但stop掉主redis后,哨兵并没有选出新的主redis。
有一种可能是哨兵改写的sentinel.conf里使用了相同的myid。
grep -nr myid
sentinel1/conf/sentinel.conf:11:sentinel myid b43e361ff80b8f9106cb1d4bb59421aa909ac370
sentinel2/conf/sentinel.conf:11:sentinel myid e19342addbcdd8d034c1e91ed74ff94a7aec2e2a
sentinel3/conf/sentinel.conf:11:sentinel myid d0393d72f69556f2047cf8c84cfa20f4df6ed4ff
解决方法是stop掉那个哨兵,删掉myid那行,然后重启哨兵。它会自动生成新的myid。
文章来自https://www.cnblogs.com/rustfisher/p/16425561.html
相关推荐
- Dubai's AI Boom Lures Global Tech as Emirate Reinvents Itself as Middle East's Silicon Gateway
-
AI-generatedimageAsianFin--Dubaiisrapidlytransformingitselffromadesertoilhubintoaglob...
- OpenAI Releases o3-pro, Cuts o3 Prices by 80% as Deal with Google Cloud Reported to Make for Compute Needs
-
TMTPOST--OpenAIisescalatingthepricewarinlargelanguagemodel(LLM)whileseekingpartnershi...
- 黄仁勋说AI Agent才是未来!但究竟有些啥影响?
-
,抓住风口(iOS用户请用电脑端打开小程序)本期要点:详解2025年大热点你好,我是王煜全,这里是王煜全要闻评论。最近,有个词被各个科技大佬反复提及——AIAgent,智能体。黄仁勋在CES展的发布...
- 商城微服务项目组件搭建(五)——Kafka、Tomcat等安装部署
-
1、本文属于mini商城系列文档的第0章,由于篇幅原因,这篇文章拆成了6部分,本文属于第5部分2、mini商城项目详细文档及代码见CSDN:https://blog.csdn.net/Eclipse_...
- Python+Appium环境搭建与自动化教程
-
以下是保姆级教程,手把手教你搭建Python+Appium环境并实现简单的APP自动化测试:一、环境搭建(Windows系统)1.安装Python访问Python官网下载最新版(建议...
- 零配置入门:用VSCode写Java代码的正确姿
-
一、环境准备:安装JDK,让电脑“听懂”Java目标:安装Java开发工具包(JDK),配置环境变量下载JDKJava程序需要JDK(JavaDevelopmentKit)才能运行和编译。以下是两...
- Mycat的搭建以及配置与启动(mycat2)
-
1、首先开启服务器相关端口firewall-cmd--permanent--add-port=9066/tcpfirewall-cmd--permanent--add-port=80...
- kubernetes 部署mysql应用(k8s mysql部署)
-
这边仅用于测试环境,一般生产环境mysql不建议使用容器部署。这里假设安装mysql版本为mysql8.0.33一、创建MySQL配置(ConfigMap)#mysql-config.yaml...
- Spring Data Jpa 介绍和详细入门案例搭建
-
1.SpringDataJPA的概念在介绍SpringDataJPA的时候,我们首先认识下Hibernate。Hibernate是数据访问解决技术的绝对霸主,使用O/R映射(Object-Re...
- 量子点格棋上线!“天衍”邀您执子入局
-
你是否能在策略上战胜量子智能?这不仅是一场博弈更是一次量子智力的较量——量子点格棋正式上线!试试你能否赢下这场量子智局!游戏玩法详解一笔一画间的策略博弈游戏目标:封闭格子、争夺领地点格棋的基本目标是利...
- 美国将与阿联酋合作建立海外最大的人工智能数据中心
-
当地时间5月15日,美国白宫宣布与阿联酋合作建立人工智能数据中心园区,据称这是美国以外最大的人工智能园区。阿布扎比政府支持的阿联酋公司G42及多家美国公司将在阿布扎比合作建造容量为5GW的数据中心,占...
- 盘后股价大涨近8%!甲骨文的业绩及指引超预期?
-
近期,美股的AI概念股迎来了一波上升行情,微软(MSFT.US)频创新高,英伟达(NVDA.US)、台积电(TSM.US)、博通(AVGO.US)、甲骨文(ORCL.US)等多股亦出现显著上涨。而从基...
- 甲骨文预计新财年云基础设施营收将涨超70%,盘后一度涨8% | 财报见闻
-
甲骨文(Oracle)周三盘后公布财报显示,该公司第四财季业绩超预期,虽然云基建略微逊于预期,但管理层预计2026财年云基础设施营收预计将增长超过70%,同时资本支出继上年猛增三倍后,新财年将继续增至...
- Springboot数据访问(整合MongoDB)
-
SpringBoot整合MongoDB基本概念MongoDB与我们之前熟知的关系型数据库(MySQL、Oracle)不同,MongoDB是一个文档数据库,它具有所需的可伸缩性和灵活性,以及所需的查询和...
- Linux环境下,Jmeter压力测试的搭建及报错解决方法
-
概述 Jmeter最早是为了测试Tomcat的前身JServ的执行效率而诞生的。到目前为止,它的最新版本是5.3,其测试能力也不再仅仅只局限于对于Web服务器的测试,而是涵盖了数据库、JM...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- Dubai's AI Boom Lures Global Tech as Emirate Reinvents Itself as Middle East's Silicon Gateway
- OpenAI Releases o3-pro, Cuts o3 Prices by 80% as Deal with Google Cloud Reported to Make for Compute Needs
- 黄仁勋说AI Agent才是未来!但究竟有些啥影响?
- 商城微服务项目组件搭建(五)——Kafka、Tomcat等安装部署
- Python+Appium环境搭建与自动化教程
- 零配置入门:用VSCode写Java代码的正确姿
- Mycat的搭建以及配置与启动(mycat2)
- kubernetes 部署mysql应用(k8s mysql部署)
- Spring Data Jpa 介绍和详细入门案例搭建
- 量子点格棋上线!“天衍”邀您执子入局
- 标签列表
-
- oracle位图索引 (74)
- oracle批量插入数据 (65)
- oracle事务隔离级别 (59)
- oracle 空为0 (51)
- oracle主从同步 (56)
- oracle 乐观锁 (53)
- redis 命令 (78)
- php redis (88)
- redis 存储 (66)
- redis 锁 (69)
- 启动 redis (66)
- redis 时间 (56)
- redis 删除 (67)
- redis内存 (57)
- redis并发 (52)
- redis 主从 (69)
- redis 订阅 (51)
- redis 登录 (54)
- redis 面试 (58)
- 阿里 redis (59)
- redis 搭建 (53)
- redis的缓存 (55)
- lua redis (58)
- redis 连接池 (61)
- redis 限流 (51)