人的一生,有许多事情,是需要放在心里慢慢回味的,过去的就莫要追悔,一切向前看吧 任何打击都不足以成为你堕落的借口,即使你改变不了这个世界,你却依然可以改变自己,选择条正确的路永远走下去。
返回顶部

快来学习Redis的主从模式和哨兵模式部署

一、Redis主从模式配置

1.1、案例拓扑图

 

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(从)

1.3、Redis安装

所有服务器上安装,在master上演示

 1 [root@master ~]# tar zxf redis-5.0.7.tar.gz
 2 [root@master ~]# cd redis-5.0.7/
 3 [root@master redis-5.0.7]# make -j2
 4 [root@master redis-5.0.7]# make PREFIX=/usr/local/redis install
 5 [root@master redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
 6 [root@master redis-5.0.7]# cd utils/
 7 [root@master 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!

1.4、修改配置文件

1.4.1、master配置文件修改

 1 [root@master 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 [root@master utils]# /etc/init.d/redis_6379 restart      #开启Redis
14 
15 [root@master utils]# netstat -anpt | grep redis
16 tcp        0      0 20.0.0.10:6379          0.0.0.0:*               LISTEN      57685/redis-server  

1.4.2、slave1配置文件修改

 1 [root@slave1 src]# vim /etc/redis/6379.conf
 2 #69行 修改监听地址为20.0.0.30(在实验环境使用),现网环境建议绑定从服务器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 [root@slave1 utils]# /etc/init.d/redis_6379 restart      #开启Redis
16 
17 [root@slave1 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  

1.4.2、slave2配置文件修改

 1 [root@slave2 src]# vim /etc/redis/6379.conf
 2 #69行 修改监听地址为20.0.0.30(在实验环境使用),现网环境建议绑定从服务器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 [root@slave2 utils]# /etc/init.d/redis_6379 restart      #开启Redis
16 
17 [root@slave2 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  

1.5、测试效果

1.5.1、验证主从效果(master上看日志)

 1 [root@master 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

1.5.2、master上验证从节点

1 [root@master 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

1.5.3、插入数据验证

 1 master上插入数据
 2 20.0.0.10:6379> set name lisi
 3 OK
 4 20.0.0.10:6379> set score 80
 5 OK
 6 20.0.0.10:6379> keys *
 7 1) "score"
 8 2) "name"
 9 20.0.0.10:6379> get name
10 "lisi"
11 20.0.0.10:6379> get score
12 "80"
13 
14 slave1上查看
15 [root@slave1 utils]# redis-cli -h 20.0.0.20 -p 6379
16 20.0.0.20:6379> keys *
17 1) "score"
18 2) "name"
19 20.0.0.20:6379> get name 
20 "lisi"
21 20.0.0.20:6379> get score
22 "80"
23 
24 slave2上查看
25 [root@slave2 utils]# redis-cli -h 20.0.0.30 -p 6379
26 20.0.0.30:6379> keys *
27 1) "score"
28 2) "name"
29 20.0.0.30:6379> get name 
30 "lisi"
31 20.0.0.30:6379> get score
32 "80"

二、Redis哨兵模式

2.1、Redis sentinel介绍

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

2.2、哨兵模式的作用

① 监控

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

② 通知(提醒)

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

③ 自动故障转移

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

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

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

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

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

2.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

2.4、哨兵模式配置

所有节点都需要修改

1 [root@master ~]# vi redis-5.0.7/sentinel.conf
2 17行/protected-mode no                                  #关闭保护模式
3 26行/daemonize yes                                      #指定sentinel为后台启动
4 36行/logfile "/var/log/sentinel.log"                    #指定日志存放路径
5 65行/dir "/var/lib/redis/6379"                          #指定数据库存放路径
6 84行/sentinel monitor mymaster 20.0.0.10 6379 2         #至少几个哨兵检测到主服务器故障了,才会进行故障迁移,全部指向masterIP
7 113行/sentinel down-after-milliseconds mymaster 30000    #判定服务器down掉的时间周期,默认30000毫秒(30秒)
8 146行/sentinel failover-timeout mymaster 180000         #故障节的的最大超时时间为180000(180秒)

2.5、启动哨兵模式

先启master,再启slave

1 [root@master ~]# redis-sentinel redis-5.0.7/sentinel.conf &
2 [1] 58571
3 
4 [root@slave1 ~]# redis-sentinel redis-5.0.7/sentinel.conf &
5 [1] 19812
6 
7 [root@slave2 ~]# redis-sentinel redis-5.0.7/sentinel.conf &
8 [1] 59917

2.6、查看哨兵信息

 1 master查看
 2 [root@master ~]# 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 [root@slave1 ~]# 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 [root@slave2 ~]# 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

2.7、故障模拟

2.7.1、

1 查看master进程号
2 [root@master ~]# 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 [root@master ~]# kill -9 57685     #master上redis-server的进程号

2.7.2、验证结果

 1 [root@master ~]# 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 [root@master ~]# 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
posted @ 2020-11-11 10:21  yy1299050947  阅读(309)  评论(0编辑  收藏  举报