redis sentinel哨兵模式安裝部署和切換
1.安裝redis
[root@mdb01 redis-4.0.14]# make cd src && make all make[1]: Entering directory `/u01/redis/redis-4.0.14/src' CC Makefile.dep make[1]: Leaving directory `/u01/redis/redis-4.0.14/src' make[1]: Entering directory `/u01/redis/redis-4.0.14/src' CC adlist.o In file included from adlist.c:34:0: zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory #include <jemalloc/jemalloc.h> ^ compilation terminated. make[1]: *** [adlist.o] Error 1 make[1]: Leaving directory `/u01/redis/redis-4.0.14/src' make: *** [all] Error 2
報錯,用下面的方式編譯
[root@mdb01 redis-4.0.14]# make MALLOC=libc [root@mdb01 src]# cd src/ [root@mdb01 src]# make install CC Makefile.dep Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install [root@mdb01 redis-4.0.14]# mkdir 6379/{run,log,conf,rdb} -p [root@mdb01 redis-4.0.14]# cp redis.conf /u01/redis/6379/conf [root@mdb01 redis-4.0.14]# cp sentinel.conf /u01/redis/6379/conf
conf ##配置檔案目錄
log ##日誌檔案目錄
run ##執行的pid檔案目錄
rdb ##快照和AOF檔案目錄
修改redis.conf如下引數
[root@mdb01 conf]# more redis.conf |grep -v '^#'|grep -v '^$' bind 127.0.0.1 192.168.61.16 protected-mode no daemonize yes pidfile /u01/redis/6379/run/redis_6379.pid logfile "/u01/redis/6379/log//redis_6379.log" masterauth redis requirepass redis
啟動
redis-server /u01/redis/6379/conf/redis.conf
設定密碼
config set requirepass redis
2.搭建從庫
搭建從庫的過程跟主庫是一樣的。只是引數檔案作為slave的主庫資訊:
slaveof 192.168.61.16 6379
slave-read-only yes
3.主從切換
殺掉主庫
從庫執行:
slaveof no one
主庫恢復後:
slaveof 192.168.61.16 6379
主庫當機到恢復的時間內,從庫寫入的資料,會在重新建立主從關係後全部丟失。因為在重新與主庫建立同步關係時,
會嘗試去主庫重新同步資料。
4081:S 04 Apr 16:44:14.374 * Connecting to MASTER 192.168.61.16:6379 4081:S 04 Apr 16:44:14.375 * MASTER <-> SLAVE sync started 4081:S 04 Apr 16:44:14.375 * Non blocking connect for SYNC fired the event. 4081:S 04 Apr 16:44:14.375 * Master replied to PING, replication can continue... 4081:S 04 Apr 16:44:14.376 * Trying a partial resynchronization (request 11331acb42a97da2b6b4300ed50608b7ccf61d7b:5562). 4081:S 04 Apr 16:44:14.377 * Full resync from master: a99fee4793c1b63ccd7f1b13ac04fa72ede2de2c:0 4081:S 04 Apr 16:44:14.377 * Discarding previously cached master state. 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: receiving 199 bytes from master 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: Flushing old data 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: Loading DB in memory 4081:S 04 Apr 16:44:14.434 * MASTER <-> SLAVE sync: Finished with success
4.搭建哨兵模式
192.168.61.16 master+sentinel
192.168.61.17 slave+sentinel
192.168.61.18 slave+sentinel
[root@mdb01 conf]# more sentinel.conf |grep -v '^#'|grep -v '^$'
port 26379 dir /tmp sentinel monitor mymaster 192.168.61.16 6379 2 #mymaster自己起的名字,主庫IP和埠,2是指兩個哨兵連線不到主庫的時候才會切換,一般設定為N/2+1(N為Sentinel總數)。 sentinel down-after-milliseconds mymaster 15000 #哨兵連線主庫超時的時間是15秒。15秒內連線不上該哨兵認為主庫出現問題,滿足兩個就會發生切換 sentinel parallel-syncs mymaster 1 #切換後,從庫每次要跟新主保持資料同步,這個時候不建議所有從庫開始跟主庫重新拉資料,雖然恢復時間最短,但是會造成沒有可用節點 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
注意這裡down-aafter-milliseconds單位是毫秒,開始我配成了15,認為是15秒,結果出現了主從一致在頻繁切換的問題。
每一臺主從上都開啟:
redis-sentinel /u01/redis/6379/conf/sentinel.conf
也可以用下面的方式
redis-server /u01/redis/6379/conf/sentinel.conf --sentinel
模擬切換:
將16主庫殺掉
01
13529:X 05 Apr 11:07:29.846 # +sdown master mymaster 192.168.61.16 6379 #一個sentinel認為16已經down掉,此時為主觀下線(sdown) 13529:X 05 Apr 11:07:29.922 # +odown master mymaster 192.168.61.16 6379 #quorum 2/2 #quorum已經達到了兩個sentinel認為16已經down,此時為客觀下線(odown) 13529:X 05 Apr 11:07:50.872 # +tilt #tilt mode entered 13529:X 05 Apr 11:08:20.942 # -tilt #tilt mode exited 13529:X 05 Apr 11:10:54.650 # +new-epoch 1361 #開啟一個新epoch,下面就開始根據偏移量選舉,然後進行切換 13529:X 05 Apr 11:10:54.651 # +vote-for-leader 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 13529:X 05 Apr 11:10:54.688 # Next failover delay: I will not start a failover before Sun Apr 5 11:16:55 2020 13529:X 05 Apr 11:10:55.834 # +config-update-from sentinel 36c1c343cc73638eae2177e5a71d4fff92cf0b97 192.168.61.17 26379 @ mymaster 192.168.61.16 6379 13529:X 05 Apr 11:10:55.834 # +switch-master mymaster 192.168.61.16 6379 192.168.61.17 6379 13529:X 05 Apr 11:10:55.835 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:10:55.835 * +slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:11:10.878 # +sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
02:
7953:X 05 Apr 11:07:29.852 # +sdown master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:07:29.921 # +odown master mymaster 192.168.61.16 6379 #quorum 3/2 7953:X 05 Apr 11:10:54.646 # +new-epoch 1361 7953:X 05 Apr 11:10:54.646 # +try-failover master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.648 # +vote-for-leader 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7953:X 05 Apr 11:10:54.651 # 84bc46cf5364f80a28d073fc012803ce230cdcdc voted for 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7953:X 05 Apr 11:10:54.651 # 84bc46cf5364f80a28d073fc012803ce230cdcde voted for 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7953:X 05 Apr 11:10:54.732 # +elected-leader master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.732 # +failover-state-select-slave master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.808 # +selected-slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.808 * +failover-state-send-slaveof-noone slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:54.880 * +failover-state-wait-promotion slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:55.767 # +promoted-slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:55.767 # +failover-state-reconf-slaves master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:55.832 * +slave-reconf-sent slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.818 # -odown master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.820 * +slave-reconf-inprog slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.820 * +slave-reconf-done slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.891 # +failover-end master mymaster 192.168.61.16 6379 7953:X 05 Apr 11:10:56.891 # +switch-master mymaster 192.168.61.16 6379 192.168.61.17 6379 7953:X 05 Apr 11:10:56.892 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 7953:X 05 Apr 11:10:56.892 * +slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 7953:X 05 Apr 11:11:11.932 # +sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
17被選舉為新的主庫,那麼這裡會有一個提升為主庫的動作
03:
7732:X 05 Apr 11:07:29.846 # +sdown master mymaster 192.168.61.16 6379 7732:X 05 Apr 11:07:30.921 # +odown master mymaster 192.168.61.16 6379 #quorum 3/2 7732:X 05 Apr 11:10:54.650 # +new-epoch 1361 7732:X 05 Apr 11:10:54.651 # +vote-for-leader 36c1c343cc73638eae2177e5a71d4fff92cf0b97 1361 7732:X 05 Apr 11:10:54.716 # Next failover delay: I will not start a failover before Sun Apr 5 11:16:55 2020 7732:X 05 Apr 11:10:55.841 # +config-update-from sentinel 36c1c343cc73638eae2177e5a71d4fff92cf0b97 192.168.61.17 26379 @ mymaster 192.168.61.16 6379 7732:X 05 Apr 11:10:55.841 # +switch-master mymaster 192.168.61.16 6379 192.168.61.17 6379 7732:X 05 Apr 11:10:55.844 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 7732:X 05 Apr 11:10:55.844 * +slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 7732:X 05 Apr 11:11:10.869 # +sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
該節點也參與了投票,重構。
將老的主庫拉起,老的主庫會作為從庫加入到叢集中
01:
13529:X 05 Apr 11:14:32.115 # -sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:17:32.623 * +fix-slave-config slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 02: 7953:X 05 Apr 11:14:32.573 # -sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 03: 7732:X 05 Apr 11:14:32.684 # -sdown slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379
主動切換:
[root@mdb02 ~]# redis-cli -p 26379 sentinel failover mymaster
OK
01:
13529:X 05 Apr 11:23:13.409 # Executing user requested FAILOVER of 'mymaster' 13529:X 05 Apr 11:23:13.409 # +new-epoch 1362 13529:X 05 Apr 11:23:13.409 # +try-failover master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.458 # +vote-for-leader 84bc46cf5364f80a28d073fc012803ce230cdcde 1362 13529:X 05 Apr 11:23:13.458 # +elected-leader master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.459 # +failover-state-select-slave master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.512 # +selected-slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.512 * +failover-state-send-slaveof-noone slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:13.589 * +failover-state-wait-promotion slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:14.490 # +promoted-slave slave 192.168.61.16:6379 192.168.61.16 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:14.490 # +failover-state-reconf-slaves master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:14.516 * +slave-reconf-sent slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.579 * +slave-reconf-inprog slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.579 * +slave-reconf-done slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.679 # +failover-end master mymaster 192.168.61.17 6379 13529:X 05 Apr 11:23:15.680 # +switch-master mymaster 192.168.61.17 6379 192.168.61.16 6379 13529:X 05 Apr 11:23:15.680 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 13529:X 05 Apr 11:23:15.680 * +slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379
02:
7953:X 05 Apr 11:23:14.186 # +new-epoch 1362 7953:X 05 Apr 11:23:14.517 # +config-update-from sentinel 84bc46cf5364f80a28d073fc012803ce230cdcde 192.168.61.16 26379 @ mymaster 192.168.61.17 6379 7953:X 05 Apr 11:23:14.517 # +switch-master mymaster 192.168.61.17 6379 192.168.61.16 6379 7953:X 05 Apr 11:23:14.517 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7953:X 05 Apr 11:23:14.517 * +slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379
03:
7732:X 05 Apr 11:23:14.186 # +new-epoch 1362 7732:X 05 Apr 11:23:14.519 # +config-update-from sentinel 84bc46cf5364f80a28d073fc012803ce230cdcde 192.168.61.16 26379 @ mymaster 192.168.61.17 6379 7732:X 05 Apr 11:23:14.519 # +switch-master mymaster 192.168.61.17 6379 192.168.61.16 6379 7732:X 05 Apr 11:23:14.519 * +slave slave 192.168.61.18:6379 192.168.61.18 6379 @ mymaster 192.168.61.16 6379 7732:X 05 Apr 11:23:14.519 * +slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379 7732:X 05 Apr 11:23:24.585 * +convert-to-slave slave 192.168.61.17:6379 192.168.61.17 6379 @ mymaster 192.168.61.16 6379
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31480688/viewspace-2684534/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Redis Sentinel哨兵模式部署Redis模式
- Redis安裝之叢集-哨兵模式(sentinel)模式Redis模式
- 【Redis】Sentinel 哨兵模式Redis模式
- Redis Sentinel哨兵模式原理及配置Redis模式
- Redis哨兵sentinelRedis
- Redis哨兵模式(sentinel)學習總結及部署記錄(主從複製、讀寫分離、主從切換)Redis模式
- Redis docker 主從模式與哨兵sentinelRedisDocker模式
- Redis 哨兵模式實現主從故障互切換Redis模式
- Redis sentinel主從切換Redis
- Redis之Sentinel高可用安裝部署Redis
- docker Redis單機主從哨兵模式切換失敗DockerRedis模式
- Redis 哨兵高可用(Sentinel)Redis
- redis sentinel哨兵 例項Redis
- redis哨兵模式的原理及部署Redis模式
- Redis | 第12章 Sentinel 哨兵模式《Redis設計與實現》Redis模式
- redis主從叢集搭建及容災部署(哨兵sentinel)Redis
- 用 docker 學習 redis 主從複製3 redis-sentinel(哨兵模式)DockerRedis模式
- Redis哨兵模式Redis模式
- Redis哨兵叢集:哨兵掛了,主從庫還能切換嗎?Redis
- Redis-3.2.1 sentinel安裝和配置小記Redis
- Redis for linux原始碼&叢集(cluster)&主從(master-slave)&哨兵(sentinel)安裝配置RedisLinux原始碼AST
- Sentinel哨兵模式解決故障轉移模式
- Redis學習筆記(十六) Sentinel(哨兵)(下)Redis筆記
- redis哨兵模式搭建Redis模式
- Redis 哨兵模式搭建Redis模式
- helm 安裝redis的主從、哨兵Redis
- 用 docker 學習 redis 主從複製3.2 redis-sentinel「哨兵模式」核心配置-命令-原理DockerRedis模式
- redis-26.哨兵模式Redis模式
- Redis哨兵模式配置測試Redis模式
- Redis之安裝部署Redis
- 【Redis 系列】redis 學習十一,redis 的哨兵模式詳解和實戰Redis模式
- 用 docker 學習 redis 主從複製3.3 redis-sentinel「哨兵模式」 資料丟失的情況DockerRedis模式
- redis哨兵模式搭建和測試Redis模式
- Redis 哨兵模式的原理及其搭建Redis模式
- redis自學(34)redisTemplate的哨兵模式Redis模式
- Redis2.8.1安裝部署Redis
- redis7.2 安裝部署Redis
- oracle goldengate for oracle rac 的安裝和切換OracleGo