国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Linux服務器 > 正文

Linux學習教程之redis哨兵集群詳解

2024-09-05 23:04:48
字體:
來源:轉載
供稿:網友

前言

Sentinel(哨兵)是用于監控redis集群中Master狀態的工具,其已經被集成在redis2.4+的版本中,下面話不多說了,來一起看看詳細的介紹吧

1、Sentinel 哨兵

Sentinel(哨兵)是Redis 的高可用性解決方案:由一個或多個Sentinel 實例 組成的Sentinel 系統可以監視任意多個主服務器,以及這些主服務器屬下的所有從服務器,并在被監視的主服務器進入下線狀態時,自動將下線主服務器屬下的某個從服務器升級為新的主服務器。

例如:

Linux,redis,哨兵集群

在Server1 掉線后:

Linux,redis,哨兵集群

升級Server2 為新的主服務器:

  Linux,redis,哨兵集群

2、Redis 主從分離

在講解Sentinel 哨兵集群之前,我們先來搭建一個簡單的主從分離(讀寫分離)。

首先,我們默認大家都已經安裝了redis,然后我們將redis.conf 拷貝多份,并且創建多個目錄,用于區分多個redis 服務:

   Linux,redis,哨兵集群

這里面,每個目錄中都有自己的redis.conf 配置文件,接下來,我們先設置主服務器的配置文件。

一、配置Master

1、修改端口

# 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 6380

redis 的默認端口是6379,這里我們把主服務器的端口設置為6380

2、修改pidfile

# If a pid file is specified, Redis writes it where specified at startup# and removes it at exit.## When the server runs non daemonized, no pid file is created if none is# specified in the configuration. When the server is daemonized, the pid file# is used even if not specified, defaulting to "/var/run/redis.pid".## Creating a pid file is best effort: if Redis is not able to create it# nothing bad happens, the server will start and run normally.pidfile /var/run/redis_6380.pid

pidfile 是我們啟動redis 的時候,linux 為我們分配的一個pid 進程號,如果這里不作修改,會影響后面redis服務的啟動

3、啟動 redis

Linux,redis,哨兵集群

啟動redis,我們可以看到,redis已經占領了6380 端口

進入客戶端

redis-cli -p 6380127.0.0.1:6380> info...# Replicationrole:masterconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0...

我們可以看到,redis 現在的角色是一個master 啟動的服務。

二、配置Slave

和上面配置 master一樣,我們需要修改端口號和pid 文件,在修改完之后,我們有兩種方法配置從服務

1、在配置文件中配置從服務

################################# REPLICATION ################################## Master-Slave replication. Use slaveof to make a Redis instance a copy of# another Redis server. A few things to understand ASAP about Redis replication.## 1) Redis replication is asynchronous, but you can configure a master to# stop accepting writes if it appears to be not connected with at least# a given number of slaves.# 2) Redis slaves are able to perform a partial resynchronization with the# master if the replication link is lost for a relatively small amount of# time. You may want to configure the replication backlog size (see the next# sections of this file) with a sensible value depending on your needs.# 3) Replication is automatic and does not need user intervention. After a# network partition slaves automatically try to reconnect to masters# and resynchronize with them.## slaveof <masterip> <masterport>slaveof 127.0.0.1 6380

我們可以在配置文件中直接修改 slaveof 屬性,我們直接配置主服務器的ip 地址,和端口號,如果這里主服務器有配置密碼

可以通過配置masterauth 來設置鏈接密碼

# If the master is password protected (using the "requirepass" configuration# directive below) it is possible to tell the slave to authenticate before# starting the replication synchronization process, otherwise the master will# refuse the slave request.## masterauth <master-password>

啟動redis 服務:

Linux,redis,哨兵集群

我們可以看到,現在有兩個現在在運行,我們進入6381的客戶端,看一下他的狀態,

# Replicationrole:slavemaster_host:127.0.0.1master_port:6380master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:71slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

我們可以看到,現在的redis 是一個從服務的角色,連接著6380的服務。

2、在服務啟動后設置

我們修改6382端口的服務器配置文件之后,啟動服務

Linux,redis,哨兵集群

進入客戶端,查看當前服務器的狀態:

# Replicationrole:masterconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

我們可以看到,當前服務器的狀態時作為一個主服務的角色在運行,我們接下來修改他的狀態:

127.0.0.1:6382> slaveof 127.0.0.1 6380//修改后狀態# Replicationrole:slavemaster_host:127.0.0.1master_port:6380master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:617slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

3、總結

我們先看一下目前master 的狀態:

# Replicationrole:masterconnected_slaves:2slave0:ip=127.0.0.1,port=6381,state=online,offset=785,lag=0slave1:ip=127.0.0.1,port=6382,state=online,offset=785,lag=0master_repl_offset:785repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:784

我們可以可以看到,兩個從服務已經在連著主服務器,上面兩種配置的區別在于,當salve 斷線重連之后,

如果我們是修改類配置文件,重連之后會自己鏈接上去master,并且同步master 上面的數據,

如果我們是手動連接上去的主服務器,重連之后,從服務器會讀取自己本地的 rdb 回復數據,而不會去自動鏈接主服務

我們如果需要設置讀寫分離,只需要在主服務器中設置:

# Note: read only slaves are not designed to be exposed to untrusted clients# on the internet. It's just a protection layer against misuse of the instance.# Still a read only slave exports by default all the administrative commands# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve# security of read only slaves using 'rename-command' to shadow all the# administrative / dangerous commands.slave-read-only yes

3、Sentinel 哨兵

1、配置端口

在sentinel.conf 配置文件中, 我們可以找到port 屬性,這里是用來設置sentinel 的端口,一般情況下,至少會需要三個哨兵對redis 進行監控,我們可以通過修改端口啟動多個sentinel 服務。

# Note: read only slaves are not designed to be exposed to untrusted clients# on the internet. It's just a protection layer against misuse of the instance.# Still a read only slave exports by default all the administrative commands# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve# security of read only slaves using 'rename-command' to shadow all the# administrative / dangerous commands.slave-read-only yes

2、配置主服務器的ip 和端口

我們把監聽的端口修改成6380,并且加上權值為2,這里的權值,是用來計算我們需要將哪一臺服務器升級升主服務器

# sentinel monitor <master-name> <ip> <redis-port> <quorum>## Tells Sentinel to monitor this master, and to consider it in O_DOWN# (Objectively Down) state only if at least <quorum> sentinels agree.## Note that whatever is the ODOWN quorum, a Sentinel will require to# be elected by the majority of the known Sentinels in order to# start a failover, so no failover can be performed in minority.## Slaves are auto-discovered, so you don't need to specify slaves in# any way. Sentinel itself will rewrite this configuration file adding# the slaves using additional configuration options.# Also note that the configuration file is rewritten when a# slave is promoted to master.## Note: master name should not include special characters or spaces.# The valid charset is A-z 0-9 and the three characters ".-_".sentinel monitor mymaster 127.0.0.1 6380 2

3、啟動Sentinel

/sentinel$ redis-sentinel sentinel.conf

Linux,redis,哨兵集群

sentinel 啟動之后,就會監視到現在有一個主服務器,兩個從服務器

當我們把其中一個從服務器器關閉之后,我們可以看到日志:

10894:X 30 Dec 16:27:03.670 # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380

日志表示,6381這個從服務器已經從主服務器中脫離了出來,我們重新把6381 接回去。

10894:X 30 Dec 16:28:43.288 * +reboot slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 638010894:X 30 Dec 16:28:43.365 # -sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380

4、關閉Master

我們手動關閉Master 之后,sentinel 在監聽master 確實是斷線了之后,將會開始計算權值,然后重新分配主服務器

Linux,redis,哨兵集群

我們可以看到,6380主服務器斷了之后,sentinel 幫我們選了6382作為新的主服務器

我們進到6382的客戶端,查看他的狀態:

# Replicationrole:masterconnected_slaves:1slave0:ip=127.0.0.1,port=6381,state=online,offset=13751,lag=0master_repl_offset:13751repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:13750

我們可以看到 6382,重slave 榮升為master

127.0.0.1:6382> set name jaycekonOK

原本的沒有權限寫,也得到了相應的權限

5、重連Master

大家可能會好奇,如果master 重連之后,會不會搶回屬于他的位置,答案是否定的,就比如你被一個小弟搶了你老大的位置,他肯給回你這個位置嗎。因此當master 回來之后,他也只能當個小弟  

Linux,redis,哨兵集群

4、Sentinel 總結

一、Sentinel的作用:

A、Master 狀態監測

B、如果Master 異常,則會進行Master-slave 轉換,將其中一個Slave作為Master,將之前的Master作為Slave

C、Master-Slave切換后,master_redis.conf、slave_redis.conf和sentinel.conf的內容都會發生改變,即master_redis.conf中會多一行slaveof的配置,sentinel.conf的監控目標會隨之調換

二、Sentinel的工作方式:

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

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

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

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

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

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

7):若沒有足夠數量的 Sentinel 同意 Master 已經下線, Master 的客觀下線狀態就會被移除。

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新和县| 武平县| 朝阳市| 安化县| 永善县| 定兴县| 山西省| 东海县| 喀什市| 丰顺县| 铁岭市| 台江县| 凌源市| 通榆县| 安乡县| 利辛县| 仁化县| 子长县| 乌兰察布市| 偏关县| 扎赉特旗| 上林县| 南木林县| 广元市| 沂源县| 远安县| 高邮市| 隆尧县| 华池县| 宣恩县| 汾西县| 上高县| 白银市| 抚州市| 桐柏县| 贡嘎县| 哈巴河县| 革吉县| 屏南县| 通化县| 拉萨市|