三节点哨兵集群部署redis
docker-compose.yaml
version: '3.1'
services:
redis01:
image: swr.cn-southwest-2.myhuaweicloud.com/vp-guiyang/redis:5.0.0
command: sh -c "redis-server /usr/local/redis/redis.conf; redis-sentinel /usr/local/redis/sentinel.conf"
ports:
- 36379:6379
- 46379:26379
volumes:
- ./redis01/data:/data
- ./redis01/redis.conf:/usr/local/redis/redis.conf
- ./redis01/sentinel.conf:/usr/local/redis/sentinel.conf
restart: always
redis02:
image: swr.cn-southwest-2.myhuaweicloud.com/vp-guiyang/redis:5.0.0
command: sh -c "redis-server /usr/local/redis/redis.conf; redis-sentinel /usr/local/redis/sentinel.conf"
ports:
- 36380:6379
- 46380:26379
volumes:
- ./redis02/data:/data
- ./redis02/redis.conf:/usr/local/redis/redis.conf
- ./redis02/sentinel.conf:/usr/local/redis/sentinel.conf
restart: always
redis03:
image: swr.cn-southwest-2.myhuaweicloud.com/vp-guiyang/redis:5.0.0
command: sh -c "redis-server /usr/local/redis/redis.conf; redis-sentinel /usr/local/redis/sentinel.conf"
ports:
- 36381:6379
- 46381:26379
volumes:
- ./redis03/data:/data
- ./redis03/redis.conf:/usr/local/redis/redis.conf
- ./redis03/sentinel.conf:/usr/local/redis/sentinel.conf
restart: always
redis配置文件需要修改的地方
redis主节点
redis 配置文件
bind 0.0.0.0
protected-mode no
port 36379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis.log"
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 "/data"
masterauth "fkabcd@124"
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
replica-announce-ip "172.18.41.8"
replica-announce-port 36379
requirepass "fkabcd@124"
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 ye
- 将bind 127.0.0.1改为bind 0.0.0.0
- protected-mode yes改为 protected no
- logfile "" 改为 logfile "/var/log/redis.log"(方便日后排查redis故障)
- databases 16 (可以根据需求该需要的数据库数量)
- 如果使用RDB做持久化,可以修改下面的配置 save 900 1 save 300 10 save 60 10000
- dbfilename dump.rdb指定rdb在保存的文件名(可以根据需要修改)
- dir /data 指定dump.rdb存放的目录,需要与挂载目录保持一致。保证数据能正常持久化到硬盘中
- requirepass 配置redis的密码
redis从节点
bind 0.0.0.0
protected-mode no
port 36380
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 "/data"
masterauth "fkabcd@124"
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
replica-announce-ip "172.18.41.8"
replica-announce-port 36380
requirepass "fkabcd@124"
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
replicaof 172.18.41.8 36379
- 在主节点的基础上加入replica-serve-stale-data yes
哨兵的配置修改
bind 0.0.0.0
protected-mode no
port 46379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis-sentinel.log"
dir "/tmp"
sentinel myid 4f0cb523cf658cf7f88bd34907ff6e7f889b45cf
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 172.18.41.8 36379 2
sentinel auth-pass mymaster fkabcd@124
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 3
sentinel known-replica mymaster 172.18.41.8 36380
sentinel known-replica mymaster 172.18.41.8 36381
sentinel known-replica mymaster 192.168.96.1 36380
sentinel known-sentinel mymaster 192.168.96.7 46381 cf930cbf3f06c6601eb294639ed446890e73ada3
sentinel known-sentinel mymaster 192.168.96.3 46380 7dcc1193de11e5adf5f31c340ca4bc14db909b1c
sentinel current-epoch 3
- logfile "" 改为 logfile "/var/log/redis-sentinel.log"(方便日后排查故障)
- sentinel announce-ip 172.18.41.8 (用来告诉其他哨兵我的地址是多少)
- sentinel announce-port 46379 (用来告诉其他哨兵我的端口是多少)
- sentinel monitor mymaster 172.18.41.8 36379 2(配置监控的redis 名字、master的地址、端口、quorum) quorm计算公式:quorum = (n / 2) + 1 (n表示主机数量)
- sentinel auth-pass mymaster 密码 配置主监控的认证密码