前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redi哨兵集群环境搭建

Redi哨兵集群环境搭建

原创
作者头像
Ehco
修改2024-01-01 17:25:43
2320
修改2024-01-01 17:25:43
举报
文章被收录于专栏:后端与架构

1.部署图

单宿主机模拟集群
单宿主机模拟集群

2.下载地址

代码语言:shell
复制
wget https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.2.3

解压

代码语言:shell
复制
tar -xzvf 7.2.3

3.编译与安装

切换到src目录

代码语言:shell
复制
make

4主节点配置

创建目录

代码语言:shell
复制
mkdir master-6379 slave-6380 slave-6381 sentinel-26379 sentinel-26380 sentinel-26381

拷贝redis.conf到master-6379目录

代码语言:shell
复制
cp redis.conf ../master-6379

配置节点

4.1 主节点配置

代码语言:shell
复制
#允许所有网络访问,根据需要指定
bind 0.0.0.0
# 保护模式设置为no,允许其他主机访问
protected-mode no
#设置密码
requirepass 123456
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes
pidfile /var/run/redis_6379.pid
#设置logfile
logfile /var/log/redis_6379
#主从同步master密码
masterauth 123456

4.2 从节点配置

复制主节点配置到从节点目录,修改配置文件;以下为修改点

代码语言:shell
复制
port 6380
pidfile /var/run/redis_6380.pid
logfile /var/log/redis_6380
#replicaof <masterip> <masterport>
replicaof 10.211.55.19 6379
代码语言:shell
复制
port 6381
pidfile /var/run/redis_6381.pid
logfile /var/log/redis_6381
#replicaof <masterip> <masterport>
replicaof 10.211.55.19 6379

4.3 哨兵节点配置

如下为哨兵节点配置,三个哨兵节点需要修改端口,pidfile和logfile配置

代码语言:shell
复制
protected-mode no
#设置端口
port 26379
pidfile /var/run/redis-sentinel-26379.pid
logfile /var/log/redis-sentinel-26379
#设置监听主节点
sentinel monitor mymaster 10.211.55.19 6379 2
#配置密码
sentinel auth-pass mymaster 123456

5.启动集群

启动数据节点

代码语言:shell
复制
[root@10 src]# ./redis-server ../../master-6379/redis.conf 
[root@10 src]# ./redis-server ../../slave-6380/redis.conf 
[root@10 src]# ./redis-server ../../slave-6381/redis.conf 

启动哨兵

代码语言:shell
复制
[root@10 src]# ./redis-sentinel ../../sentinel-26379/sentinel.conf 
[root@10 src]# ./redis-sentinel ../../sentinel-26380/sentinel.conf 
[root@10 src]# ./redis-sentinel ../../sentinel-26381/sentinel.conf 

6.查看集群状态

代码语言:shell
复制
#集群链接,默认端口,密码123456
./redis-cli -a 123456

[root@10 src]# ./redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.211.55.19,port=6380,state=online,offset=2998,lag=1
slave1:ip=10.211.55.19,port=6381,state=online,offset=2998,lag=1
master_failover_state:no-failover
master_replid:93a66d0147afbcf864e4d86abaf093d4c5fb4a03
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3137
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3137

7.高可用测试

停用主节点,集群仍然可用

选举过程
选举过程
集群状态
集群状态
重启主节点,已变为从节点
重启主节点,已变为从节点

8.SpringBoot连接测试

引入依赖及SpringBoot配置

代码语言:shell
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
代码语言:properties
复制
spring.redis.password=123456
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=10.211.55.19:26379,10.211.55.19:26380,10.211.55.19:26381

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.部署图
  • 2.下载地址
  • 3.编译与安装
  • 4主节点配置
  • 5.启动集群
  • 6.查看集群状态
  • 7.高可用测试
  • 8.SpringBoot连接测试
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档