監(jiān)控(Monitoring): Sentinel 會不斷地檢查你的主服務(wù)器和從服務(wù)器是否運作正常。 
提醒(Notification): 當被監(jiān)控的某個 Redis 服務(wù)器出現(xiàn)問題時, Sentinel 可以通過 API 向管理員或者其他應(yīng)用程序發(fā)送通知。 
自動故障遷移(Automatic failover): 當一個主服務(wù)器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將失效主服務(wù)器的其中一個從服務(wù)器升級為新的主服務(wù)器, 并讓失效主服務(wù)器的其他從服務(wù)器改為復(fù)制新的主服務(wù)器; 當客戶端試圖連接失效的主服務(wù)器時, 集群也會向客戶端返回新主服務(wù)器的地址, 使得集群可以使用新主服務(wù)器代替失效服務(wù)器。 
Redis Sentinel 是一個分布式系統(tǒng), 你可以在一個架構(gòu)中運行多個 Sentinel 進程(progress), 這些進程使用流言協(xié)議(gossip protocols)來接收關(guān)于主服務(wù)器是否下線的信息, 并使用投票協(xié)議(agreement protocols)來決定是否執(zhí)行自動故障遷移, 以及選擇哪個從服務(wù)器作為新的主服務(wù)器。 
雖然 Redis Sentinel 釋出為一個單獨的可執(zhí)行文件 redis-sentinel , 但實際上它只是一個運行在特殊模式下的 Redis 服務(wù)器, 你可以在啟動一個普通 Redis 服務(wù)器時通過給定 –sentinel 選項來啟動 Redis Sentinel 。

環(huán)境 
CentOS7.2 
redis3.2.8

服務(wù)器IP redis端口 哨兵端口 服務(wù)器角色

10.1.0.160637926379主10.1.0.161637926379從110.1.0.71637926379從22. redis程序安裝

以下是單redis安裝腳本,可適用于單redis使用。 
cat install_redis.sh

#!/usr/bin/env bash
# It\\\'s Used to be install redis.
# Created on 2016/10/19 11:18.
# @author: Chinge_Yang.
# Version: 1.0

function install_redis () {
#################################################################################################
       sourcepackage_dir=/tmp
       redis_install_dir=/usr/local/redis
       cd ${sourcepackage_dir}
       if [ ! -f redis-stable.tar.gz ]; then
               wget http://download.redis.io/releases/redis-stable.tar.gz
       fi
       cd ${makework_dir}
       tar -zxvf ${sourcepackage_dir}/redis-stable.tar.gz
       cd redis-stable
       make PREFIX=/usr/local/redis install
       return_echo make
       mkdir -p /usr/local/redis/{etc,var}
       rsync -avz redis.conf  /usr/local/redis/etc/
       sed -i \\\'s@pidfile.*@pidfile /var/run/redis-server.pid@\\\' $redis_install_dir/etc/redis.conf
       sed -i s@logfile.*@logfile $redis_install_dir/var/redis.log@ $redis_install_dir/etc/redis.conf
       sed -i s@^dir.*@dir $redis_install_dir/var@ $redis_install_dir/etc/redis.conf
       sed -i \\\'s/daemonize no/daemonize yes/g\\\' /usr/local/redis/etc/redis.conf
       sed -i \\\'s/^# bind 127.0.0.1/bind 127.0.0.1/g\\\' /usr/local/redis/etc/redis.conf
       rsync -avz ${sourcepackage_dir}/init.d/redis-server /etc/init.d/
       /etc/init.d/redis-server start
       chkconfig --add redis-server
       chkconfig redis-server on
#################################################################################################
}

install_redis

redis啟停腳本示例: 
cat redis-server

#!/bin/bash 
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 85 15
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /usr/local/redis/etc/redis.conf
# config:      /etc/sysconfig/redis
# pidfile:     /usr/local/redis/var/redis-server.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ $NETWORKING = no ] && exit 0

redis=/usr/local/redis/bin/redis-server
prog=$(basename $redis)

REDIS_CONF_FILE=/usr/local/redis/etc/redis.conf

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

lockfile=/var/lock/subsys/redis-server

start() {
   [ -x $redis ] || exit 5
   [ -f $REDIS_CONF_FILE ] || exit 6
   echo -n $Starting $prog:
   daemon $redis $REDIS_CONF_FILE
   retval=$?
   echo
   [ $retval -eq 0 ] && touch $lockfile
   return $retval
}

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

restart() {
   stop
   start
}

reload() {
   echo -n $Reloading $prog:
   killproc $redis -HUP
   RETVAL=$?
   echo
}

force_reload() {
   restart
}

rh_status() {
   status $prog
}

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
           ;;
   *)
       echo $Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
       exit 2
esac

redis-sentinel啟停腳本示例:

#!/bin/bash 
#
# redis-sentinel - this script starts and stops the redis-server sentinel daemon
#
# chkconfig:   - 85 15
# description:  Redis sentinel
# processname: redis-server
# config:      /usr/local/redis/etc/sentinel.conf
# config:      /etc/sysconfig/redis
# pidfile:     /usr/local/redis/var/redis-sentinel.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ $NETWORKING = no ] && exit 0

redis=/usr/local/redis/bin/redis-sentinel
prog=$(basename $redis)

REDIS_CONF_FILE=/usr/local/redis/etc/sentinel.conf

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

lockfile=/var/lock/subsys/redis-sentinel

start() {
   [ -x $redis ] || exit 5
   [ -f $REDIS_CONF_FILE ] || exit 6
   echo -n $Starting $prog:
   daemon $redis $REDIS_CONF_FILE --sentinel
   retval=$?
   echo
   [ $retval -eq 0 ] && touch $lockfile
   return $retval
}

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

restart() {
   stop
   start
}

reload() {
   echo -n $Reloading $prog:
   killproc $redis -HUP
   RETVAL=$?
   echo
}

force_reload() {
   restart
}

rh_status() {
   status $prog
}

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
           ;;
   *)
       echo $Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
       exit 2
esac

3. 哨兵模式配置

3臺主機相同設(shè)置:

按照前面單redis安裝方法安裝程序;

創(chuàng)建相應(yīng)數(shù)據(jù)目錄;

mkdir -p /usr/local/redis/data/redis
mkdir -p /usr/local/redis/data/sentinel
mkdir -p /usr/local/redis/sbin
vim /usr/local/redis/sbin/redis-server  # 使用上文中的示例腳本
vim /usr/local/redis/sbin/redis-sentinel  # 使用上文中的示例腳本

3.1 主redis配置

vim redis.conf

daemonize yes
pidfile /usr/local/redis/var/redis-server.pid
port 6379
tcp-backlog 128
timeout 0
tcp-keepalive 0
loglevel notice
logfile /usr/local/redis/var/redis-server.log
databases 16
save 900 1    
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/data/redis
masterauth 20170310
requirepass 20170310
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename appendonly.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

群集文件配置 
vim sentinel.conf

port 26379
pidfile /usr/local/redis/var/redis-sentinel.pid
dir /usr/local/redis/data/sentinel
daemonize yes
logfile /usr/local/redis/var/redis-sentinel.log
sentinel monitor mymaster 10.1.0.160 6379 2
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster 20170310

3.2 從redis配置

相對主redis配置,多添加了如下行:

slaveof 10.1.0.160 6379

vim redis.conf

daemonize yes
pidfile /usr/local/redis/var/redis-server.pid
port 6379
tcp-backlog 128
timeout 0
tcp-keepalive 0
loglevel notice
logfile /usr/local/redis/var/redis-server.log
databases 16
save 900 1    
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/data/redis
masterauth 20170310
requirepass 20170310
slaveof 10.1.0.160 6379  
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 90
appendonly yes
appendfilename appendonly.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

vim sentinel.conf

port 26379
pidfile /usr/local/redis/var/redis-sentinel.pid
dir /usr/local/redis/data/sentinel
daemonize yes
logfile /usr/local/redis/var/redis-sentinel.log
sentinel monitor mymaster 10.1.0.160 6379 2
sentinel config-epoch mymaster 0

3.3 啟動redis和哨兵

啟動redis,主從都要啟動 
/usr/local/redis/sbin/redis-server start 
啟動群集監(jiān)控,主從都要啟動 
/usr/local/redis/sbin/redis-sentinel start

啟動報錯處理

錯誤1:
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add \\\'vm.overcommit_memory = 1\\\' to /etc/sysctl.conf and then reboot or run the command \\\'sysctl vm.overcommit_memory=1\\\' for this to take effect.

解決方法(overcommit_memory)
1. `vim /etc/sysctl.conf`添加如下設(shè)置 , 然后`sysctl -p`
vm.overcommit_memory = 1
可選值:0、1、2。

0, 表示內(nèi)核將檢查是否有足夠的可用內(nèi)存供應(yīng)用進程使用;如果有足夠的可用內(nèi)存,內(nèi)存申請允許;否則,內(nèi)存申請失敗,并把錯誤返回給應(yīng)用進程。
1, 表示內(nèi)核允許分配所有的物理內(nèi)存,而不管當前的內(nèi)存狀態(tài)如何。
2, 表示內(nèi)核允許分配超過所有物理內(nèi)存和交換空間總和的內(nèi)存

注意:redis在dump數(shù)據(jù)的時候,會fork出一個子進程,理論上child進程所占用的內(nèi)存和parent是一樣的,比如parent占用 的內(nèi)存為8G,這個時候也要同樣分配8G的內(nèi)存給child,如果內(nèi)存無法負擔,往往會造成redis服務(wù)器的down機或者IO負載過高,效率下降。所 以這里比較優(yōu)化的內(nèi)存分配策略應(yīng)該設(shè)置為 1(表示內(nèi)核允許分配所有的物理內(nèi)存,而不管當前的內(nèi)存狀態(tài)如何)。
這里又涉及到Overcommit和OOM。

什么是Overcommit和OOM?
在Unix中,當一個用戶進程使用malloc()函數(shù)申請內(nèi)存時,假如返回值是NULL,則這個進程知道當前沒有可用內(nèi)存空間,就會做相應(yīng)的處理工作。許多進程會打印錯誤信息并退出。
Linux使用另外一種處理方式,它對大部分申請內(nèi)存的請求都回復(fù)yes,以便能跑更多更大的程序。因為申請內(nèi)存后,并不會馬上使用內(nèi)存。這種技術(shù)叫做Overcommit。
當內(nèi)存不足時,會發(fā)生OOM killer(OOM=out-of-memory)。它會選擇殺死一些進程(用戶態(tài)進程,不是內(nèi)核線程),以便釋放內(nèi)存。

Overcommit的策略
Linux下overcommit有三種策略(Documentation/vm/overcommit-accounting):
0. 啟發(fā)式策略。合理的overcommit會被接受,不合理的overcommit會被拒絕。
1. 任何overcommit都會被接受。
2. 當系統(tǒng)分配的內(nèi)存超過swap N%*%u7269理RAM(N%%u7531vm.overcommit_ratio決定)時,會拒絕commit。
overcommit的策略通過vm.overcommit_memory設(shè)置。
overcommit的百分比由vm.overcommit_ratio設(shè)置。

# echo 2 > /proc/sys/vm/overcommit_memory
# echo 80 > /proc/sys/vm/overcommit_ratio

當oom-killer發(fā)生時,linux會選擇殺死哪些進程
選擇進程的函數(shù)是oom_badness函數(shù)(在mm/oom_kill.c中),該函數(shù)會計算每個進程的點數(shù)(0~1000)。
點數(shù)越高,這個進程越有可能被殺死。
每個進程的點數(shù)跟oom_score_adj有關(guān),而且oom_score_adj可以被設(shè)置(-1000最低,1000最高)。

錯誤2:
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

echo 511 > /proc/sys/net/core/somaxconn

錯誤3:
16433:X 12 Jun 14:52:37.734 * Increased maximum number of open files to 10032 (it was originally set to 1024).

新裝的linux默認只有1024,當負載較大時,會經(jīng)常出現(xiàn)error: too many open files

ulimit -a:使用可以查看當前系統(tǒng)的所有限制值

vim /etc/security/limits.conf
在文件的末尾加上

* soft nofile 65535
* hard nofile 65535

執(zhí)行su或者重新關(guān)閉連接用戶再執(zhí)行ulimit -a就可以查看修改后的結(jié)果。

故障切換機制

啟動群集之后,群集程序默認會在主從的sentinel.conf文件中加入群集信息

主:

port 26379
pidfile /usr/local/redis/var/redis-sentinel.pid
dir /usr/local/redis/data/sentinel
daemonize yes
logfile /usr/local/redis/var/redis-sentinel.log
sentinel myid aeff525d03a2234ef834808f7991761db03a1973
sentinel monitor mymaster 10.1.0.160 6379 2
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster 20170310
# Generated by CONFIG REWRITE
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 10.1.0.71 6379
sentinel known-slave mymaster 10.1.0.161 6379
sentinel current-epoch 0

從1:

port 26379
pidfile /usr/local/redis/var/redis-sentinel.pid
dir /usr/local/redis/data/sentinel
daemonize yes
logfile /usr/local/redis/var/redis-sentinel.log
sentinel myid 01b1b7674abe648f6a2344fc5610e73b7e87cb8a
sentinel monitor mymaster 10.1.0.160 6379 2
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel current-epoch 0

從2:

port 26379
pidfile /usr/local/redis/var/redis-sentinel.pid
dir /usr/local/redis/data/sentinel
daemonize yes
logfile /usr/local/redis/var/redis-sentinel.log
sentinel myid f1589f48079b3b3b536add4e2e01a36304aeba8c
sentinel monitor mymaster 10.1.0.160 6379 2
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel current-epoch 0

模擬主故障

[root@show160 redis]# /usr/local/redis/bin/redis-cli -p 6379
127.0.0.1:6379> AUTH 20170310
OK
127.0.0.1:6379> DEBUG SEGFAULT
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> quit

從哨兵配置文件中可以看到當前的主庫的已經(jīng)發(fā)生了改變

4. 總結(jié)

redis的哨兵端口26379使用redis-cli可以連接查看哨兵相關(guān)信息,要想連接此高可用redis,可使用官方的連接客戶端。使用哨兵監(jiān)控當主故障后會自動切換從為主,當主啟動后就變成了從。至少要3哨兵和3redis節(jié)點才能允許掛一節(jié)點還能保證服務(wù)可用性。

參考資料: 
https://redis.io/topics/sentinel 
http://www.redis.cn/topics/sentinel.html 
http://www.majunwei.com/view/201610302123020678.html

更多關(guān)于云服務(wù)器,域名注冊,虛擬主機的問題,請訪問三五互聯(lián)官網(wǎng):www.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:三五互聯(lián)知識庫 » 3臺服務(wù)器Redis高可用哨兵模式

© 2010-2026   三五互聯(lián)知識庫   三五互聯(lián)旗下IDC知識庫,為您提供域名注冊,企業(yè)郵箱,虛擬主機,云服務(wù)器,云計算,網(wǎng)站建設(shè)等領(lǐng)域?qū)I(yè)的知識介紹!

閩ICP備2023011970號

wordpress template system recommended themebetter

請求次數(shù):58 次,加載用時:0.288 秒,內(nèi)存占用:9.20 MB

登錄

找回密碼

注冊