redis 哨兵机制

之前主从配置是整到多台电脑上:

配置主从复制模式,克隆一个主机,一个主机是10,另外一个主机是11,一主一从,也可以一主多从。

这次的主从复制是在同一台电脑上,开启多个服务。redis-server 指向不同的配置文件,配置文件改端口号。

192.168.25.10 主机,端口号设置为 7001,

主机和备机会相互通信,同步数据。

我们设计如下四台机器的主从复制模式:

进入

[root@localhost /]# cd /opt/redis-5.0.4    # 进入redis的安装目录
[root@localhost redis-5.0.4]# ls           #查看redis目录下的文件
00-RELEASENOTES  BUGS          COPYING  dump.rdb  Makefile   README.md   runtest          runtest-sentinel  src    utils
appendonly.aof   CONTRIBUTING  deps     INSTALL   MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests  zhucong
[root@localhost redis-5.0.4]# mkdir zhucong    #创建目录zhucong
[root@localhost redis-5.0.4]# cd zhucong
[root@localhost zhucong]# mkdir 7001 7002 7003 7004   #在zhucong里创建目录7001 7002 7003 7004
[root@localhost zhucong]# ls  #查看目录下的文件信息
7001  7002  7003  7004
### 拷贝redis.conf配置文件到7001,7002,7003,7004目录下
[root@localhost zhucong]# cp ../redis.conf 7001/redis.conf
[root@localhost zhucong]# cp ../redis.conf 7002/redis.conf
[root@localhost zhucong]# cp ../redis.conf 7003/redis.conf
[root@localhost zhucong]# cp ../redis.conf 7004/redis.conf

把7001作为主机

[root@localhost zhucong]# vim 7001/redis.conf

修改7001/redis.conf的端口号

搜索并替换

进入底行模式:

:%s/6379/7001/g  #g (global) 全局的,就是每一行都替换

关闭保护模式

开启AOF

bind ip改为局域网的ip

然后按esc键,:wq 保存并退出

7002作为备机

[root@localhost zhucong]# vim 7002/redis.conf

底行模式修改端口号

:%s/6379/7002/g

shift+g 定位到最底行,按 o 键插入编辑

slaveof 192.168.25.10 7001

vi命令 yy 复制这一行,后面配置其他的redis。 补充:如果是复制3行,就是3yy

按es键,:wq 保存退出

7003作为备机。

[root@localhost zhucong]# vim 7003/redis.conf

进入底行模式,修改端口号

:%s/6379/7003/g

shift + g 定位到最后一行, 按 o 键插入编辑。

7004作为备机

[root@localhost zhucong]# vim 7004/redis.conf

进入底行模式,修改端口号

:%s/6379/7004/g

shift + g 定位到最后一行,按 o 插入编辑。

slaveof 192.168.25.10 7001

这4个文件就准备好了。

一般先启动主机服务, 后启动备机服务。

[root@localhost zhucong]# redis-server 7001/redis.conf
1790:C 19 Mar 2021 02:35:57.019 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1790:C 19 Mar 2021 02:35:57.019 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1790, just started
1790:C 19 Mar 2021 02:35:57.019 # Configuration loaded
[root@localhost zhucong]# redis-server 7002/redis.conf
1795:C 19 Mar 2021 02:37:00.359 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1795:C 19 Mar 2021 02:37:00.359 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1795, just started
1795:C 19 Mar 2021 02:37:00.359 # Configuration loaded
[root@localhost zhucong]# redis-server 7003/redis.conf
1802:C 19 Mar 2021 02:37:07.648 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1802:C 19 Mar 2021 02:37:07.648 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1802, just started
1802:C 19 Mar 2021 02:37:07.648 # Configuration loaded
[root@localhost zhucong]# redis-server 7004/redis.conf
1807:C 19 Mar 2021 02:37:15.577 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1807:C 19 Mar 2021 02:37:15.577 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1807, just started
1807:C 19 Mar 2021 02:37:15.577 # Configuration loaded
[root@localhost zhucong]#

查看下进程,7001 , 7002 , 7003, 7004 的服务是否开启

ps -ef|grep redis

测试是否成功,可以先测试一下主机是否能往里面写东西。或者看下redis产生的日志,最好配置一下日志。

[root@localhost zhucong]# vim 7001/redis.conf

搜下 logfile, /logfile

logfile “” : 默认是没有产生日志。

可以写个文件名,日志会被放到这个文件里面。

loglevel 日志的级别,默认是 notic 通知,级别越往上越详细,记的东西越多。

真正的生产环境记多了不好,影响效率。所以logfile 默认为空。

这里先不配置。:wq 保存退出。

启动7001的客户端。

[root@localhost zhucong]# redis-cli -h 192.168.25.10 -p 7001

看下有没有东西

192.168.25.10:7001> keys *
(empty list or set)  # 因为是新建的,里面没有东西

设置值

192.168.25.10:7001> set name helloworld
OK

获取值

192.168.25.10:7001> get name
"helloworld"

退出 quit

192.168.25.10:7001> quit
[root@localhost zhucong]#

启动7002的客户端

[root@localhost zhucong]# redis-cli -h 192.168.25.10 -p 7002

获取值,看下是否能获取到7001设置的值

192.168.25.10:7002> get name
"helloworld"

备机不能写东西

提示报错信息: 不能在只读的备份机上进行写入数据

这样主从配置就配置完了。

主从复制集群模式有问题,有风险。如果备机挂了,集群不会失去作用,因为还有其他备机,或者主机都可以读数据,如果主机挂了,则整个集群就没有写入数据的能力了,只能读不能写。

这从专业角度来说也就是主机有单点故障的问题。怎么解决这个问题,我们可以使用redis 提供的哨兵 模式:

举个例子:就跟上战场打仗一样,备机就相当于在战场上冲锋的士兵,主机相当于将军,如果将军挂了,可以采取什么机制,不致于让团队群龙无首,士兵可以顶上去,效率高的当将军。需要有监控的模式,配置哨兵,还需要配置机器。哨兵1和哨兵2需要监控主机和备机的状态,隔段时间来监控,投票推举当将军(主机)。哨兵1 和 哨兵2也是需要相互通信的。哨兵认为主机挂了,让士兵讨论投票推举将军,少数服从多数。哨兵模式中可以设置阈值,超过多少个主机,认为主机挂了。

redis已经安装过哨兵的服务,只需要启动这个服务就可以了。

启动哨兵模式

配置哨兵配置文件

[root@localhost redis-5.0.4]# vim sentinel.conf

哨兵模式配置参数的含义

属性说明
port哨兵模式的端口号, 默认是6379
daemonize是否以后台服务启动的方式启动,守护线程,默认是 no,也就是不后台启动。
pidfifile进程id的文件,哨兵启动后,会自动创建pid文件,记录哨兵启动的进程号。下次启动后,读取进程文件的进程号。
logfifile日志文件的位置和名称,默认是不产生日志文件的, logfifile 如果要配置 必须写绝对路径 ,比如 logfile “/26379_log”
dir哨兵模式的工作临时目录, 默认是 /tmp
sentinel monitor mymaster 127.0.0.1 6379 2配置哨兵模式监控哪个redis 主从复制的集群,第一个参数mymaster 是监控的集群的自定义名称 127.0.0.1 主从集群中主机的ip地址 6379 是主机的端 口号, 2 指 几个哨兵认为主机挂了 他就真的挂了
sentinel parallel-syncs mymaster 1指如果主机挂了之后,选一个新的备机当主机 , 备机当选为主机后,其他的备机要从新的主机上同步数据这个参数就是配置同步数据的时候开启几个同步数据的服务
sentinel failover-timeout mymaster 180000故障转移的超时时间 默认是3分钟
sentinel down-after-milliseconds mymaster 30000配置当前哨兵什么条件才会认为 主机不可用了 30000 是30秒,也就是如果哨兵3分钟连不上主机, 哨兵就主观的认为主机已经挂了

主观掉线 客观掉线

补充:

哨兵认为,主机已经挂了,会进行故障转移,通知主机挂了去修,采用一种机制,能在故障转移的时候,能给你发个通知。如果出现故障转移,它会notify.sh脚本,比如发邮件,给手机发信息,知道主机挂了要去修。默认是不让改脚本的。

下面开始配置哨兵模式

[root@localhost redis-5.0.4]# ls     #查看文件
00-RELEASENOTES  BUGS          COPYING  dump.rdb  Makefile   README.md   runtest          runtest-sentinel  src    utils
appendonly.aof   CONTRIBUTING  deps     INSTALL   MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests  zhucong
[root@localhost redis-5.0.4]# cd zhucong  #进入zhucong目录
[root@localhost zhucong]# ls      # 查看文件
7001  7002  7003  7004  appendonly.aof  dump.rdb
[root@localhost zhucong]# mkdir shaobing  #创建哨兵的目录
[root@localhost zhucong]# ls     # 查看文件
7001  7002  7003  7004  appendonly.aof  dump.rdb  shaobing
[root@localhost zhucong]# cd shaobing             #进入shaobing目录
[root@localhost shaobing]# mkdir 26379 26380      #创建目录 26739 26380
[root@localhost shaobing]# ls                     #查看文件
26379  26380
[root@localhost shaobing]#

拷贝sentinel.conf到 26739和26380里

[root@localhost shaobing]# cp ../../sentinel.conf 26379/sentinel.conf
[root@localhost shaobing]# cp ../../sentinel.conf 26380/sentinel.conf
[root@localhost shaobing]# vim 26379/sentinel.conf 

在这里插入图片描述
在这里插入图片描述

[root@localhost shaobing]# pwd
/opt/redis-5.0.4/zhucong/shaobing
[root@localhost shaobing]# vim 26380/sentinel.conf

在这里插入图片描述
在这里插入图片描述

修改过两个配置文件后,要启动哨兵模式:

[root@localhost shaobing]# redis-sentinel 26379/sentinel.conf
[root@localhost shaobing]# redis-sentinel 26380/sentinel.conf

动态监控日志的内容

[root@localhost shaobing]# tail -f log_26379

监控日志成功的效果图:

在这里插入图片描述

ctrl + c 打断

[root@localhost shaobing]# redis-cli -h 192.168.25.10 -p 7001
192.168.25.10:7001> set name zhangsan
OK
192.168.25.10:7001> get name
"zhangsan"
192.168.25.10:7001> quit
[root@localhost shaobing]# redis-cli -h 192.168.25.10 -p 7003
192.168.25.10:7003> get name
"zhangsan"
192.168.25.10:7003> set name 33333
(error) READONLY You can't write against a read only replica.
192.168.25.10:7003> quit

关掉主进程 7001

[root@localhost shaobing]# ps -ef|grep redis
root       1354      1  0 19:44 ?        00:00:12 redis-server 192.168.25.10:7001
root       1359      1  0 19:44 ?        00:00:10 redis-server 192.168.25.10:7002
root       1366      1  0 19:44 ?        00:00:10 redis-server 192.168.25.10:7003
root       1373      1  0 19:44 ?        00:00:11 redis-server 192.168.25.10:7004
root       1489      1  1 20:25 ?        00:00:04 redis-sentinel *:26379 [sentinel]
root       1494      1  1 20:25 ?        00:00:04 redis-sentinel *:26380 [sentinel]
root       1508   1311  0 20:30 pts/1    00:00:00 grep --color=auto redis
[root@localhost shaobing]# kill -9 1354
[root@localhost shaobing]# ps -ef|grep redis
root       1359      1  0 19:44 ?        00:00:11 redis-server 192.168.25.10:7002
root       1366      1  0 19:44 ?        00:00:11 redis-server 192.168.25.10:7003
root       1373      1  0 19:44 ?        00:00:11 redis-server 192.168.25.10:7004
root       1489      1  1 20:25 ?        00:00:05 redis-sentinel *:26379 [sentinel]
root       1494      1  1 20:25 ?        00:00:04 redis-sentinel *:26380 [sentinel]
root       1510   1311  0 20:31 pts/1    00:00:00 grep --color=auto redis
[root@localhost shaobing]# redis-cli -h 192.168.25.10 -p 7001
Could not connect to Redis at 192.168.25.10:7001: Connection refused
not connected> quit
[root@localhost shaobing]#

查看下哨兵的信息

此时主机为 7003

[root@localhost shaobing]# redis-cli -h 192.168.25.10 -p 7003
192.168.25.10:7003> set name dddfg
OK
192.168.25.10:7003> get name
"dddfg"
192.168.25.10:7003>

如果主机7001修好了,要重新当士兵。

[root@localhost shaobing]# redis-server ../7001/redis.conf
1524:C 19 Mar 2021 20:52:01.838 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1524:C 19 Mar 2021 20:52:01.838 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1524, just started
1524:C 19 Mar 2021 20:52:01.838 # Configuration loaded
[root@localhost shaobing]# ps -ef | grep redis
root       1359      1  0 19:44 ?        00:00:17 redis-server 192.168.25.10:7002
root       1366      1  0 19:44 ?        00:00:18 redis-server 192.168.25.10:7003
root       1373      1  0 19:44 ?        00:00:17 redis-server 192.168.25.10:7004
root       1489      1  1 20:25 ?        00:00:20 redis-sentinel *:26379 [sentinel]
root       1494      1  1 20:25 ?        00:00:21 redis-sentinel *:26380 [sentinel]
root       1525      1  0 20:52 ?        00:00:00 redis-server 192.168.25.10:7001
root       1532   1311  0 20:52 pts/1    00:00:00 grep --color=auto redis
[root@localhost shaobing]#

启动 7001

[root@localhost shaobing]# redis-cli -h 192.168.25.10 -p 7001
192.168.25.10:7001> get name
"dddfg"
192.168.25.10:7001> set name aaa
(error) READONLY You can't write against a read only replica.
192.168.25.10:7001> quit
[root@localhost shaobing]#

哨兵模式已经配置好了。

如果只启动一个哨兵,另外一个哨兵有问题或者没启动,主机一旦发生故障,主机不会进行故障转移。必须是2个哨兵。

哨兵模式一定程度上解决了单点故障问题。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值