Skip to content

Commit

Permalink
哨兵机制
Browse files Browse the repository at this point in the history
  • Loading branch information
ballooninmyhand committed Apr 6, 2020
1 parent 31c5b11 commit f24f85b
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 17 deletions.
9 changes: 6 additions & 3 deletions docker-compose-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ services:
container_name: redis-master
ports:
- ${REDIS_HOST_PORT}:6379
- ${REDIS_SENTINEL_1_HOST_PORT}:26379
volumes:
- ${REDIS_CONF_DIR}:/etc/redis
- ${REDIS_DATA_DIR}:/data
- ${REDIS_LOG_DIR}:/var/log/redis
restart: always
networks:
default:
ipv4_address: 172.20.0.7
ipv4_address: 172.20.0.4
environment:
TZ: ${TIMEZONE}
privileged: true
Expand All @@ -22,6 +23,7 @@ services:
container_name: redis-slave-1
ports:
- ${REDIS_SLAVE_1_HOST_PORT}:6379
- ${REDIS_SENTINEL_2_HOST_PORT}:26379
volumes:
- ${REDIS_SLAVE_1_CONF_DIR}:/etc/redis
- ${REDIS_SLAVE_1_DATA_DIR}:/data
Expand All @@ -31,7 +33,7 @@ services:
- redis-master
networks:
default:
ipv4_address: 172.20.0.8
ipv4_address: 172.20.0.5
environment:
TZ: ${TIMEZONE}
privileged: true
Expand All @@ -41,6 +43,7 @@ services:
container_name: redis-slave-2
ports:
- ${REDIS_SLAVE_2_HOST_PORT}:6379
- ${REDIS_SENTINEL_3_HOST_PORT}:26379
volumes:
- ${REDIS_SLAVE_2_CONF_DIR}:/etc/redis
- ${REDIS_SLAVE_2_DATA_DIR}:/data
Expand All @@ -50,7 +53,7 @@ services:
- redis-master
networks:
default:
ipv4_address: 172.20.0.9
ipv4_address: 172.20.0.6
environment:
TZ: ${TIMEZONE}
privileged: true
Expand Down
3 changes: 3 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ REDIS_VERSION=latest

######### redis-master ######
REDIS_HOST_PORT=6379
REDIS_SENTINEL_1_HOST_PORT=26379
REDIS_DATA_DIR=./redis-master/data
REDIS_CONF_DIR=./redis-master/conf
REDIS_LOG_DIR=./redis-master/log

######### redis-slave-1 ######
REDIS_SLAVE_1_HOST_PORT=6380
REDIS_SENTINEL_2_HOST_PORT=26380
REDIS_SLAVE_1_DATA_DIR=./redis-slave-1/data
REDIS_SLAVE_1_CONF_DIR=./redis-slave-1/conf
REDIS_SLAVE_1_LOG_DIR=./redis-slave-1/log

######### redis-slave-2 ######
REDIS_SLAVE_2_HOST_PORT=6381
REDIS_SENTINEL_3_HOST_PORT=26381
REDIS_SLAVE_2_DATA_DIR=./redis-slave-2/data
REDIS_SLAVE_2_CONF_DIR=./redis-slave-2/conf
REDIS_SLAVE_2_LOG_DIR=./redis-slave-2/log
Expand Down
8 changes: 5 additions & 3 deletions redis-master/conf/redis.conf
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ supervised no
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
pidfile "/var/run/redis_6379.pid"

# Specify the server verbosity level.
# This can be one of:
Expand Down Expand Up @@ -250,7 +250,7 @@ rdbcompression yes
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb
dbfilename "dump.rdb"

# The working directory.
#
Expand All @@ -260,7 +260,7 @@ dbfilename dump.rdb
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
dir "/data"

################################# REPLICATION #################################

Expand Down Expand Up @@ -1370,3 +1370,5 @@ rdb-save-incremental-fsync yes
# the main dictionary scan
# active-defrag-max-scan-fields 1000

# Generated by CONFIG REWRITE
replicaof 172.20.0.8 6379
35 changes: 35 additions & 0 deletions redis-master/conf/sentinel.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sentinel.conf

# 端口号
port 26379

# 配置哨兵的监控参数
# 格式:sentinel monitor <mastername> <masterip> <masterport> <quorum>
# mastername 是为这个被监控的 master 起的名字
# masterip 是被监控的 master 的 IP 或主机名。因为 Docker 容器之间可以使用容器名访问,所以这里写 master 节点的容器名
# masterport 是被监控节点所监听的端口号
# quorom 设定了当几个哨兵判定这个节点失效后,才认为这个节点真的失效了
sentinel myid f84599e4178829e48714ca7a1418db972414b34c

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
# sentinel auth-pass 172.20.0.7 redis

# master 在连续多长时间无法响应 PING 指令后,就会主观判定节点下线,默认是 30s
# 格式:sentinel down-after-milliseconds <mastername> <milliseconds>
sentinel deny-scripts-reconfig yes

# 日志文件
logfile "/var/log/redis/sentinel.log"

# Generated by CONFIG REWRITE
dir "/data"
sentinel monitor redis-master 172.20.0.8 6379 2
sentinel down-after-milliseconds redis-master 20000
sentinel config-epoch redis-master 1
sentinel leader-epoch redis-master 1
sentinel known-replica redis-master 172.20.0.4 6379
sentinel known-replica redis-master 172.20.0.9 6379
sentinel known-sentinel redis-master 172.20.0.8 26379 3af4477184a27c5cd2b8cffe777305a3e84253a3
sentinel known-sentinel redis-master 172.20.0.9 26379 bbc97991cae04a23433d114fb5550212b8c511f8
sentinel current-epoch 1
3 changes: 2 additions & 1 deletion redis-master/conf/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ echo 511 > /proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 指定配置文件
redis-server /etc/redis/redis.conf
redis-server /etc/redis/redis.conf &
redis-sentinel /etc/redis/sentinel.conf
7 changes: 3 additions & 4 deletions redis-slave-1/conf/redis.conf
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ supervised no
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
pidfile "/var/run/redis_6379.pid"

# Specify the server verbosity level.
# This can be one of:
Expand Down Expand Up @@ -250,7 +250,7 @@ rdbcompression yes
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb
dbfilename "dump.rdb"

# The working directory.
#
Expand All @@ -260,7 +260,7 @@ dbfilename dump.rdb
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
dir "/data"

################################# REPLICATION #################################

Expand All @@ -283,7 +283,6 @@ dir ./
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
#
replicaof redis-master 6379

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
Expand Down
35 changes: 35 additions & 0 deletions redis-slave-1/conf/sentinel.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sentinel.conf

# 端口号
port 26379

# 配置哨兵的监控参数
# 格式:sentinel monitor <mastername> <masterip> <masterport> <quorum>
# mastername 是为这个被监控的 master 起的名字
# masterip 是被监控的 master 的 IP 或主机名。因为 Docker 容器之间可以使用容器名访问,所以这里写 master 节点的容器名
# masterport 是被监控节点所监听的端口号
# quorom 设定了当几个哨兵判定这个节点失效后,才认为这个节点真的失效了
sentinel myid 3af4477184a27c5cd2b8cffe777305a3e84253a3

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
# sentinel auth-pass 172.20.0.7 redis

# master 在连续多长时间无法响应 PING 指令后,就会主观判定节点下线,默认是 30s
# 格式:sentinel down-after-milliseconds <mastername> <milliseconds>
sentinel deny-scripts-reconfig yes

# 日志文件
logfile "/var/log/redis/sentinel.log"

# Generated by CONFIG REWRITE
dir "/data"
sentinel monitor redis-master 172.20.0.8 6379 2
sentinel down-after-milliseconds redis-master 20000
sentinel config-epoch redis-master 1
sentinel leader-epoch redis-master 1
sentinel known-replica redis-master 172.20.0.4 6379
sentinel known-replica redis-master 172.20.0.9 6379
sentinel known-sentinel redis-master 172.20.0.9 26379 bbc97991cae04a23433d114fb5550212b8c511f8
sentinel known-sentinel redis-master 172.20.0.4 26379 f84599e4178829e48714ca7a1418db972414b34c
sentinel current-epoch 1
3 changes: 2 additions & 1 deletion redis-slave-1/conf/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ echo 511 > /proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 指定配置文件
redis-server /etc/redis/redis.conf
redis-server /etc/redis/redis.conf &
redis-sentinel /etc/redis/sentinel.conf
8 changes: 4 additions & 4 deletions redis-slave-2/conf/redis.conf
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ supervised no
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
pidfile "/var/run/redis_6379.pid"

# Specify the server verbosity level.
# This can be one of:
Expand Down Expand Up @@ -250,7 +250,7 @@ rdbcompression yes
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb
dbfilename "dump.rdb"

# The working directory.
#
Expand All @@ -260,7 +260,7 @@ dbfilename dump.rdb
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
dir "/data"

################################# REPLICATION #################################

Expand All @@ -283,7 +283,7 @@ dir ./
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
#
replicaof redis-master 6379
replicaof 172.20.0.8 6379

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
Expand Down
35 changes: 35 additions & 0 deletions redis-slave-2/conf/sentinel.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sentinel.conf

# 端口号
port 26379

# 配置哨兵的监控参数
# 格式:sentinel monitor <mastername> <masterip> <masterport> <quorum>
# mastername 是为这个被监控的 master 起的名字
# masterip 是被监控的 master 的 IP 或主机名。因为 Docker 容器之间可以使用容器名访问,所以这里写 master 节点的容器名
# masterport 是被监控节点所监听的端口号
# quorom 设定了当几个哨兵判定这个节点失效后,才认为这个节点真的失效了
sentinel myid bbc97991cae04a23433d114fb5550212b8c511f8

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
# sentinel auth-pass 172.20.0.7 redis

# master 在连续多长时间无法响应 PING 指令后,就会主观判定节点下线,默认是 30s
# 格式:sentinel down-after-milliseconds <mastername> <milliseconds>
sentinel deny-scripts-reconfig yes

# 日志文件
logfile "/var/log/redis/sentinel.log"

# Generated by CONFIG REWRITE
dir "/data"
sentinel monitor redis-master 172.20.0.8 6379 2
sentinel down-after-milliseconds redis-master 20000
sentinel config-epoch redis-master 1
sentinel leader-epoch redis-master 1
sentinel known-replica redis-master 172.20.0.4 6379
sentinel known-replica redis-master 172.20.0.9 6379
sentinel known-sentinel redis-master 172.20.0.8 26379 3af4477184a27c5cd2b8cffe777305a3e84253a3
sentinel known-sentinel redis-master 172.20.0.4 26379 f84599e4178829e48714ca7a1418db972414b34c
sentinel current-epoch 1
3 changes: 2 additions & 1 deletion redis-slave-2/conf/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ echo 511 > /proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 指定配置文件
redis-server /etc/redis/redis.conf
redis-server /etc/redis/redis.conf &
redis-sentinel /etc/redis/sentinel.conf

0 comments on commit f24f85b

Please sign in to comment.