1. 程式人生 > >redis 一主二從三哨兵

redis 一主二從三哨兵

mina ase 成功 所有 listen started mod lin config

總體部署

  • 一主二從三哨兵
  • ip地址分配分別為
  1. 主 127.0.0.1:6379
  2. 從 127.0.0.1:6389
  3. 從 127.0.0.1:6399
  4. 哨兵 127.0.0.1:26379
  5. 哨兵 127.0.0.1:26389
  6. 哨兵 127.0.0.1:26399

一主二從的配置文件 redis.conf設置

主的保持默認的不變

二個從的分別打開配置文件

找到設置端口的地方

分別設置為 6389 和6399

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6389
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6399

然後都在

# slaveof <masterip> <masterport>

一行的下面增加

slaveof 127.0.0.1 6379

這樣主從的配置就ok了

三個哨兵的sentinel.conf配置

分別打開sentinel.conf配置文件

修改他們的端口為

26379 26389 26399

sentinel monitor mymaster 127.0.0.1 6379 2

配置中的這一行表示 我們要監視的主的ip和端口,mymaster是主的名稱,因為我們的主是127.0.0.1:6379,所以三個哨兵中的這個地方都不用修改。

然後所有的配置就ok了,本地測試環境貌似需要改動的地方不多。

啟動

進入src目錄

./redis-server ../redis.conf

技術分享圖片
25496:M 18 Feb 21:43:12.616 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 25496
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25496:M 18 Feb 21:43:12.618 # Server started, Redis version 3.2.8
25496:M 18 Feb 21:43:12.618 * DB loaded from disk: 0.000 seconds
25496:M 18 Feb 21:43:12.618 * The server is now ready to accept connections on port 6379
25496:M 18 Feb 21:43:21.196 * Slave 127.0.0.1:6389 asks for synchronization
25496:M 18 Feb 21:43:21.196 * Full resync requested by slave 127.0.0.1:6389
25496:M 18 Feb 21:43:21.196 * Starting BGSAVE for SYNC with target: disk
25496:M 18 Feb 21:43:21.197 * Background saving started by pid 25498
25498:C 18 Feb 21:43:21.198 * DB saved on disk
25496:M 18 Feb 21:43:21.198 * Background saving terminated with success
25496:M 18 Feb 21:43:21.198 * Synchronization with slave 127.0.0.1:6389 succeeded
25496:M 18 Feb 21:43:28.979 * Slave 127.0.0.1:6399 asks for synchronization
25496:M 18 Feb 21:43:28.979 * Full resync requested by slave 127.0.0.1:6399
25496:M 18 Feb 21:43:28.979 * Starting BGSAVE for SYNC with target: disk
25496:M 18 Feb 21:43:28.980 * Background saving started by pid 25500
25500:C 18 Feb 21:43:28.986 * DB saved on disk
25496:M 18 Feb 21:43:29.044 * Background saving terminated with success
25496:M 18 Feb 21:43:29.044 * Synchronization with slave 127.0.0.1:6399 succeeded
技術分享圖片

  • 從1

進入src目錄

./redis-server ../redis.conf

技術分享圖片
25497:S 18 Feb 21:43:21.194 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6389
 |    `-._   `._    /     _.-‘    |     PID: 25497
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25497:S 18 Feb 21:43:21.196 # Server started, Redis version 3.2.8
25497:S 18 Feb 21:43:21.196 * DB loaded from disk: 0.000 seconds
25497:S 18 Feb 21:43:21.196 * The server is now ready to accept connections on port 6389
25497:S 18 Feb 21:43:21.196 * Connecting to MASTER 127.0.0.1:6379
25497:S 18 Feb 21:43:21.196 * MASTER <-> SLAVE sync started
25497:S 18 Feb 21:43:21.196 * Non blocking connect for SYNC fired the event.
25497:S 18 Feb 21:43:21.196 * Master replied to PING, replication can continue...
25497:S 18 Feb 21:43:21.196 * Partial resynchronization not possible (no cached master)
25497:S 18 Feb 21:43:21.197 * Full resync from master: b17cd8764974c68bedacb2e5927a04e9ff15f7df:1
25497:S 18 Feb 21:43:21.198 * MASTER <-> SLAVE sync: receiving 108 bytes from master
25497:S 18 Feb 21:43:21.198 * MASTER <-> SLAVE sync: Flushing old data
25497:S 18 Feb 21:43:21.198 * MASTER <-> SLAVE sync: Loading DB in memory
25497:S 18 Feb 21:43:21.199 * MASTER <-> SLAVE sync: Finished with success
技術分享圖片
  • 從2

進入src目錄

./redis-server ../redis.conf

技術分享圖片
25499:S 18 Feb 21:43:28.975 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6399
 |    `-._   `._    /     _.-‘    |     PID: 25499
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25499:S 18 Feb 21:43:28.977 # Server started, Redis version 3.2.8
25499:S 18 Feb 21:43:28.977 * DB loaded from disk: 0.000 seconds
25499:S 18 Feb 21:43:28.977 * The server is now ready to accept connections on port 6399
25499:S 18 Feb 21:43:28.979 * Connecting to MASTER 127.0.0.1:6379
25499:S 18 Feb 21:43:28.979 * MASTER <-> SLAVE sync started
25499:S 18 Feb 21:43:28.979 * Non blocking connect for SYNC fired the event.
25499:S 18 Feb 21:43:28.979 * Master replied to PING, replication can continue...
25499:S 18 Feb 21:43:28.979 * Partial resynchronization not possible (no cached master)
25499:S 18 Feb 21:43:28.980 * Full resync from master: b17cd8764974c68bedacb2e5927a04e9ff15f7df:15
25499:S 18 Feb 21:43:29.044 * MASTER <-> SLAVE sync: receiving 108 bytes from master
25499:S 18 Feb 21:43:29.045 * MASTER <-> SLAVE sync: Flushing old data
25499:S 18 Feb 21:43:29.045 * MASTER <-> SLAVE sync: Loading DB in memory
25499:S 18 Feb 21:43:29.045 * MASTER <-> SLAVE sync: Finished with success
技術分享圖片
  • 哨兵1

進入src目錄

./redis-sentinel ../sentinel.conf

技術分享圖片
25501:X 18 Feb 21:43:35.300 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 26379
 |    `-._   `._    /     _.-‘    |     PID: 25501
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25501:X 18 Feb 21:43:35.301 # Sentinel ID is 6f3360c06cafe7807e2c1ae00fe7c791354b8b24
25501:X 18 Feb 21:43:35.301 # +monitor master mymaster 127.0.0.1 6379 quorum 2
技術分享圖片
  • 哨兵2和哨兵3啟動過程相同

驗證

新開一個命令行窗口進入redis的src目錄,用redis-cli工具登錄其中一個哨兵

./redis-cli -p 26379

連接成功後運行如下命令

sentinel master mymaster

結果如下

技術分享圖片
127.0.0.1:26379> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "127.0.0.1"
 5) "port"
 6) "6379"
 7) "runid"
 8) "b17cd8764974c68bedacb2e5927a04e9ff15f7df"
 9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "703"
19) "last-ping-reply"
20) "703"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "1090"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "3253725"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

redis 一主二從三哨兵