Singleton Redis

通过 docker-compose 快速创建单实例 Redis 容器,可以通过 localhost:6379 连接。通过 Volumn 将 redis 配置文件映射到 Docker 容器内,通过配置文件启动容器

  1. version: '3'
  2. services:
  3. redis:
  4. image: redis:4.0.8-alpine
  5. container_name: db-redis
  6. restart: always
  7. ports:
  8. - 6379:6379
  9. networks:
  10. - net_db
  11. volumes:
  12. - ./redis/config:/docker/config
  13. - ./redis/data:/data
  14. command: redis-server /docker/config/redis.conf
  15. networks:
  16. net_db:
  17. external: true

代码 1:Singleton Redis

简单的配置如下所示

  1. bind 0.0.0.0
  2. port 6379
  3. logfile "6379.log"
  4. dbfilename "dump-6379.rdb"
  5. daemonize yes
  6. rdbcompression yes

代码 2:Base Config

Master-Slave Cluster

docker-compose 创建单个容器并没有太大优势,它用于需要多个容器相互协同,共享网络和存储的场景。

首先创建 master 和 slave 的配置文件,对于 master 来说,不需要特殊配置,slave 需要指定从 master 同步。因为使用 docker-compose 创建的容器可以在同一网络呢,通过容器名 redis-master 即可访问到 master 容器。

  1. # master.conf
  2. bind 0.0.0.0
  3. port 6379
  4. dbfilename dump-master.rdb
  5. logfile "my-redis-master.log"

代码 3:master config

  1. bind 0.0.0.0
  2. port 6379
  3. dbfilename dump-slave1.rdb
  4. logfile "my-redis-slave1.log"
  5. replicaof redis-master 6379

代码 4:Slave Config

需要为三个容器指定相同的网络,并且分配不同的端口

  1. version: "3"
  2. services:
  3. redis-master:
  4. container_name: redis-master
  5. image: redis
  6. hostname: redis-master
  7. networks:
  8. - redis-replica-net
  9. volumes:
  10. - ./config/master.conf:/docker/config/redis.conf
  11. - ./master/data:/data
  12. ports:
  13. - "40001:6379"
  14. command: redis-server /docker/config/redis.conf
  15. redis-slave1:
  16. container_name: redis-slave1
  17. image: redis
  18. hostname: redis-slave1
  19. depends_on:
  20. - redis-master
  21. networks:
  22. - redis-replica-net
  23. volumes:
  24. - ./config/slave1.conf:/docker/config/redis.conf
  25. - ./slave1/data:/data
  26. ports:
  27. - "40002:6379"
  28. command: redis-server /docker/config/redis.conf
  29. redis-slave2:
  30. container_name: redis-slave2
  31. image: redis
  32. hostname: redis-slave2
  33. depends_on:
  34. - redis-master
  35. networks:
  36. - redis-replica-net
  37. volumes:
  38. - ./config/slave2.conf:/docker/config/redis.conf
  39. - ./slave2/data:/data
  40. ports:
  41. - "40003:6379"
  42. command: redis-server /docker/config/redis.conf
  43. networks:
  44. redis-replica-net:
  45. driver: bridge

代码 5:Master Slave Config

Sentinel Cluster

Redis 提供 Sentinel 机制,提供了自动容错的能力,这里的 Sentinal 节点也需要三个及以上。Master 及 Slave 的配置与原先没有任何区别。

sentinel 中需要指定 master 的 IP 和端口,以及监测 master 存活的过期时间,三个 Sentinal 的配置文件都是相同的。

  1. port 26379
  2. sentinel monitor mymaster redis-master 6379 1
  3. sentinel down-after-milliseconds mymaster 3000
  4. sentinel failover-timeout mymaster 10000
  5. protected-mode no
  6. logfile "sentinel.log"

代码 6:Sentianl Config

下面简单介绍一下 Sentinal 的同步过程,在配置文件中,Sentinal 只配置了 Master 节点的端口,所以 Sentinal 启动之后,首先就要连接 Master 节点。

  1. version: "3"
  2. services:
  3. redis-master:
  4. container_name: redis-master
  5. image: redis
  6. hostname: redis-master
  7. networks:
  8. - redis-replica-net
  9. volumes:
  10. - ./config/master.conf:/docker/config/redis.conf
  11. - ./master/data:/data
  12. ports:
  13. - "127.0.0.1:40001:6379"
  14. command: redis-server /docker/config/redis.conf
  15. redis-slave1:
  16. container_name: redis-slave1
  17. image: redis
  18. hostname: redis-slave1
  19. depends_on:
  20. - redis-master
  21. networks:
  22. - redis-replica-net
  23. volumes:
  24. - ./config/slave1.conf:/docker/config/redis.conf
  25. - ./slave1/data:/data
  26. ports:
  27. - "127.0.0.1:40002:6379"
  28. command: redis-server /docker/config/redis.conf
  29. redis-slave2:
  30. container_name: redis-slave2
  31. image: redis
  32. hostname: redis-slave2
  33. depends_on:
  34. - redis-master
  35. - redis-slave1
  36. networks:
  37. - redis-replica-net
  38. volumes:
  39. - ./config/slave2.conf:/docker/config/redis.conf
  40. - ./slave2/data:/data
  41. ports:
  42. - "127.0.0.1:40003:6379"
  43. command: redis-server /docker/config/redis.conf
  44. redis-sentinel1:
  45. container_name: redis-sentinel1
  46. image: redis
  47. hostname: redis-sentinel1
  48. networks:
  49. - redis-replica-net
  50. volumes:
  51. - ./config/sentinel.conf:/docker/config/sentinel.conf
  52. - ./sentinel1/data:/data
  53. ports:
  54. - "127.0.0.1:40004:26379"
  55. command: bash -c "chown redis:redis /docker/config/sentinel.conf && redis-server /docker/config/sentinel.conf --sentinel"
  56. redis-sentinel2:
  57. container_name: redis-sentinel2
  58. image: redis
  59. hostname: redis-sentinel2
  60. networks:
  61. - redis-replica-net
  62. volumes:
  63. - ./config/sentinel.conf:/docker/config/sentinel.conf
  64. - ./sentinel2/data:/data
  65. ports:
  66. - "127.0.0.1:40005:26379"
  67. command: bash -c "chown redis:redis /docker/config/sentinel.conf && redis-server /docker/config/sentinel.conf --sentinel"
  68. redis-sentinel3:
  69. container_name: redis-sentinel3
  70. image: redis
  71. hostname: redis-sentinel3
  72. networks:
  73. - redis-replica-net
  74. volumes:
  75. - ./config/sentinel.conf:/docker/config/sentinel.conf
  76. - ./sentinel3/data:/data
  77. ports:
  78. - "127.0.0.1:40006:26379"
  79. command: bash -c "chown redis:redis /docker/config/sentinel.conf && redis-server /docker/config/sentinel.conf --sentinel"
  80. networks:
  81. redis-replica-net:
  82. driver: bridge

代码 7:Sentinel Cluster