企业级NoSql数据库Redis集群:主从 + 哨兵 + Cluster 三架马车全记录

关系型数据库,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库 中的数据主流的 MySQL、Oracle、MS SQL Server 和 DB2 都属于这类传统数据库。

NoSQL 数据库,全称为 Not Only SQL,意思就是适用关系型数据库的时候就使用关系型数据库,不适 用的时候也没有必要非使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键 值存储(memcached、Redis)、永久性键值存储(ROMA、Redis)、面向文档的数据库 (MongoDB、CouchDB)、面向列的数据库(Cassandra、HBase),每种 NoSQL 都有其特有的使用 场景及优点。

一、背景:为什么需要 Redis 集群?

维度传统 RDBMSRedis(NoSQL)
数据模型二维表 + 关系键值 / 数据结构
扩展性垂直扩容昂贵水平扩容简单
延迟ms ~ sμs ~ ms
并发万级 QPS10W+ QPS
事务ACID 强一致弱事务 / 最终一致

随着互联网流量暴涨,单机 Redis 很快会遇到内存、网卡、CPU三重瓶颈,因此需要:

  1. 主从复制:读写分离,容灾备份

  2. Sentinel:高可用,自动故障转移

  3. Cluster:分片存储,横向扩展 16384 槽位


二、环境总览

节点IP角色端口
redis-node1172.25.254.10Master & Cluster Master6379
redis-node2172.25.254.20Slave → Sentinel → Cluster Master6379
redis-node3172.25.254.30Slave → Sentinel → Cluster Master6379
…………扩容节点……

三、安装 Redis(源码 & 系统服务)

官方下载地址: http://download.redis.io/releases/

  1. 依赖 & 编译

    dnf install -y gcc make initscripts
    tar zxf redis-7.4.0.tar.gz && cd redis-7.4.0
    make && make install
  2. 做成 systemd 服务

    # 修改 utils/install_server.sh 第 70 行,注释掉以下代码块:
        #_pid_1_exe="$(readlink -f /proc/1/exe)"
        #if [ "${_pid_1_exe##*/}" = systemd ]
        #then
        #    echo "This systems seems to use systemd."
        #    exit 1
        #fi
    vim /etc/redis/6379.conf
    修改项:
    bind * -::*  # 允许所有IPv4和IPv6地址访问
    
    /etc/init.d/redis_6379 restart
    
    netstat -antlpe | grep redis

    编译过程中报错了,已经生成了一些东西,影响重新编译了

  3. 首次启动

    systemctl daemon-reload
    systemctl enable --now redis_6379
    netstat -antlp | grep 6379

复制到其他主机

-a所有都同步


四、Redis的基本操作

1. 查看配置

CONFIG GET bind
  • CONFIG:配置管理命令

  • GET:读取动作

  • bind:要查询的参数名
    返回列表第 2 个元素即为当前值。


2. 查看全部配置

CONFIG GET *
  • **星号 ***:通配符,一次性拉取所有键值对。


3. 写入 & 读取

SET name lee
GET name

字段含义
SET创建或覆盖键
name键名
lee键值
GET根据键取值

4. 写入并带过期时间

SET name lee ex 5
  • ex 5EX 表示秒级过期,数字 5 即 5 s 后自动删除。

  • 过期后再 GET,返回 (nil)


5. 查看所有键

KEYS *
  • **星号 ***:匹配任意字符,列出当前 DB 全部键。


6. 选择数据库

SELECT 1
  • SELECT:切换逻辑库

  • 1:库编号,范围 0-15,超出返回 ERR DB index is out of range


7. 移动键到其他库

MOVE name 1
  • MOVE 键名 目标库编号

  • 成功返回 (integer) 1,失败返回 0。


8. 修改键名

RENAME name id
  • RENAME 旧键名 新键名

  • 改名后旧键名立即失效。


9. 重新设置过期

EXPIRE name 3
  • EXPIRE 键名 秒数

  • 返回 1 表示设置成功,0 表示键不存在或已设置持久化。


10. 持久化提示

/var/lib/redis/6379/dump.rdb
  • Redis 默认 RDB 快照路径,未过期数据会随服务重启自动恢复。

五、主从复制(一主两从)

  1. 主节点配置(10)
    可以关闭安全模式,这样做主从切换时,就不要一直输密码了

  2. 从节点配置(20 & 30)

    replicaof 172.25.254.100 6379
    masterauth 123456
    systemctl restart redis_6379
  3. 验证

    redis-cli -a 123456 info replication
    # role:master
    # connected_slaves:2


六、哨兵 Sentinel(高可用)

参数含义
sentinel monitor mymaster 100 6379 2监控名 & 投票阈值
down-after-milliseconds 1000010s 无响应 → SDOWN
failover-timeout 1800003 min 内必须完成故障转

在所有阶段中关闭 protected-mode no 1.在master节点中

#编辑配置文件
[root@redis-node1 ~]# cd redis-7.4.0/
[root@redis-node1 redis-7.4.0]# cp sentinel.conf  /etc/redis/
[root@redis-node1 redis-7.4.0]# vim /etc/redis/sentinel.conf
protected-mode no                                   
port 26379                                          
daemonize no                                        
pidfile /var/run/redis-sentinel.pid                 
loglevel notice                                     
sentinel monitor mymaster 172.25.254.100 6379 2     
机,2表示必须得到2票
sentinel down-after-milliseconds mymaster 10000     
master下线
sentinel parallel-syncs mymaster 1                  
master数据的slave数量
sentinel failover-timeout mymaster 180000           
#关闭保护模式
#监听端口
#进入不打如后台
#日志级别
#sentinel进程pid文件
#创建sentinel监控监控master主
#master中断时长,10秒连不上视为
#发生故障转移后,同时开始同步新
#整个故障切换的超时时间为3分钟
####复制配置文件到其他阶段
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf  
root@172.25.254.201:/etc/redis/
root@172.25.254.201's password:
sentinel.conf                                                                     
100%   
14KB   
9.7MB/s   
00:00
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf  
root@172.25.254.200:/etc/redis/
root@172.25.254.200's password:
sentinel.conf               

2 启动服务

[root@redis-node1 redis-7.4.0]# redis-sentinel  /etc/redis/sentinel.conf

注意:/etc/redis/sentinel.conf 文件在用哨兵程序调用后会更改其配置文件,如果需要重新做需要删掉文 件重新编辑

测试:

在开一个master节点终端
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN

 [root@redis-node2 ~]# redis-cli
 127.0.0.1:6379> info replications
 127.0.0.1:6379> info replication
 # Replication
 role:master
 connected_slaves:2
 slave0:ip=172.25.254.201,port=6379,state=online,offset=211455,lag=1
 slave1:ip=172.25.254.100,port=6379,state=online,offset=211455,lag=1
 master_failover_state:no-failover
 master_replid:d42fd72f3dfae94c84ca722ad1653417495ef4fd
 master_replid2:290c3407108cc6120086981b7149a6fa377594c4
 master_repl_offset:211598
 second_repl_offset:185931
 repl_backlog_active:1
 repl_backlog_size:1048576
 repl_backlog_first_byte_offset:150986
 repl_backlog_histlen:60613

两台都反应了故障,master转移了

恢复服务:


七、Redis Cluster(无中心分片)

假如三个主节点分别是:A, B, C 三个节点,采用哈希槽 (hash slot)的方式来分配16384个slot 的话它们 三个节点分别承担的slot 区间可以是:

节点A覆盖 0-5460

节点B覆盖 5461-10922

节点C覆盖 10923-16383

Redis cluster 主从架构 Redis cluster的架构虽然解决了并发的问题,但是又引入了一个新的问题,每个Redis master的高可用 如何解决?

那就是对每个master 节点都实现主从复制,从而实现 redis 高可用性

Redis使用的是内存(工业产物,具有大小的限制)

哨兵可以解决高可用问题,master发生故障可以及时切换,但是不能解决单机写入的瓶颈问题

cluster集群的作用就是,原本只有一台主机来写入的数据,现在平等的分配给集群中的每一台主机,大大提升了写入的大小

这里要删除之前的实验环境,保证一个干净的环境

删除源码编译下载的Redis

uninstall make

创建redis cluster的前提

1.每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。

2.每个节点必须开启的参数 cluster-enabled yes #必须开启集群状态,开启后redis进程会有cluster显示 cluster-config-file nodes-6380.conf #此文件有redis cluster集群自动创建和维护,不需要任何手 动操作

3.所有redis服务器必须没有任何数据 4.先启动为单机redis且没有任何key value

[root@redis-masterx ~]# vim /etc/redis/redis.conf
masterauth "123456"					#集群主从认证
requirepass "123456"				#redis登陆密码	redis-cli 命令连接redis后要用“auth 密码”进行认证
cluster-enabled yes					#开启cluster集群功能
cluster-config-file nodes-6379.conf	#指定集群配置文件
cluster-node-timeout 15000			#节点加入集群的超时时间单位是ms
 
[root@redis-master1 ~]# systemctl restart redis.service
[root@redis-master1 ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info
 
# Cluster
cluster_enabled:1

redis-cli --cluster 参数说明

[root@redis-master1 ~]# redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN                #创建集群
                     --cluster-replicas <arg>                     #指定master的副本数
  check          <host:port> or <host> <port>            #检测集群信息

  info              <host:port> or <host> <port>            #查看集群信息
  fix            <host:port> or <host> <port>                #修复集群

  reshard        <host:port> or <host> <port>            #在线热迁移集群指定主机的slots数据
          
  rebalance      <host:port> or <host> <port>         #平衡各集群主机的slot数量
       
  add-node       new_host:new_port existing_host:existing_port    #添加主机

  del-node       host:port node_id                            #删除主机

  import         host:port                                            #导入外部redis服务器的数据到当前集群

查看集群信息

八、集群扩/缩容实战

扩容(新增 1 主 1 从)

# 1. 加入空节点
redis-cli --cluster add-node 172.25.254.40:6379 172.25.254.10:6379
# 2. 迁移槽位
redis-cli --cluster reshard 172.25.254.10:6379 \
--cluster-from all --cluster-to <new-master-id> --cluster-slots 4096
# 3. 绑定从节点
redis-cli --cluster add-node 172.25.254.140:6379 172.25.254.10:6379 \
--cluster-slave --cluster-master-id <new-master-id>

缩容(下线节点)

口诀:先迁槽,再删节点

# 1. 把待下线主节点槽位迁空
redis-cli --cluster reshard 172.25.254.20:6379
# 2. 删除节点
redis-cli --cluster del-node 172.25.254.20:6379 <node-id>

但是此时70主机上是没有哈希层的,从别的主机上来


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值