会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 Redis多哨兵模式详解!Linux运维培训班!

Redis多哨兵模式详解!Linux运维培训班

时间:2025-11-05 11:36:22 来源:益强数据堂 作者:应用开发 阅读:657次

  哨兵sentinel介绍

  sentinel是哨兵运行在特殊模式下的redis服务器,redis3.0以前的模式集群一般都是借助哨兵sentinel工具来监控master节点的状态,如果master节点一场,详解训班则会进行主备切换,维培将slave节点升级为master节点,哨兵性能和高可用方面表现一般,模式特别是详解训班在主从切换瞬间

  | sentinel对节点的判断

  主观下线:

  某个sentinel节点认为(info命令)某个redis节点为不可用状态

  客观下线:

  多个sentinel认为某个redis节点为主观下线状态,将redis节点判断为客观下线,维培并发起一次针对主服务器的哨兵故障转移操作

  | 工作原理

  sentinel读入用户指定的服务器托管配置文件,为每个要被监视的模式redis节点创建相应的实例结构,并创建连向sentinel的详解训班命令连接(用于sentinel连接redis,向redis发送命令请求)和订阅连接(用于接收sentinel:hello频道消息)   每10秒每个sentinel对redis master和slave执行info replication命令,维培该命令用来确认主从关系和发现slave节点   每2秒每个sentinel向redis节点的哨兵sentinel:hello频道发送消息,并根据这些信息为其他sentinel创建相应的模式实例结构,以及命令连接   每1秒每个sentinel对其他sentinel和redis执行ping命令,详解训班用于心跳检测

  多主多从架构图

  环境准备

  | 环境介绍

  | redis配置文件

# 与redis-master112节点部署 cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379 daemonize yes pidfile "/oldboyedu/data/redis/redis.pid" loglevel notice logfile "/oldboyedu/data/redis/redis.log" dbfilename "dump.rdb" dir "/oldboyedu/data/redis" requirepass "oldboyedu" masterauth "oldboyedu" bind 10.0.0.112 127.0.0.1 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec EOF # 与redis-slave113节点部署 cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379 daemonize yes pidfile "/oldboyedu/data/redis/redis.pid" loglevel notice logfile "/oldboyedu/data/redis/redis.log" dbfilename "dump.rdb" dir "/oldboyedu/data/redis" requirepass "oldboyedu" masterauth "oldboyedu" bind 10.0.0.113 127.0.0.1 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec EOF # 与redis-slave114节点部署 cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379 daemonize yes pidfile "/oldboyedu/data/redis/redis.pid" loglevel notice logfile "/oldboyedu/data/redis/redis.log" dbfilename "dump.rdb" dir "/oldboyedu/data/redis" requirepass "oldboyedu" masterauth "oldboyedu" bind 10.0.0.114 127.0.0.1 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec EOF

  | 启动服务

# 全部节点执行 redis-server /oldboyedu/softwares/redis/redis.conf # slave节点执行 redis-cli -p 6379 -a oldboyedu SLAVEOF 10.0.0.112 6379

  部署多sentinel实例

  | 部署sentinel实例

# 全部节点执行 mkdir -pv /oldboyedu/softwares/redis26379 mkdir -pv /oldboyedu/data/redis26379 mkdir -pv /oldboyedu/logs/redis26379

  | 创建配置文件

  redis-master112主机

cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.112 127.0.0.1 port 26379 dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster oldboyedu EOF

  redis-slave113

cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.113 127.0.0.1 port 26379 dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster oldboyedu EOF

  redis-slave114

cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.114 127.0.0.1 port 26379 dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster oldboyedu EOF

  | 启动sentinel进程

# 全部节点执行 redis-sentinel /oldboyedu/softwares/redis26379/sentinel.conf &> /oldboyedu/logs/redis26379/sentinel.log &

  | 启动sentinel进程

  验证Redis是否能够自动切换

  | 手动停止主库运行,模拟主库宕机

主库上查看当前主从状态 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=10.0.0.113,port=6379,state=online,offset=84,lag=0 slave1:ip=10.0.0.114,port=6379,state=online,offset=70,lag=1 master_failover_state:no-failover master_replid:ba904226d8ed537c4e1b187e0db1deb5df9b68f8 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:84 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:84 停止主库运行 # redis-cli -p 6379 -a oldboyedu SHUTDOWN

  | 当主库宕机后,会有其他salve来接管

redis-slave113服务器 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:3710 second_repl_offset:3711 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:3710 redis-slave114服务器 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.113 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:4543 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:4543 second_repl_offset:3711 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:4543

  | 恢复master112

  会自动加入集群并成为slave角色

# redis-server /oldboyedu/softwares/redis/redis.conf # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.113 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:15166 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:15166 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:13929 repl_backlog_histlen:1238

  验证sentinel是云服务器提供商否为高可用

  手动停掉任意一个sentinel实例,再次验证主从结构能否正常使用

  | 将redis-master112的sentinel实例停用

# netstat -untalp | grep 26379 | grep LISTEN tcp 0 0 127.0.0.1:26379 0.0.0.0:* LISTEN 1162/redis-sentinel tcp 0 0 10.0.0.112:26379 0.0.0.0:* LISTEN 1162/redis-sentinel # kill -9 1162 [1]+ Killed redis-sentinel /oldboyedu/softwares/redis26379/sentinel.conf &>/oldboyedu/logs/redis26379/sentinel.log

  | 发现依旧是可以正常切换主从的

因为做上面的实验时,master节点转移到slave113服务器上了,所以以下操作为在slave113服务器执行 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=10.0.0.114,port=6379,state=online,offset=64400,lag=1 slave1:ip=10.0.0.112,port=6379,state=online,offset=64414,lag=0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:64414 second_repl_offset:3711 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:64414 # redis-cli -p 6379 -a oldboyedu SHUTDOWN Warning: Using a password with -a or -u option on the command line interface may not be safe. # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. Could not connect to Redis at 127.0.0.1:6379: Connection refused

  | master节点转移到master112服务器上了

在master112服务器上执行 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:master connected_slaves:1 slave0:ip=10.0.0.114,port=6379,state=online,offset=94729,lag=1 master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77 master_replid2:94676467400ce842b85705c7ee808db640008085 master_repl_offset:94864 second_repl_offset:78055 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:13929 repl_backlog_histlen:80936

  | 恢复slave113节点

redis-server /oldboyedu/softwares/redis/redis.conf

  | 额外停掉一台sentinel,发现不能正常切换主从

  因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票

# netstat -untalp | grep 26379 | grep LISTEN tcp 0 0 127.0.0.1:26379 0.0.0.0:* LISTEN 1178/redis-sentinel tcp 0 0 10.0.0.113:26379 0.0.0.0:* LISTEN 1178/redis-sentinel # kill -9 1178 [1]+ Killed redis-sentinel /oldboyedu/softwares/redis26379/sentinel.conf &>/oldboyedu/logs/redis26379/sentinel.log

  发现不能正常切换主从

  因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票,无法我们在配置文件中定义的"sentinel monitor mymaster 10.0.0.112 6379 2"中的"2"

  由此可以看出,主节点并未发送改变

停掉redis-master112节点的redis运行 # redis-cli -p 6379 -a oldboyedu SHUTDOWN Warning: Using a password with -a or -u option on the command line interface may not be safe. 于redis-slave113节点查看 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.112 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:148032 master_link_down_since_seconds:55 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:148032 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:124049 repl_backlog_histlen:23984 于redis-slave114节点查看 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.112 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:148032 master_link_down_since_seconds:58 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77 master_replid2:94676467400ce842b85705c7ee808db640008085 master_repl_offset:148032 second_repl_offset:78055 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:148032源码库

(责任编辑:系统运维)

上一篇:先安装VMWare10,这个没什么可说的,安装好后启动,点击新建虚拟机,因为想设置虚拟机的磁盘保存方式,所以选择自定义选择“稍后安装操作系统”选择64位的版本给自己的机器取个名字,设定虚拟机磁盘路径,电脑c盘是SSD,为了速度就安到C盘了设置处理器的配置,这里需要看个人电脑配置,我是四核8线程的i7,这里就选了2*2,假如CPU核心数不多,就选择1*2分配内存,先来2G,以后不够再改配置后面的设置,除了磁盘的设置:将虚拟磁盘存储为单个文件,其他都默认就OK设置完成后,点击编辑虚拟机配置,设置ISO镜像文件路径完成后启动虚拟机,等安装文件加载,进入语言选择,默认是英文,下面有中文的语言选择我选择安装完成后再更新,音频解码就算了,虚拟机主要是为了布开发环境清除磁盘就行,不用怕,不会把真实磁盘清理掉的=。=把我默认到哈尔滨去了。。点击国内区域,选成shanghai注意:键盘布局选择英语(美国)设置用户名,密码下面登陆Ubuntu账户,愿意的填email创建一个,我选择以后登陆,估计很久很久以后=。=安装开始,趁机赶几行代码。。SSD安装就是快,完毕重启可以登陆了桌面很小,这时候还要安装VMtool点击顶部菜单栏的虚拟机选项,下拉选择“安装VMware Tools”,会自动载入光盘,打开窗口运行“vmware-tools-upgrader-64”无反应,把VMwareTools.tar.gz 文件拷贝而到桌面,右键提取到此处,会解压为一个 vmware-tools-distrib 目录打开终端,输入以下命令$ cd 桌面/vmware-tools-distrib$ sudo ./vmware-install.pl输入密码,执行,一路回车摁下去,到最后出现“Enjoy——the VMware team”的字样,安装完成打开VMware上方菜单栏的查看-自动调整大小,设置为自动适应客户机和自动适应窗口,右上角按钮重启虚拟机,登陆后就发现虚拟机桌面铺满窗口,调整壁纸平铺方式,显示OK至此,安装完成~
下一篇:电脑编程在线使用教程网站(从零基础到编程高手,编程在线使用教程网站帮你实现技术梦想)
推荐内容
  • 解决电脑开机蓝屏错误的有效方法(针对电脑开机蓝屏错误的解决方案)
  • 新手发生了域名争议怎么办?域名争议解决办法有哪些?
  • 新手建站优化需要怎么选择域名?有什么挑选方法?
  • 国际大佬是怎么进行顶级域名投资的?有什么技巧?
  • 电脑开机错误1053的解决方法(深入分析电脑开机错误1053及如何修复)
  • 现在AI域名在域名市场咋样?域名AI将会赶超.com吗?