当前位置: 首页 > news >正文

Redis哨兵集群搭建

一、安装Redis

1.安装依赖
yum install -y gcc tcl
2.将Redis压缩包解压到对应的目录
tar -zxvf redis-2.8.0.tar.gz
mv redis-2.8.0 /usr/local
3.编译
cd /usr/local/redis-2.8.0
make && make install
4.配置redis.conf
# 任意ip都可以访问
bind 0.0.0.0
# 关闭保护模式
# protected-mode no
# 让 Redis 服务器以守护进程的方式在后台持续运行
daemonize yes
# 日志文件(要手动创建这个文件)
logfile /usr/local/redis-2.8.0/log/redis.log
save 900 1
save 300 10
save 60 1000
# 启用 Redis 在执行 RDB 持久化操作时对生成的 RDB 文件进行压缩,以节省磁盘空间
rdbcompression yes

关闭防火墙:systemctl disable firewalld.service

5.启动
[root@localhost local]# cd redis-2.8.0/src
[root@localhost src]# ./redis-server ../redis.conf
[root@localhost src]# ps aux | grep redis
root      10190  0.0  0.3 140912  7384 ?        Ssl  13:38   0:00 ./redis-server 0.0.0.0:6379
root      10194  0.0  0.0 112812   972 pts/0    S+   13:38   0:00 grep --color=auto redis

这里我的redis版本很低,因为项目太老了,支持不了高版本

二、配置

准备6台机器,都安装上Redis,一台作为主节点,两台作为从节点,三台作为哨兵节点。

ip信息:

  • 主节点ip:192.168.108.100
  • 从节点1:192.168.108.101
  • 从节点2:192.168.108.102
  • 哨兵节点1:192.168.108.131
  • 哨兵节点2:192.168.108.132
  • 哨兵节点3:192.168.108.133
1.主从配置

只需要在从节点配置上slaveof <masterip> <masterport>

主节点redis.conf:

daemonize yes
logfile /usr/local/redis-2.8.0/log/redis.log
save 900 1
save 300 10
save 60 1000
rdbcompression yes
bind  0.0.0.0
#protected-mode no

注意:日志文件要创建好

两个从节点redis.conf:

daemonize yes
logfile "/usr/local/redis-2.8.0/log/redis.log"
save 900 1
save 300 10
save 60 1000
rdbcompression yes
bind 0.0.0.0
#protected-mode no
slaveof 192.168.108.100 6379
# Generated by CONFIG REWRITE
dir "/usr/local/redis-2.8.0/src"

启动:进入src目录执行:./redis-server ../redis.conf

测试:主节点中设置的值,可以在两个从节点中get到
只有master节点上可以执行写操作,两个slave节点只能执行读操作。

2.哨兵配置

3个哨兵节点的配置文件:

# sentinel实例的端口
port 26379
# 指定监控的主节点信息 2:选举master时的quorum值
sentinel monitor mymaster 192.168.108.102 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
# Generated by CONFIG REWRITE
dir "/usr/local/redis-2.8.0/src"

启动哨兵:进入src目录执行:./redis-sentinel ../sentinel.conf

3.测试

把主节点192.168.108.100宕机,查看sentinel日志:

[root@localhost src]# ./redis-sentinel ../sentinel.conf
[1601] 24 Jun 21:02:07.829 * Max number of open files set to 10032_._                                                  _.-``__ ''-._                                             _.-``    `.  `_.  ''-._           Redis 2.8.0 (00000000/0) 64 bit.-`` .-```.  ```\/    _.,_ ''-._                                   (    '      ,       .-`  | `,    )     Running in sentinel mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379|    `-._   `._    /     _.-'    |     PID: 1601`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           http://redis.io        `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               [1601] 24 Jun 21:02:07.831 # Sentinel runid is c0d04f13c185a97c901f368348afe3f14d810885
[1601] 24 Jun 21:02:07.832 * +slave slave 192.168.108.101:6379 192.168.108.101 6379 @ mymaster 192.168.108.100 6379
[1601] 24 Jun 21:02:07.832 * +slave slave 192.168.108.102:6379 192.168.108.102 6379 @ mymaster 192.168.108.100 6379
[1601] 24 Jun 21:02:27.681 * +sentinel sentinel 192.168.108.132:26379 192.168.108.132 26379 @ mymaster 192.168.108.100 6379
[1601] 24 Jun 21:02:34.217 * +sentinel sentinel 192.168.108.133:26379 192.168.108.133 26379 @ mymaster 192.168.108.100 6379
[1601] 24 Jun 21:02:39.258 # +sdown sentinel 192.168.108.133:26379 192.168.108.133 26379 @ mymaster 192.168.108.100 6379
[1601] 24 Jun 21:05:49.959 # +sdown master mymaster 192.168.108.100 6379        // 主观认为100下线
[1601] 24 Jun 21:05:50.059 # +odown master mymaster 192.168.108.100 6379 #quorum 2/2      //quorum达标,客观认为100下线。
[1601] 24 Jun 21:05:50.059 # +new-epoch 1
[1601] 24 Jun 21:05:50.059 # +try-failover master mymaster 192.168.108.100 6379         //尝试等待100
[1601] 24 Jun 21:05:50.059 # +vote-for-leader c0d04f13c185a97c901f368348afe3f14d810885 1    //sentinel内部选举一个Leader,选中的sentinel实例去执行故障转移
[1601] 24 Jun 21:05:50.160 # +elected-leader master mymaster 192.168.108.100 6379
[1601] 24 Jun 21:05:50.160 # +failover-state-select-slave master mymaster 192.168.108.100 6379    //准备选举一个slave作为新的leader
[1601] 24 Jun 21:05:50.260 # +selected-slave slave 192.168.108.102:6379 192.168.108.102 6379 @ mymaster 192.168.108.100 6379     //选中了102这个实例
[1601] 24 Jun 21:05:50.260 * +failover-state-send-slaveof-noone slave 192.168.108.102:6379 192.168.108.102 6379 @ mymaster 192.168.108.100 6379      //让102执行slaveof noone,成为新的master
[1601] 24 Jun 21:05:50.361 * +failover-state-wait-promotion slave 192.168.108.102:6379 192.168.108.102 6379 @ mymaster 192.168.108.100 6379    //102等待提升(让其他slave执行 slaveof 192.168.108.102 6379)
[1601] 24 Jun 21:05:51.067 # +promoted-slave slave 192.168.108.102:6379 192.168.108.102 6379 @ mymaster 192.168.108.100 6379    //102正式成为master
[1601] 24 Jun 21:05:51.067 # +failover-state-reconf-slaves master mymaster 192.168.108.100 6379   //修改下线的100实例配置,让他标记为102的slave
[1601] 24 Jun 21:05:51.166 * +slave-reconf-sent slave 192.168.108.101:6379 192.168.108.101 6379 @ mymaster 192.168.108.100 6379    //修改101实例的配置,标记他为102的slave
[1601] 24 Jun 21:05:52.075 * +slave-reconf-inprog slave 192.168.108.101:6379 192.168.108.101 6379 @ mymaster 192.168.108.100 6379   //修改101实例的配置,标记他为102的slave
[1601] 24 Jun 21:05:52.075 * +slave-reconf-done slave 192.168.108.101:6379 192.168.108.101 6379 @ mymaster 192.168.108.100 6379
[1601] 24 Jun 21:05:52.175 # +failover-end master mymaster 192.168.108.100 6379   
[1601] 24 Jun 21:05:52.175 # +switch-master mymaster 192.168.108.100 6379 192.168.108.102 6379     //主从切换完成
[1601] 24 Jun 21:05:52.176 * +slave slave 192.168.108.101:6379 192.168.108.101 6379 @ mymaster 192.168.108.102 6379
[1601] 24 Jun 21:05:52.177 * +slave slave 192.168.108.100:6379 192.168.108.100 6379 @ mymaster 192.168.108.102 6379
[1601] 24 Jun 21:05:57.217 # +sdown slave 192.168.108.100:6379 192.168.108.100 6379 @ mymaster 192.168.108.102 6379
[1601] 24 Jun 21:07:50.268 # +new-epoch 2
[1601] 24 Jun 21:07:52.489 # +switch-master mymaster 192.168.108.102 6379 192.168.108.102 6379
[1601] 24 Jun 21:07:52.489 * +slave slave 192.168.108.100:6379 192.168.108.100 6379 @ mymaster 192.168.108.102 6379
[1601] 24 Jun 21:07:52.494 * +slave slave 192.168.108.101:6379 192.168.108.101 6379 @ mymaster 192.168.108.102 6379
[1601] 24 Jun 21:07:57.501 # +sdown slave 192.168.108.100:6379 192.168.108.100 6379 @ mymaster 192.168.108.102 6379

节点192.168.108.101日志:

[16135] 24 Jun 20:58:52.603 * Max number of open files set to 10032_._                                                  _.-``__ ''-._                                             _.-``    `.  `_.  ''-._           Redis 2.8.0 (00000000/0) 64 bit.-`` .-```.  ```\/    _.,_ ''-._                                   (    '      ,       .-`  | `,    )     Running in stand alone mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379|    `-._   `._    /     _.-'    |     PID: 16135`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           http://redis.io        `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               [16135] 24 Jun 20:58:52.604 # Server started, Redis version 2.8.0
[16135] 24 Jun 20:58:52.604 * The server is now ready to accept connections on port 6379
[16135] 24 Jun 20:58:52.604 * Connecting to MASTER 192.168.108.100:6379
[16135] 24 Jun 20:58:52.604 * MASTER <-> SLAVE sync started
[16135] 24 Jun 20:58:52.604 * Non blocking connect for SYNC fired the event.
[16135] 24 Jun 20:58:52.605 * Master replied to PING, replication can continue...
[16135] 24 Jun 20:58:52.605 * Partial resynchronization not possible (no cached master)
[16135] 24 Jun 20:58:52.605 * Full resync from master: 853ec52724c940634dc70d96225ac014be295683:15
[16135] 24 Jun 20:58:52.667 * MASTER <-> SLAVE sync: receiving 25 bytes from master
[16135] 24 Jun 20:58:52.668 * MASTER <-> SLAVE sync: Loading DB in memory
[16135] 24 Jun 20:58:52.668 * MASTER <-> SLAVE sync: Finished with success
[16135] 24 Jun 21:05:44.922 * Caching the disconnected master state.
[16135] 24 Jun 21:05:45.327 * Connecting to MASTER 192.168.108.100:6379
[16135] 24 Jun 21:05:45.327 * MASTER <-> SLAVE sync started
[16135] 24 Jun 21:05:45.328 # Error condition on socket for SYNC: Connection refused
[16135] 24 Jun 21:05:46.331 * Connecting to MASTER 192.168.108.100:6379
[16135] 24 Jun 21:05:46.331 * MASTER <-> SLAVE sync started
[16135] 24 Jun 21:05:46.331 # Error condition on socket for SYNC: Connection refused
[16135] 24 Jun 21:05:47.336 * Connecting to MASTER 192.168.108.100:6379
[16135] 24 Jun 21:05:47.336 * MASTER <-> SLAVE sync started
[16135] 24 Jun 21:05:47.336 # Error condition on socket for SYNC: Connection refused
[16135] 24 Jun 21:05:48.340 * Connecting to MASTER 192.168.108.100:6379
[16135] 24 Jun 21:05:48.340 * MASTER <-> SLAVE sync started
[16135] 24 Jun 21:05:48.340 # Error condition on socket for SYNC: Connection refused
[16135] 24 Jun 21:05:49.344 * Connecting to MASTER 192.168.108.100:6379
[16135] 24 Jun 21:05:49.344 * MASTER <-> SLAVE sync started
[16135] 24 Jun 21:05:49.344 # Error condition on socket for SYNC: Connection refused
[16135] 24 Jun 21:05:49.940 * Discarding previously cached master state.
[16135] 24 Jun 21:05:49.940 * MASTER MODE enabled (user request)   //升级为主节点
[16135] 24 Jun 21:05:50.807 * Slave asks for synchronization
[16135] 24 Jun 21:05:50.807 * Full resync requested by slave.
[16135] 24 Jun 21:05:50.807 * Starting BGSAVE for SYNC
[16135] 24 Jun 21:05:50.807 * Background saving started by pid 18604
[18604] 24 Jun 21:05:50.813 * DB saved on disk
[18604] 24 Jun 21:05:50.814 * RDB: 4 MB of memory used by copy-on-write
[16135] 24 Jun 21:05:50.845 * Background saving terminated with success
[16135] 24 Jun 21:05:50.845 * Synchronization with slave succeeded

节点192.168.108.102日志:

[59973] 20 Jun 23:14:33.857 * MASTER <-> SLAVE sync started
...skipping...(    '      ,       .-`  | `,    )     Running in stand alone mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379|    `-._   `._    /     _.-'    |     PID: 14218`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           http://redis.io        `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               [14218] 24 Jun 20:58:45.993 # Server started, Redis version 2.8.0
[14218] 24 Jun 20:58:45.993 * DB loaded from disk: 0.000 seconds
[14218] 24 Jun 20:58:45.993 * The server is now ready to accept connections on port 6379
[14218] 24 Jun 20:58:45.993 * Connecting to MASTER 192.168.108.100:6379
[14218] 24 Jun 20:58:45.993 * MASTER <-> SLAVE sync started
[14218] 24 Jun 20:58:45.994 * Non blocking connect for SYNC fired the event.
[14218] 24 Jun 20:58:45.994 * Master replied to PING, replication can continue...
[14218] 24 Jun 20:58:45.995 * Partial resynchronization not possible (no cached master)
[14218] 24 Jun 20:58:45.995 * Full resync from master: 853ec52724c940634dc70d96225ac014be295683:1
[14218] 24 Jun 20:58:46.035 * MASTER <-> SLAVE sync: receiving 25 bytes from master
[14218] 24 Jun 20:58:46.035 * MASTER <-> SLAVE sync: Loading DB in memory
[14218] 24 Jun 20:58:46.035 * MASTER <-> SLAVE sync: Finished with success
[14218] 24 Jun 21:05:44.917 * Caching the disconnected master state.
[14218] 24 Jun 21:05:45.778 * Connecting to MASTER 192.168.108.100:6379    // 100是主
[14218] 24 Jun 21:05:45.778 * MASTER <-> SLAVE sync started
[14218] 24 Jun 21:05:45.778 # Error condition on socket for SYNC: Connection refused
[14218] 24 Jun 21:05:46.784 * Connecting to MASTER 192.168.108.100:6379
[14218] 24 Jun 21:05:46.784 * MASTER <-> SLAVE sync started
[14218] 24 Jun 21:05:46.784 # Error condition on socket for SYNC: Connection refused
[14218] 24 Jun 21:05:47.790 * Connecting to MASTER 192.168.108.100:6379
[14218] 24 Jun 21:05:47.790 * MASTER <-> SLAVE sync started
[14218] 24 Jun 21:05:47.790 # Error condition on socket for SYNC: Connection refused
[14218] 24 Jun 21:05:48.794 * Connecting to MASTER 192.168.108.100:6379
[14218] 24 Jun 21:05:48.794 * MASTER <-> SLAVE sync started
[14218] 24 Jun 21:05:48.795 # Error condition on socket for SYNC: Connection refused
[14218] 24 Jun 21:05:49.796 * Connecting to MASTER 192.168.108.100:6379
[14218] 24 Jun 21:05:49.796 * MASTER <-> SLAVE sync started
[14218] 24 Jun 21:05:49.797 # Error condition on socket for SYNC: Connection refused
[14218] 24 Jun 21:05:50.740 * Discarding previously cached master state.
[14218] 24 Jun 21:05:50.740 * SLAVE OF 192.168.108.102:6379 enabled (user request)   //变为102的从
[14218] 24 Jun 21:05:50.800 * Connecting to MASTER 192.168.108.102:6379
[14218] 24 Jun 21:05:50.800 * MASTER <-> SLAVE sync started
[14218] 24 Jun 21:05:50.801 * Non blocking connect for SYNC fired the event.
[14218] 24 Jun 21:05:50.801 * Master replied to PING, replication can continue...
[14218] 24 Jun 21:05:50.802 * Partial resynchronization not possible (no cached master)   // 重新执行psync
[14218] 24 Jun 21:05:50.802 * Full resync from master: a3c6cdb64c33f99b657037cc024f212e517bc316:1
[14218] 24 Jun 21:05:50.841 * MASTER <-> SLAVE sync: receiving 30 bytes from master
[14218] 24 Jun 21:05:50.841 * MASTER <-> SLAVE sync: Loading DB in memory
[14218] 24 Jun 21:05:50.841 * MASTER <-> SLAVE sync: Finished with success
[14218] 24 Jun 21:07:50.848 * SLAVE OF would result into synchronization with the master we are already connected with. No operation performed.
[14218] 24 Jun 21:13:46.056 * 1 changes in 900 seconds. Saving...
[14218] 24 Jun 21:13:46.056 * Background saving started by pid 19554
[19554] 24 Jun 21:13:46.061 * DB saved on disk
[19554] 24 Jun 21:13:46.062 * RDB: 6 MB of memory used by copy-on-write
[14218] 24 Jun 21:13:46.157 * Background saving terminated with success
http://www.lryc.cn/news/382270.html

相关文章:

  • 网络爬虫requests库使用指南
  • VSCODE 配置C++ 与OPENCV
  • C语言小例程28/100
  • WPF文本绑定显示格式StringFormat设置-特殊格式时间日期和多数据绑定
  • Java包介绍
  • 【2024.6.21】今日科技时事:科技前沿大事件
  • LeetCode:经典题之1491、896 题解与延伸
  • 2024三掌柜赠书活动第二十五期:Rust 游戏开发实战
  • 基于Java蛋糕甜品商城系统设计和实现(源码+LW+调试文档+讲解等)
  • Tomcat get请求传数组集合参数
  • 信息学奥赛初赛天天练-34-CSP-J2021完善程序-按位异或、模拟算法、数组模拟环、约瑟夫问题应用
  • 【计算机视觉】人脸算法之图像处理基础知识(六)
  • 仓颉编程语言入门
  • 在前端项目中,如何处理错误和异常?
  • Ubuntu系统下修改网卡IP地址
  • Scrapy如何对爬虫数据进行清洗和处理?
  • Linux:基础IO(三.软硬链接、动态库和静态库、动精态库的制作和加载)
  • 低价可转债崩盘,发生了什么?
  • 【面试题】马上金九银十了,简历该准备起来了,面试题你准备好了吗 ?浅谈 JS 浅拷贝和深拷贝
  • 最新OPPO 真我手机 一加手机 使用adb命令永久关闭系统更新教程
  • OnlyOffice:现代办公的最佳选择
  • 【收藏】2024年必备相图数据库资源集锦!
  • Zookeeper 二、Zookeeper环境搭建
  • Web3 学习
  • Grafana+Prometheus(InfluxDB)+Jmeter使用Nginx代理搭建可视化性能测试监控平台
  • web学习笔记(六十六)项目总结
  • 红队内网攻防渗透:内网渗透之内网对抗:横向移动篇域控系统提权NetLogonADCSPACKDC永恒之蓝CVE漏洞
  • VMware Workstation安装Windows Server2019系统详细操作步骤
  • HTML5【新特性总结】
  • 【面试题】面试官:判断图是否有环?_数据结构复试问题 有向图是否有环