redis集群学习笔记1

redis 主从

redis1.conf

daemonize yes
pidfile /var/run/redis-6380.pid
logfile /var/log/redis/redis-6380.log
port 6380
bind 0.0.0.0
timeout 300
databases 16
dbfilename dump-6380.db
dir /var/lib/redis
masterauth 123456
requirepass 123456

redis2.conf

daemonize yes
pidfile /var/run/redis-6381.pid
logfile /var/log/redis/redis-6381.log
port 6381
bind 0.0.0.0
timeout 300
databases 16
dbfilename dump-6381.db
dir /var/lib/redis
masterauth 123456
requirepass 123456
slaveof 127.0.0.1 6380

启动主从

redis-server /home/ubuntu/code/redis/redis1.conf
redis-server /home/ubuntu/code/redis/redis2.conf

redis sentinel

# 哨兵sentinel实例运行的端口,默认26379  
port 26379
# 哨兵sentinel的工作目录
dir /var/lib/redis

# 哨兵sentinel监控的redis主节点的 
## ip:主机ip地址
## port:哨兵端口号
## master-name:可以自己命名的主节点名字(只能由字母A-z、数字0-9 、这三个字符".-_"组成。)
## quorum:当这些quorum个数sentinel哨兵认为master主节点失联 那么这时 客观上认为主节点失联了  
# sentinel monitor      
sentinel monitor mymaster 127.0.0.1 6380 2

# 当在Redis实例中开启了requirepass ,所有连接Redis实例的客户端都要提供密码。
# sentinel auth-pass    
sentinel auth-pass mymaster 123456  

# 指定主节点应答哨兵sentinel的最大时间间隔,超过这个时间,哨兵主观上认为主节点下线,默认30秒  
# sentinel down-after-milliseconds  
sentinel down-after-milliseconds mymaster 30000  

# 指定了在发生failover主备切换时,最多可以有多少个slave同时对新的master进行同步。这个数字越小,完成failover所需的时间就越长;反之,但是如果这个数字越大,就意味着越多的slave因为replication而不可用。可以通过将这个值设为1,来保证每次只有一个slave,处于不能处理命令请求的状态。
# sentinel parallel-syncs  
sentinel parallel-syncs mymaster 1  

# 故障转移的超时时间failover-timeout,默认三分钟,可以用在以下这些方面:
## 1. 同一个sentinel对同一个master两次failover之间的间隔时间。  
## 2. 当一个slave从一个错误的master那里同步数据时开始,直到slave被纠正为从正确的master那里同步数据时结束。  
## 3. 当想要取消一个正在进行的failover时所需要的时间。
## 4.当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来同步数据了
# sentinel failover-timeout    
sentinel failover-timeout mymaster 180000

# 当sentinel有任何警告级别的事件发生时(比如说redis实例的主观失效和客观失效等等),将会去调用这个脚本。一个脚本的最大执行时间为60s,如果超过这个时间,脚本将会被一个SIGKILL信号终止,之后重新执行。
# 对于脚本的运行结果有以下规则:  
## 1. 若脚本执行后返回1,那么该脚本稍后将会被再次执行,重复次数目前默认为10。
## 2. 若脚本执行后返回2,或者比2更高的一个返回值,脚本将不会重复执行。  
## 3. 如果脚本在执行过程中由于收到系统中断信号被终止了,则同返回值为1时的行为相同。
# sentinel notification-script    
sentinel notification-script mymaster /home/ubuntu/code/redis/notify.sh

# 这个脚本应该是通用的,能被多次调用,不是针对性的。
# sentinel client-reconfig-script  
sentinel client-reconfig-script mymaster /home/ubuntu/code/redis/reconfig.sh

Redis Sentinel的节点规划

角色 IP地址 端口号
Redis Master 127.0.0.1 6380
Redis Slave1 127.0.0.1 6381
Redis Slave2 127.0.0.1 6382
Redis Sentinel1 127.0.0.1 6390
Redis Sentinel2 127.0.0.1 6391
Redis Sentinel3 127.0.0.1 6392

redis节点配置文件

redis-6380.conf

masterauth 123456

requirepass 123456

bind 127.0.0.1

protected-mode yes

port 6380

pidfile /var/run/redis_6380.pid

logfile /var/log/redis/redis-6380.log

dbfilename dump-6380.rdb

dir ./

timeout 300

tcp-backlog 511

tcp-keepalive 300

daemonize yes

supervised no

loglevel notice

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

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

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-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 no

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

redis-6381.conf

slaveof 127.0.0.1 6380

masterauth 123456

requirepass 123456

bind 127.0.0.1

protected-mode yes

port 6381

pidfile /var/run/redis_6381.pid

logfile /var/log/redis/redis-6381.log

dbfilename dump-6381.rdb

dir ./

timeout 300

tcp-backlog 511

tcp-keepalive 300

daemonize yes

supervised no

loglevel notice

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

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

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-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 no

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

redis-6382.conf

slaveof 127.0.0.1 6380

masterauth 123456

requirepass 123456

bind 127.0.0.1

protected-mode yes

port 6382

pidfile /var/run/redis_6382.pid

logfile /var/log/redis/redis-6382.log

dbfilename dump-6382.rdb

dir ./

timeout 300

tcp-backlog 511

tcp-keepalive 300

daemonize yes

supervised no

loglevel notice

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

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

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-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 no

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

分别启动

redis-server /home/ubuntu/code/redis/redis-6380.conf
redis-server /home/ubuntu/code/redis/redis-6381.conf
redis-server /home/ubuntu/code/redis/redis-6382.conf

查看redis启动日志

31738:C 09 May 22:05:50.449 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31738:C 09 May 22:05:50.449 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=31738, just started
31738:C 09 May 22:05:50.449 # Configuration loaded
31739:M 09 May 22:05:50.451 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.11 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6380
 |    `-._   `._    /     _.-'    |     PID: 31739
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

31739:M 09 May 22:05:50.452 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
31739:M 09 May 22:05:50.452 # Server initialized
31739:M 09 May 22:05:50.452 # 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.
31739:M 09 May 22:05:50.452 # 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.
31739:M 09 May 22:05:50.452 * DB loaded from disk: 0.000 seconds
31739:M 09 May 22:05:50.452 * Ready to accept connections
31739:M 09 May 22:06:02.140 * Slave 127.0.0.1:6381 asks for synchronization
31739:M 09 May 22:06:02.140 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '9050096d712711f78c0365e1867a81024cd2c012', my replication IDs are 'd5507ce74c22213c19c39a22c1457b5c2b990806' and '0000000000000000000000000000000000000000')
31739:M 09 May 22:06:02.140 * Starting BGSAVE for SYNC with target: disk
31739:M 09 May 22:06:02.140 * Background saving started by pid 31782
31782:C 09 May 22:06:02.146 * DB saved on disk
31782:C 09 May 22:06:02.146 * RDB: 0 MB of memory used by copy-on-write
31739:M 09 May 22:06:02.172 * Background saving terminated with success
31739:M 09 May 22:06:02.172 * Synchronization with slave 127.0.0.1:6381 succeeded
31739:M 09 May 22:06:04.771 * Slave 127.0.0.1:6382 asks for synchronization
31739:M 09 May 22:06:04.771 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '9050096d712711f78c0365e1867a81024cd2c012', my replication IDs are 'f58e944b1d6773a840cfa360f14d3d5cfde39046' and '0000000000000000000000000000000000000000')
31739:M 09 May 22:06:04.771 * Starting BGSAVE for SYNC with target: disk
31739:M 09 May 22:06:04.771 * Background saving started by pid 31794
31794:C 09 May 22:06:04.777 * DB saved on disk
31794:C 09 May 22:06:04.777 * RDB: 0 MB of memory used by copy-on-write
31739:M 09 May 22:06:04.878 * Background saving terminated with success
31739:M 09 May 22:06:04.878 * Synchronization with slave 127.0.0.1:6382 succeeded

以下两行日志日志表明,redis-6380 作为 Redis主节点redis-6381redis-6382 作为 从节点,从 主节点 同步数据。

31739:M 09 May 22:06:02.140 * Slave 127.0.0.1:6381 asks for synchronization
31739:M 09 May 22:06:04.771 * Slave 127.0.0.1:6382 asks for synchronization

Sentinel配置管理

sentinel-6390.conf

daemonize yes

tcp-backlog 511
# Example sentinel.conf

# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
protected-mode no

# port 
# The port that this sentinel instance will run on
port 6390

# sentinel announce-ip 
# sentinel announce-port 
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4

# dir 
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir ./

# sentinel monitor    
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least  sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 127.0.0.1 6380 2

# sentinel auth-pass  
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
sentinel auth-pass mymaster 123456

# sentinel down-after-milliseconds  
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 5000

# sentinel parallel-syncs  
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster 1

# sentinel failover-timeout  
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
#   already tried against the same master by a given Sentinel, is two
#   times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
#   to a Sentinel current configuration, to be forced to replicate
#   with the right master, is exactly the failover timeout (counting since
#   the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
#   did not produced any configuration change (SLAVEOF NO ONE yet not
#   acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
#   reconfigured as slaves of the new master. However even after this time
#   the slaves will be reconfigured by the Sentinels anyway, but not with
#   the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel failover-timeout mymaster 180000

# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.

# NOTIFICATION SCRIPT
#
# sentinel notification-script  
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh

# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script  
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
#       
#
#  is currently always "failover"
#  is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh

# SECURITY
#
# By default SENTINEL SET will not be able to change the notification-script
# and client-reconfig-script at runtime. This avoids a trivial security issue
# where clients can set the script to anything and trigger a failover in order
# to get the program executed.

sentinel deny-scripts-reconfig yes

logfile /var/log/redis/sentinel-6390.log

sentinel-6391.conf

daemonize yes

tcp-backlog 511
# Example sentinel.conf

# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
protected-mode no

# port 
# The port that this sentinel instance will run on
port 6390

# sentinel announce-ip 
# sentinel announce-port 
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4

# dir 
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir ./

# sentinel monitor    
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least  sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 127.0.0.1 6380 2

# sentinel auth-pass  
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
sentinel auth-pass mymaster 123456

# sentinel down-after-milliseconds  
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 5000

# sentinel parallel-syncs  
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster 1

# sentinel failover-timeout  
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
#   already tried against the same master by a given Sentinel, is two
#   times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
#   to a Sentinel current configuration, to be forced to replicate
#   with the right master, is exactly the failover timeout (counting since
#   the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
#   did not produced any configuration change (SLAVEOF NO ONE yet not
#   acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
#   reconfigured as slaves of the new master. However even after this time
#   the slaves will be reconfigured by the Sentinels anyway, but not with
#   the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel failover-timeout mymaster 180000

# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.

# NOTIFICATION SCRIPT
#
# sentinel notification-script  
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh

# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script  
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
#       
#
#  is currently always "failover"
#  is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh

# SECURITY
#
# By default SENTINEL SET will not be able to change the notification-script
# and client-reconfig-script at runtime. This avoids a trivial security issue
# where clients can set the script to anything and trigger a failover in order
# to get the program executed.

sentinel deny-scripts-reconfig yes

logfile /var/log/redis/sentinel-6391.log

sentinel-6392.conf

daemonize yes

tcp-backlog 511
# Example sentinel.conf

# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
protected-mode no

# port 
# The port that this sentinel instance will run on
port 6392

# sentinel announce-ip 
# sentinel announce-port 
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4

# dir 
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir ./

# sentinel monitor    
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least  sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 127.0.0.1 6380 2

# sentinel auth-pass  
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
sentinel auth-pass mymaster 123456

# sentinel down-after-milliseconds  
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 5000

# sentinel parallel-syncs  
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster 1

# sentinel failover-timeout  
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
#   already tried against the same master by a given Sentinel, is two
#   times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
#   to a Sentinel current configuration, to be forced to replicate
#   with the right master, is exactly the failover timeout (counting since
#   the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
#   did not produced any configuration change (SLAVEOF NO ONE yet not
#   acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
#   reconfigured as slaves of the new master. However even after this time
#   the slaves will be reconfigured by the Sentinels anyway, but not with
#   the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel failover-timeout mymaster 180000

# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.

# NOTIFICATION SCRIPT
#
# sentinel notification-script  
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh

# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script  
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
#       
#
#  is currently always "failover"
#  is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh

# SECURITY
#
# By default SENTINEL SET will not be able to change the notification-script
# and client-reconfig-script at runtime. This avoids a trivial security issue
# where clients can set the script to anything and trigger a failover in order
# to get the program executed.

sentinel deny-scripts-reconfig yes

logfile /var/log/redis/sentinel-6392.log

分别启动Sentinel

sudo redis-sentinel /home/ubuntu/code/redis/sentinel-6390.conf
sudo redis-sentinel /home/ubuntu/code/redis/sentinel-6391.conf
sudo redis-sentinel /home/ubuntu/code/redis/sentinel-6392.conf

查看进程

root@VM-154-193-ubuntu:/home/ubuntu/code/redis# ps -ef | grep redis
root      3498     1  0 22:31 ?        00:00:00 redis-sentinel *:6390 [sentinel]
root      3618     1  0 22:32 ?        00:00:00 redis-sentinel *:6392 [sentinel]
root      3726     1  0 22:33 ?        00:00:00 redis-sentinel *:6391 [sentinel]
...

查看日志

节点 6390

3497:X 09 May 22:31:53.825 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3497:X 09 May 22:31:53.825 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=3497, just started
3497:X 09 May 22:31:53.825 # Configuration loaded
3498:X 09 May 22:31:53.826 * Increased maximum number of open files to 10032 (it was originally set to 1024).
3498:X 09 May 22:31:53.827 * Running mode=sentinel, port=6390.
3498:X 09 May 22:31:53.827 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3498:X 09 May 22:31:53.827 # Sentinel ID is e5b34748b7c26fdb4e5575a298cc7bd1e3088922
3498:X 09 May 22:31:53.827 # +monitor master mymaster 127.0.0.1 6380 quorum 2
3498:X 09 May 22:32:45.597 * +sentinel sentinel 1638b4200273bd1afac76b41b4a84a974961a9b2 127.0.0.1 6392 @ mymaster 127.0.0.1 6380
3498:X 09 May 22:33:28.784 * +sentinel sentinel 4d142031be715834b1ff9be01e3a02299da6e608 127.0.0.1 6391 @ mymaster 127.0.0.1 6380

6390节点Sentinel ID 为 e5b34748b7c26fdb4e5575a298cc7bd1e3088922, 通过ID把自身加入 sentinel 集群中

节点 6391

3725:X 09 May 22:33:26.715 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3725:X 09 May 22:33:26.715 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=3725, just started
3725:X 09 May 22:33:26.715 # Configuration loaded
3726:X 09 May 22:33:26.716 * Increased maximum number of open files to 10032 (it was originally set to 1024).
3726:X 09 May 22:33:26.717 * Running mode=sentinel, port=6391.
3726:X 09 May 22:33:26.717 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3726:X 09 May 22:33:26.721 # Sentinel ID is 4d142031be715834b1ff9be01e3a02299da6e608
3726:X 09 May 22:33:26.721 # +monitor master mymaster 127.0.0.1 6380 quorum 2
3726:X 09 May 22:33:26.722 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
3726:X 09 May 22:33:26.725 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6380
3726:X 09 May 22:33:27.428 * +sentinel sentinel e5b34748b7c26fdb4e5575a298cc7bd1e3088922 127.0.0.1 6390 @ mymaster 127.0.0.1 6380
3726:X 09 May 22:33:28.231 * +sentinel sentinel 1638b4200273bd1afac76b41b4a84a974961a9b2 127.0.0.1 6392 @ mymaster 127.0.0.1 6380

sentinel-6391 节点的 Sentinel ID4d142031be715834b1ff9be01e3a02299da6e608,并通过 Sentinel ID 把自身加入 sentinel 集群中。此时 sentinel 集群中已有 sentinel-6390sentinel-6391 两个节点。

节点 6392

3617:X 09 May 22:32:43.593 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3617:X 09 May 22:32:43.593 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=3617, just started
3617:X 09 May 22:32:43.593 # Configuration loaded
3618:X 09 May 22:32:43.594 * Increased maximum number of open files to 10032 (it was originally set to 1024).
3618:X 09 May 22:32:43.595 * Running mode=sentinel, port=6392.
3618:X 09 May 22:32:43.595 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3618:X 09 May 22:32:43.600 # Sentinel ID is 1638b4200273bd1afac76b41b4a84a974961a9b2
3618:X 09 May 22:32:43.600 # +monitor master mymaster 127.0.0.1 6380 quorum 2
3618:X 09 May 22:32:43.601 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
3618:X 09 May 22:32:43.604 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6380
3618:X 09 May 22:32:44.707 * +sentinel sentinel e5b34748b7c26fdb4e5575a298cc7bd1e3088922 127.0.0.1 6390 @ mymaster 127.0.0.1 6380
3618:X 09 May 22:33:28.784 * +sentinel sentinel 4d142031be715834b1ff9be01e3a02299da6e608 127.0.0.1 6391 @ mymaster 127.0.0.1 6380

sentinel-6392 节点的 Sentinel ID1638b4200273bd1afac76b41b4a84a974961a9b2,并通过 Sentinel ID 把自身加入 sentinel 集群中。此时 sentinel 集群中已有 sentinel-6390sentinel-6391 和sentinel-6392` 三个节点。

Sentinel配置刷新

现在查看sentinel的配置文件, 会发现自动生成了一些配置信息

sentinel-6390.conf

# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-slave mymaster 127.0.0.1 6382
sentinel known-sentinel mymaster 127.0.0.1 6391 4d142031be715834b1ff9be01e3a02299da6e608
sentinel known-sentinel mymaster 127.0.0.1 6392 1638b4200273bd1afac76b41b4a84a974961a9b2
sentinel current-epoch 0

写入了主从节点的信息 6391 6392

sentinel-6391.conf

# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6382
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 6392 1638b4200273bd1afac76b41b4a84a974961a9b2
sentinel known-sentinel mymaster 127.0.0.1 6390 e5b34748b7c26fdb4e5575a298cc7bd1e3088922
sentinel current-epoch 0

写入了主从节点的信息 6390 6392

sentinel-6392.conf

# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-slave mymaster 127.0.0.1 6382
sentinel known-sentinel mymaster 127.0.0.1 6391 4d142031be715834b1ff9be01e3a02299da6e608
sentinel known-sentinel mymaster 127.0.0.1 6390 e5b34748b7c26fdb4e5575a298cc7bd1e3088922
sentinel current-epoch 0

写入了主从节点的信息 6390 6391

模拟failover

杀掉6380主节点

观察 sentinel 日志信息可以看到重新选举主节点

观察 redis-server的配置文件, 发现发生了变化

6380被杀掉 6381移除了 slaveof 选项 成为了主节点

6382的 slaveof 变为 6381

6380重新启动之后 新增了 slaveof选项, 值为 6381

客户端应该连接哨兵节点

你可能感兴趣的:(NoSQL)