概述
Redis哨兵为Redis提供了高可用性。实际上这意味着你可以使用哨兵模式创建一个可以不用人为干预而应对各种故障的Redis部署。
哨兵模式还提供了其他的附加功能,如监控,通知,为客户端提供配置。
下面是在宏观层面上哨兵模式的功能列表:
监控:哨兵不断的检查master和slave是否正常的运行。
通知:当监控的某台Redis实例发生问题时,可以通过API通知系统管理员和其他的应用程序。
自动故障转移:如果一个master不正常运行了,哨兵可以启动一个故障转移进程,将一个slave升级成为master,其他的slave被重新配置使用新的master,并且应用程序使用Redis服务端通知的新地址。
配置提供者:哨兵作为Redis客户端发现的权威来源:客户端连接到哨兵请求当前可靠的master的地址。如果发生故障,哨兵将报告新地址。
IP划分:
10.0.0.140 redis-Master
10.0.0.141 redis-Slave1
10.0.0.142 redis-Slave2
拓扑图:
操作过程:
三台安装配置过程一样
1.安装依赖包
[root@redis-Master ~]# yum install gcc gcc-c++ -y
2.下载安装包,解压
[root@redis-Master ~]# cd /usr/local/src/ [root@redis-Master src]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz [root@redis-Master src]# tar -zxvf redis-4.0.1.tar.gz
3创建redis用户和目录
[root@redis-Master ~]# useradd redis [root@redis-Master ~]# mkdir /usr/local/redis
4.将解压的redis移动到刚创建的目录,并赋予权限
[root@redis-Master ~]# cd /usr/local/src/ [root@redis-Master src]# mv redis-4.0.1/* /usr/local/redis [root@redis-Master src]# chown -R redis:redis /usr/local/redis
5.编译
[root@redis-Master ~]# cd /usr/local/redis/ [root@redis-Master redis]# make MALLOC=libc [root@redis-Master redis]# make && make install
6.编辑配置文件
[root@redis-Master ~]# vim /usr/local/redis/redis.conf
daemonize yes #默认no,该参数用于定制redis服务是否以守护模式进行 protected-mode no #保护模式是否开启 bind 10.0.0.140 #本机ip logfile "/var/log/redis/redis.log" #日志存放位置 dir /usr/local/redis/rdb #数据存放位置 supervised systemd #systemd 是一个类似 supervise 的守护进程,用于管理需要后台运行的服务。
7.创建配置文件所需的目录,并重新赋权
[root@redis-Master ~]# mkdir /usr/local/redis/rdb [root@redis-Master ~]# chown -R redis:redis /usr/local/redis/ [root@redis-Master ~]# mkdir /var/log/redis [root@redis-Master ~]# chown -R redis:redis /var/log/redis
8.创建启动脚本,跟脚本执行权限
[root@redis-Master ~]# cat /usr/lib/systemd/system/redis.service
[Unit] Description=Redis persistent key-value database After=network.target [Service] User=redis Group=redis ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf --daemonize no ExecStop=/usr/local/redis/src/redis-cli -h 10.0.0.140 -p 6379 shutdown Restart=always [Install] WantedBy=multi-user.target
[root@redis-Master ~]# chmod 750 /usr/lib/systemd/system/redis.service
9.启动测试
[root@redis-Master ~]# systemctl enable redis.service
[root@redis-Master ~]# systemctl start redis.service
[root@redis-Master ~]# systemctl status redis.service
● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Active: active (running) since 一 2017-12-11 13:10:01 CST; 5s ago Main PID: 4514 (redis-server) CGroup: /system.slice/redis.service └─4514 /usr/local/redis/src/redis-server 10.0.0.140:6379 12月 11 13:10:01 redis-Master systemd[1]: Started Redis persistent key-value database. 12月 11 13:10:01 redis-Master systemd[1]: Starting Redis persistent key-value database...
三台都要配置哨兵
1.先配置主从(10.0.0.140是Master),另外两台为Slave,
Slave配置如下
[root@redis-Slave1 ~]# vim /usr/local/redis/redis.conf slaveof 10.0.0.140 6379
[root@redis-Slave2 ~]# vim /usr/local/redis/redis.conf slaveof 10.0.0.140 6379
2.配置哨兵(只改IP)
[root@redis-Master~]# vim /usr/local/redis/sentinel.conf
sentinel monitor mymaster 10.0.0.140 6379 2 #(只改IP) protected-mode no
配置完Redis和Sentinel之后,按顺序启动各个角色。启动顺序如下:
Master->Slave->Sentinel,要确保按照这个顺序依次启动。
Sentinel的启动命令和Redis类似。
测试:
[root@redis-Master ~]# cd /usr/local/redis/
[root@redis-Master redis]# redis-sentinel sentinel.conf
4559:X 11 Dec 13:26:34.859 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 4559:X 11 Dec 13:26:34.859 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=4559, just started 4559:X 11 Dec 13:26:34.859 # Configuration loaded 4559:X 11 Dec 13:26:34.860 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 4559 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 4559:X 11 Dec 13:26:34.862 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 4559:X 11 Dec 13:26:34.863 # Sentinel ID is 33890665f4d321bbf024646efdd169a16713e953 4559:X 11 Dec 13:26:34.863 # +monitor master mymaster 10.0.0.140 6379 quorum 2 4559:X 11 Dec 13:26:34.864 * +slave slave 10.0.0.141:6379 10.0.0.141 6379 @ mymaster 10.0.0.140 6379 4559:X 11 Dec 13:26:34.865 * +slave slave 10.0.0.142:6379 10.0.0.142 6379 @ mymaster 10.0.0.140 6379 4559:X 11 Dec 13:27:01.069 * +sentinel sentinel 8583cec1a7634843d0141f9a7e00e5ee5f0b4293 10.0.0.141 26379 @ mymaster 10.0.0.140 6379 4559:X 11 Dec 13:27:12.489 * +sentinel sentinel 715f518a156837119ddf282b86b4fae992b4602c 10.0.0.142 26379 @ mymaster 10.0.0.140 6379
以客户端方式查看主从
[root@redis-Slave2 ~]# cd /usr/local/redis/ [root@redis-Slave2 redis]# src/redis-cli -h 10.0.0.140 -p 26379 INFO Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.0.0.140:6379,slaves=2,sentinels=3
模拟故障将Master,down
哨兵模式会自动切换Master
[root@redis-Slave1 redis]# src/redis-sentinel sentinel.conf
14157:X 11 Dec 13:26:59.038 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 14157:X 11 Dec 13:26:59.038 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=14157, just started 14157:X 11 Dec 13:26:59.038 # Configuration loaded 14157:X 11 Dec 13:26:59.039 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 14157 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 14157:X 11 Dec 13:26:59.041 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 14157:X 11 Dec 13:26:59.042 # Sentinel ID is 8583cec1a7634843d0141f9a7e00e5ee5f0b4293 14157:X 11 Dec 13:26:59.042 # +monitor master mymaster 10.0.0.140 6379 quorum 2 14157:X 11 Dec 13:26:59.043 * +slave slave 10.0.0.141:6379 10.0.0.141 6379 @ mymaster 10.0.0.140 6379 14157:X 11 Dec 13:26:59.044 * +slave slave 10.0.0.142:6379 10.0.0.142 6379 @ mymaster 10.0.0.140 6379 14157:X 11 Dec 13:26:59.359 * +sentinel sentinel 33890665f4d321bbf024646efdd169a16713e953 10.0.0.140 26379 @ mymaster 10.0.0.140 6379 14157:X 11 Dec 13:27:12.474 * +sentinel sentinel 715f518a156837119ddf282b86b4fae992b4602c 10.0.0.142 26379 @ mymaster 10.0.0.140 6379 14157:X 11 Dec 13:31:43.607 # +sdown master mymaster 10.0.0.140 6379 14157:X 11 Dec 13:31:43.607 # +sdown sentinel 33890665f4d321bbf024646efdd169a16713e953 10.0.0.140 26379 @ mymaster 10.0.0.140 6379 14157:X 11 Dec 13:31:48.835 # +new-epoch 1 14157:X 11 Dec 13:31:50.877 # +vote-for-leader 715f518a156837119ddf282b86b4fae992b4602c 1 14157:X 11 Dec 13:31:50.877 # +tilt #tilt mode entered 14157:X 11 Dec 13:32:20.887 # -tilt #tilt mode exited 14157:X 11 Dec 13:32:20.887 # +odown master mymaster 10.0.0.140 6379 #quorum 2/2 14157:X 11 Dec 13:32:20.887 # Next failover delay: I will not start a failover before Mon Dec 11 13:37:51 2017 14157:X 11 Dec 13:32:21.759 # +config-update-from sentinel 715f518a156837119ddf282b86b4fae992b4602c 10.0.0.142 26379 @ mymaster 10.0.0.140 6379 14157:X 11 Dec 13:32:21.759 # +switch-master mymaster 10.0.0.140 6379 10.0.0.142 6379 14157:X 11 Dec 13:32:21.760 * +slave slave 10.0.0.141:6379 10.0.0.141 6379 @ mymaster 10.0.0.142 6379 14157:X 11 Dec 13:32:21.760 * +slave slave 10.0.0.140:6379 10.0.0.140 6379 @ mymaster 10.0.0.142 6379
以客户端方式查看主从,实现了Master的自动切换
[root@redis-Slave2 redis]# src/redis-cli -h 10.0.0.142 -p 26379 INFO Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.0.0.142:6379,slaves=2,sentinels=3
转载于:https://blog.51cto.com/1767340368/2049341