Singleton Redis
通过 docker-compose 快速创建单实例 Redis 容器,可以通过 localhost:6379
连接。通过 Volumn 将 redis 配置文件映射到 Docker 容器内,通过配置文件启动容器
version: '3'
services:
redis:
image: redis:4.0.8-alpine
container_name: db-redis
restart: always
ports:
- 6379:6379
networks:
- net_db
volumes:
- ./redis/config:/docker/config
- ./redis/data:/data
command: redis-server /docker/config/redis.conf
networks:
net_db:
external: true
代码 1:Singleton Redis
简单的配置如下所示
bind 0.0.0.0
port 6379
logfile "6379.log"
dbfilename "dump-6379.rdb"
daemonize yes
rdbcompression yes
代码 2:Base Config
Master-Slave Cluster
docker-compose 创建单个容器并没有太大优势,它用于需要多个容器相互协同,共享网络和存储的场景。
首先创建 master 和 slave 的配置文件,对于 master 来说,不需要特殊配置,slave 需要指定从 master 同步。因为使用 docker-compose 创建的容器可以在同一网络呢,通过容器名 redis-master
即可访问到 master 容器。
# master.conf
bind 0.0.0.0
port 6379
dbfilename dump-master.rdb
logfile "my-redis-master.log"
代码 3:master config
bind 0.0.0.0
port 6379
dbfilename dump-slave1.rdb
logfile "my-redis-slave1.log"
replicaof redis-master 6379
代码 4:Slave Config
需要为三个容器指定相同的网络,并且分配不同的端口
version: "3"
services:
redis-master:
container_name: redis-master
image: redis
hostname: redis-master
networks:
- redis-replica-net
volumes:
- ./config/master.conf:/docker/config/redis.conf
- ./master/data:/data
ports:
- "40001:6379"
command: redis-server /docker/config/redis.conf
redis-slave1:
container_name: redis-slave1
image: redis
hostname: redis-slave1
depends_on:
- redis-master
networks:
- redis-replica-net
volumes:
- ./config/slave1.conf:/docker/config/redis.conf
- ./slave1/data:/data
ports:
- "40002:6379"
command: redis-server /docker/config/redis.conf
redis-slave2:
container_name: redis-slave2
image: redis
hostname: redis-slave2
depends_on:
- redis-master
networks:
- redis-replica-net
volumes:
- ./config/slave2.conf:/docker/config/redis.conf
- ./slave2/data:/data
ports:
- "40003:6379"
command: redis-server /docker/config/redis.conf
networks:
redis-replica-net:
driver: bridge
Sentinel Cluster
Redis 提供 Sentinel 机制,提供了自动容错的能力,这里的 Sentinal 节点也需要三个及以上。Master 及 Slave 的配置与原先没有任何区别。
sentinel 中需要指定 master 的 IP 和端口,以及监测 master 存活的过期时间,三个 Sentinal 的配置文件都是相同的。
port 26379
sentinel monitor mymaster redis-master 6379 1
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 10000
protected-mode no
logfile "sentinel.log"
代码 6:Sentianl Config
下面简单介绍一下 Sentinal 的同步过程,在配置文件中,Sentinal 只配置了 Master 节点的端口,所以 Sentinal 启动之后,首先就要连接 Master 节点。
version: "3"
services:
redis-master:
container_name: redis-master
image: redis
hostname: redis-master
networks:
- redis-replica-net
volumes:
- ./config/master.conf:/docker/config/redis.conf
- ./master/data:/data
ports:
- "127.0.0.1:40001:6379"
command: redis-server /docker/config/redis.conf
redis-slave1:
container_name: redis-slave1
image: redis
hostname: redis-slave1
depends_on:
- redis-master
networks:
- redis-replica-net
volumes:
- ./config/slave1.conf:/docker/config/redis.conf
- ./slave1/data:/data
ports:
- "127.0.0.1:40002:6379"
command: redis-server /docker/config/redis.conf
redis-slave2:
container_name: redis-slave2
image: redis
hostname: redis-slave2
depends_on:
- redis-master
- redis-slave1
networks:
- redis-replica-net
volumes:
- ./config/slave2.conf:/docker/config/redis.conf
- ./slave2/data:/data
ports:
- "127.0.0.1:40003:6379"
command: redis-server /docker/config/redis.conf
redis-sentinel1:
container_name: redis-sentinel1
image: redis
hostname: redis-sentinel1
networks:
- redis-replica-net
volumes:
- ./config/sentinel.conf:/docker/config/sentinel.conf
- ./sentinel1/data:/data
ports:
- "127.0.0.1:40004:26379"
command: bash -c "chown redis:redis /docker/config/sentinel.conf && redis-server /docker/config/sentinel.conf --sentinel"
redis-sentinel2:
container_name: redis-sentinel2
image: redis
hostname: redis-sentinel2
networks:
- redis-replica-net
volumes:
- ./config/sentinel.conf:/docker/config/sentinel.conf
- ./sentinel2/data:/data
ports:
- "127.0.0.1:40005:26379"
command: bash -c "chown redis:redis /docker/config/sentinel.conf && redis-server /docker/config/sentinel.conf --sentinel"
redis-sentinel3:
container_name: redis-sentinel3
image: redis
hostname: redis-sentinel3
networks:
- redis-replica-net
volumes:
- ./config/sentinel.conf:/docker/config/sentinel.conf
- ./sentinel3/data:/data
ports:
- "127.0.0.1:40006:26379"
command: bash -c "chown redis:redis /docker/config/sentinel.conf && redis-server /docker/config/sentinel.conf --sentinel"
networks:
redis-replica-net:
driver: bridge
代码 7:Sentinel Cluster