真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Redis主從集群搭建及容災(zāi)部署(哨兵sentinel)

redis容災(zāi)部署(哨兵Sentinel)

創(chuàng)新互聯(lián)專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,成都服務(wù)器托管成都服務(wù)器托管,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。

    1、sentinel介紹

   Redis Sentinel 

   Sentinel(哨兵)是用于監(jiān)控redis集群中Master狀態(tài)的工具,其已經(jīng)被集成在redis2.4+的版本中

    2、Sentinel作用: 

1):Master狀態(tài)檢測 

2):如果Master異常,則會進行Master-Slave切換,將其中一個Slave作為Master,將之前的Master作為Slave 

3):Master-Slave切換后,master_redis.conf、slave_redis.conf和sentinel.conf的內(nèi)容都會發(fā)生改變,即master_redis.conf中會多一行slaveof的配置,sentinel.conf的監(jiān)控目標會隨之調(diào)換 

3、Sentinel工作方式: 

1):每個Sentinel以每秒鐘一次的頻率向它所知的Master,Slave以及其他 Sentinel 實例發(fā)送一個 PING 命令 

2):如果一個實例(instance)距離最后一次有效回復 PING 命令的時間超過 down-after-milliseconds 選項所指定的值, 則這個實例會被 Sentinel 標記為主觀下線。 

3):如果一個Master被標記為主觀下線,則正在監(jiān)視這個Master的所有 Sentinel 要以每秒一次的頻率確認Master的確進入了主觀下線狀態(tài)。 

4):當有足夠數(shù)量的 Sentinel(大于等于配置文件指定的值)在指定的時間范圍內(nèi)確認Master的確進入了主觀下線狀態(tài), 則Master會被標記為客觀下線 

5):在一般情況下, 每個 Sentinel 會以每 10 秒一次的頻率向它已知的所有Master,Slave發(fā)送 INFO 命令 

6):當Master被 Sentinel 標記為客觀下線時,Sentinel 向下線的 Master 的所有 Slave 發(fā)送 INFO 命令的頻率會從 10 秒一次改為每秒一次 

7):若沒有足夠數(shù)量的 Sentinel 同意 Master 已經(jīng)下線, Master 的客觀下線狀態(tài)就會被移除。 

若 Master 重新向 Sentinel 的 PING 命令返回有效回復, Master 的主觀下線狀態(tài)就會被移除。

4、主觀下線和客觀下線 

主觀下線:Subjectively Down,簡稱 SDOWN,指的是當前 Sentinel 實例對某個redis服務(wù)器做出的下線判斷。 

客觀下線:Objectively Down, 簡稱 ODOWN,指的是多個 Sentinel 實例在對Master Server做出 SDOWN 判斷,并且通過 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下線判斷,然后開啟failover.

通俗來講就是: 

redis的sentinel系統(tǒng)用來管理多個redis服務(wù)器,可以實現(xiàn)一個功能上實現(xiàn)HA的集群。該系統(tǒng)主要執(zhí)行三個任務(wù): 

1、監(jiān)控( Monitoring ): Redis Sentinel實時監(jiān)控主服務(wù)器和從服務(wù)器運行狀態(tài)。 

2、提醒(notification): 當被監(jiān)控的某個 Redis 服務(wù)器出現(xiàn)問題時, Redis Sentinel 可以向系統(tǒng)管理員發(fā)送通知, 也可以通過 API 向其他程序發(fā)送通知 

3. 故障遷移(failover發(fā)):自動主從切換

   4. 統(tǒng)一的配置管理:連接者詢問sentinel取得主從的地址

   

    5、Raft分布式算法

1. 主要用途:用于分布式系統(tǒng),系統(tǒng)容錯,以及選出領(lǐng)頭羊

2. 作者:Diego Ongaro,畢業(yè)于哈佛

3. 目前用到這個算法的項目有:

a. CoreOS : 見下面

b. ectd : a distributed, consistent shared configuration 

c. LogCabin : 分布式存儲系統(tǒng)

d. redis sentinel : redis 的監(jiān)控系統(tǒng)

    

   Sentinel使用的Raft算法核心: 原則

1. 所有sentinel都有選舉的領(lǐng)頭羊的權(quán)利

2. 每個sentinel都會要求其他sentinel選舉自己為領(lǐng)頭羊(主要由發(fā)現(xiàn)redis客觀下線的sentinel先發(fā)起選舉)

3. 每個sentinel只有一次選舉的機會

4. 采用先到先得的原則

5. 一旦加入到系統(tǒng)了,則不會自動清除(這一點很重要, why?)

6. 每個sentinel都有唯一的uid,不會因為重啟而變更

7. 達到領(lǐng)頭羊的條件是 N/2 + 1個sentinel選擇了自己

8. 采用配置紀元,如果一次選舉出現(xiàn)腦裂,則配置紀元會遞增,進入下一次選舉,所有sentinel都會處于統(tǒng)一配置紀元,以最新的為標準。

Raft算法核心: 可視圖

   Raft Visualization (算法演示)

   

   Raft分布式算法的應(yīng)用

   coreos:云計算新星 Docker 正在以火箭般的速度發(fā)展,與它相關(guān)的生態(tài)圈也漸入佳境,CoreOS 就是其中之一。CoreOS 是一個全新的、面向數(shù)據(jù)中心設(shè)計的 Linux 操作系統(tǒng),在2014年7月發(fā)布了首個穩(wěn)定版本,目前已經(jīng)完成了800萬美元的A輪融資。

    一個簡單的主從結(jié)構(gòu)加sentinel集群的架構(gòu)圖如下: 

              192.168.110.134(master redis,sentinel)----------->192.168.110.135(slave redis,sentinel)

                        |                   \          /            |

                        |                    \        /             |

                        |                     \      /              |

                        |                      \    /               |

                        |                       \  /                |

                        |                        \                  |

                        |                         \                 |

                        |                       /  \                |

                        |                      /    \               |

                        |                     /      \              |

                        |                    /        \             |           

                    192.168.110.132(sentinel)----------->192.168.110.133(slave redis,sentinel)

      上圖是一主一從節(jié)點,加上兩個部署了sentinel的集群,sentinel集群之間會互相通信,溝通交流redis節(jié)點的狀態(tài),做出相應(yīng)的判斷并進行處理,這里的主觀下線狀態(tài)和客觀下線狀態(tài)是比較重要的狀態(tài),它們決定了是否進行故障轉(zhuǎn)移。 

      

      可以 通過訂閱指定的頻道信息,當服務(wù)器出現(xiàn)故障得時候通知管理員 客戶端可以將 Sentinel 看作是一個只提供了訂閱功能的 Redis 服務(wù)

      器,你不可以使用 PUBLISH 命令向這個服務(wù)器發(fā)送信息,但你可以用 SUBSCRIBE 命令或者 PSUBSCRIBE 命令, 通過訂閱給定的頻道來獲取

      相應(yīng)的事件提醒。        

     6、配置哨兵

       

       1)、配置一個哨兵Sentinel的模式:

       把源碼中的sentinel.conf配置文件拷貝到指定目錄

       [root@web1 ~]# mkdir /usr/local/sentinel

       [root@web1 ~]# cd /usr/local/src/redis-3.2.6/

       [root@web1 redis-3.2.6]# cp sentinel.conf /usr/local/sentinel/

       [root@web1 redis-3.2.6]# cd /usr/local/sentinel/

       [root@web1 sentinel]# vim sentinel.conf 

[root@web1 sentinel]# grep -v "^#" sentinel.conf |grep -v "^$"

port 26379

dir /tmp

sentinel monitor mymaster 192.168.110.134 6379 1

sentinel down-after-milliseconds mymaster 30000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

        

        例子:

        #sentinel端口

port 26380

#工作路徑

dir "/usr/local/redis-6380"

# 守護進程模式

daemonize yes

# 指明日志文件名

logfile "./sentinel.log"

#哨兵監(jiān)控的master,主從配置一樣,在進行主從切換時6379會變成當前的master端口,

sentinel monitor mymaster 192.168.137.40 6379 1

# master或slave多長時間(默認30秒)不能使用后標記為s_down狀態(tài)。

sentinel down-after-milliseconds mymaster 5000

#若sentinel在該配置值內(nèi)未能完成failover操作(即故障時master/slave自動切換),則認為本次failover失敗。

sentinel failover-timeout mymaster 18000

#設(shè)置master和slaves驗證密碼

sentinel auth-pass mymaster 123456

#哨兵程序自動添加的部分

# Generated by CONFIG REWRITE

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 1

###指明了當前群集的從庫的ip和端口,在主從切換時該值會改變

sentinel known-slave mymaster 192.168.137.40 6380

###除了當前的哨兵還有哪些監(jiān)控的哨兵

sentinel known-sentinel mymaster 192.168.137.40 26379 7a88891a6147e202a53601ca16a3d438e9d55c9d

sentinel current-epoch 1

主要是這一項:

sentinel monitor mymaster 192.168.110.134 6379 2

my mymaster 后跟的是master的ip和端口

最后一個’2’代表我要啟動只要有2個sentinel認為master下線,就認為該master客觀下線,啟動failover并選舉產(chǎn)生新的master。

通常最后一個參數(shù)不能多于啟動的sentinel實例數(shù)。

         

        [root@web2 ~]# mkdir /usr/local/sentinel

[root@web2 ~]# cp /usr/local/src/redis-3.2.6/sentinel.conf /usr/local/sentinel/

[root@web2 ~]# cd /usr/local/sentinel/

[root@web2 sentinel]# vim sentinel.conf 

[root@web2 sentinel]# grep -v "^#" sentinel.conf |grep -v "^$"

port 26379

dir /tmp

sentinel monitor mymaster 192.168.110.134 6379 2

sentinel down-after-milliseconds mymaster 30000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

以后臺啟動模式啟動兩個sentinel(哨兵):

[root@web1 sentinel]# redis-sentinel /usr/local/sentinel/sentinel.conf &

[1] 18378

[root@web1 sentinel]# 18378:X 16 Dec 15:46:16.732 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 18378

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               

18378:X 16 Dec 15:46:16.734 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

18378:X 16 Dec 15:46:16.734 # Sentinel ID is 28c5f96738e4fe3335088d037a85ef47dfe9ab8f

18378:X 16 Dec 15:46:16.734 # +monitor master mymaster 192.168.110.134 6379 quorum 1

               

由上圖可以看到:

1、 哨兵已經(jīng)啟動,它的id為236f14b361fc5a0dc0621cf88823ed6e6252b2f3

2、 為master數(shù)據(jù)庫添加了一個監(jiān)控

3、 發(fā)現(xiàn)了2個slave(由此可以看出,哨兵無需配置slave,只需要指定master,哨兵會自動發(fā)現(xiàn)slave)

    

   [root@web1 sentinel]# ps aux|grep redis

root     18357  0.1  1.0 135572  2440 ?        Ssl  15:40   0:00 redis-server *:6379                         

root     18378  0.1  1.0 133528  2572 pts/1    Sl   15:46   0:00 redis-sentinel *:26379 [sentinel]               

root     18400  0.0  0.3 103244   864 pts/1    S+   15:51   0:00 grep redis

    6、從Redis宕機測試

        

        把其中一個從redis 的進程 kill掉:

        [[root@web1 sentinel]# kill 18357

[root@web1 sentinel]# ps aux|grep redis

root     18378  0.1  1.0 133528  2572 pts/1    Sl   15:46   0:01 redis-sentinel *:26379 [sentinel]               

root     18444  0.0  0.3 103244   864 pts/1    S+   16:00   0:00 grep redis

        

        30秒后哨兵的控制臺輸出:

[root@web1 sentinel]# 18378:X 16 Dec 16:01:16.452 # +sdown master mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.452 # +odown master mymaster 192.168.110.134 6379 #quorum 1/1

18378:X 16 Dec 16:01:16.452 # +new-epoch 9

18378:X 16 Dec 16:01:16.452 # +try-failover master mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.470 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 9

18378:X 16 Dec 16:01:16.470 # +elected-leader master mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.470 # +failover-state-select-slave master mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.546 # +selected-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.546 * +failover-state-send-slaveof-noone slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.605 * +failover-state-wait-promotion slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.699 # +promoted-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.699 # +failover-state-reconf-slaves master mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.759 # +failover-end master mymaster 192.168.110.134 6379

18378:X 16 Dec 16:01:16.759 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379

18378:X 16 Dec 16:01:16.760 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

18378:X 16 Dec 16:01:46.858 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

30秒后哨兵的控制臺輸出:

18378:X 16 Dec 16:01:16.759 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379

   18378:X 16 Dec 16:01:16.760 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

   18378:X 16 Dec 16:01:46.858 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

說明已經(jīng)監(jiān)控到剛才我人kill掉的master宕機了

        可以看出,slave重新已經(jīng)轉(zhuǎn)換成立主庫 -sdown:說明是恢復服務(wù)。

        

        [root@web2 sentinel]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

127.0.0.1:6379> 

         

         把192.168.110.134恢復:

         [root@web1 sentinel]# redis-server /usr/local/redis/etc/redis.conf 

[root@web1 sentinel]# 18378:X 16 Dec 16:06:59.825 # -sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

18378:X 16 Dec 16:07:09.806 * +convert-to-slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

[root@web1 sentinel]# 

[root@web1 sentinel]# 

[root@web1 sentinel]# ps aux|grep redis

root     18378  0.2  1.0 133528  2636 pts/1    Sl   15:46   0:02 redis-sentinel *:26379 [sentinel]               

root     18487  0.1  1.0 135572  2564 ?        Ssl  16:06   0:00 redis-server *:6379                         

root     18491  0.0  0.3 103244   864 pts/1    S+   16:08   0:00 grep redis

         

         192.168.110.134是作為從庫加入到復制中的(主庫不會切換過來):

 

測試下:

[root@web2 sentinel]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.110.134,port=6379,state=online,offset=2835,lag=1

master_repl_offset:2835

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:2834

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.110.134,port=6379,state=online,offset=9687,lag=0

master_repl_offset:9832

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:9831

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.110.134,port=6379,state=online,offset=36450,lag=0

master_repl_offset:36595

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:36594

127.0.0.1:6379> set name2 lisi

OK

127.0.0.1:6379> set age2 22

OK

127.0.0.1:6379> get name2

"lisi"

127.0.0.1:6379> get age2

"22"

127.0.0.1:6379> 

[root@web1 sentinel]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:slave

master_host:192.168.110.135

master_port:6379

master_link_status:up

master_last_io_seconds_ago:0

master_sync_in_progress:0

slave_repl_offset:73863

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

127.0.0.1:6379> get name2

"lisi"

127.0.0.1:6379> get set2

(nil)

127.0.0.1:6379> get age2

"22"

127.0.0.1:6379> 

          效果不錯:

          把主庫切換回192.168.110.134

          [root@web2 sentinel]# ps aux|grep redis

root      7063  0.1  1.2 135572  3116 ?        Ssl  14:35   0:13 redis-server *:6379         

root      7649  0.0  0.3 103244   864 pts/1    S+   16:48   0:00 grep redis

 [root@web2 sentinel]# kill 7063

 [root@web1 sentinel]# 18522:X 16 Dec 16:49:05.725 # +sdown master mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:05.725 # +odown master mymaster 192.168.110.135 6379 #quorum 1/1

18522:X 16 Dec 16:49:05.725 # +new-epoch 10

18522:X 16 Dec 16:49:05.725 # +try-failover master mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:05.736 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 10

18522:X 16 Dec 16:49:05.736 # +elected-leader master mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:05.736 # +failover-state-select-slave master mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:05.791 # +selected-slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:05.791 * +failover-state-send-slaveof-noone slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:05.875 * +failover-state-wait-promotion slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:06.233 # +promoted-slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:06.233 # +failover-state-reconf-slaves master mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:06.331 # +failover-end master mymaster 192.168.110.135 6379

18522:X 16 Dec 16:49:06.331 # +switch-master mymaster 192.168.110.135 6379 192.168.110.134 6379

18522:X 16 Dec 16:49:06.331 * +slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

18522:X 16 Dec 16:49:36.394 # +sdown slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

切換成功

[root@web1 sentinel]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

127.0.0.1:6379> 

恢復192.168.110.135的redis:

[root@web2 sentinel]# redis-server /etc/redis.conf

127.0.0.1:6379> 18522:X 16 Dec 16:52:32.351 # -sdown slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

18522:X 16 Dec 16:52:42.305 * +convert-to-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

        

        127.0.0.1:6379> 

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.110.135,port=6379,state=online,offset=13653,lag=1

master_repl_offset:13667

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:13666

127.0.0.1:6379> 

                                                                                                                                    1

        [root@web2 sentinel]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:slave

master_host:192.168.110.134

master_port:6379

master_link_status:up

master_last_io_seconds_ago:2

master_sync_in_progress:0

slave_repl_offset:16502

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

127.0.0.1:6379> 

     

     2)、配置兩個哨兵Sentinel的模式: 

         [root@web1 sentinel]# grep -v "^$"  /usr/local/sentinel/sentinel.conf |grep -v "^#"

port 26379

dir "/tmp"

sentinel myid 28c5f96738e4fe3335088d037a85ef47dfe9ab8f

sentinel monitor mymaster 192.168.110.134 6379 2

sentinel config-epoch mymaster 10

sentinel leader-epoch mymaster 10

sentinel known-slave mymaster 192.168.110.135 6379

sentinel known-sentinel mymaster 192.168.110.135 26379 16286f52de2675345d1b11bc8946adc5d3f0c360

sentinel current-epoch 10

         [root@web2 sentinel]# grep -v "^$"  /usr/local/sentinel/sentinel.conf |grep -v "^#"

port 26379

dir "/tmp"

sentinel myid 16286f52de2675345d1b11bc8946adc5d3f0c360

sentinel monitor mymaster 192.168.110.134 6379 2

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 6

sentinel known-slave mymaster 192.168.110.135 6379

sentinel known-sentinel mymaster 192.168.110.134 26379 28c5f96738e4fe3335088d037a85ef47dfe9ab8f

sentinel current-epoch 10

        

         主要是這一項:

         sentinel monitor mymaster 192.168.110.134 6379 2

         最后一個’2’代表我要啟動只要有2個sentinel認為master下線,就認為該master客觀下線,啟動failover并選舉產(chǎn)生新的master。

通常最后一個參數(shù)不能多于啟動的sentinel實例數(shù)。

         

         [root@web1 sentinel]# redis-sentinel /usr/local/sentinel/sentinel.conf &

[1] 18747

[root@web1 sentinel]# 18747:X 16 Dec 17:04:30.597 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 18747

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               

18747:X 16 Dec 17:04:30.599 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

18747:X 16 Dec 17:04:30.599 # Sentinel ID is 28c5f96738e4fe3335088d037a85ef47dfe9ab8f

18747:X 16 Dec 17:04:30.599 # +monitor master mymaster 192.168.110.134 6379 quorum 2

18747:X 16 Dec 17:05:00.659 # +sdown sentinel 16286f52de2675345d1b11bc8946adc5d3f0c360 192.168.110.135 26379 @ mymaster 192.168.110.134 6379

[root@web2 sentinel]# redis-sentinel /usr/local/sentinel/sentinel.conf &

[1] 7726

You have new mail in /var/spool/mail/root

[root@web2 sentinel]# 7726:X 16 Dec 17:04:33.835 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 7726

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               

7726:X 16 Dec 17:04:33.836 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

7726:X 16 Dec 17:04:33.836 # Sentinel ID is 16286f52de2675345d1b11bc8946adc5d3f0c360

7726:X 16 Dec 17:04:33.836 # +monitor master mymaster 192.168.110.134 6379 quorum 2

7726:X 16 Dec 17:04:34.668 # +new-epoch 10

7726:X 16 Dec 17:05:03.836 # +sdown sentinel 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 192.168.110.134 26379 @ mymaster 192.168.110.134 6379

   

   上面是有問題的:原因是bind 默認是僅僅監(jiān)控本機和 192.168.1.1這兩個IP地址

   # bind 127.0.0.1 192.168.1.1

   

   添加: bind 0.0.0.0

   就沒問題了

   配置文件:

   [root@web1 ~]# grep -v "^$" /usr/local/sentinel/sentinel.conf |grep -v "^#"

bind 0.0.0.0

port 26379

dir "/tmp"

sentinel myid 28c5f96738e4fe3335088d037a85ef47dfe9ab8f

sentinel monitor mymaster 192.168.110.135 6379 2

sentinel config-epoch mymaster 14

sentinel leader-epoch mymaster 14

sentinel known-slave mymaster 192.168.110.134 6379

sentinel known-sentinel mymaster 192.168.110.135 26379 16286f52de2675345d1b11bc8946adc5d3f0c360

sentinel current-epoch 14

  

   主要是下面兩項:

   bind 0.0.0.0

   sentinel monitor mymaster 192.168.110.135 6379 2

   

   如果設(shè)置了密碼則添加下面一項:

   # sentinel auth-pass

   sentinel auth-pass def_master 012_345^678-90

   [root@web1 ~]# redis-sentinel /usr/local/sentinel/sentinel.conf &

[1] 1963

[root@web1 ~]# 1963:X 19 Dec 11:55:29.670 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 1963

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               

1963:X 19 Dec 11:55:29.671 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

1963:X 19 Dec 11:55:29.671 # Sentinel ID is 28c5f96738e4fe3335088d037a85ef47dfe9ab8f

1963:X 19 Dec 11:55:29.672 # +monitor master mymaster 192.168.110.134 6379 quorum 2

   上面表示啟動沒問題的:

   kill掉一個從redis,觀察sentinel控制臺輸出信息:

   

[root@web1 ~]# kill 1912

[root@web1 ~]# ps aux|grep redis

root      1963  0.2  1.0 133528  2588 pts/0    Sl   11:55   0:00 redis-sentinel 0.0.0.0:26379 [sentinel]         

root      1992  0.0  0.3 103244   864 pts/0    S+   11:58   0:00 grep redis

[root@web1 ~]# 1963:X 19 Dec 11:59:06.906 # +sdown master mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:06.963 # +odown master mymaster 192.168.110.134 6379 #quorum 2/2

1963:X 19 Dec 11:59:06.963 # +new-epoch 14

1963:X 19 Dec 11:59:06.963 # +try-failover master mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:06.972 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 14

1963:X 19 Dec 11:59:06.992 # 16286f52de2675345d1b11bc8946adc5d3f0c360 voted for 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 14

1963:X 19 Dec 11:59:07.035 # +elected-leader master mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:07.035 # +failover-state-select-slave master mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:07.136 # +selected-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:07.136 * +failover-state-send-slaveof-noone slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:07.199 * +failover-state-wait-promotion slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:08.171 # +promoted-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:08.171 # +failover-state-reconf-slaves master mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:08.260 # +failover-end master mymaster 192.168.110.134 6379

1963:X 19 Dec 11:59:08.260 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379

1963:X 19 Dec 11:59:08.260 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

1963:X 19 Dec 11:59:38.290 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

另一臺:

[root@web2 ~]# redis-sentinel /usr/local/sentinel/sentinel.conf &

[1] 1809

[root@web2 ~]# 1809:X 19 Dec 11:55:35.698 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 1809

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               

1809:X 19 Dec 11:55:35.700 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

1809:X 19 Dec 11:55:35.700 # Sentinel ID is 16286f52de2675345d1b11bc8946adc5d3f0c360

1809:X 19 Dec 11:55:35.700 # +monitor master mymaster 192.168.110.134 6379 quorum 2

1809:X 19 Dec 11:59:06.895 # +sdown master mymaster 192.168.110.134 6379

1809:X 19 Dec 11:59:06.999 # +new-epoch 14

1809:X 19 Dec 11:59:07.001 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 14

1809:X 19 Dec 11:59:08.020 # +odown master mymaster 192.168.110.134 6379 #quorum 2/2

1809:X 19 Dec 11:59:08.020 # Next failover delay: I will not start a failover before Mon Dec 19 12:05:07 2016

1809:X 19 Dec 11:59:08.284 # +config-update-from sentinel 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 192.168.110.134 26379 @ mymaster 192.168.110.134 6379

1809:X 19 Dec 11:59:08.284 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379

1809:X 19 Dec 11:59:08.284 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

1809:X 19 Dec 11:59:38.344 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

[root@web2 ~]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

127.0.0.1:6379> 

恢復192.168.110.134:

[root@web1 ~]# redis-server /usr/local/redis/etc/redis.conf 

[root@web1 ~]# 1963:X 19 Dec 12:14:24.393 # -sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

[root@web1 ~]# redis-cli 

127.0.0.1:6379> info replication

# Replication

role:slave

master_host:192.168.110.135

master_port:6379

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:38769

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

127.0.0.1:6379> 

135上的顯示:

repl_backlog_histlen:0

127.0.0.1:6379> 1809:X 19 Dec 12:14:24.105 # -sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379

127.0.0.1:6379> 

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.110.134,port=6379,state=online,offset=2404,lag=1

master_repl_offset:2404

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:2403

也是僅僅作為從。

         2)、配置三個哨兵Sentinel的模式:

            

            [root@web2 ~]# scp redis-3.2.6.tar.gz 192.168.110.133:/root

The authenticity of host '192.168.110.133 (192.168.110.133)' can't be established.

RSA key fingerprint is 60:6c:0d:f4:3b:f8:42:b1:c9:7e:ab:c4:bc:83:d1:09.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.110.133' (RSA) to the list of known hosts.

root@192.168.110.133's password: 

redis-3.2.6.tar.gz                                                                                 100% 1509KB   1.5MB/s   00:00

            [root@node2 ~]# iptables -I INPUT 6 -m state --state NEW -p tcp --dport 6379 -j ACCEPT

            [root@node2 ~]# iptables -I INPUT 7 -m state --state NEW -p tcp --dport 26379 -j ACCEPT

            [root@node2 ~]# tar -zxvf redis-3.2.6.tar.gz -C /usr/local/src/

            [root@node2 ~]# cd /usr/local/src/redis-3.2.6/

            [root@node2 redis-3.2.6]# make

            [root@node2 redis-3.2.6]# make install


當前名稱:Redis主從集群搭建及容災(zāi)部署(哨兵sentinel)
鏈接分享:http://weahome.cn/article/jsoigo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部