redis的主从复制(读写分离)/哨兵(主从切换)配置

 准备两个redis服务,两台机器,依次命名文件夹子master,slave1

10.10.10.7

10.10.10.8

1、master修改配置文件

[root@db2 conf]# cat 6379.conf |grep -v "^$"|grep -v "^#"
daemonize yes (守护进程运行)
pidfile "/usr/local/redis/var/run/redis_6379.pid"
port 6379   (运行端口)
timeout 0
loglevel notice
logfile "/usr/local/redis/var/log/redis_6379.log"
databases 16
save 900 1
save 300 10
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump_6379.rdb"
dir "/usr/local/redis/var/db"
masterauth "IU8b2W4Nt9fIfLksLYfC"    (主从同步认证密钥,需在slave开启,配上也无所谓。)
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
requirepass "IU8b2W4Nt9fIfLksLYfC"
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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

2、slave修改配置文件

[root@db1 conf]# cat 6379.conf |grep -v "^$"|grep -v "^#"
daemonize yes
pidfile "/usr/local/redis/var/run/redis_6379.pid"
port 6379
timeout 0
loglevel notice
logfile "/usr/local/redis/var/log/redis_6379.log"
databases 16
save 900 1
save 300 10
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump_6379.rdb"
dir "/usr/local/redis/var/db"
masterauth "IU8b2W4Nt9fIfLksLYfC"    (主从同步认证密钥,需在slave开启)
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
requirepass "IU8b2W4Nt9fIfLksLYfC"   (登录认证密码)
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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
slaveof 10.10.10.8 6379 (指定master ip 端口)

3、分别启动两个redis服务。登录进去查看状态

master:输入info

slave:输入info

4、Redis哨兵(sentinel)实现主从切换:

(分别在master和slave上)编写配置文件:sentinel.conf

#sentinel端口
port 26379
dir "/usr/local/redis/var/db"
# 守护进程模式
daemonize yes
# 指明日志文件名
logfile "/usr/local/redis/var/sentinel.log"
#哨兵监控的master,主从配置一样,
sentinel monitor mymaster 10.10.10.8 6379 1
## master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds mymaster 5000
##若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout mymaster 18000
##设置master和slaves验证密码
sentinel auth-pass mymaster 123456

5、然后启动

redis-sentinel sentinel.conf --sentinel 

会看到master是10.10.10.7,然后停止master的redis进程后,会发现日志里发生了变化。

+convert-to-slave slave 10.10.10.7:6379 10.10.10.7 6379 @ mymaster 10.10.10.8 6379

ip已经由原来的10.10.10.7变为10.10.10.8,而sentinel.conf也会增加一段信息。


# Generated by CONFIG REWRITE
sentinel config-epoch mymaster 2
sentinel leader-epoch mymaster 2
sentinel known-slave mymaster 10.10.10.8 6379
sentinel known-sentinel mymaster 10.10.10.7 26379 36fa39aed6988abeeffd6f558a5cdf50bd572fa1
sentinel current-epoch 2

posted @ 2018-03-13 15:03  GeminiMp  阅读(513)  评论(0编辑  收藏  举报