redis主从模式和哨兵模式配置

一、Redis主从模式配置
redis主从模式和哨兵模式配置
环境
redis主从模式和哨兵模式配置
Redis安装:所有服务器均需要安装
1 [[email protected] ~]# tar zxf redis-5.0.7.tar.gz
2 [[email protected] ~]# cd redis-5.0.7/
3 [[email protected] redis-5.0.7]# make -j2
4 [[email protected] redis-5.0.7]# make PREFIX=/usr/local/redis install
5 [[email protected] redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
6 [[email protected] redis-5.0.7]# cd utils/
7 [[email protected] utils]# ./install_server.sh
8 Welcome to the redis service installer
9 This script will help you easily set up a running redis server
10
11 Please select the redis port for this instance: [6379]
12 Selecting default: 6379
13 Please select the redis config file name [/etc/redis/6379.conf]
14 Selected default - /etc/redis/6379.conf
15 Please select the redis log file name [/var/log/redis_6379.log]
16 Selected default - /var/log/redis_6379.log
17 Please select the data directory for this instance [/var/lib/redis/6379]
18 Selected default - /var/lib/redis/6379
19 Please select the redis executable path [/usr/local/bin/redis-server]
20 Selected config:
21 Port : 6379
22 Config file : /etc/redis/6379.conf
23 Log file : /var/log/redis_6379.log
24 Data dir : /var/lib/redis/6379
25 Executable : /usr/local/bin/redis-server
26 Cli Executable : /usr/local/bin/redis-cli
27 Is this ok? Then press ENTER to go on or Ctrl-C to abort.
28 Copied /tmp/6379.conf => /etc/init.d/redis_6379
29 Installing service…
30 Successfully added to chkconfig!
31 Successfully added to runlevels 345!
32 Starting Redis server…
33 Installation successful!

修改配置文件
master配置文件修改
1 [[email protected] src]# vim /etc/redis/6379.conf
2 #69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
3 bind 20.0.0.10
4 #136行 开启守护进程
5 daemonize yes
6 #171行 修改日志文件目录
7 logfile /var/log/redis_6379.log
8 #263行 修改工作目录
9 dir /var/lib/redis/6379
10 #699行 开启AOF持久化功能
11 appendonly yes
12
13 [[email protected] utils]# /etc/init.d/redis_6379 restart #开启Redis
14
15 [[email protected] utils]# netstat -anpt | grep redis
16 tcp 0 0 20.0.0.10:6379 0.0.0.0:* LISTEN 57685/redis-server

slave1配置文件修改
1 [[email protected] src]# vim /etc/redis/6379.conf
2 #69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
3 bind 20.0.0.20
4 #136行 开启守护进程
5 daemonize yes
6 #171行 修改日志文件目录
7 logfile /var/log/redis_6379.log
8 #263行 修改工作目录
9 dir /var/lib/redis/6379
10 #699行 开启AOF持久化功能
11 appendonly yes
12 #287行 修改IP和端口
13 replicaof 20.0.0.10 6379
14
15 [[email protected] utils]# /etc/init.d/redis_6379 restart #开启Redis
16
17 [[email protected] utils]# netstat -anpt | grep redis #查看端口号
18 tcp 0 0 20.0.0.20:6379 0.0.0.0:* LISTEN 19037/redis-server
19 tcp 0 0 20.0.0.20:34397 20.0.0.10:6379 ESTABLISHED 19037/redis-server

slave2配置文件修改
1 [[email protected] src]# vim /etc/redis/6379.conf
2 #69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
3 bind 20.0.0.30
4 #136行 开启守护进程
5 daemonize yes
6 #171行 修改日志文件目录
7 logfile /var/log/redis_6379.log
8 #263行 修改工作目录
9 dir /var/lib/redis/6379
10 #699行 开启AOF持久化功能
11 appendonly yes
12 #287行 修改IP和端口
13 replicaof 20.0.0.10 6379
14
15 [[email protected] utils]# /etc/init.d/redis_6379 restart #开启Redis
16
17 [[email protected] utils]# netstat -anpt | grep redis
18 tcp 0 0 20.0.0.30:6379 0.0.0.0:* LISTEN 59182/redis-server
19 tcp 0 0 20.0.0.30:46648 20.0.0.10:6379 ESTABLISHED 59182/redis-server

验证主从效果(master上看日志)
1 [[email protected] utils]# vi /var/log/redis_6379.log
2 …省略部分内容
3 57685:M 11 Nov 2020 10:51:17.605 * Replica 20.0.0.20:6379 asks for synchronization #slave1服务器IP
4 57685:M 11 Nov 2020 10:51:17.605 * Full resync requested by replica 20.0.0.20:6379
5 57685:M 11 Nov 2020 10:51:17.605 * Starting BGSAVE for SYNC with target: disk
6 57685:M 11 Nov 2020 10:51:17.606 * Background saving started by pid 57691
7 57691:C 11 Nov 2020 10:51:17.607 * DB saved on disk
8 57691:C 11 Nov 2020 10:51:17.607 * RDB: 4 MB of memory used by copy-on-write
9 57685:M 11 Nov 2020 10:51:17.658 * Background saving terminated with success
10 57685:M 11 Nov 2020 10:51:17.658 * Synchronization with replica 20.0.0.20:6379 succeeded
11 57685:M 11 Nov 2020 10:55:07.287 * Replica 20.0.0.30:6379 asks for synchronization #slave2服务器IP
12 57685:M 11 Nov 2020 10:55:07.287 * Full resync requested by replica 20.0.0.30:6379
13 57685:M 11 Nov 2020 10:55:07.287 * Starting BGSAVE for SYNC with target: disk
14 57685:M 11 Nov 2020 10:55:07.288 * Background saving started by pid 57734
15 57734:C 11 Nov 2020 10:55:07.290 * DB saved on disk
16 57734:C 11 Nov 2020 10:55:07.290 * RDB: 4 MB of memory used by copy-on-write
17 57685:M 11 Nov 2020 10:55:07.372 * Background saving terminated with success
18 57685:M 11 Nov 2020 10:55:07.372 * Synchronization with replica 20.0.0.30:6379 succeeded

master上验证从节点
1 [[email protected] utils]# redis-cli -h 20.0.0.10 -p 6379
2 20.0.0.10:6379> info replication
3 # Replication
4 role:master
5 connected_slaves:2
6 slave0:ip=20.0.0.20,port=6379,state=online,offset=2702,lag=0
7 slave1:ip=20.0.0.30,port=6379,state=online,offset=2702,lag=1

二、哨兵模式
Redis sentinel介绍
Redis Sentinel是Redis高可用的实现方案。Sentinel是一个管理多个Redis实例的工具,它可以实现对Redis的监控、通知、自动故障转移。

哨兵模式的作用
① 监控

不断的检查master和slave是否正常运行。master存活检测、master与slave运行情况检测

② 通知(提醒)

当被监控的服务器出现问题时,向其他(哨兵间,客户端)发送通知。

③ 自动故障转移

断开master与slave连接,选取一个slave作为master,将其他slave连接到新的master,并告知客户端新的服务器地址

PS:哨兵也是一台redis服务器,只是不提供数据服务

哨兵的启动依赖于主从模式,所以须把主从模式安装好的情况下再去做哨兵模式,所有节点上都需要部署哨兵模式,哨兵模式会监控所有的redis工作节点是否正常,当

master出现问题的时候,因为其他节点与主节点失去联系,因此会投票,投票过半就认为这个master的确出现问题,然后会通知哨兵间,然后从slaves中选取一个作为新的

master,至少需要3个或3个以上服务器

环境
redis主从模式和哨兵模式配置
哨兵模式配置
所有服务器都要修改配置文件
[[email protected] ~]# 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秒)

启动哨兵模式
先启master,再启slave
[[email protected] ~]# redis-sentinel redis-5.0.7/sentinel.conf &
[1] 58571

[[email protected] ~]# redis-sentinel redis-5.0.7/sentinel.conf &
[1] 19812

[[email protected] ~]# redis-sentinel redis-5.0.7/sentinel.conf &
[1] 59917

查看哨兵信息
master查看
2 [[email protected] ~]# redis-cli -h 20.0.0.10 -p 26379 info Sentinel
3 # Sentinel
4 sentinel_masters:1
5 sentinel_tilt:0
6 sentinel_running_scripts:0
7 sentinel_scripts_queue_length:0
8 sentinel_simulate_failure_flags:0
9 master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3
10
11 slave1查看
12 [[email protected] ~]# redis-cli -h 20.0.0.20 -p 26379 info Sentinel
13 # Sentinel
14 sentinel_masters:1
15 sentinel_tilt:0
16 sentinel_running_scripts:0
17 sentinel_scripts_queue_length:0
18 sentinel_simulate_failure_flags:0
19 master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3
20
21 slave2上查看
22 [[email protected] ~]# redis-cli -h 20.0.0.20 -p 26379 info Sentinel
23 # Sentinel
24 sentinel_masters:1
25 sentinel_tilt:0
26 sentinel_running_scripts:0
27 sentinel_scripts_queue_length:0
28 sentinel_simulate_failure_flags:0
29 master0:name=mymaster,status=ok,address=20.0.0.10:6379,slaves=2,sentinels=3

故障模拟
1 查看master进程号
2 [[email protected] ~]# ps -ef | grep redis
3 root 57685 1 0 10:51 ? 00:00:06 /usr/local/bin/redis-server 20.0.0.10:6379
4 root 58567 1 0 12:07 ? 00:00:01 redis-sentinel *:26379 [sentinel]
5 root 58649 58470 0 12:15 pts/1 00:00:00 grep --color=auto redis
6
7 杀死master上redis-server的进程号
8 [[email protected] ~]# kill -9 57685 #master上redis-server的进程号

验证结果
1 [[email protected] ~]# tail -f /var/log/sentinel.log
2 58567:X 11 Nov 2020 12:18:14.388 # +failover-state-reconf-slaves master mymaster 20.0.0.10 6379
3 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
4 58567:X 11 Nov 2020 12:18:14.776 # -odown master mymaster 20.0.0.10 6379
5 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
6 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
7 58567:X 11 Nov 2020 12:18:15.483 # +failover-end master mymaster 20.0.0.10 6379
8 58567:X 11 Nov 2020 12:18:15.483 # +switch-master mymaster 20.0.0.10 6379 20.0.0.30 6379 #从master转到了slave2上
9 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
10 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
11 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
12
13 [[email protected] ~]# redis-cli -p 26379 INFO Sentinel
14 # Sentinel
15 sentinel_masters:1
16 sentinel_tilt:0
17 sentinel_running_scripts:0
18 sentinel_scripts_queue_length:0
19 sentinel_simulate_failure_flags:0
20 master0:name=mymaster,status=ok,address=20.0.0.30:6379,slaves=2,sentinels=3