一、環境說明:
作用 |
IP地址 |
端口 |
操作系統版本 |
安裝目錄 |
哨兵文件 |
主庫 |
172.16.10.80 |
6379 |
Redhat 6.7 |
/redis5.0/redis-5.0.0 |
Sentinel6379.conf |
從一 |
172.16.10.81 |
6380 |
Redhat 6.7 |
/redis5.0/redis-5.0.0 |
Sentinel6380.conf |
從二 |
172.16.10.82 |
6381 |
Redhat 6.7 |
/redis5.0/redis-5.0.0 |
Sentinel6381.conf |
Redis一主二從主要是用於讀寫分離和容災,配置是配從不配主,主從復制首次同步是全量,后面是增量同步;但是只要是重新連接master,一次完全同步(全量復制)將被自動執行。
二、安裝
Redis 一主二從的安裝其實跟單機的安裝區別不大,本次安裝我們可以先修改參數文件后在安裝,可以參考我的單機的安裝博客:
https://www.cnblogs.com/XianZhe/p/11681718.html
或者直接百度 redis 5.0.5 linux 之類關鍵字,也可以百度的到。
2.1. 配置yum
a) 開啟端口
1.使用firewalld
172.16.10.80
firewall-cmd --permanent --add-port=6379/tcp //添加端口。這里主機的端口是6379,根據個人redis的端口進行開放
firewall-cmd --reload //重新加載
然后配置剩下的從機
2.使用iptables
直接關掉或者開放端口
這里進行關掉
vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
setenforce 0
service iptables stop
chkconfig iptables off
b) 三台服務器配置好yum
mount /dev/sr0 /mnt/
cd /etc/yum.repos.d/
mv redhat.repo redhat.repo.bak
mv rhel-source.repo rhel-source.repo.bak
vi /etc/yum.repos.d/rhel-debuginfo.repo
[rhel-debuginfo]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=file:///mnt/
enabled=1
gpgcheck=0
yum -y install make gcc*
2.2. 參數配置
mkdir -p /redis5.0
把解壓包上傳到/redis5.0
tar -zxvf redis-5.0.0.tar.gz
需要修改以及注釋的參數如下:
Master:
cd /redis5.0/redis-5.0.0
save 900 1
#save 300 10
#save 60 10000
bind 172.6.10.80
daemonize yes
loglevel warning
timeout 60
logfile "6379.log"
dbfilename dump6379.rdb
maxmemory-policy volatile-ttl
auto-aof-rewrite-min-size 10GB
masterauth redis --主庫的密碼(本機可以不配置)最好配置因為等下配置哨兵,如果主機掛了重啟之后(等於變成了從機)不能直接連上。(主機會根據哨兵投票決定)
Slave1:
cd /redis5.0/redis-5.0.0
save 900 1
#save 300 10
#save 60 10000
bind 172.6.10.81
daemonize yes
loglevel warning
logfile "6380.log"
dbfilename dump6380.rdb
pidfile /var/run/redis_6380.pid
port 6380
timeout 60
maxmemory-policy volatile-ttl
auto-aof-rewrite-min-size 10GB
slaveof 172.16.10.80 6379
masterauth redis --主庫的密碼
Salve2:
cd /redis5.0/redis-5.0.0
save 900 1
#save 300 10
#save 60 10000
bind 172.6.10.82
daemonize yes
loglevel warning
port 6381
timeout 60
dbfilename dump6381.rdb
logfile "6381.log"
pidfile /var/run/redis_6381.pid
maxmemory-policy volatile-ttl
auto-aof-rewrite-min-size 10GB
slaveof 172.16.10.80 6379
masterauth redis --主庫的密碼
2.3. 安裝
cd /redis5.0/redis-5.0.0
make
make install
安裝完成。
啟動如下:
redis-server /redis5.0/redis-5.0.0/redis6379.conf
redis-server /redis5.0/redis-5.0.0/redis6380.conf
redis-server /redis5.0/redis-5.0.0/redis6381.conf
由於參數已經修改完了,后面修改密碼:
config set requirepass "redis"
並且寫入配置文件:
例如關閉提示需要密碼就說明修改成功。
同理修改下面兩台服務器:
主從復制的密碼配置:
查看主機角色信息:
查看從機一配置:
查看從機二配置:
可以看出同步狀態正常。
三、測試
3.1. 注意
1、如果沒有把slaveof 參數配置到參數文件中,從庫重啟后需要重新slaveof;即從庫會變成主庫(沒有配置slaveof參數文件)
2、在主從搭建完成后,后面添加的從庫slaveof后,會自動把數據從主庫同步過來,生產上會有一定的性能影響。
3、從機只有讀的權限,主機才能夠寫操作。
3.1. Master 掛掉slave變master(反客為主)
master 掛掉后,手動的讓從庫一升為主庫。
關閉主庫。
查看從庫1,2
slave 變成主庫
slaveof no one
3.1. 哨兵模式配置測試
Redis的哨兵(sentinel) 系統用於管理多個 Redis 服務器,該系統執行以下三個任務:
監控(Monitoring): 哨兵(sentinel) 會不斷地檢查你的Master和Slave是否運作正常。
提醒(Notification):當被監控的某個 Redis出現問題時, 哨兵(sentinel) 可以通過 API 向管理員或者其他應用程序發送通知。
自動故障遷移(Automatic failover):當一個Master不能正常工作時,哨兵(sentinel) 會開始一次自動故障遷移操作,它會將失效Master的其中一個Slave升級為新的Master, 並讓失效Master的其他Slave改為復制新的Master;如果修復好的master重新啟動后,原master變成slave。
簡單配置如下:
在安裝的redis目錄下,有sentinel.conf文件,可以設置超時等等。
本次測試是我自己新建一個文件格式如下:
sentinel monitor 主機名 主機ip 主機端口 票數n 票數多余n的從機作為主機
由於是三台服務器(誰有2票就誰為新master)
vi sentinel6379.conf
sentinel monitor redis01 172.16.10.80 6379 2
sentinel auth-pass redis01 redis #master如果設置密碼 沒有可以不用這一行
sentinel down-after-milliseconds redis01 5000 #修改心跳為5000毫秒
protected-mode no --關閉保護進程模式,防止哨兵不能互相通信
啟動:
redis-sentinel /redis5.0/redis-5.0.0/sentinel6379.conf
或者后台啟動:
redis-sentinel /redis5.0/redis-5.0.0/sentinel6379.conf --sentinel &
vi sentinel6380.conf
sentinel monitor redis01 172.16.10.80 6379 2
sentinel auth-pass redis01 redis
sentinel down-after-milliseconds redis01 5000
protected-mode no --關閉保護進程模式,防止哨兵不能互相通信
啟動:
vi sentinel6381.conf
sentinel monitor redis01 172.16.10.80 6379 2
sentinel auth-pass redis01 redis
sentinel down-after-milliseconds redis01 5000
protected-mode no --關閉保護進程模式,防止哨兵不能互相通信
3.4. 哨兵模式的反客為主(自動)
查看各個節點的哨兵模式情況:
Master:
redis-cli -h 172.16.10.80 -p 26379
info sentinel
Slave1:
redis-cli -h 172.16.10.81 -p 26379
info sentinel
Slave2:
redis-cli -h 172.16.10.82 -p 26379
info sentinel
模擬master down掉測試:
查看:
原master哨兵進程日志:
從中看出failover到原slave1了。
查看原salve1(現master) 哨兵進程日志:
Slave1 為新master了。
查看slave2日志說明:
Salve2變成原slave1的從庫了。
查看新master信息:
角色為master,一個從庫,為原slave1。
查看新從庫信息:
為slave角色,新的主庫為10.81。
測試是否同步:
同步成功。
3.3. 原master修好后
原master修好后,啟動后:
變成現在master的從機了,並不會變成獨立的一台master。
哨兵日志提示為10.81的從庫了。
測試:
把現在新master數據同步過來了沒問題,也可以自行測試。
總結:
1、 大的互聯網公司都會使用哨兵模式主從或者集群模式24*7提供業務以及提供讀寫分離,減輕系統的壓力,身邊使用最多的就是大家知道的知乎網站。
2、 在failover之后,原master修復好后會變為新master的從庫,並不會變成獨立的一台master。