一、Redis安裝
準(zhǔn)備三臺(tái)主機(jī),分別安裝Redis(本文以redis6.0.6為例)
我這里:
10.27.3.121 (主)
10.27.3.122 (從)
10.27.3.123 (從)
1.下載并解壓redis安裝包
wget http://download.redis.io/releases/redis-6.0.6.tar.gz
tar -zxvf redis-6.0.6.tar.gz
2.編譯安裝
進(jìn)入解壓后的redis目錄下
cd redis-6.0.6
檢測(cè)是否有g(shù)cc環(huán)境,若提示無(wú)此命令則需要先安裝gcc環(huán)境(此處gcc版本如果低于5.3,后續(xù)編譯可能會(huì)報(bào)錯(cuò))
gcc -v
yum install -y gcc
如果gcc低于5.3版本執(zhí)行以下命令升級(jí)
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
#注意:scl命令啟用只是臨時(shí)的,推出xshell或者重啟就會(huì)恢復(fù)到原來(lái)的gcc版本。
#如果要長(zhǎng)期生效的話,執(zhí)行如下:
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

編譯到指定目錄
make PREFIX=/usr/local/redis6 install
如果看到此提示,表示編譯成功

安裝Redis,進(jìn)入解壓后redis目錄的utils目錄下
cd utils
執(zhí)行install_server.sh腳本
./install_server.sh
如果報(bào)下圖錯(cuò)誤

打開(kāi)install_server.sh,注釋掉下面的內(nèi)容:
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
#unset _pid_1_exe
重新執(zhí)行
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 16379
Please select the redis config file name [/etc/redis/16379.conf] /usr/local/redis6/conf/redis.conf
Please select the redis log file name [/var/log/redis_16379.log] /usr/local/redis6/log/redis.log
Please select the data directory for this instance [/var/lib/redis/16379]
Selected default - /var/lib/redis/16379
Please select the redis executable path [] /usr/local/redis6/bin/redis-server
Selected config:
Port : 16379
Config file : /usr/local/redis6/conf/redis.conf
Log file : /usr/local/redis6/log/redis.log
Data dir : /var/lib/redis/16379
Executable : /usr/local/redis6/bin/redis-server
Cli Executable : /usr/local/redis6/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/16379.conf => /etc/init.d/redis_16379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
至此redis安裝成功,運(yùn)行腳本后默認(rèn)會(huì)啟動(dòng)redis,查看redis進(jìn)程

以后就可以使用系統(tǒng)服務(wù)的命令來(lái)操作redis了:
systemctl stop redis_16379
systemctl status redis_16379
systemctl start redis_16379
systemctl restart redis_16379
redis.conf配置
開(kāi)啟守護(hù)進(jìn)程模式
daemonize yes
去除bind的ip,注釋掉下面這一行,否則無(wú)法使用遠(yuǎn)程連接,只能本地連接redis。
# bind 127.0.0.1
關(guān)閉保護(hù)模式,將protected-mode的yes改為no,也是開(kāi)啟遠(yuǎn)程連接。
protected-mode no
改完后重啟redis
systemctl restart redis_16379
三臺(tái)服務(wù)器都需要安裝。
二、集群搭建
1.從節(jié)點(diǎn)的配置(兩臺(tái)從節(jié)點(diǎn)主機(jī)都需要配置)
從節(jié)點(diǎn)也是redis服務(wù),只需要修改redis.conf配置文件中的內(nèi)容,重啟redis即可
# 聲明主節(jié)點(diǎn),舊的版本是slaveof配置,兩者等價(jià)
replicaof 10.27.3.121 16379
# 配置主節(jié)點(diǎn)訪問(wèn)密碼,若主節(jié)點(diǎn)沒(méi)有密碼則可忽略
masterauth 123456
配置完畢后啟動(dòng)從節(jié)點(diǎn)服務(wù),可以在主節(jié)點(diǎn)的redis日志中看到兩臺(tái)從機(jī)連接成功,構(gòu)建了redis復(fù)制集(主從結(jié)構(gòu))。


2.哨兵節(jié)點(diǎn)的配置
哨兵模式的主要作用就是監(jiān)控Redis集群,所以我們搭建哨兵模式之前要確認(rèn)我們的Redis集群已經(jīng)成功搭建了。哨兵模式的搭建其實(shí)也相對(duì)比較簡(jiǎn)單,只需要配置主節(jié)點(diǎn)監(jiān)控即可。
進(jìn)入主節(jié)點(diǎn)的redis解壓縮目錄下,將sentinel.conf配置文件移動(dòng)到redis安裝目錄下
mv sentinel.conf /usr/local/redis6/conf/
修改sentinel.conf配置文件
# sentinel監(jiān)聽(tīng)端口,默認(rèn)是26379,可以修改。
port 26379
# 是否開(kāi)啟守護(hù)進(jìn)程
daemonize yes
# pid文件所在目錄
pidfile "/var/run/redis-sentinel.pid"
# sentinel日志所在目錄
logfile "/usr/local/redis6/log/redis-sentinel.log"
# 哨兵sentinel的工作目錄
dir "/tmp"
# 告訴sentinel去監(jiān)聽(tīng)地址為ip:port的一個(gè)master,這里的master-name可以自定義,quorum是一個(gè)數(shù)字,指明當(dāng)有多少個(gè)sentinel認(rèn)為一個(gè)master失效時(shí),master才算真正失效。master-name只能包含英文字母,數(shù)字,和“.-_”這三個(gè)字符需要注意的是master-ip 要寫(xiě)真實(shí)的ip地址而不要用回環(huán)地址(127.0.0.1)
sentinel monitor mymaster 10.27.3.121 16379 2
# 這個(gè)配置項(xiàng)指定了需要多少失效時(shí)間,一個(gè)master才會(huì)被這個(gè)sentinel主觀地認(rèn)為是不可用的。 單位是毫秒,默認(rèn)為30秒
sentinel down-after-milliseconds mymaster 10000
# failover-timeout 可以用在以下這些方面:
# 1. 同一個(gè)sentinel對(duì)同一個(gè)master兩次failover之間的間隔時(shí)間。
# 2. 當(dāng)一個(gè)slave從一個(gè)錯(cuò)誤的master那里同步數(shù)據(jù)開(kāi)始計(jì)算時(shí)間。直到slave被糾正為向正確的master那里同步數(shù)據(jù)時(shí)。
# 3.當(dāng)想要取消一個(gè)正在進(jìn)行的failover所需要的時(shí)間。
# 4.當(dāng)進(jìn)行failover時(shí),配置所有slaves指向新的master所需的最大時(shí)間。不過(guò),即使過(guò)了這個(gè)超時(shí),slaves依然會(huì)被正確配置為指向master,但是就不按parallel-syncs所配置的規(guī)則來(lái)了。
sentinel failover-timeout mymaster 180000
啟動(dòng)sentinel
/usr/local/redis6/bin/redis-sentinel /usr/local/redis6/conf/sentinel.conf
查看sentinel日志
tail -f /usr/local/redis6/log/redis-sentinel.log

三、測(cè)試
當(dāng)前三臺(tái)主機(jī)
10.27.3.121(主節(jié)點(diǎn))

10.27.3.122(從節(jié)點(diǎn))

10.27.3.123(從節(jié)點(diǎn))

如果此時(shí)當(dāng)主節(jié)點(diǎn)掛了
systemctl stop redis_16379
哨兵會(huì)自動(dòng)選舉出新的主節(jié)點(diǎn),我們發(fā)現(xiàn)10.27.3.122變成了主節(jié)點(diǎn)

10.27.3.123(從節(jié)點(diǎn))

哨兵模式正常工作。