一、Redis主从模式配置
1、案例拓扑图
2、环境说明
主机名称 |
IP地址 |
redis版本和角色说明 |
master |
20.0.0.10 |
redis 5.0.7(主) |
slave1 |
20.0.0.20 |
redis 5.0.7(从) |
slave2 |
20.0.0.30 |
redis 5.0.7(从) |
3、Redis安装
[root@master ~]# cd redis-5.0.7/
[root@master redis-5.0.7]# make -j2
[root@master redis-5.0.7]# make PREFIX=/usr/local/redis install
[root@master redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@master redis-5.0.7]# cd utils/
[root@master utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/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!
(1)、master配置文件修改
#69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
bind 20.0.0.10
#136行 开启守护进程
daemonize yes
#171行 修改日志文件目录
logfile /var/log/redis_6379.log
#263行 修改工作目录
dir /var/lib/redis/6379
#699行 开启AOF持久化功能
appendonly yes
tcp 0 0 20.0.0.10:6379 0.0.0.0:* LISTEN 57685/redis-server
(2)、slave1配置文件修改
#69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
bind 20.0.0.20
#136行 开启守护进程
daemonize yes
#171行 修改日志文件目录
logfile /var/log/redis_6379.log
#263行 修改工作目录
dir /var/lib/redis/6379
#699行 开启AOF持久化功能
appendonly yes
#287行 修改IP和端口
replicaof 20.0.0.10 6379
tcp 0 0 20.0.0.20:6379 0.0.0.0:* LISTEN 19037/redis-server
tcp 0 0 20.0.0.20:34397 20.0.0.10:6379 ESTABLISHED 19037/redis-server
(3)、slave2配置文件修改
#69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
bind 20.0.0.30
#136行 开启守护进程
daemonize yes
#171行 修改日志文件目录
logfile /var/log/redis_6379.log
#263行 修改工作目录
dir /var/lib/redis/6379
#699行 开启AOF持久化功能
appendonly yes
#287行 修改IP和端口
replicaof 20.0.0.10 6379
tcp 0 0 20.0.0.30:6379 0.0.0.0:* LISTEN 59182/redis-server
tcp 0 0 20.0.0.30:46648 20.0.0.10:6379 ESTABLISHED 59182/redis-server
(1)、验证主从效果(master上看日志)
[root@master utils]# vi /var/log/redis_6379.log
......省略部分内容
57685:M 11 Nov 2020 10:51:17.605 * Replica 20.0.0.20:6379 asks for synchronization #slave1服务器IP
57685:M 11 Nov 2020 10:51:17.605 * Full resync requested by replica 20.0.0.20:6379
57685:M 11 Nov 2020 10:51:17.605 * Starting BGSAVE for SYNC with target: disk
57685:M 11 Nov 2020 10:51:17.606 * Background saving started by pid 57691
57691:C 11 Nov 2020 10:51:17.607 * DB saved on disk
57691:C 11 Nov 2020 10:51:17.607 * RDB: 4 MB of memory used by copy-on-write
57685:M 11 Nov 2020 10:51:17.658 * Background saving terminated with success
57685:M 11 Nov 2020 10:51:17.658 * Synchronization with replica 20.0.0.20:6379 succeeded
57685:M 11 Nov 2020 10:55:07.287 * Replica 20.0.0.30:6379 asks for synchronization #slave2服务器IP
57685:M 11 Nov 2020 10:55:07.287 * Full resync requested by replica 20.0.0.30:6379
57685:M 11 Nov 2020 10:55:07.287 * Starting BGSAVE for SYNC with target: disk
57685:M 11 Nov 2020 10:55:07.288 * Background saving started by pid 57734
57734:C 11 Nov 2020 10:55:07.290 * DB saved on disk
57734:C 11 Nov 2020 10:55:07.290 * RDB: 4 MB of memory used by copy-on-write
57685:M 11 Nov 2020 10:55:07.372 * Background saving terminated with success
57685:M 11 Nov 2020 10:55:07.372 * Synchronization with replica 20.0.0.30:6379 succeeded
(2)、master上验证从节点
[root@master utils]# redis-cli -h 20.0.0.10 -p 6379
20.0.0.10:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=20.0.0.20,port=6379,state=online,offset=2702,lag=0
slave1:ip=20.0.0.30,port=6379,state=online,offset=2702,lag=1
(3)、插入数据验证
20.0.0.10:6379> set name lisi
OK
20.0.0.10:6379> set score 80
OK
20.0.0.10:6379> keys *
1) "score"
2) "name"
20.0.0.10:6379> get name
"lisi"
20.0.0.10:6379> get score
"80"
[root@slave1 utils]# redis-cli -h 20.0.0.20 -p 6379
20.0.0.20:6379> keys *
1) "score"
2) "name"
20.0.0.20:6379> get name
"lisi"
20.0.0.20:6379> get score
"80"
[root@slave2 utils]# redis-cli -h 20.0.0.30 -p 6379
20.0.0.30:6379> keys *
1) "score"
2) "name"
20.0.0.30:6379> get name
"lisi"
20.0.0.30:6379> get score
"80"
二、Redis哨兵模式
1、Redis sentinel介绍
Redis Sentinel是Redis高可用的实现方案。Sentinel是一个管理多个Redis实例的工具,它可以实现对Redis的监控、通知、自动故障转移。
2、哨兵模式的作用
① 监控
不断的检查master和slave是否正常运行。master存活检测、master与slave运行情况检测
② 通知(提醒)
当被监控的服务器出现问题时,向其他(哨兵间,客户端)发送通知。
③ 自动故障转移
断开master与slave连接,选取一个slave作为master,将其他slave连接到新的master,并告知客户端新的服务器地址
PS:哨兵也是一台redis服务器,只是不提供数据服务
哨兵的启动依赖于主从模式,所以须把主从模式安装好的情况下再去做哨兵模式,所有节点上都需要部署哨兵模式,哨兵模式会监控所有的redis工作节点是否正常,当
master出现问题的时候,因为其他节点与主节点失去联系,因此会投票,投票过半就认为这个master的确出现问题,然后会通知哨兵间,然后从slaves中选取一个作为新的
master,至少需要3个或3个以上服务器
3、环境说明
主机名称 | IP地址 | redis版本和角色说明 |
---|---|---|
master | 20.0.0.10:6379 | redis 5.0.7(主) |
slave1 | 20.0.0.20:6379 | redis 5.0.7(从) |
slave2 | 20.0.0.30:6379 | redis 5.0.7(从) |
master | 20.0.0.10:26379 | Sentinel1 |
slave1 | 20.0.0.20:26379 | Sentinel2 |
slave2 | 20.0.0.30:26379 | Sentinel3 |
4、哨兵模式配置
所有节点都需要修改
[root@master ~]# vi redis-5.0.7/sentinel.conf
17行/protected-mode no #关闭保护模式
26行/daemonize yes #指定sentinel为后台启动
36行/logfile "/var/log/sentinel.log" #指定日志存放路径
65行/dir "/var/lib/redis/6379" #指定数据库存放路径
84行/sentinel monitor mymaster 20.0.0.10 6379 2 #至少几个哨兵检测到主服务器故障了,才会进行故障迁移,全部指向masterIP
113行/sentinel down-after-milliseconds mymaster 30000 #判定服务器down掉的时间周期,默认30000毫秒(30秒)
146行/sentinel failover-timeout mymaster 180000 #故障节的的最大超时时间为180000(180秒)
5、启动哨兵模式
先启master,再启slave
[1] 58571
[1] 19812
[1] 59917
6、查看哨兵信息
[root@master ~]# redis-cli -h 20.0.0.10 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3
[root@slave1 ~]# redis-cli -h 20.0.0.20 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3
[root@slave2 ~]# redis-cli -h 20.0.0.20 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3
(1)、
[root@master ~]# ps -ef | grep redis
root 57685 1 0 10:51 ? 00:00:06 /usr/local/bin/redis-server 20.0.0.10:6379
root 58567 1 0 12:07 ? 00:00:01 redis-sentinel *:26379 [sentinel]
root 58649 58470 0 12:15 pts/1 00:00:00 grep --color=auto redis
[root@master ~]# kill -9 57685 #master上redis-server的进程号
(2)、验证结果
58567:X 11 Nov 2020 12:18:14.388 # +failover-state-reconf-slaves master mymaster 20.0.0.10 6379
58567:X 11 Nov 2020 12:18:14.467 * +slave-reconf-sent slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379
58567:X 11 Nov 2020 12:18:14.776 # -odown master mymaster 20.0.0.10 6379
58567:X 11 Nov 2020 12:18:15.428 * +slave-reconf-inprog slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379
58567:X 11 Nov 2020 12:18:15.428 * +slave-reconf-done slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379
58567:X 11 Nov 2020 12:18:15.483 # +failover-end master mymaster 20.0.0.10 6379
58567:X 11 Nov 2020 12:18:15.483 # +switch-master mymaster 20.0.0.10 6379 20.0.0.30 6379 #从master转到了slave2上
58567:X 11 Nov 2020 12:18:15.483 * +slave slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.30 6379
58567:X 11 Nov 2020 12:18:15.483 * +slave slave 20.0.0.10:6379 20.0.0.10 6379 @ mymaster 20.0.0.30 6379
58567:X 11 Nov 2020 12:18:45.535 # +sdown slave 20.0.0.10:6379 20.0.0.10 6379 @ mymaster 20.0.0.30 6379
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=20.0.0.30:6379,slaves=2,sentinels=3