Redis一主多从哨兵模式

首先配置一主多从示例如下:

1、两台主机IP地址如下:

主: 192.168.3.81  端口:6379

从:192.168.3.82   端口:6379

从:192.168.3.82   端口:6380 

 

2、首先建立Redis数据存储目录

mkdir /var/lib/data/

3、master(6379端口)Redis配置文件设置

vim /etc/redis-6379.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis_6379.log
databases 16

save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /var/lib/data

masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass lian919


appendonly yes
appendfilename "appendonly_6379.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
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

3、slave(6379端口)配置文件设置

vim /etc/redis-6379.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis_6379.log
databases 16

save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /var/lib/data

slaveof 192.168.3.81 6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456


appendonly yes
appendfilename "appendonly_6379.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
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

 

4、slave(6380端口)配置文件设置

vim /etc/redis-6380.conf

bind 0.0.0.0
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile /var/log/redis/redis_6380.log
databases 16

save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6380.rdb
dir /var/lib/data

slaveof 192.168.3.81 6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456


appendonly yes
appendfilename "appendonly_6380.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
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

注:关于配置文件说明查看:https://www.cnblogs.com/albert919/p/16511082.html

5、启动Redis主和从,启动命令如下

redis主启动命令:

redis-server /etc/redis-6379.conf

redis从(注:6379和6380端口)启动命令

redis-server /etc/redis-6379.conf 

redis-server /etc/redis-6380.conf

6、查看redis是否启动成功命令

ps  -ef |grep redis

注:如果有redis启动进程说明启动成功

 

7、查看从库连接主库是否成功。查看命令使用进入redis交互模式输入 info replication:查看如同示例

 注:redis主从复制故障恢复

  1、redis主库主库出现故障后,直接把从库切换为主库命令为
  slaveof no one

  2、redis如果是一主多从的情况,主库出现故障后,把其中一台从库切换为主库后。其它从库需要重新建立的的主从关系
  slaveof 切换新的主库IP地址 切换新的主库端口

 

二、 哨兵模式配置

设置3个哨兵模式监控redis主从,如果redis主库宕机,至少两个哨兵节点同意才能切换。

3个哨兵分别建立在redis主从上面,redis主架设哨兵1,redis从架设哨兵2,哨兵3

1、哨兵模式端口分别为:

  1、redis主哨兵(IP:192.168.3.81):

    哨兵1端口:26379 (注:配置文件默认端口就是26379)

  2、redis从哨兵(IP: 192.168.3.82)

    哨兵2端口:26379

    哨兵3端口:26380

2、redis主哨兵(192.168.3.81)26379端口配置文件如下:

vim /etc/redis-sentinel-26379.conf

port 26379

protected-mode no

daemonize yes

dir /var/lib/data

logfile /var/log/redis/sentinel_26379.log

sentinel monitor mymaster 192.168.3.81 6379 2

sentinel auth-pass mymaster 123456

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

3、redis从哨兵(IP:192.168.3.82)26379端口配置文件

vim /etc/redis-sentinel-26379.conf

port 26379

protected-mode no

daemonize yes

dir /var/lib/data

logfile /var/log/redis/sentinel_26379.log

sentinel monitor mymaster 192.168.3.81 6379 2

sentinel auth-pass mymaster 123456

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

redis从哨兵(IP:192.168.3.82)26380端口配置文件

vim /etc/redis-sentinel-26380.conf

port 26380

protected-mode no

daemonize yes

dir /var/lib/data

logfile /var/log/redis/sentinel_26380.log

sentinel monitor mymaster 192.168.3.81 6379 2

sentinel auth-pass mymaster 123456

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

注:

  1、sentinel monitor mymaster 192.168.3.81 6379 2

    monitor:代表监控

    mymaster:设置的别名,这个别名可以根据自己进行修改

    192.168.3.81 6379:192.168.3.81:代表的是redis主IP地址 6379:代表的是redis主端口

    2:判断redis主节点失败至少需要2个Sentinel节点同意切换

  2、关于配置文件说明访问:https://www.cnblogs.com/albert919/p/16511082.html

4、哨兵启动命令

26379端口哨兵启动命令:

redis-sentinel /etc/redis-sentinel-26379.conf

26380端口哨兵启动命令

redis-sentinel /etc/redis-sentinel-26380.conf

5、查看哨兵连接状态

redis-cli -p 26379 info sentinel

 

master0:name=mymaster,status=ok,address=192.168.3.81:6379,slaves=2,sentinels=3 红色方框内容说明

name=mymaster  #这里的mymaster显示是配置文件里面sentinel monitor mymaster 192.168.3.81 6379 2 这里的mymaster设置

status=ok #连接状态

address=192.168.3.81:6379 #哨兵监控redis主ip地址和端口

slaves=2 #这里显示有两个redis从节点

sentinels=3 #这里代表有3个哨兵节点

 

 

 

posted @ 2023-02-21 22:12  点点滴滴的努力  阅读(201)  评论(0编辑  收藏  举报