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

相关文章

Spring Boot整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三

Redis中Set结构使用过程与原理说明

《Redis中Set结构使用过程与原理说明》本文解析了RedisSet数据结构,涵盖其基本操作(如添加、查找)、集合运算(交并差)、底层实现(intset与hashtable自动切换机制)、典型应用场... 目录开篇:从购物车到Redis Set一、Redis Set的基本操作1.1 编程常用命令1.2 集

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

k8s搭建nfs共享存储实践

《k8s搭建nfs共享存储实践》本文介绍NFS服务端搭建与客户端配置,涵盖安装工具、目录设置及服务启动,随后讲解K8S中NFS动态存储部署,包括创建命名空间、ServiceAccount、RBAC权限... 目录1. NFS搭建1.1 部署NFS服务端1.1.1 下载nfs-utils和rpcbind1.1