redis集羣--多哨兵模式搭建

  • 本文目標

本文記錄redis集羣多哨兵的搭建和測試。redis

  • 安裝準備

centos版本:6.7centos

redis版本:3.2.3bash

安裝方式:源碼安裝socket

  • 操做步驟

    6379爲主節點,6389和6399爲從節點; 26379,26479,26579爲哨兵節點。測試

一、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 26479 26579

sentinel monitor mymaster 127.0.0.1 6379 2
配置中的這一行表示 咱們要監視的主的ip和端口,mymaster是主的名稱,由於咱們的主是127.0.0.1:6379,因此三個哨兵中的這個地方都不用修改。

三、啓動每一個服務

redis-server /usr/local/redis_cluster/6379.conf
redis-server /usr/local/redis_cluster/6389.conf
redis-server /usr/local/redis_cluster/6399.conf
redis-sentinel /usr/local/redis_cluster/26379.conf
redis-sentinel /usr/local/redis_cluster/26479.conf
redis-sentinel /usr/local/redis_cluster/26579.conf

四、測試

4.1 正常狀況

哨兵26379spa

[root@itsecond src]# redis-sentinel /usr/local/redis_cluster/26379.conf
3259:X 26 Feb 22:40:12.836 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 3259
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3259:X 26 Feb 22:40:12.838 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3259:X 26 Feb 22:40:12.847 # Sentinel ID is 7d47930e60e1fcdc5099efdd614472372ae3ffad
3259:X 26 Feb 22:40:12.847 # +monitor master mymaster 127.0.0.1 6379 quorum 2
3259:X 26 Feb 22:40:12.847 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3259:X 26 Feb 22:40:12.848 * +slave slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3259:X 26 Feb 22:40:18.897 * +sentinel sentinel 297ee9394b66ca80fae2d6605311d346765a9901 127.0.0.1 26479 @ mymaster 127.0.0.1 6379
3259:X 26 Feb 22:40:21.280 * +sentinel sentinel 337468c9cc89e34a76519a7a370946f2c7529ff3 127.0.0.1 26579 @ mymaster 127.0.0.1 6379

哨兵26479code

[root@itsecond src]# redis-sentinel /usr/local/redis_cluster/26479.conf
3262:X 26 Feb 22:40:16.820 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26479
 |    `-._   `._    /     _.-'    |     PID: 3262
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3262:X 26 Feb 22:40:16.821 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3262:X 26 Feb 22:40:16.830 # Sentinel ID is 297ee9394b66ca80fae2d6605311d346765a9901
3262:X 26 Feb 22:40:16.830 # +monitor master mymaster 127.0.0.1 6379 quorum 2
3262:X 26 Feb 22:40:16.831 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3262:X 26 Feb 22:40:16.832 * +slave slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3262:X 26 Feb 22:40:16.951 * +sentinel sentinel 7d47930e60e1fcdc5099efdd614472372ae3ffad 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
3262:X 26 Feb 22:40:21.280 * +sentinel sentinel 337468c9cc89e34a76519a7a370946f2c7529ff3 127.0.0.1 26579 @ mymaster 127.0.0.1 6379

哨兵26579server

[root@itsecond src]# redis-sentinel /usr/local/redis_cluster/26579.conf
3265:X 26 Feb 22:40:19.276 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26579
 |    `-._   `._    /     _.-'    |     PID: 3265
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3265:X 26 Feb 22:40:19.277 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3265:X 26 Feb 22:40:19.287 # Sentinel ID is 337468c9cc89e34a76519a7a370946f2c7529ff3
3265:X 26 Feb 22:40:19.287 # +monitor master mymaster 127.0.0.1 6379 quorum 2
3265:X 26 Feb 22:40:19.288 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 22:40:19.288 * +slave slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 22:40:20.928 * +sentinel sentinel 297ee9394b66ca80fae2d6605311d346765a9901 127.0.0.1 26479 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 22:40:21.058 * +sentinel sentinel 7d47930e60e1fcdc5099efdd614472372ae3ffad 127.0.0.1 26379 @ mymaster 127.0.0.1 6379

4.2 從機宕機與恢復

哨兵的反應ip

#宕機
3259:X 26 Feb 22:59:34.700 # +sdown slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
#恢復
3259:X 26 Feb 23:02:40.808 * +reboot slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3259:X 26 Feb 23:02:40.867 # -sdown slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379

4.3主機宕機

哨兵26379ci

3259:X 26 Feb 23:03:44.035 # +sdown master mymaster 127.0.0.1 6379
3259:X 26 Feb 23:03:44.072 # +new-epoch 1
3259:X 26 Feb 23:03:44.074 # +vote-for-leader 337468c9cc89e34a76519a7a370946f2c7529ff3 1
3259:X 26 Feb 23:03:44.125 # +odown master mymaster 127.0.0.1 6379 #quorum 3/2
3259:X 26 Feb 23:03:44.125 # Next failover delay: I will not start a failover before Mon Feb 26 23:09:44 2018
3259:X 26 Feb 23:03:44.382 # +config-update-from sentinel 337468c9cc89e34a76519a7a370946f2c7529ff3 127.0.0.1 26579 @ mymaster 127.0.0.1 6379
3259:X 26 Feb 23:03:44.382 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6399
3259:X 26 Feb 23:03:44.383 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6399
3259:X 26 Feb 23:03:44.383 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
3259:X 26 Feb 23:04:14.402 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399

哨兵26479

3262:X 26 Feb 23:03:43.949 # +sdown master mymaster 127.0.0.1 6379
3262:X 26 Feb 23:03:44.071 # +new-epoch 1
3262:X 26 Feb 23:03:44.072 # +vote-for-leader 337468c9cc89e34a76519a7a370946f2c7529ff3 1
3262:X 26 Feb 23:03:44.385 # +config-update-from sentinel 337468c9cc89e34a76519a7a370946f2c7529ff3 127.0.0.1 26579 @ mymaster 127.0.0.1 6379
3262:X 26 Feb 23:03:44.385 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6399
3262:X 26 Feb 23:03:44.385 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6399
3262:X 26 Feb 23:03:44.385 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
3262:X 26 Feb 23:04:14.432 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399

哨兵26579

3265:X 26 Feb 23:03:43.995 # +sdown master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.068 # +odown master mymaster 127.0.0.1 6379 #quorum 2/2
3265:X 26 Feb 23:03:44.068 # +new-epoch 1
3265:X 26 Feb 23:03:44.068 # +try-failover master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.070 # +vote-for-leader 337468c9cc89e34a76519a7a370946f2c7529ff3 1
3265:X 26 Feb 23:03:44.073 # 297ee9394b66ca80fae2d6605311d346765a9901 voted for 337468c9cc89e34a76519a7a370946f2c7529ff3 1
3265:X 26 Feb 23:03:44.074 # 7d47930e60e1fcdc5099efdd614472372ae3ffad voted for 337468c9cc89e34a76519a7a370946f2c7529ff3 1
3265:X 26 Feb 23:03:44.170 # +elected-leader master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.170 # +failover-state-select-slave master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.223 # +selected-slave slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.223 * +failover-state-send-slaveof-noone slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.282 * +failover-state-wait-promotion slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.286 # +promoted-slave slave 127.0.0.1:6399 127.0.0.1 6399 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.286 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:44.382 * +slave-reconf-sent slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:45.193 # -odown master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:45.330 * +slave-reconf-inprog slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:45.330 * +slave-reconf-done slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:45.412 # +failover-end master mymaster 127.0.0.1 6379
3265:X 26 Feb 23:03:45.412 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6399
3265:X 26 Feb 23:03:45.413 * +slave slave 127.0.0.1:6389 127.0.0.1 6389 @ mymaster 127.0.0.1 6399
3265:X 26 Feb 23:03:45.413 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
3265:X 26 Feb 23:04:15.473 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399

4.4主機恢復

哨兵26379的反應

3259:X 26 Feb 23:35:29.084 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
3259:X 26 Feb 23:35:39.082 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399

哨兵26479和26579的反應

3262:X 26 Feb 23:35:29.358 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6399
相關文章
相關標籤/搜索