十三、Linux(CentOS7) Redis集群模式和哨兵模式配置

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 一、Redis集群配置创建集群目录mkdir -p /usr/local/redis-clustercd /usr/local/redis-clustermkdir 6379 6378修改配置文件vi redis.conf

一、Redis集群配置


创建集群目录


mkdir -p /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir 6379 6378


修改配置文件


vi redis.conf


daemonize yes
port 6379
dir /usr/local/redis-cluster/6379/
cluster-enabled yes #启动集群模式
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
bind 0.0.0.0
protected-mode no
appendonly yes
#如果要设置密码需要增加如下配置:
 #(设置redis访问密码)
requirepass 123456
 #(设置集群节点间访问密码,跟上面一致)
masterauth 123456


把修改后的配置文件,copy到6379、6378,修改第2、3、5项里的端口号,可以用批量

%s/源字符串/目的字符串/g


启动redis:


./redis-server  /usr/local/redis-cluster/6379/redis.conf
./redis-server  /usr/local/redis-cluster/6378/redis.conf


查看是否启动成功


ps -ef | grep redis


用redis-cli创建整个redis集群:


测试环境:


./redis-cli --cluster create 192.168.10.195:6379 192.168.10.195:6378 192.168.10.124:6379 192.168.10.124:6378 192.168.10.100:6379 192.168.10.100:6378 --cluster-replicas 1 -a sbjcptTest


生产环境:


./redis-cli --cluster create 10.1.8.111:6301 10.1.8.111:6302 10.1.8.112:6303 10.1.8.112:6304 10.1.8.113:6305 10.1.8.113:6306 --cluster-replicas 1 -a sbjcptTest


验证集群:


./redis-cli -c -a sbjcptTest -h 192.168.10.195 -p 6379
./redis-cli -c -a xxx -h 10.1.8.111 -p 6301
./redis-cli -c -a sbjcptTest -h 10.1.8.112 -p 6301


常用命令:


# 查看集群信息
cluster info
# 查看节点列表
cluster nodes


二、Redis哨兵模式配置(主备):


创建数据存放目录


mkdir /data
mkdir /data/redis
mkdir /data/redis/redis-log
mkdir /data/redis/data


首先配置Redis的主服务器,修改redis.conf文件如下


# 使得Redis服务器可以跨网络访问
bind 0.0.0.0
dir "/data/redis/data"
daemonize yes
logfile "/data/redis/redis-log/redis.log"


# 设置密码
requirepass "123456"


# 主服务器密码,注意:有关slaveof的配置只是配置从服务器,主服务器不需要配置
masterauth 123456


配置Redis的从服务器,修改配置文件redis.conf


# 使得Redis服务器可以跨网络访问
bind 0.0.0.0
dir "/data/redis/data"
daemonize yes
logfile "/data/redis/redis-log/redis.log"
# 设置密码
requirepass "123456"
# 主服务器密码,,这个都要配置,不然主从无法用
masterauth 123456
# 注意:有关slaveof的配置只是配置从服务器,主服务器不需要配置
slaveof 192.168.10.195 6379
# 关闭防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
# 启动:./redis-server ../redis.conf
# 查看集群是否正常:
redis-cli  -h 192.168.10.195 -p 6379 -a 123456 info Replication
[root@localhost src]# redis-cli  -h 192.168.10.195 -p 6379 -a 123456 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.


# Replication
role:master
connected_slaves:2
slave0:ip=192.168.10.100,port=6379,state=online,offset=70,lag=0
slave1:ip=192.168.10.124,port=6379,state=online,offset=70,lag=0
master_replid:808f22bacf3af9192301aba5c63afff7d60f3b41
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
# 测试
redis-cli  -h 192.168.10.195 -p 6379 -a 123456
AUTH 123456
set k1 v1
exit
# 测试
redis-cli  -h 192.168.10.124 -p 6379 -a 123456
AUTH 123456
get k1


安装哨兵


# 3台Redis服务器都需执行
vi   sentinel.conf


mkdir /data/redis/sentinel-log
dataport 26379
protected-mode no
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/data/redis/sentinel-log/sentinel.log"
dir /tmp
sentinel monitor mymaster 192.168.10.195 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456


启动哨兵:


./redis-sentinel  ../sentinel.conf


测试查看哨兵:


./redis-cli -h 192.168.10.195 -p 26379 INFO Sentinel
./redis-cli  -h 192.168.10.195 -p 6379 -a 123456 info Replication
./redis-cli -h 10.1.8.112 -p 26379 INFO Sentinel
./redis-cli  -h 10.1.8.112 -p 6379 -a 123456 info Replication


关闭命令:


pkill redis-sentinel
pkill redis-server


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
8天前
|
存储 运维 NoSQL
Redis Cluster集群模式部署
Redis Cluster集群模式部署
31 4
|
8天前
|
缓存 运维 NoSQL
Redis主从模式部署
Redis主从模式部署
31 4
|
8天前
|
运维 监控 NoSQL
Redis Sentinel哨兵模式部署
Redis Sentinel哨兵模式部署
31 2
|
10天前
|
NoSQL Linux 网络安全
基于 centOS7 的 redis 安装
基于 centOS7 的 redis 安装
39 1
|
17天前
|
NoSQL 关系型数据库 应用服务中间件
jdk1.8、mysql、redis、nginx centos云服务器安装配置
jdk1.8、mysql、redis、nginx centos云服务器安装配置
|
19天前
|
Linux
centos bond多网关配置 bond多网关路由
centos bond多网关配置 bond多网关路由
|
24天前
|
存储 负载均衡 监控
redis 集群模式(redis cluster)介绍
redis 集群模式(redis cluster)介绍
|
13天前
|
运维 NoSQL Serverless
Serverless 应用引擎产品使用合集之需要配置什么才能够使用Redis
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
18天前
|
监控 NoSQL Redis
Redis哨兵,Redis哨兵核心功能如何一个云服务器完成6个节点的搭建-docker什么是docker是否可以把六个容器,都写到同一个ym配置中,一次都启动,不就直接保证互通问题了吗?
Redis哨兵,Redis哨兵核心功能如何一个云服务器完成6个节点的搭建-docker什么是docker是否可以把六个容器,都写到同一个ym配置中,一次都启动,不就直接保证互通问题了吗?
|
18天前
|
存储 NoSQL 网络协议
主从复制,Could not connect to Redis at 127.0.0.1:6380: Connection refusednot connected> exit,1.主从模式如何指
主从复制,Could not connect to Redis at 127.0.0.1:6380: Connection refusednot connected> exit,1.主从模式如何指