Redis基于Sentinel哨兵高可用方案

下載最新redis版本,當前最新版本為  2.8.19  2014-12-30

安裝redis

首頁地址:http://redis.io/

最新穩定版下載地址: http://download.redis.io/releases/redis-2.8.19.tar.gz

# tar -xvf redis-2.8.19.tar.gz

# cd redis-2.8.19

# make install

# make test

l  報錯

# make test

cd src && make test

make[1]: Entering directory `/data/software/redis-2.8.19/src’

You need tcl 8.5 or newer in order to run the Redis test

make[1]: *** [test] Error 1

make[1]: Leaving directory `/data/software/redis-2.8.19/src’

make: *** [test] Error 2

l  解決

安裝tcl

http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html

yum install tcl tcl-devel -y

命令用法簡介 

redis-server -h
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>
 
Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose
 
Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

配置主從

# mkdir /etc/redis
#cp sentinel.conf redis.conf /etc/redis –pa

配置項解釋

###斜體字為2.82.4新的配置項

daemonize yes         ###daemon后臺運行模式
pidfile /var/run/redis.pid   ###pidfile
port 6379   ###默認6379端口
tcp-backlog 511   ###tcp三次握手等待確認ack最大的隊列數
bind 192.168.65.128 127.0.0.1  ###綁定的監聽的ip,可同時寫多個ip,不寫默認監控0.0.0.0
timeout 0  ###關閉空閑客戶端,0為disable
tcp-keepalive 0 ###是否開啟tcp長連接,定義socket連接timeout時長
loglevel notice  ###定義日志級別為notice(輸出必要的日志)
logfile "/var/log/redis/redis.log" ###定義日志輸出目錄
databases 16  ###允許redis定義的最大的db簇
###開啟rdb并應用如下數據保存策略,aof和rdb可同時啟用,aof強烈建議開啟
save 900 1     
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes  ###有任何問題導致的bgsave失敗都停止redis的對外服務
rdbcompression yes  ###rdb寫壓縮
rdbchecksum yes  ###rdb文件正確性檢查
dbfilename dump.rdb  ###rdb文件名稱(dump.rdb可任意起)
dir /var/lib/redis/     ###rdb文件寫目錄
# slaveof <masterip> <masterport>    ###如果是slave需要寫明master
# masterauth <master-password>    ###master密碼驗證(因為redis基于內存,作用不大)
slave-serve-stale-data no  ###如果master宕機,slave是否還正常對外服務no->停止服務
slave-read-only yes    ###salve是否只讀(默認只讀)
repl-diskless-sync no   ### Disk-backed 啟動新進程寫rdb到disk,然后增量傳輸到salves 方式二:Diskless 直接寫salve sockers,而不用寫disk,當網絡好的時候建議使用,但因為用到的是M/S+哨兵的架構,隨時可能會進行主從切換,這個方式暫時不用
repl-diskless-sync-delay 5  ##每5s傳輸一次diskless開啟后生效
repl-ping-slave-period 3   ###slave每3s ping測試master是否存活
repl-timeout 10      ###定義replicationtxuq  timeout時間,這里的時間要大于repl-ping-slave-period里的時間,不然后一直發生low traffic between the master and the slave問題
repl-disable-tcp-nodelay no   ###yes 一條線40milliseconds發送一次數據包,意識著更小的tcp packets和更小的帶寬去發送到slave,但在slave會有相應的數據延遲. No,剛相反.一般上我們期望是低延遲,所以最選擇no是個不錯的選擇,但如果在復雜的網絡情況或m/s之間有很多中繼網絡,這里建議修改為yes
slave-priority 100  ###slave優先級別,數字越小優先級別越高,當master宕機時優先選取slave
# min-slaves-to-write 3   ###當最少有3個slave延遲<= 10s時,master才正常提供服務
# min-slaves-max-lag 10  ###當最少有3個slave延遲<= 10s時,master才正常提供服務
maxmemory  1536000000   #1.5G   ###單位<bytes> ,redis最大占用內存數,當超過1.5G時redis會根據策略清理內存key
#############6種清理策略
# volatile-lru -> remove the key with an expire set using an LRU algorithm ##推薦使用
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy volatile-lru  ###使用LRU算法清理過期key
# maxmemory-samples 3  ###LRU算法和TTL算法均為模糊算法,該精確算法redis會選擇3個keys選擇最少使用的一個key進行刪除, 個人不建議使用
appendonly yes   ###aof持久化,增量寫disk,比aof慢,但更可靠.企業的默認選擇,有的會兩者均開啟
appendfilename "appendonly.aof"  ###aof文件名
# appendfsync always
appendfsync everysec   ###建議使用該方式
# appendfsync no
no-appendfsync-on-rewrite no  ##建議no, 主要用來緩和redis調用fsync()寫數據時間長的問題.當BGSAVE或BGREWRITEAOF被調用期間,fsync()進程將阻止被調用,即相當于
auto-aof-rewrite-percentage 100   ###當文件大小達到64mb的100%大小時開始rewrite aof文件
auto-aof-rewrite-min-size 64mb   ###當文件大小達到64mb的100%大小時開始rewrite aof文件
aof-load-truncated yes   ###當aof文件被損壞時,redis將返回錯誤并退出
lua-time-limit 5000   ###LUA scripts最大執行時間,單位(milliseconds),超出后返回查詢錯誤并寫日志
slowlog-log-slower-than 10000 ###單位microseconds(毫秒) 1000000 microseconds=1 s,記錄執行時長超過10000 microseconds的執行命令
slowlog-max-len 128  ###最大長度為128
latency-monitor-threshold 0   ###監控相關,關閉就好
notify-keyspace-events ""   ###空表示關閉,發布相關key的操作記錄到所有 client
######下面是高級設置,個人保持為默認配置
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

運行redis

# redis-server /etc/redis/redis.conf 

Info輸出項解釋:

redis.conf中的配置相對應

# redis-cli -h Mrds
Mrds:6379> info
# Server
redis_version:2.8.19     ###redis版本號
redis_git_sha1:00000000  ###git SHA1
redis_git_dirty:0   ###git dirty flag
redis_build_id:78796c63e58b72dc
redis_mode:standalone   ###redis運行模式
os:Linux 2.6.32-431.el6.x86_64 x86_64   ###os版本號
arch_bits:64  ###64位架構
multiplexing_api:epoll  ###調用epoll算法
gcc_version:4.4.7   ###gcc版本號
process_id:25899   ###服務器進程PID
run_id:eae356ac1098c13b68f2b00fd7e1c9f93b1c6a2c   ###Redis的隨機標識符(用于sentinel和集群)
tcp_port:6379   ###Redis監聽的端口號
uptime_in_seconds:6419 ###Redis運行時長(s為單位)
uptime_in_days:0  ###Redis運行時長(天為單位)
hz:10
lru_clock:10737922  ###以分鐘為單位的自增時鐘,用于LRU管理
config_file:/etc/redis/redis.conf   ###redis配置文件
 
# Clients
connected_clients:1   ###已連接客戶端的數量(不包括通過從屬服務器連接的客戶端)
client_longest_output_list:0   ###當前連接的客戶端中最長的輸出列表
client_biggest_input_buf:0   ###當前連接的客戶端中最大的輸出緩存
blocked_clients:0  ###正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客戶端的數量 需監控
 
# Memory
used_memory:2281560   ###由 Redis 分配器分配的內存總量,以字節(byte)為單位
used_memory_human:2.18M   ###以更友好的格式輸出redis占用的內存
used_memory_rss:2699264   ###從操作系統的角度,返回 Redis 已分配的內存總量(俗稱常駐集大?。?。這個值和 top 、 ps 等命令的輸出一致
used_memory_peak:22141272  ### Redis 的內存消耗峰值(以字節為單位)
used_memory_peak_human:21.12M  ###以更友好的格式輸出redis峰值內存占用
used_memory_lua:35840  ###LUA引擎所使用的內存大小
mem_fragmentation_ratio:1.18  ###used_memory_rss 和 used_memory 之間的比率
mem_allocator:jemalloc-3.6.0
 
###在理想情況下, used_memory_rss 的值應該只比 used_memory 稍微高一點兒。當 rss > used ,且兩者的值相差較大時,表示存在(內部或外部的)內存碎片。內存碎片的比率可以通過 mem_fragmentation_ratio 的值看出。
當 used > rss 時,表示 Redis 的部分內存被操作系統換出到交換空間了,在這種情況下,操作可能會產生明顯的延遲。
 
 
# Persistence
loading:0  ###記錄服務器是否正在載入持久化文件
rdb_changes_since_last_save:0   ###距離最近一次成功創建持久化文件之后,經過了多少秒
rdb_bgsave_in_progress:0   ###記錄了服務器是否正在創建 RDB 文件
rdb_last_save_time:1420023749  ###最近一次成功創建 RDB 文件的 UNIX 時間戳
rdb_last_bgsave_status:ok   ###最近一次創建 RDB 文件的結果是成功還是失敗
rdb_last_bgsave_time_sec:0  ###最近一次創建 RDB 文件耗費的秒數
rdb_current_bgsave_time_sec:-1  ###如果服務器正在創建 RDB 文件,那么這個域記錄的就是當前的創建操作已經耗費的秒數
aof_enabled:1   ###AOF 是否處于打開狀態
aof_rewrite_in_progress:0   ###服務器是否正在創建 AOF 文件
aof_rewrite_scheduled:0   ###RDB 文件創建完畢之后,是否需要執行預約的 AOF 重寫操作
aof_last_rewrite_time_sec:-1  ###最近一次創建 AOF 文件耗費的時長
aof_current_rewrite_time_sec:-1  ###如果服務器正在創建 AOF 文件,那么這個域記錄的就是當前的創建操作已經耗費的秒數
aof_last_bgrewrite_status:ok  ###最近一次創建 AOF 文件的結果是成功還是失敗
aof_last_write_status:ok 
aof_current_size:176265  ###AOF 文件目前的大小
aof_base_size:176265  ###服務器啟動時或者 AOF 重寫最近一次執行之后,AOF 文件的大小
aof_pending_rewrite:0  ###是否有 AOF 重寫操作在等待 RDB 文件創建完畢之后執行
aof_buffer_length:0   ###AOF 緩沖區的大小
aof_rewrite_buffer_length:0  ###AOF 重寫緩沖區的大小
aof_pending_bio_fsync:0  ###后臺 I/O 隊列里面,等待執行的 fsync 調用數量
aof_delayed_fsync:0    ###被延遲的 fsync 調用數量
 
# Stats
total_connections_received:8466  ###服務器已接受的連接請求數量
total_commands_processed:900668   ###服務器已執行的命令數量
instantaneous_ops_per_sec:1   ###服務器每秒鐘執行的命令數量
total_net_input_bytes:82724170
total_net_output_bytes:39509080
instantaneous_input_kbps:0.07
instantaneous_output_kbps:0.02
rejected_connections:0  ###因為最大客戶端數量限制而被拒絕的連接請求數量
sync_full:2
sync_partial_ok:0
sync_partial_err:0
expired_keys:0   ###因為過期而被自動刪除的數據庫鍵數量
evicted_keys:0   ###因為最大內存容量限制而被驅逐(evict)的鍵數量。
keyspace_hits:0  ###查找數據庫鍵成功的次數。
keyspace_misses:500000   ###查找數據庫鍵失敗的次數。
pubsub_channels:0  ###目前被訂閱的頻道數量
pubsub_patterns:0  ###目前被訂閱的模式數量
latest_fork_usec:402  ###最近一次 fork() 操作耗費的毫秒數
 
# Replication
role:master   ###如果當前服務器沒有在復制任何其他服務器,那么這個域的值就是 master ;否則的話,這個域的值就是 slave 。注意,在創建復制鏈的時候,一個從服務器也可能是另一個服務器的主服務器
connected_slaves:2   ###2個slaves
slave0:ip=192.168.65.130,port=6379,state=online,offset=1639,lag=1
slave1:ip=192.168.65.129,port=6379,state=online,offset=1639,lag=0
master_repl_offset:1639
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1638
 
# CPU
used_cpu_sys:41.87  ###Redis 服務器耗費的系統 CPU
used_cpu_user:17.82  ###Redis 服務器耗費的用戶 CPU
used_cpu_sys_children:0.01  ###后臺進程耗費的系統 CPU
used_cpu_user_children:0.01  ###后臺進程耗費的用戶 CPU
 
# Keyspace
db0:keys=3101,expires=0,avg_ttl=0   ###keyspace 部分記錄了數據庫相關的統計信息,比如數據庫的鍵數量、數據庫已經被刪除的過期鍵數量等。對于每個數據庫,這個部分都會添加一行以下格式的信息

創建redis目錄:

# mkdir /var/run/redis
# chown redis. /var/run/redis

 Redis基于Sentinel哨兵高可用方案

測試1: Sentinel哨兵高自動可用切換

Sentinel配置

啟動前:

port 26379
dir "/var/lib/redis/tmp"   ###定義目錄存放
sentinel monitor mymaster 192.168.65.128 6379 2     ###監控mymaster(可自定義-但只能包括A-z 0-9和”._-”)
sentinel down-after-milliseconds mymaster 30000   ###mymaster多久不響應認為SDOWN
sentinel parallel-syncs mymaster 1   ###指定最大同時同步新maser配置的salve數量
sentinel failover-timeout mymaster 180000  ###2次failover切換時間

啟動后(redis.con配置文件完全由sentinel控制,請不要再隨意手動改動):

port 26379
dir "/var/lib/redis/tmp"
sentinel monitor mymaster 192.168.65.128 6379 2
sentinel config-epoch mymaster 18   ###確認mymater SDOWN時長
sentinel leader-epoch mymaster 18  ###同時一時間最多18個slave可同時更新配置,建議數字不要太大,以免影響正常對外提供服務
sentinel known-slave mymaster 192.168.65.129 6379   ###已知的slave
sentinel known-slave mymaster 192.168.65.130 6379   ###已知的slave
sentinel known-sentinel mymaster 192.168.65.130 26379 be964e6330ee1eaa9a6b5a97417e866448c0ae40    ###已知slave的唯一id
sentinel known-sentinel mymaster 192.168.65.129 26379 3e468037d5dda0bbd86adc3e47b29c04f2afe9e6  ###已知slave的唯一id
sentinel current-epoch 18  ####當前可同時同步的salve數最大同步閥值

開啟sentinel

開啟sentinel后會后redis.conf會由sentinel進行管理

# redis-server /etc/redis/sentinel.conf --sentinel &> /var/log/redis/sentinel.log &

啟動sentinel各服務器輸出信息

Mrds 
Srds1 Srds2
的輸出結果應該是一樣的.

Mrds輸出:

                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 30176
  `-._    `-._  `-./  _.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |           http://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              
 
[30176] 06 Jan 20:26:03.709 # Sentinel runid is 61b67b4ee4bbd9835f1713d8e900f2abb9fb5658
[30176] 06 Jan 20:26:03.710 # +monitor master mymaster 192.168.65.128 6379 quorum 2
[30176] 06 Jan 20:26:04.711 * +slave slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.128 6379
[30176] 06 Jan 20:26:04.711 * +slave slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379
[30176] 06 Jan 20:27:38.824 * +sentinel sentinel 192.168.65.129:26379 192.168.65.129 26379 @ mymaster 192.168.65.128 6379
[30176] 06 Jan 20:27:43.062 * +sentinel sentinel 192.168.65.130:26379 192.168.65.130 26379 @ mymaster 192.168.65.128 6379

Redis-cli> info輸出信息:

 Redis基于Sentinel哨兵高可用方案

Srds1輸出 

[4993] 06 Jan 20:27:36.788 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 4993
  `-._    `-._  `-./  _.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |           http://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              
 
[4993] 06 Jan 20:27:36.790 # Sentinel runid is 157f2df2543470ecd35d92fba75eaa8069c3f1a0
[4993] 06 Jan 20:27:36.790 # +monitor master mymaster 192.168.65.128 6379 quorum 2
[4993] 06 Jan 20:27:36.797 * +slave slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.128 6379
[4993] 06 Jan 20:27:36.797 * +slave slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379
[4993] 06 Jan 20:27:38.427 * +sentinel sentinel 192.168.65.128:26379 192.168.65.128 26379 @ mymaster 192.168.65.128 6379
[4993] 06 Jan 20:27:43.064 * +sentinel sentinel 192.168.65.130:26379 192.168.65.130 26379 @ mymaster 192.168.65.128 6379

Redis-cli> info輸出信息

 Redis基于Sentinel哨兵高可用方案

Srds2輸出

                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 22660
  `-._    `-._  `-./  _.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |           http://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              
 
[22660] 06 Jan 20:27:41.032 # Sentinel runid is e4b5faa87975a4b8e82a93476f1675a62511cfba
[22660] 06 Jan 20:27:41.032 # +monitor master mymaster 192.168.65.128 6379 quorum 2
[22660] 06 Jan 20:27:41.036 * +slave slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.128 6379
[22660] 06 Jan 20:27:41.036 * +slave slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379
[22660] 06 Jan 20:27:42.586 * +sentinel sentinel 192.168.65.128:26379 192.168.65.128 26379 @ mymaster 192.168.65.128 6379
[22660] 06 Jan 20:27:42.895 * +sentinel sentinel 192.168.65.129:26379 192.168.65.129 26379 @ mymaster 192.168.65.128 6379

Redis-cli> info 輸出信息

 Redis基于Sentinel哨兵高可用方案

redis.conf最新信息:

測試1: 自動切換

1.       設置slave-priority的值Srds1100,Srds299,方便主觀確認新maser就為Srds1,因為slave-priority數字越小優先級越高

2.       關閉Mrds redis-server進程

Redis基于Sentinel哨兵高可用方案

3.       確認failover信息

Srds1輸出:

[4993] 06 Jan 21:16:31.080 # +sdown master mymaster 192.168.65.128 6379 //確認master SDOWN
[4993] 06 Jan 21:16:31.165 # +odown master mymaster 192.168.65.128 6379 #quorum 2/2 //投票2/2確認master宕機
[4993] 06 Jan 21:16:31.165 # +new-epoch 1
[4993] 06 Jan 21:16:31.165 # +try-failover master mymaster 192.168.65.128 6379 //嘗試failover 192.168.65.128
[4993] 06 Jan 21:16:31.170 # +vote-for-leader 157f2df2543470ecd35d92fba75eaa8069c3f1a0 1   //投票選舉master
[4993] 06 Jan 21:16:31.178 # 192.168.65.128:26379 voted for 157f2df2543470ecd35d92fba75eaa8069c3f1a0 1    //128一票
[4993] 06 Jan 21:16:31.180 # 192.168.65.130:26379 voted for 157f2df2543470ecd35d92fba75eaa8069c3f1a0 1   //130一票
[4993] 06 Jan 21:16:31.271 # +elected-leader master mymaster 192.168.65.128 6379
[4993] 06 Jan 21:16:31.272 # +failover-state-select-slave master mymaster 192.168.65.128 6379  //對mymaster進行故障切換
[4993] 06 Jan 21:16:31.331 # +selected-slave slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379  //slave130當選新的master
[4993] 06 Jan 21:16:31.331 * +failover-state-send-slaveof-noone slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379
[4993] 06 Jan 21:16:31.410 * +failover-state-wait-promotion slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379 //failover的狀態為等待130聲明故障切換成功
[4993] 06 Jan 21:16:32.182 # +promoted-slave slave 192.168.65.130:6379 192.168.65.130 6379 @ mymaster 192.168.65.128 6379  //130聲明故障切換完畢并且切換成功
[4993] 06 Jan 21:16:32.182 # +failover-state-reconf-slaves master mymaster 192.168.65.128 6379  //failover當前狀態為重新加載新的master配置
[4993] 06 Jan 21:16:32.232 * +slave-reconf-sent slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.128 6379
[4993] 06 Jan 21:16:33.198 * +slave-reconf-inprog slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.128 6379 //129 doing新配置
[4993] 06 Jan 21:16:33.198 * +slave-reconf-done slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.128 6379 //129 done新配置
[4993] 06 Jan 21:16:33.282 # +failover-end master mymaster 192.168.65.128 6379 //failover結束
[4993] 06 Jan 21:16:33.282 # +switch-master mymaster 192.168.65.128 6379 192.168.65.130 6379   //master由128切換為130
[4993] 06 Jan 21:16:33.283 * +slave slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.130 6379  //129狀態為salve
[4993] 06 Jan 21:16:33.293 * +slave slave 192.168.65.128:6379 192.168.65.128 6379 @ mymaster 192.168.65.130 6379 //128狀態為slave
[4993] 06 Jan 21:17:03.337 # +sdown slave 192.168.65.128:6379 192.168.65.128 6379 @ mymaster 192.168.65.130 6379  //128狀態為sdown
 

Srds2輸出:

[22660] 06 Jan 21:16:31.115 # +sdown master mymaster 192.168.65.128 6379  //mymaster 128 SDOWN
[22660] 06 Jan 21:16:31.175 # +new-epoch 1
[22660] 06 Jan 21:16:31.179 # +vote-for-leader 157f2df2543470ecd35d92fba75eaa8069c3f1a0 1 //選舉new master
[22660] 06 Jan 21:16:31.200 # +odown master mymaster 192.168.65.128 6379 #quorum 3/2 //3個哨兵均確認old master 128 down, sdown à odown
[22660] 06 Jan 21:16:31.200 # Next failover delay: I will not start a failover before Tue Jan  6 21:22:31 2015 //我將進行failover替換128
[22660] 06 Jan 21:16:32.239 # +config-update-from sentinel 192.168.65.129:26379 192.168.65.129 26379 @ mymaster 192.168.65.128 6379  //從129同步新配置
[22660] 06 Jan 21:16:32.239 # +switch-master mymaster 192.168.65.128 6379 192.168.65.130 6379 //master由130切換為128
[22660] 06 Jan 21:16:32.240 * +slave slave 192.168.65.129:6379 192.168.65.129 6379 @ mymaster 192.168.65.130 6379 //129為130 slave
[22660] 06 Jan 21:16:32.249 * +slave slave 192.168.65.128:6379 192.168.65.128 6379 @ mymaster 192.168.65.130 6379 //128為130slave
[22660] 06 Jan 21:17:02.286 # +sdown slave 192.168.65.128:6379 192.168.65.128 6379 @ mymaster 192.168.65.130 6379 //128 sdown


Redis.confsentinel控制

配置自動更新– Mrds

Redis基于Sentinel哨兵高可用方案

配置自動更新-Srds1

Redis.conf自動變更為

Redis基于Sentinel哨兵高可用方案

Sentinel.conf自動變更為:

 Redis基于Sentinel哨兵高可用方案

配置自動更新-Srds2

Redis.conf自動變更:

Redis基于Sentinel哨兵高可用方案

Sentinel自動變更:

 Redis基于Sentinel哨兵高可用方案

 Redis基于Sentinel哨兵高可用方案

************************自動切換成功************************

再次啟動Mrds redis

Srds2上可以看到128會被作為 slave自動加進來,

 Redis基于Sentinel哨兵高可用方案

Maser redis.conf的最后一行會被強制加入

 Redis基于Sentinel哨兵高可用方案

測試2: Mrds是否能再被選舉為maser

1.       啟動Mrds上的redis進程

Redis基于Sentinel哨兵高可用方案

2.       關閉Srds2上的redis進程,確認master是否被切換為Srds1(應該切換到Srds1)

10s內自動切換至 Srds1

Redis基于Sentinel哨兵高可用方案

3.       關閉Srds1上的redis進程,確認master是否被切換為Mrds(應該切換到Mrds)

 Redis基于Sentinel哨兵高可用方案

Ok,至此切換測試全部結束.恭喜

Over-自動切換測試完畢

Tcp-backlog含義

2.8版本中的redis有新增配置項  tcp-backlog  511(默認128)

我們看到Send-Q的值為100, 即是我們配置的tcp-backlog. 為了搞清楚這個值的意思, 了解了下tcp的三次握手進行中的一些queue的知識. 參考下圖我們可以看到在server接收到sny的時候會進入到一個syn queue隊列, server端最終收到ack時轉換到accept queue隊列. 上面終端顯示在listen狀態下的連接, Send-Q就是這個accept queue隊列的最大值. 只有server端執行了accept后才會從這個隊列中移除這個連接. 這個值的大小是受somaxconn影響的, 因為是取的它們兩者的最小值, 所以如果要調大的話必需修改內核的somaxconn. 

Redis基于Sentinel哨兵高可用方案

Sentinel簡介:

  • Monitoring. Sentinel constantly checks if your master and slave instances are
    working as expected. 
    時時監控
  • Notification. Sentinel can notify the system administrator, or another computer
    program, via an API, that something is wrong with one of the monitored
    Redis instances. 
    通知
  • Automatic failover. If a master is not working as expected, Sentinel can start a
    failover process where a slave is promoted to master, the other additional
    slaves are reconfigured to use the new master, and the applications using
    the Redis server informed about the new address to use when connecting. 
    自動切換
  • Configuration provider. Sentinel acts as a source of authority for
    clients service discovery: clients connect to Sentinels in order to ask
    for the address of the current Redis master responsible for a given
    service. If a failover occurs, Sentinels will report the new address.

和主從配置最大不一樣的地方是:

Redis.conf配置完全由sentinel監管和配置。

Mini Example:

命令格式:

sentinel <option_name> <master_name> <option_value>
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
 
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

Quorum 投票

投票數可定義,單臺機器只能定義為SDOWN,只有投票通過后才可以定義為ODOWN,

另外在MASTER的選舉中,假如總共有5臺機器,我們定義2臺機器發現就可以認為是ODOWN,2臺機器發現后,這兩臺機器中的一臺會進行failover,前提是被其它3臺認證通過.

配置項解釋:

1.     ##sentinel實例之間的通訊端口  
2.     ##redis-0  
3.     port 26379  
4.     ##sentinel需要監控的master信息:<mastername> <masterIP> <masterPort> <quorum>  
5.     ##<quorum>應該小于集群中slave的個數,只有當至少<quorum>個sentinel實例提交"master失效"  
6.     ##才會認為master為O_DWON("客觀"失效)  
7.     sentinel monitor def_master 127.0.0.1 6379 2  
8.       
9.     sentinel auth-pass def_master 012_345^678-90  
10.     
11.   ##master被當前sentinel實例認定為“失效”的間隔時間  
12.   ##如果當前sentinel與master直接的通訊中,在指定時間內沒有響應或者響應錯誤代碼,那么  
13.   ##當前sentinel就認為master失效(SDOWN,“主觀”失效)  
14.   ##<mastername> <millseconds>  
15.   ##默認為30秒  
16.   sentinel down-after-milliseconds def_master 30000  
17.     
18.   ##當前sentinel實例是否允許實施“failover”(故障轉移)  
19.   ##no表示當前sentinel為“觀察者”(只參與"投票".不參與實施failover),  
20.   ##全局中至少有一個為yes  
21.   sentinel can-failover def_master yes  
22.     
23.   ##當新master產生時,同時進行“slaveof”到新master并進行“SYNC”的slave個數。  
24.   ##默認為1,建議保持默認值  
25.   ##在salve執行salveof與同步時,將會終止客戶端請求。  
26.   ##此值較大,意味著“集群”終止客戶端請求的時間總和和較大。  
27.   ##此值較小,意味著“集群”在故障轉移期間,多個salve向客戶端提供服務時仍然使用舊數據。  
28.   sentinel parallel-syncs def_master 1  
29.     
30.   ##failover過期時間,當failover開始后,在此時間內仍然沒有觸發任何failover操作,  
31.   ##當前sentinel將會認為此次failoer失敗。  
32.   sentinel failover-timeout def_master 900000  
33.     
34.   ##當failover時,可以指定一個“通知”腳本用來告知系統管理員,當前集群的情況。  
35.   ##腳本被允許執行的最大時間為60秒,如果超時,腳本將會被終止(KILL)  
36.   ##腳本執行的結果:  
37.   ## 1    -> 稍后重試,最大重試次數為10;   
38.   ## 2    -> 執行結束,無需重試  
39.   ##sentinel notification-script mymaster /var/redis/notify.sh  
40.     
41.   ##failover之后重配置客戶端,執行腳本時會傳遞大量參數,請參考相關文檔  
# sentinel client-reconfig-script <master-name> <script-path>  

編碼大的配置會作為主配置:

Because every configuration has a different version number, the greater version always wins over smaller versions.
slave-priority of zero in order to be never selected by Sentinels as the new master
stanley 2014/12/28 22:23:50

    PING This command simply returns PONG.
    SENTINEL masters Show a list of monitored masters and their state.
    SENTINEL master <master name> Show the state and info of the specified master.
    SENTINEL slaves <master name> Show a list of slaves for this master, and their state.
    SENTINEL get-master-addr-by-name <master name> Return the ip and port number of the master with that name. If a failover is in progress or terminated successfully for this master it returns the address and port of the promoted slave.
    SENTINEL reset <pattern> This command will reset all the masters with matching name. The pattern argument is a glob-style pattern. The reset process clears any previous state in a master (including a failover in progress), and removes every slave and sentinel already discovered and associated with the master.
    SENTINEL failover <master name> Force a failover as if the master was not reachable, and without asking for agreement to other Sentinels (however a new version of the configuration will be published so that the other Sentinels will update their configurations).

增加多臺sentinels

If you need to add multiple Sentinels at once, it is suggested to add it one after the other, waiting for all the other Sentinels to already know about the first one before adding the next.

減少sentinels:

Stop the Sentinel process of the Sentinel you want to remove.
Send a SENTINEL RESET * command to all the other Sentinel instances (instead of * you can use the exact master name if you want to reset just a single master). One after the other, waiting at least 30 seconds between instances.
Check that all the Sentinels agree about the number of Sentinels currently active, by inspecting the output of SENTINEL MASTER mastername of every Sentinel.


依賴計算機時間:

Redis Sentinel is heavily dependent on the computer time:

TILT mode:

if the time difference is negative or unexpectedly big (2 seconds or more) the TILT mode is entered
If everything appears to be normal for 30 second, the TILT mode is exited.

附件:

 Redis起停腳本 

#!/bin/sh
#
# redis        init file for starting up the redis daemon
#
# chkconfig:   - 20 80
# description: Starts and stops the redis daemon.

# Source function library.
. /etc/rc.d/init.d/functions

name="redis-server"
exec="/usr/local/bin/$name"
pidfile="/var/run/redis/redis.pid"
REDIS_CONFIG="/etc/redis/redis.conf"

[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/lock/subsys/redis

start() {
    [ -f $REDIS_CONFIG ] || exit 6
    [ -x $exec ] || exit 5
    echo -n $"Starting $name: "
    daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $name: "
    killproc -p $pidfile $name
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    false
}

rh_status() {
    status -p $pidfile $name
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
        exit 2
esac
exit $?

 

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

(2)
stanleystanley
上一篇 2015-03-03
下一篇 2015-03-04

相關推薦

  • 16 文本處理工具

    文本處理工具一 一、雜項知識整理 1、ps axo user,ruser,cmd 查看命令發起者的身份和真正的登錄身份: [root@localhost test]# ps axo user,ruser,cmd USER     RUSER  &nb…

    Linux干貨 2016-08-05
  • shell腳本編程的執行語句

    順序執行     從上到下逐條執行命令,執行完所有命令及退出。 選擇執行     (1)&&,||     &&:代表and,&&左側為真右側則執行     ||:代表or…

    Linux干貨 2016-08-24
  • 搭建LAMP+NFS(附加mysql共享NFS目錄練習總結)

    練習一 環境布局: 主機6 route A (10.1.32.14)作為DNS服務器 主機centos 7 class (10.1.32.3)作為Apache,PHP(模塊方式)服務器1(有discuz程序) 主機centos 7 server (10.1.32.13)作為Apache,php(模塊方式)服務器2(無discuz) 主機centos 6 cl…

    系統運維 2016-10-25
  • MHA–master在線修復

    本次實驗基于【MHA+keepalive 高可用MYSQL集群】(續) 一、恢復背景 二、修復故障節點及MHA集群 三、提升已修復的節點為master 四、注意事項 一、恢復背景 1、設備清單 2、:percona-xtrabackup-2.3.2 (注:生產環境應定時備份;不應等到數據庫DOWN后才備份,以免加劇集群的壓力) 備份數據庫操作: [root@…

    2017-03-01
  • linux文件系統及文件操作

    1、Linux文件系統:    文件和目錄被組織成一個單根倒置樹結構,從根目錄”/”開始。    文件系統分層結構LSB (Linux Standard Base)如下: 2、主要目錄: /boot: 系統啟動相關的文件,如內核、initrd,以及grub(bootloader) /etc:配置文件 /home:用戶的…

    2017-07-23
  • Jerry首作_linux基礎

    一、計算機的組成及其功能 硬件 馮·洛伊曼體系 控制器(Control):是整個計算機的中樞神經,其功能是對程序規定的控制信息進行解釋,根據其要求進行控制,調度程序、數據、地址,協調計算機各部分工作及內存與外設的訪問等。 運算器(Datapath):運算器的功能是對數據進行各種算術運算和邏輯運算,即對數據進行加工處理。 存儲器(Memory):存儲器的功能是…

    Linux干貨 2016-10-25
欧美性久久久久