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跟数据库的数据同步问题

《详谈redis跟数据库的数据同步问题》文章讨论了在Redis和数据库数据一致性问题上的解决方案,主要比较了先更新Redis缓存再更新数据库和先更新数据库再更新Redis缓存两种方案,文章指出,删除R... 目录一、Redis 数据库数据一致性的解决方案1.1、更新Redis缓存、删除Redis缓存的区别二

Redis与缓存解读

《Redis与缓存解读》文章介绍了Redis作为缓存层的优势和缺点,并分析了六种缓存更新策略,包括超时剔除、先删缓存再更新数据库、旁路缓存、先更新数据库再删缓存、先更新数据库再更新缓存、读写穿透和异步... 目录缓存缓存优缺点缓存更新策略超时剔除先删缓存再更新数据库旁路缓存(先更新数据库,再删缓存)先更新数

Redis事务与数据持久化方式

《Redis事务与数据持久化方式》该文档主要介绍了Redis事务和持久化机制,事务通过将多个命令打包执行,而持久化则通过快照(RDB)和追加式文件(AOF)两种方式将内存数据保存到磁盘,以防止数据丢失... 目录一、Redis 事务1.1 事务本质1.2 数据库事务与redis事务1.2.1 数据库事务1.

mac安装redis全过程

《mac安装redis全过程》文章内容主要介绍了如何从官网下载指定版本的Redis,以及如何在自定义目录下安装和启动Redis,还提到了如何修改Redis的密码和配置文件,以及使用RedisInsig... 目录MAC安装Redis安装启动redis 配置redis 常用命令总结mac安装redis官网下

Mycat搭建分库分表方式

《Mycat搭建分库分表方式》文章介绍了如何使用分库分表架构来解决单表数据量过大带来的性能和存储容量限制的问题,通过在一对主从复制节点上配置数据源,并使用分片算法将数据分配到不同的数据库表中,可以有效... 目录分库分表解决的问题分库分表架构添加数据验证结果 总结分库分表解决的问题单表数据量过大带来的性能

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

Redis分布式锁使用及说明

《Redis分布式锁使用及说明》本文总结了Redis和Zookeeper在高可用性和高一致性场景下的应用,并详细介绍了Redis的分布式锁实现方式,包括使用Lua脚本和续期机制,最后,提到了RedLo... 目录Redis分布式锁加锁方式怎么会解错锁?举个小案例吧解锁方式续期总结Redis分布式锁如果追求

如何在一台服务器上使用docker运行kafka集群

《如何在一台服务器上使用docker运行kafka集群》文章详细介绍了如何在一台服务器上使用Docker运行Kafka集群,包括拉取镜像、创建网络、启动Kafka容器、检查运行状态、编写启动和关闭脚本... 目录1.拉取镜像2.创建集群之间通信的网络3.将zookeeper加入到网络中4.启动kafka集群