目录
本篇文章专注于讲解redis在windows环境下使用Redis Sentinel(哨兵)实现高可用方案。
想要在windows环境下实现redis高可用,先要准备以下材料或知识点。
下载的压缩文件内容如下图所示。
具体结构如下图所示
应用层连接redis集群,集群包括3个redis服务(1主2从)和3个哨兵。
哨兵用于监控主redis可用性。若长时间不可用,则哨兵进行投票选举出新的主redis。原来的主redis降级为从redis。哨兵会将所有redis的配置进行自动更新。
将redis复制一份。一个文件夹用于部署redis服务,一个文件夹用于部署哨兵。
规定redis服务端口为28380~28382。将redis.windiws-service.config改名为redis-28380.conf。打开进行修改。
bind 127.0.0.1
,默认为监听所有ip。protected-mode yes
为protected-mode no
,默认redis开启了保护模式,只允许本机通过127.0.0.1访问,其他ip无法访问。port 6379
为port 28380
loglevel debug
为loglevel notice
logfile "server_log.txt"
为logfile "server_log_28380.txt"
syslog-ident redis
为syslog-ident "redis-28380"
修改不保存rdb文件
注释掉一下三行,由于rdb会每次全量写文件,当数据量较大时会对redis稳定性造成一定影响,尤其是rbd保存失败时会一定时间内拒绝写入数据。
save 900 1
save 300 10
save 60 10000
创建密码
修改# requirepass foobared
为requirepass "test1"
新增masterauth "test1"
若要设置密码,单台redis设置
requirepass
即可。集群redis必须同时设置requirepass
和masterauth
。若主redis没有设置masterauth
当它变为从时,就认为主无密码。若从redis没有设置requirepass
,当它变为主时,会没有密码。以上两种情况自动切换都无法自动连接到新的主。
启用aof保存
修改appendonly no
为appendonly yes
修改appendfilename "appendonly.aof"
为appendfilename "appendonly_28380.aof"
aof写入的是redis命令,每次向文件后面添加,因此对IO性能影响较小。最坏情况主redis丢失1s的数据。
配置改好后保存,这个当作主redis的配置。将改配置复制2份,分别改名为redis-28381.conf和redis-28382.conf
port 28380
为port 28381
logfile "server_log-28380.txt"
为logfile "server_log_28381.txt"
syslog-ident "redis-28380"
为syslog-ident "redis-28381"
修改从库对应的主库ip
修改# slaveof
为slaveof 127.0.0.1 28380
23880做为主库,填写的就是28380的端口
修改从库竞选主库优先级
修改slave-priority 100
为slave-priority 99
将每个从库设置为不通的优先级,数字较低的优先被竞选为主库。
修改aof保存的文件名
修改appendfilename "appendonly_28380.aof"
为appendfilename "appendonly_28381.aof"
port 28380
为port 28382
logfile "server_log-28380.txt"
为logfile "server_log_28382.txt"
syslog-ident "redis-28380"
为syslog-ident "redis-28382"
修改从库对应的主库ip
修改# slaveof
为slaveof 127.0.0.1 28380
23880做为主库,填写的就是28380的端口
修改从库竞选主库优先级
修改slave-priority 100
为slave-priority 98
将每个从库设置为不通的优先级,数字较低的优先被竞选为主库。
修改aof保存的文件名
修改appendfilename "appendonly_28380.aof"
为appendfilename "appendonly_28382.aof"
通过命令redis-server --service-install 配置名 --service-name 服务名
将redis安装为服务。
先cd到redis所在目录,在cmd命令行中输入一下三条语句将三个redis安装成功为windows服务。
redis-server --service-install redis-28380.conf --service-name redis-28380
redis-server --service-install redis-28381.conf --service-name redis-28381
redis-server --service-install redis-28382.conf --service-name redis-28382
设置服务别名是为了在服务中能更好的看到哪个是哪个服务,否则默认都是redis服务。
可以直接在服务中右键启动或者通过命令redis-server --service-start --service-name 服务名
启动指定服务。
通过redis-cli 连接到各个redis服务。
F:\Study\redis> redis-cli -p 28380 -a test1
127.0.0.1:28380> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=28382,state=online,offset=183,lag=1
slave1:ip=127.0.0.1,port=28381,state=online,offset=183,lag=1
master_repl_offset:183
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:182
可以看到我们的主从配置已经生效了。
连接到从库28381
F:\Study\redis> redis-cli -p 28381 -a test1
127.0.0.1:28381> info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:28380
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:323
slave_priority:99
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
连接到从库28382
F:\Study\redis> redis-cli -p 28382 -a test1
127.0.0.1:28382> info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:28380
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:379
slave_priority:99
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
现在我们往主库写入数据会自动并主动同步到从库。
配置中默认配了以下配置了
slave-read-only yes
,从库是只读的,不允许写入。
设置三个哨兵端口为 24381~24383
配置哨兵24381
dir ./
logfile "sentinel.28481.txt"
port 28481
sentinel monitor master 127.0.0.1 28380 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 30000
sentinel auth-pass 127.0.0.1 28380 test1
sentinel monitor master 127.0.0.1 28380 2
设置主redis的别名为master,后面都通过master表示主redis。后面的2表示2个哨兵检测到主redis挂了即为挂了,需要重新选举出新的主redis,而原来的主redis降级为从redis。sentinel down-after-milliseconds master 5000
哨兵多久连不上主redis即认为它挂了sentinel failover-timeout master 30000
投票选举超时时间,超过时间没有选出则该轮投票失败。
sentinel auth-pass master test1
主redis的密码 。
由于哨兵只能设置主的密码,因此主从的密码应该设置为一样的。
配置哨兵24382
dir ./
logfile "sentinel.28482.txt"
port 28482
sentinel monitor master 127.0.0.1 28380 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 30000
sentinel auth-pass 127.0.0.1 28380 test1
配置哨兵24383
dir ./
logfile "sentinel.28483.txt"
port 28483
sentinel monitor master 127.0.0.1 28380 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 30000
sentinel auth-pass 127.0.0.1 28380 test1
通过命令redis-server.exe 配置 --sentinel
启动哨兵
redis-server redis-sentinel-28481.conf --sentinel
redis-server redis-sentinel-28482.conf --sentinel
redis-server redis-sentinel-28483.conf --sentinel
启动哨兵28481后查看日志
[8464] 21 Nov 02:02:05.258 # +tilt #tilt mode entered
[8464] 21 Nov 08:33:01.513 # +tilt #tilt mode entered
[8464] 21 Nov 08:33:31.561 # -tilt #tilt mode exited_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 28481
| `-._ `._ / _.-' | PID: 22332
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[22332] 05 Jan 12:01:48.399 # Sentinel ID is 48693b2911456ef3da6ebfd14d1adf46e76fbb8b
[22332] 05 Jan 12:01:48.399 # +monitor master master 127.0.0.1 28382 quorum 2
[22332] 05 Jan 12:01:53.399 # +sdown sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28382
[22332] 05 Jan 12:01:53.399 # +sdown sentinel 162c66f370550a9926b794abecfb431cf3f8bcc9 127.0.0.1 28483 @ master 127.0.0.1 28382
[22332] 05 Jan 12:02:13.404 # +sdown master master 127.0.0.1 28382
同时启动完会自动在哨兵的配置文件中生成一些配置,完整的redis-sentinel-28481.conf配置如下
dir "F:\\Study\\redis\\redis-sentinel"
logfile "sentinel.28481.txt"
port 28481
sentinel myid 48693b2911456ef3da6ebfd14d1adf46e76fbb8b
sentinel monitor master 127.0.0.1 28381 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 30000
# Generated by CONFIG REWRITE
sentinel auth-pass master test1
sentinel config-epoch master 11
sentinel leader-epoch master 11
sentinel known-slave master 127.0.0.1 28382
sentinel known-slave master 127.0.0.1 28380
sentinel known-sentinel master 127.0.0.1 28482 ce50397a76e2e3ca165ce407859cbc94d7caf504
sentinel known-sentinel master 127.0.0.1 28483 162c66f370550a9926b794abecfb431cf3f8bcc9
sentinel current-epoch 11
启动后会为哨兵生成一个id,同时会记录一些关键信息。
将主redis关闭,让哨兵自动切换主从。
哨兵24381日志
[22332] 05 Jan 12:34:49.286 # +sdown master master 127.0.0.1 28380
[22332] 05 Jan 12:34:49.295 # +new-epoch 16
[22332] 05 Jan 12:34:49.296 # +vote-for-leader ce50397a76e2e3ca165ce407859cbc94d7caf504 16
[22332] 05 Jan 12:34:49.363 # +odown master master 127.0.0.1 28380 #quorum 3/2
[22332] 05 Jan 12:34:49.363 # Next failover delay: I will not start a failover before Sat Jan 05 12:35:49 2019
[22332] 05 Jan 12:34:50.397 # +config-update-from sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28380
[22332] 05 Jan 12:34:50.397 # +switch-master master 127.0.0.1 28380 127.0.0.1 28382
[22332] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28382
[22332] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
[22332] 05 Jan 12:34:55.442 # +sdown slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
哨兵24382日志
[8760] 05 Jan 12:34:49.237 # +sdown master master 127.0.0.1 28380
[8760] 05 Jan 12:34:49.292 # +odown master master 127.0.0.1 28380 #quorum 2/2
[8760] 05 Jan 12:34:49.292 # +new-epoch 16
[8760] 05 Jan 12:34:49.292 # +try-failover master master 127.0.0.1 28380
[8760] 05 Jan 12:34:49.293 # +vote-for-leader ce50397a76e2e3ca165ce407859cbc94d7caf504 16
[8760] 05 Jan 12:34:49.296 # 162c66f370550a9926b794abecfb431cf3f8bcc9 voted for ce50397a76e2e3ca165ce407859cbc94d7caf504 16
[8760] 05 Jan 12:34:49.296 # 48693b2911456ef3da6ebfd14d1adf46e76fbb8b voted for ce50397a76e2e3ca165ce407859cbc94d7caf504 16
[8760] 05 Jan 12:34:49.394 # +elected-leader master master 127.0.0.1 28380
[8760] 05 Jan 12:34:49.394 # +failover-state-select-slave master master 127.0.0.1 28380
[8760] 05 Jan 12:34:49.470 # +selected-slave slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:49.470 * +failover-state-send-slaveof-noone slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:49.541 * +failover-state-wait-promotion slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:50.341 # +promoted-slave slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:50.341 # +failover-state-reconf-slaves master master 127.0.0.1 28380
[8760] 05 Jan 12:34:50.396 * +slave-reconf-sent slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:51.380 * +slave-reconf-inprog slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:51.446 # -odown master master 127.0.0.1 28380
[8760] 05 Jan 12:34:52.393 * +slave-reconf-done slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28380
[8760] 05 Jan 12:34:52.468 # +failover-end master master 127.0.0.1 28380
[8760] 05 Jan 12:34:52.468 # +switch-master master 127.0.0.1 28380 127.0.0.1 28382
[8760] 05 Jan 12:34:52.469 * +slave slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28382
[8760] 05 Jan 12:34:52.469 * +slave slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
[8760] 05 Jan 12:34:57.562 # +sdown slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
哨兵24383日志
[26484] 05 Jan 12:34:49.161 # +sdown master master 127.0.0.1 28380
[26484] 05 Jan 12:34:49.294 # +new-epoch 16
[26484] 05 Jan 12:34:49.295 # +vote-for-leader ce50397a76e2e3ca165ce407859cbc94d7caf504 16
[26484] 05 Jan 12:34:50.284 # +odown master master 127.0.0.1 28380 #quorum 3/2
[26484] 05 Jan 12:34:50.284 # Next failover delay: I will not start a failover before Sat Jan 05 12:35:49 2019
[26484] 05 Jan 12:34:50.397 # +config-update-from sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28380
[26484] 05 Jan 12:34:50.397 # +switch-master master 127.0.0.1 28380 127.0.0.1 28382
[26484] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28382
[26484] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
[26484] 05 Jan 12:34:55.437 # +sdown slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
具体投票过程这里不做具体分析。
当选出新的主redis,哨兵会对redis配置进行更新,将主redis的slaveof XXX XXX
配置删除,将从的slaveof XXX XXX
设置为主的地址。同时哨兵会监控redis28380。当28380恢复后会将slaveof XXX XXX
添加到它的配置中。
连接到28382查看
127.0.0.1:28382> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=28381,state=online,offset=193307,lag=1
slave1:ip=127.0.0.1,port=28380,state=online,offset=193441,lag=1
master_repl_offset:193575
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:193574
若我们此时动态新增一个从redis,端口为28383,则复制一个28380的配置进行修改,将优先级改为97。并启动服务即可。
redis-28383日志如下
[14048] 05 Jan 12:55:20.421 * Redis 3.2.100 (00000000/0) 64 bit, standalone mode, port 28383, pid 14048 ready to start.
[14048] 05 Jan 12:55:20.422 # Server started, Redis version 3.2.100
[14048] 05 Jan 12:55:20.423 * The server is now ready to accept connections on port 28383
[14048] 05 Jan 12:55:20.424 * Connecting to MASTER 127.0.0.1:28382
[14048] 05 Jan 12:55:20.426 * MASTER <-> SLAVE sync started
[14048] 05 Jan 12:55:20.427 * Non blocking connect for SYNC fired the event.
[14048] 05 Jan 12:55:20.428 * Master replied to PING, replication can continue...
[14048] 05 Jan 12:55:20.428 * Partial resynchronization not possible (no cached master)
[14048] 05 Jan 12:55:20.432 * Full resync from master: 2c344529e4acdc44cd311818fe8179825877a9f4:241347
[14048] 05 Jan 12:55:20.653 * MASTER <-> SLAVE sync: receiving 107 bytes from master
[14048] 05 Jan 12:55:20.655 * MASTER <-> SLAVE sync: Flushing old data
[14048] 05 Jan 12:55:20.655 * MASTER <-> SLAVE sync: Loading DB in memory
[14048] 05 Jan 12:55:20.656 * MASTER <-> SLAVE sync: Finished with success
[14048] 05 Jan 12:55:20.660 * Background append only file rewriting started by pid 29392
[14048] 05 Jan 12:55:20.827 * AOF rewrite child asks to stop sending diffs.
[14048] 05 Jan 12:55:20.927 # fork operation complete
[14048] 05 Jan 12:55:20.928 * Background AOF rewrite terminated with success
[14048] 05 Jan 12:55:20.929 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
[14048] 05 Jan 12:55:20.931 * Background AOF rewrite finished successfully
再看下28382的主从连接信息
127.0.0.1:28382> info Replication
# Replication
role:master
connected_slaves:3
slave0:ip=127.0.0.1,port=28381,state=online,offset=268634,lag=0
slave1:ip=127.0.0.1,port=28380,state=online,offset=268768,lag=0
slave2:ip=127.0.0.1,port=28383,state=online,offset=268634,lag=0
master_repl_offset:268768
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:268767
哨兵也能检测到新的从库连接[22332] 05 Jan 12:55:25.420 * +slave slave 127.0.0.1:28383 127.0.0.1 28383 @ master 127.0.0.1 28382
通过上述配置,就完成了redis高可用方案。
我使用的是StackExchange.Redis
连接redis。
它本身就支持主从连接,在建立连接的时候输入多个连接地址接口。由于从库不允许写入。因此它能辨别出哪个是主哪个是从。主从切换后写入数据的时候重新判定哪个是主库。
ConfigurationOptions optiOns= ConfigurationOptions.Parse("password=test1");
options.EndPoints.Add("127.0.0.1", 28380);
options.EndPoints.Add("127.0.0.1", 28381);
options.EndPoints.Add("127.0.0.1", 28382);
ConfigurationOptions optiOns= ConfigurationOptions.Parse("127.0.0.1:28380,127.0.0.1:28381,127.0.0.1:28382,keepAlive=5,password=test1");
通过该篇文章详细的探究了window下的redis高可用方案如何实现。本文对具体配置没有做深入探究,仅仅为了抓住重点,具体配置其他的文档都介绍的比较详细,但是一些细节并没有说明,通过该片文章将reids高可用的坑都填满。若有错误,欢迎指正。