当前位置:   article > 正文

Redis哨兵集群搭建_redis哨兵集群部署

redis哨兵集群部署

一、安装Redis

1.安装依赖
yum install -y gcc tcl
  • 1
2.将Redis压缩包解压到对应的目录
tar -zxvf redis-2.8.0.tar.gz
mv redis-2.8.0 /usr/local
  • 1
  • 2
3.编译
cd /usr/local/redis-2.8.0
make && make install
  • 1
  • 2
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

关闭防火墙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
  • 1
  • 2
  • 3
  • 4
  • 5

这里我的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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意:日志文件要创建好

两个从节点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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

启动:进入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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

启动哨兵:进入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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

节点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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

节点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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/989332
推荐阅读
相关标签
  

闽ICP备14008679号