搭建 Redis 集群

  1. version: '3.7'
  2. services:
  3. master:
  4. image: redis
  5. container_name: redis-master
  6. ports:
  7. - 6379:6379
  8. slave1:
  9. image: redis
  10. container_name: redis-slave-1
  11. ports:
  12. - 6380:6379
  13. command: redis-server --slaveof redis-master 6379
  14. slave2:
  15. image: redis
  16. container_name: redis-slave-2
  17. ports:
  18. - 6381:6379
  19. command: redis-server --slaveof redis-master 6379

搭建 Sentinel 集群

  1. version: '3.7'
  2. services:
  3. sentinel1:
  4. image: redis
  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
  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
  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

修改 Sentinel 配置文件

需要三份 sentinel.conf 配置文件,分别为 sentinel1.confsentinel2.confsentinel3.conf,配置文件内容相同

  1. port 26379
  2. dir /tmp
  3. # 自定义集群名,其中 127.0.0.1 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
  4. sentinel monitor mymaster 127.0.0.1 6379 2
  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

查看集群是否生效

  1. docker exec -it redis-sentinel-1 /bin/bash
  2. redis-cli -p 26379
  3. sentinel master mymaster
  4. sentinel slaves mymaster