Redis 使用docker搭建集群环境(哨兵模式)

分类:软件编程
阅读:1033
作者:majingjing
发布:2021-11-30 23:46

哨兵模式

工作机制

image-20211130232001698

Redis Sentinel 基于主从复制模式,只是引入了哨兵来监控与自动处理故障。

  1. 监控master、slave是否正常运行
  2. 当master出现故障时,能自动将一个slave转换为master
  3. 多个哨兵可以监控同一个Redis,哨兵之间也会自动监控

优缺点

优点:

  1. 哨兵模式基于主从复制模式,所以主从复制模式有的优点,哨兵模式也有
  2. 哨兵模式下,master挂掉可以自动进行切换,系统可用性更高

缺点:

  1. 同样也继承了主从模式难以在线扩容的缺点,Redis的容量受限于单机配置
  2. 需要额外的资源来启动sentinel进程,实现相对复杂一点,同时slave节点作为备份节点不提供服务

集群搭建

image-20211130233608807

目录结构如上图
sentinel.conf 内容
  1. port 26379
  2. dir /tmp
  3. sentinel monitor mymaster 172.18.0.2 6379 2
  4. sentinel auth-pass mymaster 12345678
  5. sentinel down-after-milliseconds mymaster 30000
  6. sentinel parallel-syncs mymaster 1
  7. sentinel failover-timeout mymaster 180000
  8. sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 172.18.0.2 6379 2
此处ip获取方式
执行命令: docker inspect redis-master
可以找到 “IPAddress”: “172.18.0.2”

拷贝3份文件
  1. cp sentinel.conf sentinel1.conf
  2. cp sentinel.conf sentinel2.conf
  3. cp sentinel.conf sentinel3.conf
docker-compose.yml 内容
  1. version: '3'
  2. services:
  3. sentinel1:
  4. image: redis:6.2.6
  5. container_name: redis-sentinel-1
  6. ports:
  7. - 26379:26379
  8. command: redis-sentinel /usr/local/etc/redis/sentinel.conf
  9. volumes:
  10. - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
  11. sentinel2:
  12. image: redis:6.2.6
  13. container_name: redis-sentinel-2
  14. ports:
  15. - 26380:26379
  16. command: redis-sentinel /usr/local/etc/redis/sentinel.conf
  17. volumes:
  18. - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
  19. sentinel3:
  20. image: redis:6.2.6
  21. container_name: redis-sentinel-3
  22. ports:
  23. - 26381:26379
  24. command: redis-sentinel /usr/local/etc/redis/sentinel.conf
  25. volumes:
  26. - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
  27. networks:
  28. default:
  29. external:
  30. name: majj
执行命令 docker-compose up -d

image-20211130234453617

验证

image-20211130235313528

image-20211201211632617

image-20211201213711697

image-20211201214615120

image-20211201214846555

image-20211201221519211

验证已经完成了, 我们看到哨兵模式已经生效了.