开发者学堂课程【Redis 入门到精通(进阶篇):哨兵-哨兵结构搭建】学习笔记与课程紧密联系,让用户快速学习知识
课程地址:https://developer.aliyun.com/learning/course/765/detail/13462
哨兵-哨兵结构搭建
目录
一,配置哨兵
二,操作哨兵模式
本章主要讲解启用哨兵模式的操作
一,配置哨兵
1.配置一拖二的主从结构
2.配置三个哨兵(配置相同,端口不同)
参看 sentinel.conf
3.启动哨兵
redis-sentinel sentinel-端口号 conf
二,操作哨兵模式
步骤
1.为了便于操作,建一些个窗口,哨兵1,2,3用来专门看它的服务启动。日志会在这里呈现。master 和 slave 三个窗口,之后后边,起两个这个哨兵的客户端,其实起一个也是可以的。到时候让大家看一下方便的切换,前面的 master,连接 master 客户端。同理,前面的 slave 连接 slave 客户端,也就后面这四个是客户端。
2.先做准备工作,先把前面的配置信息,处理一下。进入到 redis 的安装目录下,在这里边可以看到有一个配置文件,用的是 redis.conf 还有 sentinel.conf,这就是哨兵的配置文件。
以下图为讲解步骤2的流程图,可供参考:
3.这时可以去查看一下,输入 cat sentinel.conf。输入和后,里边的信息比较多,需要去点一些。这些配置,是并不陌生的。
①.第一行,哨兵本身也是一个服务,所以对外也有一个服务端口,通常是用6379前面再加一个2,用来做他的端口。
②.第二行,对应的哨兵的一些工作信息存储时,把目录挂上。
③.第三行,这是来设置哨兵的一个核心工作,因为哨兵在这里主要工作就是出问题后的解决,也就是说你监控的对象是谁,就是谁。第三行中的2是判断对象是不是挂的一个标准,也就是说,如果设了一个2,两个哨兵认定它挂就是挂的。通常设定哨兵的数量的一半加一,这就是为什么我们哨兵数量要设单数的原因。
④.第四行,down-after millions seconds,是说相应的对象,连接他多长时间没响应,认定它挂,这个名称记得和上面两个对应上就可以,mymaster 是主机的一个master的名称,后面3万是30秒。
⑤.第五行,当他挂着的时候,会有新的来进行上任,新的上任之后再进行数据同步时,值越小,对服务器的性能压力也就越小,设的越大,压力越大。与之对应的,压力越小,速度越慢,压力越大,速度越快。这就根据计算机的性能来看。也就是有几条线开始同时数据同步。
⑥.第六行,进行同步,如果中间特别慢,它是不能接受的。多长时间同步没完成就算超时,同步超时的一个设定在这里,是以毫秒单位。也就是180秒三分钟。30秒认定。主机下线三分钟,认定同步超时。
以下代码为所讲解的代码:
[root@localhost redis-4.0.0]# cat sentine1.conf l grep-v "#" | grep -v "^$" port 26379
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-mi11iseconds mymaster 30000
sentinel paralle1-syncs mymasterl
sentinel failover-timeout mymaster 180000
4.之后把它复制以后进行拷贝。放到/conf/sentinel-26379.conf。之后是需要修改的,输入 vim sentinal,进入之后,修改以下目录,修改为 redis-4.0.0/data,作为数据保存的目录。下面的内容是不需要再次进行修改的。
以下为所讲解的代码:
port 26379
dir /redis-4.0.0/data
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
5.退出之后快速复制两个,把26379改成26380之后结束。之后再把26379改成26381.
以下为讲解的运行后的代码
[root@localhost conf] # sed's/26379/26380/g' sentinal/26379/26380/g’sentinel-26380.conf
[root@localhost conf]#sed's/26379/26381/g' sentinal/26379/26381/g’sentinel-26381.conf
[root@localhost conf]# cat sentine1-26380.conf
port 26380
dir /redis-4.0.0/data
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-mi1liseconds mymaster 30000 sentine7 paralle1-syncs mymaster 1
sentine1 failover timeout mymaster 180000
[root@localhost conf]#
6.搭完哨兵之后需要把主从搭建起来,,先用原来的6379和6380就行,然后再去做一个6381。之后可以看一下运行情况。
[root@localhost conf]# cat redis-6379.conf
port 6379
daemonize no
#1ogfile "6379.10g"
dir /redis-4.0.0/data dbfilenamedump-6379.rdb rdbcompression yes rdbchecksum yes
save 10 2
appendonly yes
appendfsync always
appendfilename appendon1y-6379.aof
bind 127.0.0.1
databases 16
再去看一下6380的运行情况:
#logfile"6380.1og”
dir /redis-4.0.0/data
slaveof 127.0.0.1 6379
[root@localhost conf]#
7.之后去数据文件夹把文件给清理掉,运行过程为
[root@localhost conf]# cd/
[root@localhost /]# cd data
bash: cd: data: No such file or directory
[root@localhost /]# cd redis-4.0.0/
[root@localhost redis-4.0.0]# cd data
[root@localhost data]# 11
total 68
-rw-r--r--. root root . 56446 Oct 14 05:59 6379.log
-rw-r--r-- root root 3910 0ct 14 06:30 appendonTy-6379.aof
-rw-r--r-- root root 327 Oct 14 08:10 dump-6379.rdb
-rw-r--r-- 1 root root 344 oct 14 07:09 dump.rdb
[root@localhost data]# rm -rf.8
[root@localhost data]# rm -rf *
[root@localhost data]#.11
total 0
[root@localhost data]# cd
[root@localhost redis-4.0.0]# cd conf
[root@localhost .conf]# 11
total 24
8.准备工作结束之后,接下来,进行启动。启动的顺序为,先起主机,再起丛机,最后再起哨兵。哨兵的启动与服务器启动,稍有不同。
以下为哨兵启动后的状态:
24464:x14 oct11:54:39.369#
WARNING: The TCP backlog setting of 511 cannot be enforced because/proc/sys/net/core/somaxconn is set o the lower value of 128.
9.之后就可以连一下客户端,但是需要注意的一点是 get,set 操作是不被允许的。这里只允许哨兵的操作。以下为操作结果
# Sentinel
sentinel_masters:1 sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_1ength:0 sentine1_simulate_failure_Flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves-2,sentinels=l127.0.0.1:26379>
10.在哨兵客户端里看配置文件,以下是所看的文件
[root@locathost /]# cat /redis-4.0.0/conf/sentine1-26379.cont port 26379
dir "/redis-4.0.0/data"
sentinel myid 0249e5c2fdde04cbe1059aala137b9b352f0f7fo sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster o
sentinel 1eader-epoch mymaster o
# Generated by CONFIG REWRITE
sentinel sentinel knowneslave mymaster 133:8:8:1 8389
sentinel current-epoch o[rootalocalhost /]#
11.再起第二个哨兵,输入
# redis-sentinel /redis-4.0.0/conf/sentine1-26380.conf启动,运行结果为:
24584:x14 11:58:18.671
# WARNING: The TCP back1og setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
24584:x 14 oct 11:58:18.674 # Sentinel id is 1be01b18c639a586280b5467fe9706c435e56fd9
24584:x 14 oct 11:58:18.674 # +monitor master mymaster 127.0.0.1 6379 quorum 2
24584:x14
oct11:58:19.542*+sentine1
sentine7 0249e5c2fdde04cbe1059aala137b9b352f0f7f0 127.0.0.1 26379 0.mymaster 127.0.0.1 6379
这里的最后一行可以发现添加了哨兵,这里也是与哨兵客户端有所联系的,这里它们是有所识别的。
12.接着来看数据,首先先看主从是否吧工作正常。
首先现在主输入
[rootalocalhost /]# redis-c1i -p 6379
127.0.0.1:6379> set name it [ex seconds] [px mi11iseconds] [nx|xx]
之后在从输入
[root@localhost /]# redis-cli -p 6380
127.0.0.1:6380> get name .
"itheima”
127.0.0.1:6380>
结果是可以得到的,也就是说明主从没有问题。
哨兵在这里的作用是当把主宕掉之后,经过一系列的操作与投票,可以看到由6379替换成诶6381.