Java小强个人技术博客站点    手机版
当前位置: 首页 >> 软件 >> Centos7配置Redis哨兵

Centos7配置Redis哨兵

10481 软件 | 2022-1-25

REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型数据库。

Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。

Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。


要在同一台虚拟机开启3个实例,必须准备三份不同的配置文件和目录,配置文件所在目录也就是工作目录。

mkdir s1 s2 s3


然后我们在s1目录创建一个sentinel.conf文件,添加下面的内容

port 27001
sentinel announce-ip 192.168.3.22
sentinel monitor mymaster 192.168.3.22 7001 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
dir "/root/redis/s1"

说明:

port 27001:是当前sentinel实例的端口

sentinel monitor mymaster 192.168.3.22 7001 2:指定主节点信息

  mymaster:主节点名称,自定义,任意写

  192.168.3.22 7001:主节点的ip和端口

  2:选举master时的quorum值

  

然后将s1/sentinel.conf文件拷贝到s2、s3两个目录中

# 方式一:逐个拷贝
cp s1/sentinel.conf s2
cp s1/sentinel.conf s3
# 方式二:管道组合命令,一键拷贝
echo s2 s3 | xargs -t -n 1 cp s1/sentinel.conf


修改s2、s3两个文件夹内的配置文件,将端口分别修改为27002、27003:

sed -i -e 's/27001/27002/g' -e 's/s1/s2/g' s2/sentinel.conf
sed -i -e 's/27001/27003/g' -e 's/s1/s3/g' s3/sentinel.conf


启动

nohup redis-sentinel /root/redis/s1/sentinel.conf > /root/redis/s1/s1.log 2>&1 &
nohup redis-sentinel /root/redis/s2/sentinel.conf > /root/redis/s2/s2.log 2>&1 &
nohup redis-sentinel /root/redis/s3/sentinel.conf > /root/redis/s3/s3.log 2>&1 &


启动哨兵后,关闭7001,哨兵会自动选取新的节点作为主节点。


推荐您阅读更多有关于“ 集群 节点 redis Centos7 主从 哨兵 ”的文章

上一篇:Centos7上Redis主从集群 下一篇:Centos7配置Redis主从关系

猜你喜欢

发表评论:

评论:

回复 Java小强 评论于 2022-01-25 14:13
7001关闭,再启动,哨兵日志:
109164:X 25 Jan 2022 14:02:06.191 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
109164:X 25 Jan 2022 14:02:06.191 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=109164, just started
109164:X 25 Jan 2022 14:02:06.191 # Configuration loaded
109164:X 25 Jan 2022 14:02:06.191 * Increased maximum number of open files to 10032 (it was originally set to 1024).
109164:X 25 Jan 2022 14:02:06.191 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
(    '      ,       .-`  | `,    )     Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 27001
|    `-._   `._    /     _.-'    |     PID: 109164
  `-._    `-._  `-./  _.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                  
|    `-._`-._        _.-'_.-'    |           https://redis.io      
  `-._    `-._`-.__.-'_.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                  
|    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              

109164:X 25 Jan 2022 14:02:06.192 # Sentinel ID is 1dc0f171c67b5e28813b6b8acc7ba009938630a8
109164:X 25 Jan 2022 14:02:06.192 # +monitor master mymaster 192.168.3.22 7001 quorum 2
109164:X 25 Jan 2022 14:02:11.256 # +sdown sentinel 3cefe8c322c7c275cb904fad3c7b1b02d0f12f4a 192.168.3.22 27002 @ mymaster 192.168.3.22 7001
109164:X 25 Jan 2022 14:02:11.256 # +sdown sentinel b3a2d0d7a86c05ea3ff74e583999018834ab056d 192.168.3.22 27003 @ mymaster 192.168.3.22 7001
109164:X 25 Jan 2022 14:02:16.722 # -sdown sentinel 3cefe8c322c7c275cb904fad3c7b1b02d0f12f4a 192.168.3.22 27002 @ mymaster 192.168.3.22 7001
109164:X 25 Jan 2022 14:02:26.933 # -sdown sentinel b3a2d0d7a86c05ea3ff74e583999018834ab056d 192.168.3.22 27003 @ mymaster 192.168.3.22 7001
109164:X 25 Jan 2022 14:03:25.591 # +sdown master mymaster 192.168.3.22 7001
109164:X 25 Jan 2022 14:03:25.701 # +new-epoch 1
109164:X 25 Jan 2022 14:03:25.709 # +vote-for-leader b3a2d0d7a86c05ea3ff74e583999018834ab056d 1
109164:X 25 Jan 2022 14:03:26.649 # +odown master mymaster 192.168.3.22 7001 #quorum 3/2
109164:X 25 Jan 2022 14:03:26.649 # Next failover delay: I will not start a failover before Tue Jan 25 14:05:25 2022
109164:X 25 Jan 2022 14:03:26.825 # +config-update-from sentinel b3a2d0d7a86c05ea3ff74e583999018834ab056d 192.168.3.22 27003 @ mymaster 192.168.3.22 7001
109164:X 25 Jan 2022 14:03:26.825 # +switch-master mymaster 192.168.3.22 7001 192.168.3.22 7003
109164:X 25 Jan 2022 14:03:26.825 * +slave slave 192.168.3.22:7002 192.168.3.22 7002 @ mymaster 192.168.3.22 7003
109164:X 25 Jan 2022 14:03:26.825 * +slave slave 192.168.3.22:7001 192.168.3.22 7001 @ mymaster 192.168.3.22 7003
109164:X 25 Jan 2022 14:03:31.848 # +sdown slave 192.168.3.22:7001 192.168.3.22 7001 @ mymaster 192.168.3.22 7003