1. 程式人生 > 其它 >Redis-redis哨兵叢集

Redis-redis哨兵叢集

技術標籤:配置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

redis-server redis-conf

啟動redis,我們可以看到,redis已經佔領了6380 埠

進入客戶端

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 現在的角色是一個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>

3、Sentinel 哨兵

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

# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379

我們把監聽的埠修改成6380,並且加上權值為2,quorum是說當此master至少被多少個哨兵認為主觀下線時,才會觸發failover

# 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