Docker 搭建Redis集群(哨兵模式)
一、简介
Redis集群的哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。 哨兵模式作用:
-
通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。
-
当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。
除了监控Redis服务之外,哨兵之间也会互相监控。本文采用一主、双从、三哨兵方式
二、配置
部署方式为:docker compose:
第一步:创建redis docker-compose.yml配置文件
version: '3.4' services: master: image: redis container_name: redis-master restart: always command: redis-server --port 16380 --requirepass 123456 # 16380 是定义的主库端口,默认:6379; --requirepass 123456 是redis密码。 ports: - 16380:16380 # 将容器的16380端口映射到宿主机的16380端口上,第一个16380为宿主机端口。 slave1: image: redis container_name: redis-slave-1 restart: always command: redis-server --slaveof 127.0.0.1 16380 --port 16381 --requirepass 123456 --masterauth 123456 # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16381 是定义的从库端口,默认:6379; --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。 ports: - 16381:16381 slave2: image: redis container_name: redis-slave-2 restart: always command: redis-server --slaveof 127.0.0.1 16380 --port 16382 --requirepass 123456 --masterauth 123456 # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16382 是定义的从库端口,默认:6379; --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。 ports: - 16382:16382
docker-compose -f docker-compose.yml up -d
第三步:创建sentinel docker-compose.yml sentinel1.conf sentinel2.conf sentinel3.conf配置文件
docker-compose.yml
version: '3.4' services: sentinel1: image: redis container_name: redis-sentinel-1 command: redis-sentinel /home/ec2-user/dockerfile/sentinel/sentinel.conf # 自定义路径,可更改,但是需要和volumes中的路径相同。 restart: always ports: - 26380:26380 volumes: - ./sentinel1.conf:/home/ec2-user/dockerfile/sentinel/sentinel.conf # 自定义路径,可更改,但是需要和command中的路径相同。 sentinel2: image: redis container_name: redis-sentinel-2 command: redis-sentinel /home/ec2-user/dockerfile/sentinel/sentine2.conf restart: always ports: - 26381:26381 volumes: - ./sentinel2.conf:/home/ec2-user/dockerfile/sentinel/sentine2.conf sentinel3: image: redis container_name: redis-sentinel-3 command: redis-sentinel /home/ec2-user/dockerfile/sentinel/sentine3.conf restart: always ports: - 26382:26382 volumes: - ./sentinel3.conf:/home/ec2-user/dockerfile/sentinel/sentine3.conf
sentinel1.conf
port 26380 daemonize no pidfile /var/run/redis-sentinel.pid dir /tmp sentinel monitor mymaster 127.0.0.1 16380 2 # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换 sentinel auth-pass mymaster 123456 # 主机密码 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
sentinel2.conf
port 26381 daemonize no pidfile /var/run/redis-sentinel.pid dir /tmp sentinel monitor mymaster 127.0.0.1 16380 2 # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换 sentinel auth-pass mymaster 123456 # 主机密码 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
sentinel3.conf
port 26382 daemonize no pidfile /var/run/redis-sentinel.pid dir /tmp sentinel monitor mymaster 127.0.0.1 16380 2 # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换 sentinel auth-pass mymaster 123456 # 主机密码 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
docker-compose -f docker-compose.yml up -d
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16933475.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步