redis灵魂拷问:怎么搭建一个哨兵主从集群

2024-01-31 13:50

本文主要是介绍redis灵魂拷问:怎么搭建一个哨兵主从集群,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

实验环境准备

 安装redis主从集群

安装哨兵集群 

测试 

 总结


一直在使用redis,但是从来没有搭建过redis集群,今天来从0到1搭建一套redis哨兵主从集群。

实验环境准备

1.本地部署环境:
vmware虚机:2个(192.168.59.132和192.168.59.141)
操作虚机系统:centos7
192.168.59.132:部署一个redis实例作为主节点,端口6379,部署一个哨兵节点,端口26379
192.168.59.141:部署了2个实例,端口分别是6379和6389,作为192.168.59.132上实例的从节点,部署两个哨兵节点,端口26379和26389
redis版本:6.0.7

2.升级gcc
注意centos7默认gcc版本是4.8.5,但是安装6.0.7需要使用gcc版本是5.3以上,所以先升级gcc

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

这时我们再查看一下gcc版本,可以看到升级成功了:

[root@master redis-6.0.7]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC) 

3.编译安装包
官网下载redis安装包后,我们进行安装

tar -xvf redis-6.0.7.tar.gz
cd redis-6.0.7/
make
make install PREFIX=/usr/local/redis

 安装redis主从集群

安装前提示:
1.redis的日志路径和日志文件都需要创建好,redis不会主动创建
2.如果启动不指定redis.conf文件,会使用默认安装路径下的文件,在/usr/local/redis/etc这个目录

上一步编译成功后,我们修改主节点机器上(192.168.59.132)redis.conf文件,修改下面3行

#bind 127.0.0.1
bind 19.168.59.132
# requirepass foobared
requirepass foobared
#logfile ""
logfile /root/redis/redis-6.0.7/logs/redis-6379.log

 之后进入src目录,执行如下命令后启动成功:

[root@master redis-6.0.7]# ./src/redis-server redis.conf 
37652:C 05 Sep 2020 06:22:39.156 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
37652:C 05 Sep 2020 06:22:39.156 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=37652, just started
37652:C 05 Sep 2020 06:22:39.156 # Configuration loaded
37652:M 05 Sep 2020 06:22:39.157 * Increased maximum number of open files to 10032 (it was originally set to 1024)._._                                                  _.-``__ ''-._                                             _.-``    `.  `_.  ''-._           Redis 6.0.7 (00000000/0) 64 bit.-`` .-```.  ```\/    _.,_ ''-._                                   (    '      ,       .-`  | `,    )     Running in standalone mode|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379|    `-._   `._    /     _.-'    |     PID: 37652`-._    `-._  `-./  _.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |           http://redis.io        `-._    `-._`-.__.-'_.-'    _.-'                                   |`-._`-._    `-.__.-'    _.-'_.-'|                                  |    `-._`-._        _.-'_.-'    |                                  `-._    `-._`-.__.-'_.-'    _.-'                                   `-._    `-.__.-'    _.-'                                       `-._        _.-'                                           `-.__.-'                                               37652:M 05 Sep 2020 06:22:39.159 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
37652:M 05 Sep 2020 06:22:39.159 # Server initialized
37652:M 05 Sep 2020 06:22:39.159 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
37652:M 05 Sep 2020 06:22:39.164 * Loading RDB produced by version 6.0.7
37652:M 05 Sep 2020 06:22:39.164 * RDB age 38 seconds
37652:M 05 Sep 2020 06:22:39.164 * RDB memory usage when created 0.77 Mb
37652:M 05 Sep 2020 06:22:39.164 * DB loaded from disk: 0.000 seconds
37652:M 05 Sep 2020 06:22:39.164 * Ready to accept connections

使用客户端测试,可以看到已经启动成功:

[root@master redis-6.0.7]# ./src/redis-cli -h 192.168.59.132 -a foobared
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.59.132:6379> keys *
(empty array)

 修改从节点机器(192.168.59.141)redis.conf文件,增加下面2行:

#下面内容新增,作为192.168.59.132的从节点,同时需要密码认证
slaveof 192.168.59.132 6379
masterauth foobared

安装好以后,我们在192.168.59.141这个虚机的redis解压目录下,copy一份redis.conf到redis6389.conf,修改下面3行然后增加2行从节点的配置:

#pidfile /var/run/redis_6379.pid
pidfile /var/run/redis_6389.pid
#port 6379
port 6389
#logfile ""
logfile /root/redis/redis-6.0.7/logs/redis-6389.log
#下面内容新增,作为192.168.59.132的从节点,同时需要密码认证
slaveof 192.168.59.132 6379
masterauth foobared

 之后启动这2个redis实例,命令如下:

./src/redis-server redis.conf
./src/redis-server redis6389.conf

启动成功后我们进入主节点客户端,查看从节点状态:

[root@master redis-6.0.7]# ./src/redis-cli -h 192.168.59.132 -a foobared
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.59.132:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.59.141,port=6379,state=online,offset=1176,lag=0
slave1:ip=192.168.59.141,port=6389,state=online,offset=1176,lag=1
master_replid:bcd22e05007e87182251ab6b24601878c89f85f0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1176
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1176

到这里,集群就搭建成功了。我们写入一个数据:

192.168.59.132:6379> set foo 123456
OK

之后我们到从节点查看,可以看到,数据同步成功:

[root@worker1 redis-6.0.7]# ./src/redis-cli -h 192.168.59.141 -p 6389 -a foobared
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.59.141:6389> get foo
"123456"

安装哨兵集群 

 哨兵的安装我选择在192.168.59.132安装一个实例,在192.168.59.141安装2个实例,192.168.59.132上sentinel.conf的配置修改内容如下:

# protected-mode no
protected-mode no
# bind 127.0.0.1 192.168.1.1
bind 192.168.59.132
#daemonize no
daemonize yes
#logfile ""
logfile /root/redis/redis-6.0.7/logs/sentinel-26379.log
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor master 192.168.59.132 6379 2
#sentinel failover-timeout mymaster 180000
sentinel failover-timeout master 180000
#sentinel down-after-milliseconds mymaster 30000
sentinel down-after-milliseconds master 30000
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
sentinel auth-pass master foobared

192.168.59.141上sentinel26379.conf的修改跟上面一样,sentinel26389.conf的配置修改内容端口不一样,如下:

#port 26379
port 26389
# protected-mode no
protected-mode no
# bind 127.0.0.1 192.168.1.1
bind 192.168.59.132
#daemonize no
daemonize yes
#logfile ""
logfile /root/redis/redis-6.0.7/logs/sentinel-26389.log
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor master 192.168.59.132 6379 2
#sentinel failover-timeout mymaster 180000
sentinel failover-timeout master 180000
#sentinel down-after-milliseconds mymaster 30000
sentinel down-after-milliseconds master 30000
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
sentinel auth-pass master foobared

配置好后,执行启动命令:

src/./redis-sentinel sentinel.conf
src/./redis-sentinel sentinel26379.conf
src/./redis-sentinel sentinel26389.conf

 启动成功日志如下:

34887:X 05 Sep 2020 03:06:13.420 * Increased maximum number of open files to 10032 (it was originally set to 1024).
34887:X 05 Sep 2020 03:06:13.421 * Running mode=sentinel, port=26379.
34887:X 05 Sep 2020 03:06:13.421 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
34887:X 05 Sep 2020 03:06:13.422 # Sentinel ID is bd7bc3f82bef60606578a8aeb8bf1631e6d7f941
34887:X 05 Sep 2020 03:06:13.422 # +monitor master master 192.168.59.132 6379 quorum 2
34887:X 05 Sep 2020 03:06:13.423 * +slave slave 192.168.59.141:6379 192.168.59.141 6379 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:06:13.424 * +slave slave 192.168.59.141:6389 192.168.59.141 6389 @ master 192.168.59.132 6379

可见,已经对2个redis从节点进行了监控。

测试 

 我们把redis主节点kill调,查看哨兵日志:

34887:X 05 Sep 2020 03:12:09.696 # +sdown master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:09.799 # +odown master master 192.168.59.132 6379 #quorum 2/2
34887:X 05 Sep 2020 03:12:09.799 # +new-epoch 1
34887:X 05 Sep 2020 03:12:09.799 # +try-failover master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:09.802 # +vote-for-leader bd7bc3f82bef60606578a8aeb8bf1631e6d7f941 1
34887:X 05 Sep 2020 03:12:09.869 # bd7bc3f82bef60606578a8aeb8bf1631e6d7f941 voted for bd7bc3f82bef60606578a8aeb8bf1631e6d7f941 1
34887:X 05 Sep 2020 03:12:09.947 # +elected-leader master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:09.947 # +failover-state-select-slave master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.020 # +selected-slave slave 192.168.59.141:6389 192.168.59.141 6389 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.020 * +failover-state-send-slaveof-noone slave 192.168.59.141:6389 192.168.59.141 6389 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.097 * +failover-state-wait-promotion slave 192.168.59.141:6389 192.168.59.141 6389 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.400 # +promoted-slave slave 192.168.59.141:6389 192.168.59.141 6389 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.400 # +failover-state-reconf-slaves master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.469 * +slave-reconf-sent slave 192.168.59.141:6379 192.168.59.141 6379 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:10.945 # -odown master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:11.484 * +slave-reconf-inprog slave 192.168.59.141:6379 192.168.59.141 6379 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:11.484 * +slave-reconf-done slave 192.168.59.141:6379 192.168.59.141 6379 @ master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:11.540 # +failover-end master master 192.168.59.132 6379
34887:X 05 Sep 2020 03:12:11.540 # +switch-master master 192.168.59.132 6379 192.168.59.141 6389
34887:X 05 Sep 2020 03:12:11.541 * +slave slave 192.168.59.141:6379 192.168.59.141 6379 @ master 192.168.59.141 6389
34887:X 05 Sep 2020 03:12:11.541 * +slave slave 192.168.59.132:6379 192.168.59.132 6379 @ master 192.168.59.141 6389
34887:X 05 Sep 2020 03:12:41.597 # +sdown slave 192.168.59.132:6379 192.168.59.132 6379 @ master 192.168.59.141 6389

从日志中看出,master节点已经从192.168.59.132切换到了6379 192.168.59.141 6389,可见哨兵已经生效了。

我们在springboot中配置redis,配置如下:

#------redis配置------------#
spring.redis.database=0
#spring.redis.host=192.168.59.138
#spring.redis.password=
#spring.redis.port=6379
#spring.redis.port=6379
spring.redis.sentinel.master=master
#配置哨兵节点
spring.redis.sentinel.nodes=192.168.59.132:26379,192.168.59.141:26379,192.168.59.141:26389
spring.redis.password=foobared
spring.redis.timeout=5000
#最大连接数
spring.redis.lettuce.pool.max-active=50
#最大阻塞等待时间
spring.redis.lettuce.pool.max-wait=5000
#连接池中最大空闲连接
spring.redis.lettuce.pool.max-idle=50
#连接池中最小空闲连接
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.time-between-eviction-runs=1
spring.main.allow-bean-definition-overriding=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

下面代码输出12345678,可以看到集群已经可以使用

@Test
public void testGet(){//Assert.assertEquals(redisTemplate.opsForValue().get("zhujinjun"), "123456");redisTemplate.opsForValue().set("foo", "12345678");System.out.println(redisTemplate.opsForValue().get("foo"));
}

上面源代码地址:

https://github.com/jinjunzhu/spring-boot-mybatis.git

 总结

 本文主要讲述了redis哨兵主从集群的搭建,这个过程不算太复杂,有一些详细参数放到后面在讲解。感兴趣的同学欢迎参考我们的实验案例搭建集群。

 

微信公众号,欢迎关注,一起学习成长

 

这篇关于redis灵魂拷问:怎么搭建一个哨兵主从集群的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/663989

相关文章

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Redis实现分布式锁全过程

《Redis实现分布式锁全过程》文章介绍Redis实现分布式锁的方法,包括使用SETNX和EXPIRE命令确保互斥性与防死锁,Redisson客户端提供的便捷接口,以及Redlock算法通过多节点共识... 目录Redis实现分布式锁1. 分布式锁的基本原理2. 使用 Redis 实现分布式锁2.1 获取锁

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject