linux 下Redis 5.0主从复制(一主二从)哨兵模式的搭建
文档结构如下: 一、环境说明: |
Redis一主二从主要是用于读写分离和容灾,配置是配从不配主,主从复制首次同步是全量,后面是增量同步;但是只要是重新连接master,一次完全同步(全量复制)将被自动执行。?
二、安装
Redis 一主二从的安装其实跟单机的安装区别不大,本次安装我们可以先修改参数文件后在安装,可以参考我的单机的安装博客:
或者直接百度 redis 5.0 linux 之类关键字,也可以百度的到。
2.1.?? 配置yum
a)?? 关闭防火墙
vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#???? enforcing - SELinux security policy is enforced.
#???? permissive - SELinux prints warnings instead of enforcing.
#???? disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#???? targeted - Targeted processes are protected,
#???? mls - Multi Level Security protection.
SELINUXTYPE=targeted
setenforce 0
service iptables stop
chkconfig iptables off
b)?? 三台服务器配置好yum
mount /dev/sr0 /mnt/
cd /etc/yum.repos.d/
mv redhat.repo redhat.repo.bak
mv rhel-source.repo rhel-source.repo.bak
vi /etc/yum.repos.d/rhel-debuginfo.repo
[rhel-debuginfo]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=file:///mnt/
enabled=1?
gpgcheck=0? ?
yum -y install make gcc*
2.2.?? 参数配置
mkdir -p /redis5.0
把解压包上传到/redis5.0
tar -zxvf redis-5.0.0.tar.gz
需要修改以及注释的参数如下:
Master:
cd /redis5.0/redis-5.0.0
save 900 1
#save 300 10
#save 60 10000
bind 172.6.10.80?
daemonize yes
loglevel warning
timeout 60
logfile "6379.log"
dbfilename dump6379.rdb
maxmemory-policy volatile-ttl
auto-aof-rewrite-min-size 10GB
masterauth redis?? --主库的密码(本机可以不配置)
Slave1:
cd /redis5.0/redis-5.0.0
save 900 1
#save 300 10
#save 60 10000
bind 172.6.10.81
daemonize yes
loglevel warning
logfile "6380.log"
dbfilename dump6380.rdb
pidfile /var/run/redis_6380.pid
port 6380
timeout 60
maxmemory-policy volatile-ttl
auto-aof-rewrite-min-size 10GB
slaveof 172.16.10.80 6379
masterauth redis?? --主库的密码
Salve2:
cd /redis5.0/redis-5.0.0
save 900 1
#save 300 10
#save 60 10000
bind 172.6.10.82
daemonize yes
loglevel warning
port 6381
timeout 60
dbfilename dump6381.rdb
logfile "6381.log"
pidfile /var/run/redis_6381.pid
maxmemory-policy volatile-ttl
auto-aof-rewrite-min-size 10GB
slaveof 172.16.10.80 6379
masterauth redis?? --主库的密码
2.3.?? 安装
cd /redis5.0/redis-5.0.0
make
make install
?
安装完成。
启动如下:
redis-server /redis5.0/redis-5.0.0/redis6379.conf
redis-server /redis5.0/redis-5.0.0/redis6380.conf
redis-server /redis5.0/redis-5.0.0/redis6381.conf
?
由于参数已经修改完了,后面修改密码:
config set requirepass "redis"
?
并且写入配置文件:
?
例如关闭提示需要密码就说明修改成功。
同理修改下面两台服务器:
?
主从复制的密码配置:
?
查看主机角色信息:
?
查看从机一配置:
?
查看从机二配置:
?
?
可以看出同步状态正常。
三、测试
3.1.? 注意
1、如果没有把slaveof 参数配置到参数文件中,从库重启后需要重新slaveof;即从库会变成主库(没有配置slaveof参数文件)
2、在主从搭建完成后,后面添加的从库slaveof后,会自动把数据从主库同步过来,生产上会有一定的性能影响。
3、从机只有读的权限,主机才能够写操作。
3.1.?? Master 挂掉slave变master(反客为主)
master 挂掉后,手动的让从库一升为主库。
关闭主库。
?
查看从库1,2
?
slave 变成主库
slaveof no one
?
3.1.?? 哨兵模式配置测试
Redis的哨兵(sentinel) 系统用于管理多个 Redis 服务器,该系统执行以下三个任务:
监控(Monitoring): 哨兵(sentinel) 会不断地检查你的Master和Slave是否运作正常。
提醒(Notification):当被监控的某个 Redis出现问题时,哨兵(sentinel) 可以通过 API 向管理员或者其他应用程序发送通知。
?自动故障迁移(Automatic failover):当一个Master不能正常工作时,哨兵(sentinel) 会开始一次自动故障迁移操作,它会将失效Master的其中一个Slave升级为新的Master,并让失效Master的其他Slave改为复制新的Master;如果修复好的master重新启动后,原master变成slave。
简单配置如下:
在安装的redis目录下,有sentinel.conf文件,可以设置超时等等。
本次测试是我自己新建一个文件格式如下:
?
sentinel monitor 主机名 主机ip 主机端口 票数n ?票数多余n的从机作为主机
由于是三台服务器(谁有2票就谁为新master)
vi sentinel6379.conf
sentinel monitor redis01 172.16.10.80 6379 2
sentinel auth-pass redis01 redis?? #master如果设置密码 没有可以不用这一行
sentinel down-after-milliseconds redis01 5000? #修改心跳为5000毫秒
protected-mode no??? --关闭保护进程模式,防止哨兵不能互相通信
启动:
redis-sentinel /redis5.0/redis-5.0.0/sentinel6379.conf
或者后台启动:
redis-sentinel /redis5.0/redis-5.0.0/sentinel6379.conf --sentinel &
?
vi sentinel6380.conf
sentinel monitor redis01 172.16.10.80 6379 2
sentinel auth-pass redis01 redis?
sentinel down-after-milliseconds redis01 5000
protected-mode no??? --关闭保护进程模式,防止哨兵不能互相通信
启动:
?
vi sentinel6381.conf
sentinel monitor redis01 172.16.10.80 6379 2
sentinel auth-pass redis01 redis??
sentinel down-after-milliseconds redis01 5000
protected-mode no??? --关闭保护进程模式,防止哨兵不能互相通信
?
3.4.?? 哨兵模式的反客为主(自动)
查看各个节点的哨兵模式情况:
Master:
redis-cli -h 172.16.10.80 -p 26379
info sentinel
?
Slave1:
redis-cli -h 172.16.10.81 -p 26379
info sentinel
?
Slave2:
redis-cli -h 172.16.10.82 -p 26379
info sentinel
?
模拟master down掉测试:
?
查看:
原master哨兵进程日志:
?
从中看出failover到原slave1了。
查看原salve1(现master) 哨兵进程日志:
?
Slave1 为新master了。
查看slave2日志说明:
?
Salve2变成原slave1的从库了。
查看新master信息:
?
角色为master,一个从库,为原slave1。
查看新从库信息:
?
为slave角色,新的主库为10.81。
测试是否同步:
?
同步成功。
3.3.?? 原master修好后
原master修好后,启动后:
?
变成现在master的从机了,并不会变成独立的一台master。
?
哨兵日志提示为10.81的从库了。
测试:
把现在新master数据同步过来了没问题,也可以自行测试。
??
总结:
1、 大的互联网公司都会使用哨兵模式主从或者集群模式24*7提供业务以及提供读写分离,减轻系统的压力,身边使用最多的就是大家知道的知乎网站。
2、 在failover之后,原master修复好后会变为新master的从库,并不会变成独立的一台master。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!