? 基于Sentinel實現redis主從自動切換

Sentinel(哨兵)是用于監控redis集群中Master狀態的工具,它可以實現對redis的監控、通知、自動故障轉移。

Sentinel作用:

  1. Master狀態檢測

  2. 當被監控的某個 Redis Master異常無法連接時 Sentinel 可以向系統管理員發送通知, 也可以通過 API 向其他程序發送通知,并且進行Master-Slave切換,將其中一個Slave作為Master,將之前的Master作為Slave

  3. Master-Slave切換后,masterredis.conf、slaveredis.conf和sentinel.conf的內容都會發生改變,即master_redis.conf中會多一行slaveof的配置,sentinel.conf的監控目標會隨之調換。當應用程序連接Redis 服務器時, Redis Sentinel會告之新的主服務器地址和端口。

Redis 主從部署:

192.168.11.12:6379  master              
192.168.11.12:6381  slave          
192.168.11.13:6379  slave           
192.168.11.13:6381  slave            
192.168.11.14:6379  slave           
192.168.11.14:6381  slave

Sentinel:

192.168.11.12                 
192.168.11.13           
192.168.11.14

具體部署(簡寫) mkdir -p /opt/redis6379/{bin,etc,var,log} 
mkdir -p /opt/redis6381/{bin,etc,var,log}

tar zxmf redis-2.8.19.tar.gz               
cd redis-2.8.19               
make             
make PREFIX=/opt/redis6379 install     
make PREFIX=/opt/redis6381 install

master config:

cat redis6379.conf
daemonize yes
pidfile /opt/redis6379/redis6379.pid
port 6379
tcp-backlog 10240
maxclients 10000
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile /opt/redis6379/log/redis6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6379.rdb
dir /export/redis6379/
slave-priority 100
maxmemory 2500mb
maxmemory-policy allkeys-lru
appendonly no
appendfilename "appendonly01.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
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-entries 512
list-max-ziplist-value 64
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

slave config:

cat redis6379.conf
daemonize yes
pidfile /opt/redis6379/redis6379.pid
port 6379
tcp-backlog 10240
maxclients 10000
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile /opt/redis6379/log/redis6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6379.rdb
dir /export/redis6379/
slave-priority 100
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
slaveof 192.168.11.12 6379
maxmemory 2500mb
maxmemory-policy allkeys-lru
appendonly no
appendfilename "appendonly01.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
save 900 1
save 300 100
save 60 10000
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
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

啟動:

/opt/redis6379/bin/redis-server /opt/redis6379/etc/redis6379.conf 

/opt/redis6381/bin/redis-server /opt/redis6381/etc/redis6381.conf  

# ps aux | grep redis
root       6379  0.2  0.7 137368  7520 ?        Ssl  08:19   0:00 /opt/redis6379/bin/redis-server 0.0.0.0:6379                     
root       6457  0.1  0.7 137368  7504 ?        Ssl  08:19   0:00 /opt/redis6381/bin/redis-server 0.0.0.0:6381

查看主從狀態:

# /opt/redis6379/bin/redis-cli -h 127.0.0.1 -p 6379 INFO Replication
# Replication
role:master
connected_slaves:5
slave0:ip=192.168.11.12,port=6381,state=online,offset=449,lag=1
slave2:ip=192.168.11.13,port=6379,state=online,offset=449,lag=1
slave4:ip=192.168.11.13,port=6381,state=online,offset=449,lag=1
slave2:ip=192.168.11.14,port=6379,state=online,offset=449,lag=1
slave4:ip=192.168.11.14,port=6381,state=online,offset=449,lag=1
master_repl_offset:449
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:448

查看同步:

192.168.11.12:6379
127.0.0.1:6379> set aa 123
OK

192.168.11.13:6379
127.0.0.1:6379> get aa
"123"

192.168.11.14:6381
127.0.0.1:6381> get aa
"123"

數據以同步

配置sentinel:

mkdir -p /opt/redis6379/sentinel

sentinel.conf:

port 26379
dir "/opt/redis6379/sentinel"
sentinel monitor master 192.168.11.12 6379 2
sentinel down-after-milliseconds master 60000
sentinel config-epoch master 1
sentinel leader-epoch master 1
sentinel known-slave master 192.168.11.12 6381
sentinel known-slave master 192.168.11.14 6381
# Generated by CONFIG REWRITE
sentinel known-slave master 192.168.11.112 6379
sentinel known-slave master 192.168.11.112 6381
sentinel known-slave master 192.168.11.14 6379
sentinel known-sentinel master 192.168.11.112 26379 459b949dd141a301f93d764d92bb04af9450870b
sentinel known-sentinel master 192.168.11.14 26379 9638d9c07c83c8cf6b3cac0b419867d9a4eeb17c
sentinel current-epoch 1

說明: port 監聽端口 
dir Sentinel服務運行時使用的臨時文件夾 sentinel monitor master IP 端口 x 監控的 redis master ip 端口 判斷為失效至少需要2個 Sentinel進程的同意,只要同意Sentinel的數量不達標,自動failover就不會執行

一下配置啟動之后無法看到 sentinel failover-timeout master 180000 : 如果在該時間(ms)內未能完成failover操作,則認為該failover失敗 sentinel parallel-syncs master 1:指定了在執行故障轉移時,最多可以有多少個從Redis實例在同步新的主實例,在從Redis實例較多的情況下這個數字越小,同步的時間越長,完成故障轉移所需的時間就越長

啟動 redis-sentinel :

/opt/redis6379/bin/redis-sentinel /opt/redis6379/sentinel/sentinel.conf  2>&1 /opt/redis6379/sentinel.log &

模擬故障:

192.168.11.12:6379
/opt/redis6379/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

查看:

192.168.11.12:6381
/opt/redis6379/bin/redis-cli -h 127.0.0.1 -p 6381 info Replication
# Replication
role:slave
master_host:192.168.11.14
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:4265
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

master 已經主觀轉移

本文進行簡單的sentinel測試 
后面進行redis參數優化講解以及測試線上master方案 
twemproxy 
codis 
redis cluster

原創文章,作者:可樂,如若轉載,請注明出處:http://www.www58058.com/10499

(0)
可樂可樂
上一篇 2016-02-14 10:22
下一篇 2016-02-14 14:04

相關推薦

  • SHELL編程之數組運用及YUM軟件包管理

    SHELL編程中,當要引用到多個值的時候,一個一個地進行變量賦值會讓我們的腳本變得繁瑣,不利于代碼的優化,所以,就需要通過數組進行定義,優化代碼,減少不必要的定義和命令操作。 SHELL中的數組:存儲多個元素的連續內存空間 數組名:整個屬組只有一個名字 數組索引: 編號從0開始   數組名[索引]   ${array_name[index…

    Linux干貨 2016-08-24
  • Linux中的權限修改指令及正則表達式

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限 [root@centos6 ~]# cp -r /etc/skel/ /home/tuser1 [root@centos6 ~]# ls -la&…

    Linux干貨 2016-10-24
  • N26第一周作業

    第一周作業   1. 描述計算機的組成及其功能。   計算機主要由處理器(CPU)、存儲器、輸入設備、輸出設備。 CPU由運算器、控制器、寄存器、緩存器組成,提供運算。 存儲器即內存,RAM(Random Access Memory),內存為cpu運算時提供數據存儲。 Input:用戶下指令的設備,使計算機能夠與用戶進行交互。 Outpu…

    Linux干貨 2017-01-03
  • 任務計劃使用方法

    概述     任務計劃其實就是針對未來的某一刻或者是某一周期內設置要執行的工作;     任務計劃分為兩種:         1、一次性任務:在指定的未來的某個時間點僅執行一次任務;  &n…

    Linux干貨 2015-03-26
  • N25第1周作業

    1.計算機的組成及功能 地址:博客園http://www.cnblogs.com/qingyangzi/p/6133274.html 2.linux主要的發行版及其區別和聯系 地址:博客園http://www.cnblogs.com/qingyangzi/p/6135801.html 3.linux哲學思想 地址:博客園http://www.cnblogs.…

    Linux干貨 2016-12-05
  • Shell腳本、特殊變量、declare、算術運算、退出狀態、條件測試、括號區別

    Shell腳本、特殊變量、declare、算術運算、退出狀態、條件測試、括號區別 Shell腳本基礎(一) 練習

    Linux干貨 2016-08-15

評論列表(1條)

  • stanley
    stanley 2016-02-14 10:23

    樣式略亂,內容詳盡

欧美性久久久久