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)檢測(cè) 

2):如果Master異常,則會(huì)進(jìn)行Master-Slave切換,將其中一個(gè)Slave作為Master,將之前的Master作為Slave 

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

3、Sentinel工作方式: 

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

2):如果一個(gè)實(shí)例(instance)距離最后一次有效回復(fù) PING 命令的時(shí)間超過(guò) down-after-milliseconds 選項(xiàng)所指定的值, 則這個(gè)實(shí)例會(huì)被 Sentinel 標(biāo)記為主觀下線。 

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

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

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

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

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

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

4、主觀下線和客觀下線 

主觀下線:Subjectively Down,簡(jiǎn)稱 SDOWN,指的是當(dāng)前 Sentinel 實(shí)例對(duì)某個(gè)redis服務(wù)器做出的下線判斷。 

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

通俗來(lái)講就是: 

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

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

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

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

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

   

    5、Raft分布式算法

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

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

3. 目前用到這個(gè)算法的項(xiàng)目有:

a. CoreOS : 見下面

b. ectd : a distributed, consistent shared configuration 

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

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

    

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

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

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

3. 每個(gè)sentinel只有一次選舉的機(jī)會(huì)

4. 采用先到先得的原則

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

6. 每個(gè)sentinel都有唯一的uid,不會(huì)因?yàn)橹貑⒍兏?/p>

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

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

Raft算法核心: 可視圖

   Raft Visualization (算法演示)

   

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

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

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

      

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

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

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

     6、配置哨兵

       

       1)、配置一個(gè)哨兵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"

# 守護(hù)進(jìn)程模式

daemonize yes

# 指明日志文件名

logfile "./sentinel.log"

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

sentinel monitor mymaster 192.168.137.40 6379 1

# master或slave多長(zhǎng)時(shí)間(默認(rèn)30秒)不能使用后標(biāo)記為s_down狀態(tài)。

sentinel down-after-milliseconds mymaster 5000

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

sentinel failover-timeout mymaster 18000

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

sentinel auth-pass mymaster 123456

#哨兵程序自動(dòng)添加的部分

# Generated by CONFIG REWRITE

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 1

###指明了當(dāng)前群集的從庫(kù)的ip和端口,在主從切換時(shí)該值會(huì)改變

sentinel known-slave mymaster 192.168.137.40 6380

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

sentinel known-sentinel mymaster 192.168.137.40 26379 7a88891a6147e202a53601ca16a3d438e9d55c9d

sentinel current-epoch 1

主要是這一項(xiàng):

sentinel monitor mymaster 192.168.110.134 6379 2

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

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

通常最后一個(gè)參數(shù)不能多于啟動(dòng)的sentinel實(shí)例數(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

以后臺(tái)啟動(dòng)模式啟動(dòng)兩個(gè)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)啟動(dòng),它的id為236f14b361fc5a0dc0621cf88823ed6e6252b2f3

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

3、 發(fā)現(xiàn)了2個(gè)slave(由此可以看出,哨兵無(wú)需配置slave,只需要指定master,哨兵會(huì)自動(dòng)發(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宕機(jī)測(cè)試

        

        把其中一個(gè)從redis 的進(jìn)程 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秒后哨兵的控制臺(tái)輸出:

[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秒后哨兵的控制臺(tái)輸出:

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

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

        可以看出,slave重新已經(jīng)轉(zhuǎn)換成立主庫(kù) -sdown:說(shuō)明是恢復(fù)服務(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恢復(fù):

         [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是作為從庫(kù)加入到復(fù)制中的(主庫(kù)不會(huì)切換過(guò)來(lái)):

 

測(cè)試下:

[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> 

          效果不錯(cuò):

          把主庫(kù)切換回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> 

恢復(fù)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)、配置兩個(gè)哨兵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

        

         主要是這一項(xiàng):

         sentinel monitor mymaster 192.168.110.134 6379 2

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

通常最后一個(gè)參數(shù)不能多于啟動(dòng)的sentinel實(shí)例數(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

   

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

   # bind 127.0.0.1 192.168.1.1

   

   添加: bind 0.0.0.0

   就沒(méi)問(wèn)題了

   配置文件:

   [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

  

   主要是下面兩項(xiàng):

   bind 0.0.0.0

   sentinel monitor mymaster 192.168.110.135 6379 2

   

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

   # sentinel auth-pass <master-name> <password>

   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

   上面表示啟動(dòng)沒(méi)問(wèn)題的:

   kill掉一個(gè)從redis,觀察sentinel控制臺(tái)輸出信息:

   

[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

另一臺(tái):

[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> 

恢復(fù)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)、配置三個(gè)哨兵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

            root@node2 redis-3.2.6]# mkdi

網(wǎng)頁(yè)名稱:Redis主從集群搭建及容災(zāi)部署(哨兵sentinel)
網(wǎng)站路徑:http://muchs.cn/article10/jsoigo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷、響應(yīng)式網(wǎng)站微信小程序、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站