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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容