Redis的主從模式和哨兵模式部署
阿新 • • 發佈:2020-11-23
Redis的主從模式和哨兵模式部署
目錄
- 一、Redis主從模式配置
- 1.1、案例拓撲圖
- 1.2、環境說明
- 1.3、Redis安裝
- 1.4、修改配置檔案
- 1.5、測試效果
- 二、Redis哨兵模式
- 2.1、Redis sentinel介紹
- 2.2、哨兵模式的作用
- 2.3、環境說明
- 2.4、哨兵模式配置
- 2.5、啟動哨兵模式
- 2.6、檢視哨兵資訊
- 2.7、故障模擬
一、Redis主從模式配置
1.1、案例拓撲圖
1.2、環境說明
主機名稱 |
IP地址 |
redis版本和角色說明 |
master |
20.0.0.10 |
redis 5.0.7(主) |
slave1 |
20.0.0.20 |
redis 5.0.7(從) |
slave2 |
20.0.0.30 |
redis 5.0.7(從) |
1.3、Redis安裝
所有伺服器上安裝,在master上演示
1 [root@master ~]# tar zxf redis-5.0.7.tar.gz 2 [root@master ~]# cd redis-5.0.7/ 3 [root@master redis-5.0.7]# make -j2 4 [root@master redis-5.0.7]# make PREFIX=/usr/local/redis install 5 [root@master redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/ 6 [root@master redis-5.0.7]# cd utils/ 7 [root@master utils]# ./install_server.sh 8 Welcome to the redis service installer 9 This script will help you easily set up a running redis server 10 11 Please select the redis port for this instance: [6379] 12 Selecting default: 6379 13 Please select the redis config file name [/etc/redis/6379.conf] 14 Selected default - /etc/redis/6379.conf 15 Please select the redis log file name [/var/log/redis_6379.log] 16 Selected default - /var/log/redis_6379.log 17 Please select the data directory for this instance [/var/lib/redis/6379] 18 Selected default - /var/lib/redis/6379 19 Please select the redis executable path [/usr/local/bin/redis-server] 20 Selected config: 21 Port : 6379 22 Config file : /etc/redis/6379.conf 23 Log file : /var/log/redis_6379.log 24 Data dir : /var/lib/redis/6379 25 Executable : /usr/local/bin/redis-server 26 Cli Executable : /usr/local/bin/redis-cli 27 Is this ok? Then press ENTER to go on or Ctrl-C to abort. 28 Copied /tmp/6379.conf => /etc/init.d/redis_6379 29 Installing service... 30 Successfully added to chkconfig! 31 Successfully added to runlevels 345! 32 Starting Redis server... 33 Installation successful!
1.4、修改配置檔案
1.4.1、master配置檔案修改
1 [root@master src]# vim /etc/redis/6379.conf 2 #69行 修改監聽地址為20.0.0.10(在實驗環境使用),現網環境建議繫結從伺服器IP地址 3 bind 20.0.0.10 4 #136行 開啟守護程序 5 daemonize yes 6 #171行 修改日誌檔案目錄 7 logfile /var/log/redis_6379.log 8 #263行 修改工作目錄 9 dir /var/lib/redis/6379 10 #699行 開啟AOF持久化功能 11 appendonly yes 12 13 [root@master utils]# /etc/init.d/redis_6379 restart #開啟Redis 14 15 [root@master utils]# netstat -anpt | grep redis 16 tcp 0 0 20.0.0.10:6379 0.0.0.0:* LISTEN 57685/redis-server
1.4.2、slave1配置檔案修改
1 [root@slave1 src]# vim /etc/redis/6379.conf 2 #69行 修改監聽地址為20.0.0.10(在實驗環境使用),現網環境建議繫結從伺服器IP地址 3 bind 20.0.0.20 4 #136行 開啟守護程序 5 daemonize yes 6 #171行 修改日誌檔案目錄 7 logfile /var/log/redis_6379.log 8 #263行 修改工作目錄 9 dir /var/lib/redis/6379 10 #699行 開啟AOF持久化功能 11 appendonly yes 12 #287行 修改IP和埠 13 replicaof 20.0.0.10 6379 14 15 [root@slave1 utils]# /etc/init.d/redis_6379 restart #開啟Redis 16 17 [root@slave1 utils]# netstat -anpt | grep redis #檢視埠號 18 tcp 0 0 20.0.0.20:6379 0.0.0.0:* LISTEN 19037/redis-server 19 tcp 0 0 20.0.0.20:34397 20.0.0.10:6379 ESTABLISHED 19037/redis-server
1.4.2、slave2配置檔案修改
1 [root@slave2 src]# vim /etc/redis/6379.conf 2 #69行 修改監聽地址為20.0.0.10(在實驗環境使用),現網環境建議繫結從伺服器IP地址 3 bind 20.0.0.30 4 #136行 開啟守護程序 5 daemonize yes 6 #171行 修改日誌檔案目錄 7 logfile /var/log/redis_6379.log 8 #263行 修改工作目錄 9 dir /var/lib/redis/6379 10 #699行 開啟AOF持久化功能 11 appendonly yes 12 #287行 修改IP和埠 13 replicaof 20.0.0.10 6379 14 15 [root@slave2 utils]# /etc/init.d/redis_6379 restart #開啟Redis 16 17 [root@slave2 utils]# netstat -anpt | grep redis 18 tcp 0 0 20.0.0.30:6379 0.0.0.0:* LISTEN 59182/redis-server 19 tcp 0 0 20.0.0.30:46648 20.0.0.10:6379 ESTABLISHED 59182/redis-server
1.5、測試效果
1.5.1、驗證主從效果(master上看日誌)
1 [root@master utils]# vi /var/log/redis_6379.log 2 ......省略部分內容 3 57685:M 11 Nov 2020 10:51:17.605 * Replica 20.0.0.20:6379 asks for synchronization #slave1伺服器IP 4 57685:M 11 Nov 2020 10:51:17.605 * Full resync requested by replica 20.0.0.20:6379 5 57685:M 11 Nov 2020 10:51:17.605 * Starting BGSAVE for SYNC with target: disk 6 57685:M 11 Nov 2020 10:51:17.606 * Background saving started by pid 57691 7 57691:C 11 Nov 2020 10:51:17.607 * DB saved on disk 8 57691:C 11 Nov 2020 10:51:17.607 * RDB: 4 MB of memory used by copy-on-write 9 57685:M 11 Nov 2020 10:51:17.658 * Background saving terminated with success 10 57685:M 11 Nov 2020 10:51:17.658 * Synchronization with replica 20.0.0.20:6379 succeeded 11 57685:M 11 Nov 2020 10:55:07.287 * Replica 20.0.0.30:6379 asks for synchronization #slave2伺服器IP 12 57685:M 11 Nov 2020 10:55:07.287 * Full resync requested by replica 20.0.0.30:6379 13 57685:M 11 Nov 2020 10:55:07.287 * Starting BGSAVE for SYNC with target: disk 14 57685:M 11 Nov 2020 10:55:07.288 * Background saving started by pid 57734 15 57734:C 11 Nov 2020 10:55:07.290 * DB saved on disk 16 57734:C 11 Nov 2020 10:55:07.290 * RDB: 4 MB of memory used by copy-on-write 17 57685:M 11 Nov 2020 10:55:07.372 * Background saving terminated with success 18 57685:M 11 Nov 2020 10:55:07.372 * Synchronization with replica 20.0.0.30:6379 succeeded
1.5.2、master上驗證從節點
1 [root@master utils]# redis-cli -h 20.0.0.10 -p 6379 2 20.0.0.10:6379> info replication 3 # Replication 4 role:master 5 connected_slaves:2 6 slave0:ip=20.0.0.20,port=6379,state=online,offset=2702,lag=0 7 slave1:ip=20.0.0.30,port=6379,state=online,offset=2702,lag=1
1.5.3、插入資料驗證
1 master上插入資料 2 20.0.0.10:6379> set name lisi 3 OK 4 20.0.0.10:6379> set score 80 5 OK 6 20.0.0.10:6379> keys * 7 1) "score" 8 2) "name" 9 20.0.0.10:6379> get name 10 "lisi" 11 20.0.0.10:6379> get score 12 "80" 13 14 slave1上檢視 15 [root@slave1 utils]# redis-cli -h 20.0.0.20 -p 6379 16 20.0.0.20:6379> keys * 17 1) "score" 18 2) "name" 19 20.0.0.20:6379> get name 20 "lisi" 21 20.0.0.20:6379> get score 22 "80" 23 24 slave2上檢視 25 [root@slave2 utils]# redis-cli -h 20.0.0.30 -p 6379 26 20.0.0.30:6379> keys * 27 1) "score" 28 2) "name" 29 20.0.0.30:6379> get name 30 "lisi" 31 20.0.0.30:6379> get score 32 "80"Top
二、Redis哨兵模式
2.1、Redis sentinel介紹
Redis Sentinel是Redis高可用的實現方案。Sentinel是一個管理多個Redis例項的工具,它可以實現對Redis的監控、通知、自動故障轉移。
2.2、哨兵模式的作用
① 監控
不斷的檢查master和slave是否正常執行。master存活檢測、master與slave執行情況檢測
② 通知(提醒)
當被監控的伺服器出現問題時,向其他(哨兵間,客戶端)傳送通知。
③ 自動故障轉移
斷開master與slave連線,選取一個slave作為master,將其他slave連線到新的master,並告知客戶端新的伺服器地址
PS:哨兵也是一臺redis伺服器,只是不提供資料服務
哨兵的啟動依賴於主從模式,所以須把主從模式安裝好的情況下再去做哨兵模式,所有節點上都需要部署哨兵模式,哨兵模式會監控所有的redis工作節點是否正常,當
master出現問題的時候,因為其他節點與主節點失去聯絡,因此會投票,投票過半就認為這個master的確出現問題,然後會通知哨兵間,然後從slaves中選取一個作為新的
master,至少需要3個或3個以上伺服器
2.3、環境說明
主機名稱 | IP地址 | redis版本和角色說明 |
---|---|---|
master | 20.0.0.10:6379 | redis 5.0.7(主) |
slave1 | 20.0.0.20:6379 | redis 5.0.7(從) |
slave2 | 20.0.0.30:6379 | redis 5.0.7(從) |
master | 20.0.0.10:26379 | Sentinel1 |
slave1 | 20.0.0.20:26379 | Sentinel2 |
slave2 | 20.0.0.30:26379 | Sentinel3 |
2.4、哨兵模式配置
所有節點都需要修改
1 [root@master ~]# vi redis-5.0.7/sentinel.conf 2 17行/protected-mode no #關閉保護模式 3 26行/daemonize yes #指定sentinel為後臺啟動 4 36行/logfile "/var/log/sentinel.log" #指定日誌存放路徑 5 65行/dir "/var/lib/redis/6379" #指定資料庫存放路徑 6 84行/sentinel monitor mymaster 20.0.0.10 6379 2 #至少幾個哨兵檢測到主伺服器故障了,才會進行故障遷移,全部指向masterIP 7 113行/sentinel down-after-milliseconds mymaster 30000 #判定伺服器down掉的時間週期,預設30000毫秒(30秒) 8 146行/sentinel failover-timeout mymaster 180000 #故障節的的最大超時時間為180000(180秒)
2.5、啟動哨兵模式
先啟master,再啟slave
1 [root@master ~]# redis-sentinel redis-5.0.7/sentinel.conf & 2 [1] 58571 3 4 [root@slave1 ~]# redis-sentinel redis-5.0.7/sentinel.conf & 5 [1] 19812 6 7 [root@slave2 ~]# redis-sentinel redis-5.0.7/sentinel.conf & 8 [1] 59917
2.6、檢視哨兵資訊
1 master檢視 2 [root@master ~]# redis-cli -h 20.0.0.10 -p 26379 info Sentinel 3 # Sentinel 4 sentinel_masters:1 5 sentinel_tilt:0 6 sentinel_running_scripts:0 7 sentinel_scripts_queue_length:0 8 sentinel_simulate_failure_flags:0 9 master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3 10 11 slave1檢視 12 [root@slave1 ~]# redis-cli -h 20.0.0.20 -p 26379 info Sentinel 13 # Sentinel 14 sentinel_masters:1 15 sentinel_tilt:0 16 sentinel_running_scripts:0 17 sentinel_scripts_queue_length:0 18 sentinel_simulate_failure_flags:0 19 master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3 20 21 slave2上檢視 22 [root@slave2 ~]# redis-cli -h 20.0.0.20 -p 26379 info Sentinel 23 # Sentinel 24 sentinel_masters:1 25 sentinel_tilt:0 26 sentinel_running_scripts:0 27 sentinel_scripts_queue_length:0 28 sentinel_simulate_failure_flags:0 29 master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3
2.7、故障模擬
2.7.1、
1 檢視master程序號 2 [root@master ~]# ps -ef | grep redis 3 root 57685 1 0 10:51 ? 00:00:06 /usr/local/bin/redis-server 20.0.0.10:6379 4 root 58567 1 0 12:07 ? 00:00:01 redis-sentinel *:26379 [sentinel] 5 root 58649 58470 0 12:15 pts/1 00:00:00 grep --color=auto redis 6 7 殺死master上redis-server的程序號 8 [root@master ~]# kill -9 57685 #master上redis-server的程序號
2.7.2、驗證結果
1 [root@master ~]# tail -f /var/log/sentinel.log 2 58567:X 11 Nov 2020 12:18:14.388 # +failover-state-reconf-slaves master mymaster 20.0.0.10 6379 3 58567:X 11 Nov 2020 12:18:14.467 * +slave-reconf-sent slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379 4 58567:X 11 Nov 2020 12:18:14.776 # -odown master mymaster 20.0.0.10 6379 5 58567:X 11 Nov 2020 12:18:15.428 * +slave-reconf-inprog slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379 6 58567:X 11 Nov 2020 12:18:15.428 * +slave-reconf-done slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379 7 58567:X 11 Nov 2020 12:18:15.483 # +failover-end master mymaster 20.0.0.10 6379 8 58567:X 11 Nov 2020 12:18:15.483 # +switch-master mymaster 20.0.0.10 6379 20.0.0.30 6379 #從master轉到了slave2上 9 58567:X 11 Nov 2020 12:18:15.483 * +slave slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.30 6379 10 58567:X 11 Nov 2020 12:18:15.483 * +slave slave 20.0.0.10:6379 20.0.0.10 6379 @ mymaster 20.0.0.30 6379 11 58567:X 11 Nov 2020 12:18:45.535 # +sdown slave 20.0.0.10:6379 20.0.0.10 6379 @ mymaster 20.0.0.30 6379 12 13 [root@master ~]# redis-cli -p 26379 INFO Sentinel 14 # Sentinel 15 sentinel_masters:1 16 sentinel_tilt:0 17 sentinel_running_scripts:0 18 sentinel_scripts_queue_length:0 19 sentinel_simulate_failure_flags:0 20 master0:name=mymaster,status=ok,address=20.0.0.30:6379,slaves=2,sentinels=3