Redis Sentinel 主从集群部署

节点规划

节点IP 角色 端口
192.168.93.140 Redis Master + Sentinel 6379,26379
192.168.93.141 Redis Slave + Sentinel 6379,26379
192.168.93.142 Redis Slave + Sentinel 6379,26379

主从集群部署

创建目录

mkdir -p /data/redis/{data,conf}

创建redis配置文件

cat > /data/redis/conf/redis.conf << EOF
bind 0.0.0.0
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis/redis-server.pid
loglevel notice
#logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
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
appendonly yes
appendfilename "redis-staging-ao.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
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
aof-rewrite-incremental-fsync yes
requirepass 123456
masterauth 123456

# 从节点配置
#slaveof <master_ip> 6379
EOF

创建哨兵配置文件

cat > /data/redis/conf/sentinel.conf << EOF
daemonize no
pidfile "/var/run/redis/redis-sentinel.pid"
#logfile "/var/log/redis/redis-sentinel.log"
bind 0.0.0.0
port 26379
sentinel monitor mymaster 192.168.93.140 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF

启动容器

docker run -d --name redis-server \
--restart always \
--net host \
-v /etc/localtime:/etc/localtime:ro \
-v /data/redis/data:/data \
-v /data/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /data/redis/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
redis:6.2.10 \
redis-server /usr/local/etc/redis/redis.conf
docker run -d --name redis-sentinel \
--restart always \
--net host \
-v /etc/localtime:/etc/localtime:ro \
-v /data/redis/data:/data \
-v /data/redis/conf:/usr/local/etc/redis \
redis:6.2.10 \
redis-sentinel /usr/local/etc/redis/sentinel.conf

集群验证

查看哨兵配置

docker exec -it redis-sentinel redis-cli -p 26379 sentinel master mymaster

查看集群信息

docker exec -it redis-server redis-cli -p 6379 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.91.142,port=6379,state=online,offset=59312,lag=0
slave1:ip=192.168.91.141,port=6379,state=online,offset=59312,lag=0
master_failover_state:no-failover
master_replid:d6b4569077d75635dfc115546c4197506b337ee6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:59312
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:59312

关闭主节点redis服务,验证从节点是否切换为主节点

docker exec -it redis-sentinel redis-cli -p 6379 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.91.141,port=6379,state=online,offset=84559,lag=1
slave1:ip=192.168.91.140,port=6379,state=online,offset=84716,lag=1
master_failover_state:no-failover
master_replid:3b26ded16e965bd71b7f617b7fd458e55dcdf4f2
master_replid2:d6b4569077d75635dfc115546c4197506b337ee6
master_repl_offset:84716
second_repl_offset:64802
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84716
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/Redis-Sentinel-主从集群部署/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏