您现在的位置是:网站首页> 编程资料编程资料
Redis sentinel哨兵集群的实现步骤_Redis_
2023-05-27
414人已围观
简介 Redis sentinel哨兵集群的实现步骤_Redis_
一、Redis sentinel哨兵集群概述
(1)Redis哨兵概述
*Sentinel 哨兵:这是一个分布式系统,该进程是用于监控Redis集群中Master主服务器的工作状态,在Master主服务器发生故障时,可以实现Master和Slave服务器的秒级切换,保证系统有一个Master主服务器,提供了Redis集群的高可用,在Reids2.6.版本时被加入,到2.8版本之后得到了稳定
Redis哨兵和Redis主从的区别:
Redis哨兵:主服务器出现故障后,会有一个从服务器代替主服务器
Redis主从:主服务器出现故障后,从服务器不会做任何事
(2)Redis哨兵的工作机制
哨兵只需要部署在master主服务器上即可
工作进程:
监控(Monitoring):哨兵通过流言协议(gossip protocols)会不断检查集群中每一台服务器是否运作正常
提醒(Notification):当哨兵监控的某个redis服务器出现问题时,哨兵可以通过API(应用程序接口)向管理员或者其他应用程序发送通知
自动故障转移(Automatic failover):在集群中如果有一个Master主服务器出现故障时,哨兵会通过投票协议(Agreement Protocols)开始一次自动故障迁移操作,他会选择一台数据较完整的Slave从服务器升级为主服务器,当客户端试图连接失效的Master主服务器时,集群也会向客户端返回新的Master主服务器的地址,使得集群可以使用现在的Master替换掉失效的Master。
Master和Slave切换后,Master的redis主配置文件、Slave的redis主配置文件和哨兵的配置文件的内容都会发生相应的改变,即原来的Master的redis主配置文件会多一行Slave服务器的配置,之后哨兵的监控目标就会改变到现在的Master主服务器上
(3)哨兵的三个定时监控任务
每隔10秒,每个Sentinel节点会向主节点和从节点发送info命令获取Redis数据节点的信息
作用:
通过向主节点执行info命令,获取从节点的信息,这也是为什么Sentinel节点不需要显式配置监控从节点。当有新的从节点加入时都可以立刻感知出来,当节点不可达或者故障转移后,可以通过info命令实时更新节点拓扑信息。
每隔1秒,每个Sentinel节点会向主节点、从节点、发送一条ping命令做一次心跳检测,来确认这些节点当前是否可达如果主节点挂掉,那么sentinel,就会从剩余的从节点选择一个数据比较完整来做主节点
二、部署Redis哨兵系统
(1)实验环境
系统 | ip | 主机名 | Redis版本 | 端口 | 扮演角色 |
---|---|---|---|---|---|
Centos7.4 | 192.168.100.202 | master | Redis-5.0.4 | Redis:6379 Sentinel:26379 | Master |
Centos7.4 | 192.168.100.203 | slave1 | Redis-5.0.4 | Redis:6379 | Slave |
Centos7.4 | 192.168.100.204 | slave2 | Redis-5.0.4 | Redis:6379 | Slave |
(2)实验步骤 -在每台服务器上都安装Redis
安装步骤相同,主机名、ip不同,下面只写Master配置
[root@Centos7 ~]# hostnamectl set-hostname master [root@Centos7 ~]# su [root@master ~]# systemctl stop firewalld [root@master ~]# setenforce 0 setenforce: SELinux is disabled [root@master ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载 mount: /dev/sr0 已经挂载或 /mnt 忙 /dev/sr0 已经挂载到 /mnt 上 [root@master ~]# ll 总用量 1928 -rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg -rw-r--r-- 1 root root 1966337 6月 9 01:16 redis-5.0.4.tar.gz [root@master ~]# tar xf redis-5.0.4.tar.gz [root@master ~]# cd redis-5.0.4 [root@master redis-5.0.4]# make [root@master redis-5.0.4]# mkdir -p /usr/local/redis [root@master redis-5.0.4]# cp /root/redis-5.0.4/src/redis-server /usr/local/redis/ [root@master redis-5.0.4]# cp /root/redis-5.0.4/src/redis-cli /usr/local/redis/ [root@master redis-5.0.4]# cp /root/redis-5.0.4/redis.conf /usr/local/redis/ [root@master redis-5.0.4]# vim /usr/local/redis/redis.conf #修改 。。。。。。 68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 69 bind 192.168.100.202 #修改为本机地址,如果为127.0.0.1就只能本机访问 70 。。。。。。 87 # are explicitly listed using the "bind" directive. 88 protected-mode no #关闭redis的保护模式,如果为yes的话其他客户端就无法连接到此服务器 89 。。。。。。 135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 136 daemonize yes #开启redis的后台守护程序,即在redis开启之后是放在后台运行的 137 。。。。。。 262 # Note that you must specify a directory here, not a file name. 263 dir /usr/local/redis/rdb 264 。。。。。。 506 # 507 requirepass 123123 #去掉注释,修改redis的密码为123123 508 #保存退出 [root@slave2 redis-5.0.4]# mkdir /usr/local/redis/rdb [root@master redis-5.0.4]# vim /etc/init.d/redis #!/bin/sh # chkconfig: 2345 80 90 # description: Start and Stop redis #PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/redis/redis-server REDIS_CLI=/usr/local/redis/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/usr/local/redis/redis.conf" AUTH="123123" LISTEN_IP=$(netstat -utpln |grep redis-server |awk '{print $4}'|awk -F':' '{print $1}') case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -h $LISTEN_IP -p $REDISPORT -a $AUTH SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac [root@master redis-5.0.4]# chkconfig --add redis [root@master redis-5.0.4]# chmod 755 /etc/init.d/redis [root@master redis-5.0.4]# ln -s /usr/local/redis/* /usr/local/bin/ [root@master redis-5.0.4]# /etc/init.d/redis start Starting Redis server... 5233:C 09 Jun 2021 01:25:53.069 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5233:C 09 Jun 2021 01:25:53.069 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5233, just started 5233:C 09 Jun 2021 01:25:53.069 # Configuration loaded Redis is running... [root@master redis-5.0.4]# netstat -anpt | grep 6379 tcp 0 0 192.168.100.202:6379 0.0.0.0:* LISTEN 5234/redis-server 1
-做redis主从
******(1)Master配置 [root@master redis-5.0.4]# vim /usr/local/redis/redis.conf #修改 。。。。。。 292 # 293 masterauth 123123 #配置主服务器密码,哨兵有一个问题,就是当主服务器坏掉,切换到从服务器时,原来的主服务器可以正常运行之后,再次加入集群是加不进去的,因为哨兵没有配置主服务器的密码,所以无法连接,所以在使用哨兵集群时,要把每台的主服务器密码都配置上,每台redis的密码最好都一样 294 。。。。。。 456 # 457 min-replicas-to-write 1 #设置slave服务器的数量,当slave服务器少于这个数量时,Master主服务器会停止接收客户端的一切写请求 458 min-replicas-max-lag 10 #设置主服务器和从服务器之间同步数据的超时时间,当超过此时间时,master主服务器会停止客户端的一切写操作,单位为秒 459 # 。。。。。。 [root@master redis-5.0.4]# /etc/init.d/redis restart #重启redis Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped Starting Redis server... 5291:C 09 Jun 2021 02:04:39.132 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5291:C 09 Jun 2021 02:04:39.132 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5291, just started 5291:C 09 Jun 2021 02:04:39.132 # Configuration loaded Redis is running... ******(2)Slave1配置 [root@slave1 redis-5.0.4]# vim /usr/local/redis/redis.conf 。。。。。。 285 # 286 replicaof 192.168.100.202 6379 #在从服务器上指定主服务器的ip和端口 287 。。。。。。 292 # 293 masterauth 123123 #指定主服务器上redis的密码 294 。。。。。。 #保存退出 [root@slave redis-5.0.4]# /etc/init.d/redis restart #重启服务 Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped Starting Redis server... 5304:C 09 Jun 2021 02:11:32.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5304:C 09 Jun 2021 02:11:32.241 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5304, just started 5304:C 09 Jun 2021 02:11:32.241 # Configuration loaded Redis is running... ******(3)Slave2配置 [root@slave2 redis-5.0.4]# vim /usr/local/redis/redis.conf 。。。。。。 286 replicaof 192.168.100.204 6379 287 288 # If the master is password protected (using the "requirepass" configuration 289 # directive below) it is possible to tell the replica to authenticate before 290 # starting the replication synchronization process, otherwise the master will 291 # refuse the replica request. 292 # 293 masterauth 123123 294 。。。。。。 #保存退出 [root@slave2 redis-5.0.4]# /etc/init.d/redis restart Stopping ... Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Redis stopped Starting Redis server... 5253:C 09 Jun 2021 17:50:25.680 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5253:C 09 Jun 2021 17:50:25.680 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5253, just started 5253:C 09 Jun 2021 17:50:25.680 # Configuration loaded Redis is running... ******(3)验证主从是否成功 [root@master ~]# redis-cli -h 192.168.100.202 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.202:6379> set aaa bbb OK 192.168.100.202:6379> set bbb ccc OK 192.168.100.202:6379> keys * 1) "aaa" 2) "bbb" [root@slave1 ~]# redis-cli -h 192.168.100.203 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.100.203:6379> keys * 1) "bbb" 2) "aaa" 192.168.100.203:6379> set ttt fff (error) READONLY You can't write against a read only replica. #从服务器无法写入数据 [root@slave2 redis-5.0.4]# redis-cli -h 192.168.100.204 -a 123123 Warning: Using a password with '-a' or '-u' option on the command line interfa
相关内容
- Redis与本地缓存的结合实现_Redis_
- Redis数据结构SortedSet的底层原理解析_Redis_
- Redis分布式锁解决秒杀超卖问题_Redis_
- Redis实现Session共享与单点登录_Redis_
- redis 主从哨兵模式实现一主二从_Redis_
- Redis解决Session共享问题的方法详解_Redis_
- Redis 单机安装和哨兵模式集群安装的实现_Redis_
- 使用高斯Redis实现二级索引的方法_Redis_
- RediSearch加RedisJSON大于Elasticsearch的搜索存储引擎_Redis_
- Redis官方ORM框架比RedisTemplate更优雅_Redis_
点击排行
本栏推荐
