replica-read-only yes 开启从机的只读模式
redis主从同步数据,可以通过网络直接传输rdb,也可以直接生成rdb到磁盘,然后磁盘读取到从机;
repl-backlog-size 1mb
主和从之间是会有一个增量复制,其实在redis,如果这是一个redis,然后它肯定是先通过rdb给过你一个全量的数据,然后且在他redis内部,它还维护一个小的消息队列,这个消息队列多大,你给出一个大小。 然后第一次的时候,这个文件如果发给了一个redis,redis会 load这个rdb对发送完、收完之后在短时间内比如他挂掉了,然后他又活了,活了之后,然后他肯定还想和别人同步,那么这个数据可能老数据了,他想要人家最全的数据,你可以让他重新落一遍,重新覆盖一遍,但是这样可能传几个g的数据,也可以拿着rdb里边的信息,然后以自己同步的一个数据的offset,然后去redis里的队列里边,你给出一个offset,给出一个偏移量,然后这个offset比如说8,你上次取得的8,人家到20了,那么你从8到20,给你传过就可以了,但是时候取决于队列大小。如果你设置的是1兆,然后你操作特别慢,变更的事特别慢,一年可能一兆都写不满,这事没问题,但是如果你如果写的很快,你就挂了三秒钟,那一秒钟的时候1兆写满了,那么数据会挤出,所以这里边,把8到12已经挤出去了,这时候也告诉你8、9取不到了,这时候要触发一个全量的落rdb的一个过程。
min-replicas-to-write 3 min-replicas-max-lag 10 就是最小给几个写成功。可以规定,最少写几个写成功这么一个数值。如果你关心数据的一个准确和一致性的时候,你是可以设置这件事情的。当然你看能不能给你注掉,所以这个时候是要跟你实际业务场景,你到底是不是要关心这件事情,因为如果开启他的话,就有点慢慢的追向强一致性那边去靠拢,是不是慢慢就在去靠拢了。
哨兵-sentinel
单点故障可以通过主从/主备解决,但是需要人工维护主的故障问题,此时可以用HA高可用哨兵模式解决。 1.监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。 2.提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。 3.自动故障迁移(Automatic failover):当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。 新建 26379.conf ,26380.conf ,26381.conf 文件
port 26379 //哨兵端口号 26380 26381
sentinel monitor mymaster 127.0.0.1 6379 //监控该ip端口号的redis实例
redis-server /root/test/26379.conf --sentinel
[root@iZuf6ir4og87e245nh5ltuZ ~]
28108:X 10 Jan 2022 21:48:33.355
28108:X 10 Jan 2022 21:48:33.355
28108:X 10 Jan 2022 21:48:33.355
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 28108
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
28108:X 10 Jan 2022 21:48:33.357
28108:X 10 Jan 2022 21:48:33.358
28108:X 10 Jan 2022 21:48:33.358
28108:X 10 Jan 2022 21:48:33.359 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379 6380跟随6379
28108:X 10 Jan 2022 21:48:33.360 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379 6381跟随6379
28108:X 10 Jan 2022 21:49:01.032 * +sentinel sentinel eafc0c81e7a958f1317b896d980f04d58b032528 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
28108:X 10 Jan 2022 21:49:18.668 * +sentinel sentinel ed7c6f838052f628a9373e661b2a5214dd505976 127.0.0.1 26381 @ mymaster 127.0.0.1 6379
28108:X 10 Jan 2022 21:54:54.497
28108:X 10 Jan 2022 21:54:54.566
28108:X 10 Jan 2022 21:54:54.568
28108:X 10 Jan 2022 21:54:54.597
28108:X 10 Jan 2022 21:54:54.597
28108:X 10 Jan 2022 21:54:55.507
28108:X 10 Jan 2022 21:54:55.507
28108:X 10 Jan 2022 21:54:55.507 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380 选举6380
28108:X 10 Jan 2022 21:54:55.507 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
28108:X 10 Jan 2022 21:55:25.509
[root@iZuf6ir4og87e245nh5ltuZ ~]
28112:X 10 Jan 2022 21:48:58.984
28112:X 10 Jan 2022 21:48:58.984
28112:X 10 Jan 2022 21:48:58.984
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26380
| `-._ `._ / _.-' | PID: 28112
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
28112:X 10 Jan 2022 21:48:58.986
28112:X 10 Jan 2022 21:48:58.988
28112:X 10 Jan 2022 21:48:58.988
28112:X 10 Jan 2022 21:48:58.988 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:48:58.990 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:48:59.827 * +sentinel sentinel 5e872f7f1f80229199314ea18e0963cbac4f6d48 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:49:18.668 * +sentinel sentinel ed7c6f838052f628a9373e661b2a5214dd505976 127.0.0.1 26381 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:54:54.473
28112:X 10 Jan 2022 21:54:54.563
28112:X 10 Jan 2022 21:54:54.563
28112:X 10 Jan 2022 21:54:54.563
28112:X 10 Jan 2022 21:54:54.565
28112:X 10 Jan 2022 21:54:54.568
28112:X 10 Jan 2022 21:54:54.568
28112:X 10 Jan 2022 21:54:54.641
28112:X 10 Jan 2022 21:54:54.641
28112:X 10 Jan 2022 21:54:54.742
28112:X 10 Jan 2022 21:54:54.742 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:54:54.797 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:54:55.442
28112:X 10 Jan 2022 21:54:55.442
28112:X 10 Jan 2022 21:54:55.506 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:54:55.729
28112:X 10 Jan 2022 21:54:56.444 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:54:56.444 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
28112:X 10 Jan 2022 21:54:56.521
28112:X 10 Jan 2022 21:54:56.521
28112:X 10 Jan 2022 21:54:56.521 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
28112:X 10 Jan 2022 21:54:56.521 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
28112:X 10 Jan 2022 21:55:26.538
[root@iZuf6ir4og87e245nh5ltuZ ~]# redis-server /root/test/26381.conf --sentinel
28116:X 10 Jan 2022 21:49:16.651 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28116:X 10 Jan 2022 21:49:16.651 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=28116, just started
28116:X 10 Jan 2022 21:49:16.651 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26381
| `-._ `._ / _.-' | PID: 28116
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http:
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
28116:X 10 Jan 2022 21:49:16.652 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
28116:X 10 Jan 2022 21:49:16.654 # Sentinel ID is ed7c6f838052f628a9373e661b2a5214dd505976
28116:X 10 Jan 2022 21:49:16.654 # +monitor master mymaster 127.0.0.1 6379 quorum 2
28116:X 10 Jan 2022 21:49:16.655 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
28116:X 10 Jan 2022 21:49:16.656 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
28116:X 10 Jan 2022 21:49:17.386 * +sentinel sentinel eafc0c81e7a958f1317b896d980f04d58b032528 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
28116:X 10 Jan 2022 21:49:18.103 * +sentinel sentinel 5e872f7f1f80229199314ea18e0963cbac4f6d48 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
28116:X 10 Jan 2022 21:54:54.419 # +sdown master mymaster 127.0.0.1 6379
28116:X 10 Jan 2022 21:54:54.566 # +new-epoch 1
28116:X 10 Jan 2022 21:54:54.568 # +vote-for-leader eafc0c81e7a958f1317b896d980f04d58b032528 1
28116:X 10 Jan 2022 21:54:55.507 # +config-update-from sentinel eafc0c81e7a958f1317b896d980f04d58b032528 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
28116:X 10 Jan 2022 21:54:55.507 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
28116:X 10 Jan 2022 21:54:55.507 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
28116:X 10 Jan 2022 21:54:55.507 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
28116:X 10 Jan 2022 21:55:25.540 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
6379断开6380成为新选举的主机
哨兵通过发布订阅知道主有几个从机;哨兵可以互相感知其余哨兵的存在
通过psubscribe * 查看发布订阅的通道
127.0.0.1:6379> PSUBSCRIBE *
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "*"
3) (integer) 1
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26379,5e872f7f1f80229199314ea18e0963cbac4f6d48,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26381,ed7c6f838052f628a9373e661b2a5214dd505976,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
3) "__sentinel__:hello"
4) "127.0.0.1,26380,eafc0c81e7a958f1317b896d980f04d58b032528,0,mymaster,127.0.0.1,6379,0"
1) "pmessage"
2) "*"
|