Redis主从复制 + 哨兵模式(CentOS7)

一、任务

  • 实验目标:实现Redis主从复制
  • 实验方式:一主二仆 + 哨兵模式

一、准备工作

  • 安装CentOS7(以后补)
  • 安装Redis(以后补)

二、修改配置

  1. 停止当前运行的Redis

进入redis客户端:

redis-cli 
# 因为一开始是默认配置,直接执行以上语句
# 如果需要指定ip或者端口号,使用以下语句
redis-cli -h ip -p 端口

执行shutdown语句
在这里插入图片描述

执行完quit退出

在这里插入图片描述
2. 进入配置文件存放的路径,创建三份配置文件

# 我这里对应的一主二仆
# redis6379.conf(master)
# redis6380.conf(slave)
# redis6381.conf(slave)
vim redis6379.conf
vim redis6380.conf
vim redis6381.conf
  1. 配置三份文件

6379:

# include作用包含公共内用,这里把默认配置文件作为公共文件
include /usr/local/bin/redis-3.2.5/redis.conf
# 然后添加以下额外属性
pidfile "/var/run/redis6379.pid" # 指定 pid文件
port 6379 # 指定启动端口
dbfilename "dump6379.rdb" # 指定持久化文件名称

6380:

# include作用包含公共内用,这里把默认配置文件作为公共文件
include /usr/local/bin/redis-3.2.5/redis.conf
# 然后添加以下额外属性
pidfile "/var/run/redis6380.pid" # 指定 pid文件
port 6380 # 指定启动端口
dbfilename "dump6380.rdb" # 指定持久化文件名称

6381:

# include作用包含公共内用,这里把默认配置文件作为公共文件
include /usr/local/bin/redis-3.2.5/redis.conf
# 然后添加以下额外属性
pidfile "/var/run/redis6381.pid" # 指定 pid文件
port 6381 # 指定启动端口
dbfilename "dump6381.rdb" # 指定持久化文件名称

注意:Redis默认不开启AOF

在这里插入图片描述
如果开始了AOF,请在配置以上三个配置文件时添加 appendfilename 及其对应文件名(格式:xxx.aof)

四、启动Redis

启动Redis,并查看其状态

# 启动服务,因为端口发生改变,应加上ip和端口
[root@MyLinux redis-3.2.5]# redis-server redis6379.conf 
[root@MyLinux redis-3.2.5]# redis-server redis6380.conf 
[root@MyLinux redis-3.2.5]# redis-server redis6381.conf 
# 查看状态
[root@MyLinux redis-3.2.5]# ps -ef |grep redis

成功如图:

在这里插入图片描述

五、配置主从关系

:刚启动的三个Redis服务,没有仆从关系,都是master
进入redis客户端,查看服务器状态:
命令:

info replication

6379:
在这里插入图片描述6380:
在这里插入图片描述6381:在这里插入图片描述

1、配置一主二仆(原则:配从(服务器)不配主(服务器))

在从服务器(6380和6381)客户端下执行以下命令,主服务器(6379)不动:

slaveof 127.0.0.1 6379

2、查看状态

info replication

6379:

在这里插入图片描述
6380:

在这里插入图片描述
6381:

在这里插入图片描述

六、测试

往主服务器插入数据
注:此时从服务器不再能做插入数据操作,只能读;主服务器可读可写

七、哨兵模式(sentinel)

原因:一般Redis主从配置下,主机down 了之后,其他从机处于待命状态,不会自动上升为主服务器,只能手动操作,不方便。

解决:哨兵模式,监察主服务器状态,如果主服务器down 了,哨兵根据规则监测,确定为down,选择其中一台从机上升为主机。

配置:
检查安装目录有没有sentinel.conf

ll *.conf # 查看当前目录的所有conf文件

检查没有,创建

vim sentinel.conf

注: 实现哨兵模式只需要一行配置,这里只做启动配置不做其他配置

往配置文件中添加以下内容即可:

# mymaster为监控对象起的服务器名称
sentinel  monitor  mymaster  Redis主服务启动的ip  Redis主服务启动的端口 1
# 最后一项相当于是检测机制,1:当有一个哨兵检测出主服务down了之后,就会指定一个从服务作为主服务上线;
# 以此类推:n:当有n个哨兵检测出主服务down了之后,就会指定一个从服务作为主服务上线

从服务器推选成主服务器规则

1、选择优先级靠前的

优先级在redis.conf中slave-priority 100(越小级别越高)

2、选择偏移量最大的

偏移量是指获得原主数据最多的

3、选择runid最小的从服务

每个redis实例启动后都会随机生成一个40位的runid

启动哨兵:

redis-sentinel  /myredis/sentinel.conf 

附上Redis sentinel默认配置文件
注: 启动只需要复制以下内容到自己创建的空的sentinel.conf下,同样配置以上内容

#
# Example:
#
# sentinel announce-ip 1.2.3.4

# dir <working-directory>
# Every long running process should have a well-defined working directory.
# Example sentinel.conf

# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
# protected-mode no

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

# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4

# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/tmp"

# 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 myid 156ee028e9099a1019fcaaa8f6081939f2090eb2

# sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd

# sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel monitor mymaster 127.0.0.1 6380 1

# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel config-epoch mymaster 3

# sentinel failover-timeout <master-name> <milliseconds>
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
#   already tried against the same master by a given Sentinel, is two
#   times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
#   to a Sentinel current configuration, to be forced to replicate
#   with the right master, is exactly the failover timeout (counting since
#   the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
#   did not produced any configuration change (SLAVEOF NO ONE yet not
#   acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
#   reconfigured as slaves of the new master. However even after this time
#   the slaves will be reconfigured by the Sentinels anyway, but not with
#   the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel leader-epoch mymaster 3

# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.

# NOTIFICATION SCRIPT
#
# sentinel notification-script <master-name> <script-path>
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh

# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script <master-name> <script-path>
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
#
# <state> is currently always "failover"
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh

八、其他

1 切入点问题?slave1、slave2是从头开始复制还是从切入点开始复制?比如从k4进来,那之前的123是否也可以复制

从头复制
**注:**切入点指的是什么时候从服务器连接上主服务器

2 从机是否可以写?set可否?

不可以,从服务器不再能做插入数据操作,只能读;主服务器可读可写

3 主机shutdown后情况如何?从机是上位还是原地待命

原地待命,保持从服务器身份(可读不可写),等待从机上线(有问题:哨兵模式)

4 主机又回来了后,主机新增记录,从机还能否顺利复制?

5 其中一台从机down后情况如何?依照原有它能跟上大部队吗?

如果使用slavof命令指定的从机,down之后重新上线,会变成主机,重新设置slaveof指定原有的主机会再次变为从服务器,复制主服务器内容

解决:
方式一:在配置文件配置 slaveof 属性 比如 :slaveof 127.0.0.1 6379
方式二:哨兵模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值