环境:
redis哨兵模式:
在redis主从的基础上,实现负载,当主库宕机时,由哨兵机制,在众多的从库中,选举出新的主库,当旧主库启用时,将会变成新主库的从库
缺点:
新主库出现后,无法自动切换为主库,需要手工去php切换,在这一瞬间会对后端数据库造成极大的负载,可能直接导致后端数据宕机
案例演示,根据以上Top
一、redis软件安装
安装环境软件支持
[root@localhost src]# yum install gcc-c++ -y
下载redis
[root@localhost src]# wget -c http://download.redis.io/releases/redis-5.0.5.tar.gz
编译安装
[root@localhost src]# tar xf redis-5.0.5.tar.gz
[root@localhost src]# cd redis-5.0.5
[root@localhost redis-5.0.5]# vim src/Makefile
PREFIX?=/usr/local/redis
[root@localhost redis-5.0.5]# make PREFIX=/usr/local/redis MALLOC=libc install
[root@localhost redis-5.0.5]# make install
[root@localhost redis-5.0.5]# mkdir -p /usr/local/redis/6379
[root@localhost redis-5.0.5]# cp redis.conf /usr/local/redis/6379/6379.conf
[root@localhost redis-5.0.5]# cp /usr/local/redis/bin/redis-server /usr/local/redis/bin/redis6379-server
[root@localhost redis-5.0.5]#
[root@localhost redis-5.0.5]# ls /usr/local/redis/
6379 bin
[root@localhost redis-5.0.5]# ls /usr/local/redis/6379/
6379.conf
[root@localhost redis-5.0.5]# ls /usr/local/redis/bin/
redis6379-server redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
编译sysctl.conf文件
[root@localhost redis-5.0.5]# vim /etc/sysctl.conf
net.core.somaxconn=512
vm.overcommit_memory=1
让redis负责内存管理
[root@localhost redis-5.0.5]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
执行sysctl -p,让内核重新读取sysctl
[root@localhost redis-5.0.5]# sysctl -p
net.core.somaxconn = 512
vm.overcommit_memory = 1
配置启动脚本
[root@localhost redis-5.0.5]# /usr/src/redis-5.0.5/utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 6379 #端口
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis/6379/6379.conf #配置文件
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/6379/redis_6379.log #日志文件
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/6379/ #数据目录
Please select the redis executable path [] /usr/local/redis/bin/redis6379-server #启动脚本
Selected config:
Port : 6379
Config file : /usr/local/redis/6379/6379.conf
Log file : /usr/local/redis/6379/redis_6379.log
Data dir : /usr/local/redis/6379/
Executable : /usr/local/redis/bin/redis6379-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
如此,就可以通过以下命令,直接进行启动、停止及重启了
[root@localhost redis-5.0.5]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
编辑配置文件
vim /usr/local/redis/6379/6379.conf
配置rdb与aof持久化
注:这里只要配置下面4行,其他默认即可
bind 0.0.0.0 监听IP
port 6379 监听端口
appendonly yes 开启AOF持久化
no-appendfsync-on-rewrite yes 当rdb写入时,停止aof写入
[root@localhost redis-5.0.5]# vim /usr/local/redis/6379/6379.conf
70 bind 0.0.0.0 监听IP
89 protected-mode yes 可以查看键值
93 port 6379 监听端口
102 tcp-backlog 511 tcp队列长度,这里默认
114 timeout 0 客户端与服务器之间的连接超时时间,0为永不超时
131 tcp-keepalive 300 每300秒给客户端发送ACK握手包
137 daemonize yes 后台启动
148 supervised no 可以通过upstart和systemd管理redis守护进程
159 pidfile /var/run/redis_6379.pid 进程文件
167 loglevel notice 日志级别,默认即可
172 logfile /usr/local/redis/6379/redis_6379.log 日志文件路径
176 # syslog-enabled no 是否输出到系统日志
187 databases 16 数据库个数0-15编号,共16个数据库
195 always-show-logo yes 启动时是否显示日志
219 save 900 1
220 save 300 10
221 save 60 10000
236 stop-writes-on-bgsave-error yes 快照出问题时,不可写
242 rdbcompression yes rdb模式,是否启动压缩,启用
251 rdbchecksum yes 对rdb数据文件操作时,启动校验
254 dbfilename dump.rdb rdb持久化的快照文件名
264 dir /usr/local/redis/6379/ rdb文件路径
以下是AOF模式配置,这里rdb和AOF持久化都启用
700 appendonly yes 开启AOF持久化
704 appendfilename "appendonly.aof" AOF持久化快照文件名
730 appendfsync everysec 每秒保存至快照
752 no-appendfsync-on-rewrite yes 当rdb写入时,停止aof写入
771 auto-aof-rewrite-percentage 100 aof文件增长100%时,重写
772 auto-aof-rewrite-min-size 64mb aof文件大于64M时,重写
796 aof-load-truncated yes aof文件加载时,忽略最后一条命令
服务重启
[root@localhost redis-5.0.5]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost redis-5.0.5]# ps -ef|grep redis
root 2835 1 0 12:29 ? 00:00:00 /usr/local/redis/bin/redis6379-server 0.0.0.0:6379
root 2840 1672 0 12:29 pts/1 00:00:00 grep --color=auto redis
关闭selinux及配置防火墙
[root@localhost redis-5.0.5]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost redis-5.0.5]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost redis-5.0.5]# firewall-cmd --reload
success
[root@localhost redis-5.0.5]# firewall-cmd --zone=public --list-ports
80/tcp 22/tcp 6379/tcp
二、配置主从
编译从库192.168.1.11
[root@localhost ~]# vim /usr/local/redis/6379/6379.conf
287 # replicaof <masterip> <masterport>
288 replicaof 192.168.1.21 6379 主库:192.168.1.21 端口:6379
服务重启
[root@localhost ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
编译从从库192.168.1.12
[root@localhost ~]# vim /usr/local/redis/6379/6379.conf
287 # replicaof <masterip> <masterport>
288 replicaof 192.168.1.21 6379 主库:192.168.1.22 端口:6379
服务重启
[root@localhost ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
三、配置redis哨兵
进入源码目录,复制哨兵配置文件
[root@localhost ~]# cd /usr/src/redis-5.0.5
[root@localhost redis-5.0.5]# cp sentinel.conf /usr/local/redis/
[root@localhost redis-5.0.5]# ls /usr/local/redis/
6379 bin sentinel.conf
编辑哨兵配置文件sentinel.conf
注:只需要修改以下字段,其他默认
16 bind 0.0.0.0 监听IP
22 port 26379 监听端口
27 daemonize yes 后台运行
37 logfile “entinel.log” 日志文件名
66 dir /usr/local/redis/ 日志存放路径
85 sentinel monitor mymaster 192.168.1.21 6379 2 意思是,如果有两个从库认为主库挂了,那么就重新选举出新主库
186 sentinel notification-script mymaster /usr/local/redis/notify.sh 故障转移通知脚本
[root@localhost redis-5.0.5]# vim /usr/local/redis/sentinel.conf
16 bind 0.0.0.0 监听IP
22 port 26379 监听端口
27 daemonize yes 后台运行
32 pidfile /var/run/redis-sentinel.pid 进程文件
37 logfile "entinel.log" 日志文件名
66 dir /usr/local/redis/ 日志存放路径
85 sentinel monitor mymaster 192.168.1.21 6379 2 意思是,如果有两个从库认为主库挂了,那么就重新选举出新主库
114 sentinel down-after-milliseconds mymaster 30000 3W毫秒后,认为主库失联
122 sentinel parallel-syncs mymaster 1 故障转移期间,由1个从库先同步,剩余的排队
147 sentinel failover-timeout mymaster 180000 故障转移超时时间
186 sentinel notification-script mymaster /usr/local/redis/notify.sh 故障转移通知脚本
220 sentinel deny-scripts-reconfig yes 禁止修改脚本
编译故障转移通知脚本
[root@localhost redis-5.0.5]# vim /usr/local/redis/notify.sh
#!/bin/bash
#####################
TO="631646620@qq.com"
SUBJECT="redis 发生故障转移"
CONTEXT="redis 发生故障转移"
echo -e $CONTEXT | mailx -s $SUBJECT $TO
给邮件脚本添加可执行权限
[root@localhost ~]# chmod +x /usr/local/redis/notify.sh
将配置文件复制到其他从库中
/usr/local/redis/sentinel.conf
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# scp sentinel.conf root@192.168.1.11:/usr/local/redis/
[root@localhost redis]# scp sentinel.conf root@192.168.1.12:/usr/local/redis/
[root@localhost redis]# scp notify.sh root@192.168.1.11:/usr/local/redis/
[root@localhost redis]# scp notify.sh root@192.168.1.12:/usr/local/redis/
启动配置sentinel服务
[root@localhost ~]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.conf
查看角色信息
192.168.1.21上查看
[root@localhost redis]# ifconfig|grep 192.168
inet 192.168.1.21 netmask 255.255.255.0 broadcast 192.168.1.255
[root@localhost redis]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> role
1) "master" 角色为:主库
2) (integer) 28917
3) 1) 1) "192.168.1.11" 从库
2) "6379"
3) "28639"
2) 1) "192.168.1.12" 从库
2) "6379"
3) "28778"
192.168.1.12上查看
[root@localhost ~]# ifconfig|grep 192.168
inet 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> role
1) "slave" 角色:从库
2) "192.168.1.21" 主库
3) (integer) 6379
4) "connected"
5) (integer) 33546
将主库进程关闭,测试故障转移及邮件是否发送
主库关闭服务
[root@localhost redis]# ifconfig |grep 192.168
inet 192.168.1.21 netmask 255.255.255.0 broadcast 192.168.1.255
[root@localhost redis]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@localhost redis]# ps -ef|grep redis 只有哨兵的进程
root 3211 1 0 16:11 ? 00:00:02 /usr/local/redis/bin/redis-sentinel 0.0.0.0:26379 [sentinel]
root 3237 1672 0 16:17 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1480/master
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 3211/redis-sentinel
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1385/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1480/master
tcp6 0 0 :::22 :::* LISTEN 1385/sshd
3分钟后,查看从库信息
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> role
1) "slave"
2) "192.168.1.12" 主库变成了192.168.1.12
3) (integer) 6379
4) "connected"
5) (integer) 88977
127.0.0.1:6379>
[root@localhost ~]# ifconfig|grep 192.168
inet 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> role
1) "master" 角色:主库
2) (integer) 98068
3) 1) 1) "192.168.1.11" 从库
2) "6379"
3) "97929"
已发生故障转移
[root@localhost redis]#
You have new mail in /var/spool/mail/root 提示有邮件
[root@localhost redis]# tailf /var/spool/mail/root 查看邮件
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Message-Id: <20200616081830.1E83A802C4CC@localhost.localdomain>
From: root@localhost.localdomain (root)
redis 发生故障转移
--1E83A802C4CC.1592295510/localhost.localdomain--
---------------------------end