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

環境 
CentOS7.2 
redis3.2.8

服務器IP redis端口 哨兵端口 服務器角色

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臺主機相同設置:

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

創建相應數據目錄;

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 
啟動群集監控,主從都要啟動 
/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`添加如下設置 , 然后`sysctl -p`
vm.overcommit_memory = 1
可選值:0、1、2。

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

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

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

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

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

當oom-killer發生時,linux會選擇殺死哪些進程
選擇進程的函數是oom_badness函數(在mm/oom_kill.c中),該函數會計算每個進程的點數(0~1000)。
點數越高,這個進程越有可能被殺死。
每個進程的點數跟oom_score_adj有關,而且oom_score_adj可以被設置(-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,當負載較大時,會經常出現error: too many open files

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

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

* soft nofile 65535
* hard nofile 65535

執行su或者重新關閉連接用戶再執行ulimit -a就可以查看修改后的結果。

故障切換機制

啟動群集之后,群集程序默認會在主從的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

從哨兵配置文件中可以看到當前的主庫的已經發生了改變

4. 總結

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

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

更多關于云服務器域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn

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

© 2010-2025   三五互聯知識庫   三五互聯旗下IDC知識庫,為您提供域名注冊,企業郵箱,虛擬主機,云服務器,云計算,網站建設等領域專業的知識介紹!

閩ICP備2023011970號

wordpress template system recommended themebetter

請求次數:58 次,加載用時:0.412 秒,內存占用:9.20 MB

登錄

找回密碼

注冊

主站蜘蛛池模板: 亚洲国产精品久久无人区| 精品无码人妻| 色婷婷久久综合中文久久一本| 漯河市| 国产女人18毛片水真多1| 国产睡熟迷奷系列网站| 国产精品大全中文字幕| 一本加勒比hezyo无码人妻| 人妻少妇久久中文字幕| 激情97综合亚洲色婷婷五| 国产精品普通话国语对白露脸| 激情综合网一区二区三区| 国产360激情盗摄全集| 午夜精品极品粉嫩国产尤物| 小嫩批日出水无码视频免费| 一区二区福利在线视频| 国产成人精品亚洲精品密奴| 国产成人无码久久久精品一| 3d全彩无码啪啪本子全彩| 欧美在线人视频在线观看| 中国女人熟毛茸茸A毛片| 国产福利社区一区二区| 国产精品国产精品国产专区不卡| 久久亚洲精品11p| 不卡在线一区二区三区视频| 成人性生交片无码免费看| 久久精品女人的天堂av| 202丰满熟女妇大| 国产一区二区精品久久凹凸| 天天躁日日躁狠狠躁中文字幕| 国产老熟女伦老熟妇露脸| 欧美亚洲国产日韩一区二区| 唐河县| 国产欧美日韩免费看AⅤ视频| 好男人视频免费| 无码专区视频精品老司机| 激情视频乱一区二区三区| 中年国产丰满熟女乱子正在播放 | 麻豆一区二区中文字幕| 国产美女直播亚洲一区色| 人妻少妇久久中文字幕|