redis哨兵集群搭載操作手冊(cè)

1.Redis 主從分離

首先,我們默認(rèn)大家都已經(jīng)安裝了redis,然后我們將redis.conf 拷貝多份,并且創(chuàng)建多個(gè)目錄,用于區(qū)分多個(gè)redis 服務(wù):

這里面,每個(gè)目錄中都有自己的redis.conf 配置文件,接下來(lái),我們先設(shè)置主服務(wù)器的配置文件。

一、配置Master

1、修改端口

# Accept connections on the specified port,defaultis6379(IANA #815344).

# If port 0is specified Redis will not listen on a TCP socket.

port 6380

redis 的默認(rèn)端口是6379,這里我們把主服務(wù)器的端口設(shè)置為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 是我們啟動(dòng)redis 的時(shí)候,linux 為我們分配的一個(gè)pid 進(jìn)程號(hào),如果這里不作修改,會(huì)影響后面redis服務(wù)的啟動(dòng)

?3、啟動(dòng) redis

啟動(dòng)redis,我們可以看到,redis已經(jīng)占領(lǐng)了6380 端口

進(jìn)入客戶端

redis-cli -p 6380

127.0.0.1:6380> info

...

# 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

我們可以看到,redis 現(xiàn)在的角色是一個(gè)master 啟動(dòng)的服務(wù)。

二、配置Slave

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

1、在配置文件中配置從服務(wù)

################################# 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屬性,我們直接配置主服務(wù)器的ip 地址,和端口號(hào),如果這里主服務(wù)器有配置密碼

可以通過(guò)配置masterauth來(lái)設(shè)置鏈接密碼

# If the masterispasswordprotected(usingthe"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

啟動(dòng)redis 服務(wù):

我們可以看到,現(xiàn)在有兩個(gè)現(xiàn)在在運(yùn)行,我們進(jìn)入6381的客戶端,看一下他的狀態(tài),

# Replication

role:slave

master_host:127.0.0.1

master_port:6380

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:71

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

我們可以看到,現(xiàn)在的redis 是一個(gè)從服務(wù)的角色,連接著6380的服務(wù)。

2、在服務(wù)啟動(dòng)后設(shè)置

我們修改6382端口的服務(wù)器配置文件之后,啟動(dòng)服務(wù)

127.0.0.1:6382> slaveof 127.0.0.1 6380

//修改后狀態(tài)

# Replication

role:slave

master_host:127.0.0.1

master_port:6380

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:617

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

3、總結(jié)

   我們先看一下目前master 的狀態(tài):

# Replication

role:master

connected_slaves:2

slave0:ip=127.0.0.1,port=6381,state=online,offset=785,lag=0

slave1:ip=127.0.0.1,port=6382,state=online,offset=785,lag=0

master_repl_offset:785

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:784

我們可以可以看到,兩個(gè)從服務(wù)已經(jīng)在連著主服務(wù)器,上面兩種配置的區(qū)別在于,當(dāng)salve 斷線重連之后,

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

如果我們是手動(dòng)連接上去的主服務(wù)器,重連之后,從服務(wù)器會(huì)讀取自己本地的 rdb 回復(fù)數(shù)據(jù),而不會(huì)去自動(dòng)鏈接主服務(wù)

我們?nèi)绻枰O(shè)置讀寫(xiě)分離,只需要在主服務(wù)器中設(shè)置:

# 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、Sentinel 哨兵


1、配置端口

在sentinel.conf 配置文件中, 我們可以找到port 屬性,這里是用來(lái)設(shè)置sentinel 的端口,一般情況下,至少會(huì)需要三個(gè)哨兵對(duì)redis 進(jìn)行監(jiān)控,我們可以通過(guò)修改端口啟動(dòng)多個(gè)sentinel 服務(wù)。

# port # The port that this sentinel instance will run on

port 26379

2、配置主服務(wù)器的ip 和端口

我們把監(jiān)聽(tīng)的端口修改成6380,并且加上權(quán)值為2,這里的權(quán)值,是用來(lái)計(jì)算我們需要將哪一臺(tái)服務(wù)器升級(jí)升主服務(wù)器

# 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、啟動(dòng)Sentinel

./redis-sentinel sentinel.conf --sentinel &

sentinel 啟動(dòng)之后,就會(huì)監(jiān)視到現(xiàn)在有一個(gè)主服務(wù)器,兩個(gè)從服務(wù)器

?  當(dāng)我們把其中一個(gè)從服務(wù)器器關(guān)閉之后,我們可以看到日志:

10894:X30Dec16:27:03.670# +sdown slave127.0.0.1:6381127.0.0.16381@ mymaster127.0.0.16380

  日志表示,6381這個(gè)從服務(wù)器已經(jīng)從主服務(wù)器中脫離了出來(lái),我們重新把6381 接回去。

10894:X30Dec16:28:43.288* +reboot slave127.0.0.1:6381127.0.0.16381@ mymaster127.0.0.1638010894:X30Dec16:28:43.365# -sdown slave127.0.0.1:6381127.0.0.16381@ mymaster127.0.0.16380

4、關(guān)閉Master?

我們手動(dòng)關(guān)閉Master 之后,sentinel 在監(jiān)聽(tīng)master 確實(shí)是斷線了之后,將會(huì)開(kāi)始計(jì)算權(quán)值,然后重新分配主服務(wù)器

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

我們進(jìn)到6382的客戶端,查看他的狀態(tài):

# Replication

role:master

connected_slaves:1

slave0:ip=127.0.0.1,port=6381,state=online,offset=13751,lag=0

master_repl_offset:13751

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:13750

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

127.0.0.1:6382>set name jaycekon

OK

原本的沒(méi)有權(quán)限寫(xiě),也得到了相應(yīng)的權(quán)限

5、重連Master

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

3、Sentinel 總結(jié)

、Sentinel的作用:

A、Master 狀態(tài)監(jiān)測(cè)

B、如果Master 異常,則會(huì)進(jìn)行Master-slave 轉(zhuǎn)換,將其中一個(gè)Slave作為Master,將之前的Master作為Slave?

C、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)換?



、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.集群

集群至少需要3主3從,且每個(gè)實(shí)例使用不同的配置文件,主從不用配置,集群會(huì)自己選。

修改每個(gè)實(shí)例的配置文件:

??? cluster-enabled yes? --開(kāi)啟集群

??? cluster-config-file nodes-6382.conf --集群配置文件名,每個(gè)實(shí)例配置的要不同,redis會(huì)根據(jù)文件名自動(dòng)新建

用集群工具創(chuàng)建集群:

我們可以用集群工具進(jìn)行集群,該工具是redis源碼包中,用ruby編寫(xiě),所以需要先安裝ruby。

1、安裝rubygems

yum install ruby(這一步建議去官網(wǎng)下載ruby包2.2版本以上的,官網(wǎng)地址:http://www.ruby-lang.org/en/downloads/)

yum install rubygems

gem install redis(ruby版本必須大于等于2.2)

2、把6個(gè)redis實(shí)例都起來(lái),每個(gè)實(shí)例的集群都打開(kāi)。

3、redis安裝目錄的src執(zhí)行./redis-trib.rb create --replicas 1 127.0.0.1:6380

127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384

127.0.0.1:6385

提示信息如下:

Connecting to node 127.0.0.1:6380: OK

Connecting to node 127.0.0.1:6381: OK

Connecting to node 127.0.0.1:6382: OK

Connecting to node 127.0.0.1:6383: OK

Connecting to node 127.0.0.1:6384: OK

Connecting to node 127.0.0.1:6385: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:6380

127.0.0.1:6381

127.0.0.1:6382

Adding replica 127.0.0.1:6383 to 127.0.0.1:6380

Adding replica 127.0.0.1:6384 to 127.0.0.1:6381

Adding replica 127.0.0.1:6385 to 127.0.0.1:6382

M: d4f906940d68714db787a60837f57fa496de5d12 127.0.0.1:6380 slots:0-5460 (5461 slots) master

M: b547d05c9d0e188993befec4ae5ccb430343fb4b 127.0.0.1:6381 slots:5461-10922 (5462 slots) master

M: 887fe91bf218f203194403807e0aee941e985286 127.0.0.1:6382 slots:10923-16383 (5461 slots) master

S: e0f6559be7a121498fae80d44bf18027619d9995 127.0.0.1:6383 replicates d4f906940d68714db787a60837f57fa496de5d12

S: a61dbf654c9d9a4d45efd425350ebf720a6660fc 127.0.0.1:6384 replicates b547d05c9d0e188993befec4ae5ccb430343fb4b

S: 551e5094789035affc489db267c8519c3a29f35d 127.0.0.1:6385 replicates 887fe91bf218f203194403807e0aee941e985286

Can I set the above configuration? (type 'yes' to accept):

輸入yes,這樣集群就建立了。

登錄任一臺(tái)redis,執(zhí)行 info cluster,提示cluster_enabled:1

集群過(guò)程:

首先redis-trib.rb會(huì)以客戶端的形式嘗試連接所有的節(jié)點(diǎn),并發(fā)送PING命令以確定節(jié)點(diǎn)能夠正常服務(wù)。如果有任何節(jié)點(diǎn)無(wú)法連接,則創(chuàng)建失敗。同時(shí)發(fā)送 INFO 命令獲取每個(gè)節(jié)點(diǎn)的運(yùn)行ID以及是否開(kāi)啟了集群功能(即cluster_enabled為1)。 準(zhǔn)備就緒后集群會(huì)向每個(gè)節(jié)點(diǎn)發(fā)送 CLUSTER MEET命令,格式為 CLUSTER MEET ip port,這個(gè)命令用來(lái)告訴當(dāng)前節(jié)點(diǎn)指定ip和port上在運(yùn)行的節(jié)點(diǎn)也是集群的一部分,從而使得6個(gè)節(jié)點(diǎn)最終可以歸入一個(gè)集群。

然后redis-trib.rb會(huì)分配主從數(shù)據(jù)庫(kù)節(jié)點(diǎn),分配的原則是盡量保證每個(gè)主數(shù)據(jù)庫(kù)運(yùn)行在不同的IP地址上,同時(shí)每個(gè)從數(shù)據(jù)庫(kù)和主數(shù)據(jù)庫(kù)均不運(yùn)行在同一IP地址上,以保證系統(tǒng)的容災(zāi)能力

3主3從,當(dāng)1個(gè)主故障,大家會(huì)給對(duì)應(yīng)的從投票,把從立為主,若沒(méi)有從數(shù)據(jù)庫(kù)可以恢復(fù)則redis集群就down了。

客戶端連接:

使用redis-cli -c -p 任意一個(gè)端口

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容