docker-compose 搭建 Redis Sentinel 测试环境

本文介绍如何使用 docker-compose 快速搭建一个 Redis Sentinel 测试环境。其中 Redis 集群为 1 个 master 节点,2 个 slave 节点,Sentinel 为 3 个节点。请勿用于生产环境。

主机环境

系统 centos 7.5,ip 192.168.223.111,docker 1.13.1,docker-compose 1.23.2,redis 5.0.5。

创建配置文件

redis1.conf 内容:

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

redis2.conf 内容:

bind 0.0.0.0
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

redis3.conf 内容:

bind 0.0.0.0
protected-mode yes
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6381.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

sentinel1.conf 内容:

port 26379
sentinel monitor mymaster 192.168.223.111 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel2.conf 内容:

port 26380
sentinel monitor mymaster 192.168.223.111 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel3.conf 内容:

port 26381
sentinel monitor mymaster 192.168.223.111 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

创建 docker-compose 文件

内容如下:

version: '3.0'
services:
master:
image: redis
container_name: redis-master
ports:
- 6379:6379
network_mode: host
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis1.conf:/usr/local/etc/redis/redis.conf slave1:
image: redis
container_name: redis-slave-1
ports:
- 6380:6380
network_mode: host
command: redis-server /usr/local/etc/redis/redis.conf --slaveof 192.168.223.111 6379
volumes:
- ./redis2.conf:/usr/local/etc/redis/redis.conf slave2:
image: redis
container_name: redis-slave-2
ports:
- 6381:6381
network_mode: host
command: redis-server /usr/local/etc/redis/redis.conf --slaveof 192.168.223.111 6379
volumes:
- ./redis3.conf:/usr/local/etc/redis/redis.conf sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- 26379:26379
network_mode: host
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- 26380:26380
network_mode: host
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26381
network_mode: host
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

因为要从其他地方连接测试环境,故使用 network_mode: host 参数直接使用宿主机网络。

启动、停止

启动所有服务

$ docker-compose up -d

停止某一服务

$ docker-compose stop master

启动某一服务

$ docker-compose start master

停止并删除所有服务

$ docker-compose down

验证

查看集群 master 与 slaves 信息

$ docker run -it --rm redis redis-cli -h 192.168.223.111 -p 26379
192.168.223.111:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
... 192.168.223.111:26379> sentinel slaves mymaster
1) 1) "name"
2) "127.0.0.1:6380"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6380"
...

查看主机角色

$ docker run -it --rm redis redis-cli -h 192.168.223.111 -p 6379
192.168.223.111:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=324122,lag=0
slave1:ip=127.0.0.1,port=6380,state=online,offset=324122,lag=1
master_replid:94984a18426200e4f785b02ed46da6177a6bdd6f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:324388
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:324388
... $ docker run -it --rm redis redis-cli -h 192.168.223.111 -p 6380
192.168.223.111:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:346620
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:94984a18426200e4f785b02ed46da6177a6bdd6f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:346620
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:346620
...

最新文章

  1. android studio 2.0 GPU Debugger使用说明
  2. linux启动流程
  3. 安装jdk For Windows
  4. SQL server 表之间的关系生成图
  5. XAML
  6. 虚拟机移动后重启网络时提示Device does not seem to be present
  7. gvim设置字体和隐藏菜单栏工具栏
  8. 前端性能优化(三)——传统 JavaScript 优化的误区
  9. ASP.NET优化性能方法之一禁用调试模式(转)
  10. C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)
  11. [CSS3] 学习笔记-CSS定位
  12. vba读文本如果文本文件太大会提示错误!
  13. Android JNI 学习(十):String Operations Api & Other Apis
  14. leecode第一百五十五题(最小栈)
  15. selenium 安装
  16. jquery使用ajax提交form表单
  17. TableStore:单行操作
  18. 《SQL Server 2008从入门到精通》--20180723
  19. Linux磁盘处理
  20. day 53 Django form 组件 autu 组件

热门文章

  1. char指针类型的传值和传址
  2. java对象实例化 静态块,对象块,构造函数执行顺序
  3. 【刷题笔记】LeetCode 48. Rotate Image
  4. 关于linux的一些常用的指令
  5. mac bash上显示git分支与状态
  6. static的变量是放在哪里
  7. hibernate 常用注解
  8. centos7.3安装wordpress
  9. WebAPI 生成验证码
  10. setclock - 用系统时间来设置硬件时间