主從切換技術的方法是:當主服務器宕機後,須要手動把一臺從服務器切換爲主服務器,這就須要人工干預,費事費力,還會形成一段時間內服務不可用。這不是一種推薦的方式,更多時候,咱們優先考慮哨兵模式。javascript
哨兵模式是一種特殊的模式,首先Redis提供了哨兵的命令,哨兵是一個獨立的進程,做爲進程,它會獨立運行。其原理是哨兵經過發送命令,等待Redis服務器響應,從而監控運行的多個Redis實例。java
這裏的哨兵有兩個做用linux
經過發送命令,讓Redis服務器返回監控其運行狀態,包括主服務器和從服務器。redis
當哨兵監測到master宕機,會自動將slave切換成master,而後經過發佈訂閱模式通知其餘的從服務器,修改配置文件,讓它們切換主機。服務器
然而一個哨兵進程對Redis服務器進行監控,可能會出現問題,爲此,咱們可使用多個哨兵進行監控。各個哨兵之間還會進行監控,這樣就造成了多哨兵模式。網絡
用文字描述一下故障切換(failover)的過程。假設主服務器宕機,哨兵1先檢測到這個結果,系統並不會立刻進行failover過程,僅僅是哨兵1主觀的認爲主服務器不可用,這個現象成爲主觀下線。當後面的哨兵也檢測到主服務器不可用,而且數量達到必定值時,那麼哨兵之間就會進行一次投票,投票的結果由一個哨兵發起,進行failover操做。切換成功後,就會經過發佈訂閱模式,讓各個哨兵把本身監控的從服務器實現切換主機,這個過程稱爲客觀下線。這樣對於客戶端而言,一切都是透明的。app
配置3個哨兵和1主2從的Redis服務器來演示這個過程。socket
特別注意:使用Redis哨兵模式,最少須要3個節點(一主多從結構)async
2、Redis主從複製搭建① 配置mastertcp
# 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 6379
② 修改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_6379.pid
pidfile 是咱們啓動redis 的時候,linux 爲咱們分配的一個pid 進程號,若是這裏不做修改,會影響後面redis服務的啓動
③ 啓動 redis
[root@yunwei ~] # redis-server redis.conf 進入redis: redis-cli -p 6379 127.0.0.1:6379> 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 如今的角色是一個master 啓動的服務。
和上面配置 master同樣,咱們須要修改端口號和pid 文件,在修改完以後,咱們有兩種方法配置從服務。
① 在配置文件中配置從服務
################################# 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 6379
咱們能夠在配置文件中直接修改 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 服務:
[root@yunwei ~] # redis-server redis.conf 進入redis: redis-cli -p 6379
使用==info命令==,查看一下slave主機的狀態:
# Replication role:slave master_host:127.0.0.1 master_port:6379 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
咱們能夠看到,如今的redis 是一個從服務的角色,鏈接着6379的服務。接下來咱們再來看一下目前master 的狀態:
# Replication role:master connected_slaves:2 slave0:ip=192.168.11.129,port=6379,state=online,offset=785,lag=0 slave1:ip=192.168.11.130,port=6379,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
咱們若是須要設置讀寫分離,只須要在主服務器中設置:
# 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
Redis01/Redis02/Redis03 => redis.conf bind 0.0.0.0 ... protected-mode no => 哨兵必須配置protected-mode,外部網絡鏈接redis服務3、Sentinel哨兵
在sentinel.conf 配置文件中, 咱們能夠找到port 屬性,這裏是用來設置sentinel 的端口,通常狀況下,至少會須要三個哨兵對 redis 進行監控。
# port <sentinel-port> # The port that this sentinel instance will run on port 26379
在slave從服務器上,咱們把須要設置主服務器的IP和端口,而且加上權值爲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 192.168.11.128 6379 2
[root@yunwei ~] # redis-sentinel sentinel.conf
sentinel 啓動以後,就會監視到如今有一個主服務器,兩個從服務器。
redis-sentinel啓動警告問題:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
翻譯:對一個高負載的環境來講tcp設置128這個值,過小了。
解決方案:
echo 511 > /proc/sys/net/core/somaxconn
命令就把這個問題解決了。可是這個只是暫時的。若是想要永久解決,打開/etc/sysctl.conf,在這裏面添net.core.somaxconn=1024 而後執行sysctl -p 就能夠永久消除這個warning
咱們手動關閉Master 以後,sentinel 在監聽master 確實是斷線了以後,將會開始計算權值,而後從新分配主服務器。
128799:X 29 May 12:08:35.657# +failover-end master mymaster 192.168.11.128 6379 128799:X 29 May 12:08:35.657# +switch-master mymaster 192.168.11.128 6379 192.168.11.130 6379
你們可能會好奇,若是master 重連以後,會不會搶回屬於他的位置,答案是否認的,就好比你被一個小弟搶了你老大的位置,他肯給回你這個位置嗎。所以當master回來以後,他也只能當個小弟。
① Master 狀態監測
② 若是Master 異常,則會進行Master-slave 轉換,將其中一個Slave做爲Master,將以前的Master做爲Slave
③ Master-Slave切換後,master_redis.conf、slave_redis.conf和sentinel.conf的內容都會發生改變,即master_redis.conf中會多一行slaveof的配置,sentinel.conf的監控目標會隨之調換