统一看下master跟slave的日志吧。
=========master==========
4848:M 06 Aug 09:20:10.814 # Server started, Redis version 3.0.3
4848:M 06 Aug 09:20:10.814 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4848:M 06 Aug 09:20:10.814 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4848:M 06 Aug 09:20:10.814 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4848:M 06 Aug 09:20:10.814 * DB loaded from disk: 0.000 seconds
4848:M 06 Aug 09:20:10.814 * The server is now ready to accept connections on port 6379
4848:M 06 Aug 09:27:49.448 * Slave 10.117.41.36:6379 asks for synchronization
4848:M 06 Aug 09:27:49.449 * Full resync requested by slave 10.117.41.36:6379
4848:M 06 Aug 09:27:49.449 * Starting BGSAVE for SYNC with target: disk
4848:M 06 Aug 09:27:49.449 * Background saving started by pid 4856
4856:C 06 Aug 09:27:49.460 * DB saved on disk
4856:C 06 Aug 09:27:49.460 * RDB: 6 MB of memory used by copy-on-write
4848:M 06 Aug 09:27:49.548 * Background saving terminated with success
4848:M 06 Aug 09:27:49.548 * Synchronization with slave 10.117.41.36:6379 succeeded
=========end==========
========slave==========
28060:S 06 Aug 09:27:49.444 # Server started, Redis version 3.0.3
28060:S 06 Aug 09:27:49.444 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
28060:S 06 Aug 09:27:49.444 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
28060:S 06 Aug 09:27:49.444 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
28060:S 06 Aug 09:27:49.444 * The server is now ready to accept connections on port 6379
28060:S 06 Aug 09:27:49.444 * Connecting to MASTER 10.117.41.242:6379
28060:S 06 Aug 09:27:49.444 * MASTER <-> SLAVE sync started
28060:S 06 Aug 09:27:49.447 * Non blocking connect for SYNC fired the event.
28060:S 06 Aug 09:27:49.448 * Master replied to PING, replication can continue...
28060:S 06 Aug 09:27:49.448 * Partial resynchronization not possible (no cached master)
28060:S 06 Aug 09:27:49.449 * Full resync from master: 88f015eff5a360a7432f5ed9f9e92556dadd0eb5:1
28060:S 06 Aug 09:27:49.549 * MASTER <-> SLAVE sync: receiving 38 bytes from master
28060:S 06 Aug 09:27:49.549 * MASTER <-> SLAVE sync: Flushing old data
28060:S 06 Aug 09:27:49.549 * MASTER <-> SLAVE sync: Loading DB in memory
28060:S 06 Aug 09:27:49.549 * MASTER <-> SLAVE sync: Finished with success
=============end================ 8.查看主从同步状态:
[iyunv@yunwei_dev01 ~]# /data/install/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a test123 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.117.41.36,port=6379,state=online,offset=969,lag=1
master_repl_offset:969
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:968
[iyunv@yunwei_dev02 ~]# /data/install/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a test123 info Replication
# Replication
role:slave
master_host:10.117.41.242
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:955
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
9.查看主从数据
[iyunv@yunwei_dev02 redis]# /data/install/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a test123
127.0.0.1:6379> get myname
“brucefeng"
[iyunv@yunwei_dev01 ~]# /data/install/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a test123
127.0.0.1:6379> set name_a "jack"
OK
[iyunv@yunwei_dev02 ~]# /data/install/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a test123
127.0.0.1:6379> get name_a
“jack" 二.配置Redis Sentinel
Sentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中
Sentinel作用:
1):Master状态检测
2):如果Master异常,则会进行Master-Slave切换,将其中一个Slave作为Master,将之前的Master作为Slave
3):Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换
Sentinel工作方式:
1):每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个 PING 命令
2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sentinel 标记为主观下线。
3):如果一个Master被标记为主观下线,则正在监视这个Master的所有 Sentinel 要以每秒一次的频率确认Master的确进入了主观下线状态。
4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认Master的确进入了主观下线状态, 则Master会被标记为客观下线
5):在一般情况下, 每个 Sentinel 会以每 10 秒一次的频率向它已知的所有Master,Slave发送 INFO 命令
6):当Master被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave 发送 INFO 命令的频率会从 10 秒一次改为每秒一次
7):若没有足够数量的 Sentinel 同意 Master 已经下线, Master 的客观下线状态就会被移除。
若 Master 重新向 Sentinel 的 PING 命令返回有效回复, Master 的主观下线状态就会被移除。
1. 配置文件sentinel.conf
port 26379
daemonize yes
sentinel monitor yunwei_redis_master 10.117.41.242 6379 1
sentinel auth-pass yunwei_redis_master test123