redis-sentinel—哨兵模式
-
哨兵簡介:Redis Sentinel
Sentinel(哨兵)是用于監控redis叢集中Master狀态的工具,其已經被內建在redis2.4+的版本中是Redis官方推薦的高可用性(HA)解決方案。
2.作用
1):Master狀态檢測
2):如果Master異常,則會進行Master-Slave切換,将其中一個Slave作為Master,将之前的Master作為Slave
3):Master-Slave切換後,sentinel.conf的監控目标會随之調換
3.工作模式
1):每個Sentinel以每秒鐘一次的頻率向它所知的Master,Slave以及其他 Sentinel 執行個體發送一個 PING 指令
2):如果一個執行個體(instance)距離最後一次有效回複 PING 指令的時間超過 down-after-milliseconds 選項所指定的值, 則這個執行個體會被 Sentinel 标記為主觀下線。
3):如果一個Master被标記為主觀下線,則正在監視這個Master的所有 Sentinel 要以每秒一次的頻率确認Master的确進入了主觀下線狀态。
4):當有足夠數量的 Sentinel(大于等于配置檔案指定的值)在指定的時間範圍内确認Master的确進入了主觀下線狀态, 則Master會被标記為客觀下線 。
4、主觀下線和客觀下線
主觀下線:Subjectively Down,簡稱 SDOWN,指的是目前 一個Sentinel 執行個體對某個redis伺服器做出的下線判斷。
客觀下線:Objectively Down, 簡稱 ODOWN,指的是多個 Sentinel 執行個體在對Master Server做出 SDOWN 判斷,并且通過 SENTINEL is-master-down-by-addr 指令互相交流之後,得出的Master Server下線判斷,然後開啟failover
5、配置哨兵模式
1.每台機器上修改redis主配置檔案redis.conf檔案設定:bind 0.0.0.0 ---已經操作
2.每台機器上修改sentinel.conf配置檔案:修改如下配置
[[email protected] src]# cd ..
[[email protected] redis]# vim sentinel.conf
sentinel monitor mymaster 10.0.0.137 6379 2 #當叢集中有2個sentinel認為master死了時,才能真正認為該master已經不可用了。 (slave上面寫的是master的ip,master寫自己ip)
sentinel down-after-milliseconds mymaster 3000 #機關毫秒
sentinel failover-timeout mymaster 10000 #若sentinel在該配置值内未能完成failover(故障轉移)操作(即故障時master/slave自動切換),則認為本次failover失敗。
protected-mode no #關閉加密模式--新添加到sentinel配置檔案中
3.每台機器啟動哨兵服務:
[[email protected] redis]# ./src/redis-sentinel sentinel.conf
注意:在生産環境下将哨兵模式啟動放到背景執行: ./src/redis-sentinel sentinel.conf &
将master的哨兵模式退出(Crtl+c),再将redis服務stop了,在兩台slave上面檢視其中一台是否切換為master:(沒有優先級,為随機切換)
擴充:php安裝redis的子產品
1.安裝php7.0
[[email protected] ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[[email protected] ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[[email protected] ~]# yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64 php70w-devel zlib-devel -y
[[email protected] ~]# yum -y install php70w-fpm
[[email protected] ~]# yum install -y make gcc zlib-devel libmemcached-devel gi
=================================================================================
[[email protected] ~]# wget http://pecl.php.net/get/redis-4.0.0RC2.tgz #下載下傳擴充
[[email protected] ~]# tar xzf redis-4.0.0RC2.tgz
[[email protected] ~]# cd redis-4.0.0RC2
[[email protected] redis-4.0.0RC2]# /usr/bin/phpize #---生成./configure 這個配置檔案
[[email protected] redis-4.0.0RC2]# ./configure -with-php-config=/usr/bin/php-config
...
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands
[[email protected] redis-4.0.0RC2]# make && make install #編譯,最後顯示如下表示成功
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/lib64/php/modules/
=======================================
[[email protected] ~]# vi /etc/php.ini #添加擴充
搜尋: \.so
2.安裝nginx,配置nginx的yum源--略
配置nginx連接配接php
[[email protected] ~]# vi /etc/nginx/conf.d/nginx.conf
server {
listen 80;
server_name localhost;
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
編輯php頁面
[[email protected] ~]# cd /usr/share/nginx/html/
[[email protected] html]# vi index.php
<?php
phpinfo();
?>
啟動nginx與php
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# systemctl start php-fpm
通路:
安裝擴充完成!