redis的主從複製(讀寫分離)/哨兵(主從切換)配置

 準備兩個redis服務,兩臺機器,依次命名文件夾子master,slave1redis

10.10.10.7app

10.10.10.8ui

一、master修改配置文件lua

[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日誌

二、slave修改配置文件orm

[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 端口)blog

三、分別啓動兩個redis服務。登陸進去查看狀態進程

master:輸入infoip

slave:輸入info同步

四、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

五、而後啓動

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 REWRITEsentinel config-epoch mymaster 2sentinel leader-epoch mymaster 2sentinel known-slave mymaster 10.10.10.8 6379sentinel known-sentinel mymaster 10.10.10.7 26379 36fa39aed6988abeeffd6f558a5cdf50bd572fa1sentinel current-epoch 2

相關文章
相關標籤/搜索