centos7 redis主從部署-Sentinel(哨兵模式)
本文使用一台伺服器和多個端口實作 ,本機ip : 192.168.3.10
如果使用多台伺服器則需要防火牆開放對應的端口(防火牆設定如果報錯根據錯誤百度自行解決)
[[email protected] /]# firewall-cmd --state // 檢視firewall防火牆情況
[[email protected] /]# service firewalld start // 開啟firewall防火牆
[[email protected] /]# firewall-cmd --add-port=6379/tcp //添加端口重新開機後需重新開機添加
[[email protected] /]# firewall-cmd --permanent --add-port=6379/tcp //添加端口重新開機 permanent參數表示永久有效
[[email protected] /]# firewall-cmd --remove-port=6379/tcp //删除端口
[[email protected] /]# firewall-cmd --query-port=6379/tcp //檢視端口
[[email protected] /]# firewall-cmd --list-all //檢視所有firewall端口
[[email protected] /]# firewall-cmd --reload //重新開機防火牆
1、安裝redis
[[email protected] updown]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz //下載下傳
[[email protected] updown]# tar zxvf redis-5.0.5.tar.gz //解壓
[[email protected] updown]# cd redis-5.0.5
[[email protected] redis-5.0.5]# make //編譯
[[email protected] redis-5.0.5]# make install PREFIX=/usr/local/redis //安裝到/usr/local/redis
[[email protected] redis-5.0.5]# mkdir /usr/local/redis/bin/conf //在安裝目錄中建立配置檔案目錄
[[email protected] redis-5.0.5]# cp redis.conf /usr/local/redis/bin/conf/ //主服務配置檔案預設端口6379
[[email protected] redis-5.0.5]# cp redis.conf /usr/local/redis/bin/conf/redis-6380.conf //從服務配置檔案端口6380
[[email protected] redis-5.0.5]# cp redis.conf /usr/local/redis/bin/conf/redis-6381.conf //從服務配置檔案端口6381
[[email protected] redis-5.0.5]# cp redis.conf /usr/local/redis/bin/conf/redis-6382.conf //從服務配置檔案端口6382
2、修改主配置檔案
[[email protected] redis-5.0.5]# vim /usr/local/redis/bin/conf/redis.conf
1) 找到bind 127.0.0.1 修改成 bind 0.0.0.0 //都可以通路
2)找到 protected-mode yes 修改成 protected-mode no //關閉保護模式
3) 找到daemonize no 修改成 daemonize yes //守護線程yes redis會在背景運作, no 進入redis的指令行界面
3、修改從配置檔案
[[email protected] redis-5.0.5]# vim /usr/local/redis/bin/conf/redis-6380.conf
1) 找到bind 127.0.0.1 修改成 #bind 127.0.0.1 //登出它開啟遠端通路
2) 找到daemonize no 修改成 daemonize yes //守護線程yes redis會在背景運作, no 進入redis的指令行界面
3) 找到 protected-mode 值設定為 no //關閉保護模式
4) 找到port //值修改端口為6380
5) 找到 pidfile //值修改為/usr/local/redis/conf/redis-6380.pid
6) 找到rdbchecksum yes 在他下面添加兩行(在末尾添加就可以)
slaveof 192.168.3.10 6379 //slaveof指令可以将目前伺服器轉變為指定伺服器的從屬服務
slave-read-only yes //隻讀
注意:
我的redis版本不是 slaveof 而是 replicaof
下面這兩行和上面的兩行功能一樣
replicaof 192.168.3.10 7379
replica-read-only yes //隻讀
7) /usr/local/redis/bin/conf/redis-6381.conf
/usr/local/redis/bin/conf/redis-6382.conf
這兩個配置檔案同上配置
4、啟動redis 服務執行個體
[[email protected] redis-5.0.5]# cd /usr/local/redis/bin
[[email protected] bin]# ./redis-server conf/redis.conf //先啟動主伺服器
[[email protected] bin]# ./redis-server conf/redis-6380.conf //啟動從伺服器
[[email protected] bin]# ./redis-server conf/redis-6381.conf //啟動從伺服器
[[email protected] bin]# ./redis-server conf/redis-6382.conf //啟動從伺服器
5、檢視是否啟動
[[email protected] bin]# ps -ef | grep redis
root 26421 1 0 19:24 ? 00:00:11 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 23823 1 0 18:10 ? 00:00:13 ./redis-server *:6380
root 23937 1 0 18:16 ? 00:00:12 ./redis-server *:6381
root 23937 1 0 18:16 ? 00:00:12 ./redis-server *:6382
6、驗證主從
[[email protected] bin]# ./redis-cli //預設 6379 主服務
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.3.10,port=6380,state=online,offset=210,lag=1
slave1:ip=192.168.3.10,port=6381,state=online,offset=224,lag=1
slave2:ip=192.168.3.10,port=6382,state=online,offset=224,lag=1
master_replid:33763613f871403444b07f81f77ac48783009ff0
127.0.0.1:6379> set name fuj
[[email protected] bin]# ./redis-cli -p 6380 //6380 從服務
127.0.0.1:6380> get name
"fuj"
這中主從模式一旦主服務挂掉整個redis服務将不可用。它不能實作從節點自動更新成主服務,為了實作從節點自動更新成主服務,這裡将使用哨兵模式。下面為基于主從哨兵模式配置
1、在上面的redis解壓檔案夾複制哨兵配置檔案到安裝目錄配置目錄中(我這裡同樣放在/usr/local/redis/bin/conf中)
[[email protected] redis-5.0.5]# cp sentinel.conf /usr/local/redis/bin/conf/ //主伺服器
[[email protected] redis-5.0.5]# cp sentinel.conf /usr/local/redis/bin/conf/sentinel-6380.conf
[[email protected] redis-5.0.5]# cp sentinel.conf /usr/local/redis/bin/conf/sentinel-6381.conf
[[email protected] redis-5.0.5]# cp sentinel.conf /usr/local/redis/bin/conf/sentinel-6382.conf
2、修sentinel.conf 檔案
[[email protected] bin]# vim conf/sentinel.conf
1)找到port 26379 修改成對應的端口
2)找到daemonize no 修改成 daemonize yes
3)找到sentinel monitor mymaster 127.0.0.1 6379 2修改成sentinel monitor r1 192.168.3.10 6379 1
r1 監控名稱可以自定義(配置檔案裡所有mymaster修改成此處設定的監控名稱)
192.168.3.10 哨兵監控的ip 主ip
6379 端口
1 選舉次數
4)修改 sentinel-6380.conf sentinel-6381.conf sentinel-6382.conf
找到port 26379 修改成對應的端口26380 26381 26382
找到daemonize no 修改成 daemonize yes
找到sentinel monitor mymaster 127.0.0.1 6379 2修改成sentinel monitor r1 192.168.3.10 6379 1
r1 監控名稱可以自定義(配置檔案裡所有mymaster修改成此處設定的監控名稱)
192.168.3.10 哨兵監控的ip
6379 端口
1 選舉次數
3、開啟哨兵
[[email protected] bin]# ./redis-sentinel conf/sentinel.conf
[[email protected] bin]# ./redis-sentinel conf/sentinel-26380.conf
[[email protected] bin]# ./redis-sentinel conf/sentinel-26381.conf
[[email protected] bin]# ./redis-sentinel conf/sentinel-26382.conf
4、驗證
[[email protected] bin]# ./redis-cli -p 26380
127.0.0.1:26380>info
[[email protected] bin]# ./redis-cli -p 6379 shutdown //關掉主等一會兒
[[email protected] bin]# ./redis-cli -p 26380
127.0.0.1:26380>info
此時主已經自動切換
[[email protected] bin]# ./redis-cli -p 6382 //進入主服務
127.0.0.1:6382>info
此時隻有隻有兩個從服務了
此時主已經自動切換
[[email protected] bin]# ./redis-server conf/redis-6379.conf //打開關閉的6379服務
[[email protected] bin]# ./redis-cli -p 6382 //進入主服務
127.0.0.1:6382>info
此時從服務恢複到3個 6379服務變成了6382的從服務了
以上配置沒有密碼設定。這裡隻做配置,沒有對原理做講解,如果需要知道原理請自行百度!