本文主要是介绍全网最最最最详细的企业级NoSql数据库Redis集群,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一 关系型数据库和 NoSQL 数据库
1.1 数据库主要分为两大类:关系型数据库与 NoSQL 数据库
1.2 为什么还要用 NoSQL 数据库呢?
二 Remote Dictionary Server 简介
2.1 什么是redis
2.2:redis特性
2.3 Redis应用场景
2.4 缓存的实现流程
三 Redis的安装
3.1 rpm包方式安装
3.2 源码安装
四:Redis 主从复制
4.1:环境配置
4.2:配置主从同步
4.21.修改mastser节点的配置文件
4.22.配置slave节点
4.23:测试效果
4.3:主从同步过程
五:Redis的哨兵(高可用)
5.1:Redis哨兵
5.2:哨兵的实验过程
5.3:在整个架构中可能会出现的问题
六:Redis Cluster(无中心化设计)
6.1 :Redis Cluster 工作原理
6.2:创建redis cluster的前提
6.3:部署redis cluster
6.4 :redis-cli --cluster 参数说明
6.5:创建redis-cluster
6.6:集群扩容
6.7:clsuter集群维护
一 关系型数据库和 NoSQL 数据库
1.1 数据库主要分为两大类:关系型数据库与 NoSQL 数据库
1.2 为什么还要用 NoSQL 数据库呢?
二 Remote Dictionary Server 简介
2.1 什么是redis
2.2:redis特性
- 速度快: 10W QPS,基于内存,C语言实现
- 单线程
- 持久化
- 支持多种数据结构
- 支持多种编程语言
- 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
- 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
- 主从复制
- 支持高可用和分布式
- 纯内存
- 非阻塞
- 避免线程切换和竞态消耗
2.3 Redis应用场景
- Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
- 缓存:数据查询、电商网站商品信息、新闻内容
- 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
- 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
- 消息队列:ELK的日志缓存、部分业务的订阅发布系统
- 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能
2.4 缓存的实现流程
三 Redis的安装
3.1 rpm包方式安装
[root@redis-node1 ~] # dnf install redis -y正在更新 Subscription Management 软件仓库。无法读取客户身份This system is not registered with an entitlement server. You can use "rhc" or"subscription-manager" to register.上次元数据过期检查: 0:01:14 前,执行于 2024 年 08 月 05 日 星期一 13 时 30 分 23 秒。依赖关系解决。=================================================================================================================软件包 架构 版本 仓库大小=================================================================================================================安装 :redis x86_64 6 .2.7-1.el9AppStream 1 .3 M事务概要=================================================================================================================安装 1 软件包总计: 1.3 M安装大小: 4.7 M
3.2 源码安装
# 解压源码包[root@redis-node1 ~] # tar zxf redis-7.4.0.tar.gz[root@redis-node1 ~] # lsredis-7.4.0 redis-7.4.0.tar.gz# 安装编译工具[root@redis-node1 redis-7.4.0] # dnf install make gcc initscripts-10.11.6-1.el9.x86_64 -y# 执行编译命令[root@redis-node1 redis-7.4.0] # make[root@redis-node1 redis-7.4.0] # make install# 启动 Redis[root@redis-node1 redis-7.4.0] # cd utils/[root@redis-node1 utils] # ./install_server.shWelcome to the redis service installerThis script will help you easily set up a running redis serverThis systems seems to use systemd. # 提示系统使用的是 systemd 的初始化方式Please take a look at the provided example service unit files in this directory,and adapt and install them. Sorry[root@redis-node1 utils] # vim install_server.sh # 解决报错问题#bail if this system is managed by systemd#_pid_1_exe="$(readlink -f /proc/1/exe)"#if [ "${_pid_1_exe##*/}" = systemd ]#then# echo "This systems seems to use systemd."# echo "Please take a look at the provided example service unit files inthis directory, and adapt and install them. Sorry!"# exit 1#fi[root@redis-node1 utils] # ./install_server.shWelcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] # 端口号Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf] # 配置文件Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log] # 日志 Selected default - /var/log/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] # 数据目录Selected default - /var/lib/redis/6379Please select the redis executable path [/usr/local/bin/redis-server] # 命令路径Selected config:Port : 6379Config file : /etc/redis/6379.confLog file : /var/log/redis_6379.logData dir : /var/lib/redis/6379Executable : /usr/local/bin/redis-serverCli Executable : /usr/local/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort.Copied /tmp/6379.conf = > /etc/init.d/redis_6379Installing service ...Successfully added to chkconfig!Successfully added to runlevels 345 !Starting Redis server...Installation successful!# 配置 redis[root@redis-node1 utils] # vim /etc/redis/6379.confbind * - ::*[root@redis-node1 utils] # /etc/init.d/redis_6379 restartStopping ...Redis stoppedStarting Redis server...[root@redis-node1 utils] # netstat -antlpe | grep redistcp 0 0 0 .0.0.0:6379 0 .0.0.0:* LISTEN 067267 38417 /redis-servertcp6 0 0 :::6379# 查看信息[root@redis-node1 utils]# redis-cli127.0.0.1:6379> info#复制到其他机器
[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root
[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.30:/root
[root@redis-node1 ~]# cd /usr/local/bin/
[root@redis-node1 bin]# ll
total 29440
-rwxr-xr-x 1 root root 6374112 Aug 25 14:43 redis-benchmark
lrwxrwxrwx 1 root root 12 Aug 25 14:43 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root 12 Aug 25 14:43 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 7215976 Aug 25 14:43 redis-cli
lrwxrwxrwx 1 root root 12 Aug 25 14:43 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 16548768 Aug 25 14:43 redis-server
[root@redis-node1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin
[root@redis-node1 bin]# rsync -al * root@172.25.254.30:/usr/local/bin
#node2\node3
[root@redis-node2 ~]# cd redis-7.4.0/
[root@redis-node2 redis-7.4.0]# dnf install initscripts -y
[root@redis-node2 redis-7.4.0]# cd utils/
[root@redis-node2 utils]# ./install_server.sh
[root@redis-node2 utils]# vim /etc/redis/6379.conf
bind * -::*
[root@redis-node2 utils]# /etc/init.d/redis_6379 restart
四:Redis 主从复制
4.1:环境配置
4.2:配置主从同步
4.21.修改mastser节点的配置文件
[root@redis-node1 & node2 & node3 ~] # vim /etc/redis/6379.confprotected-mode no # 关闭 protected 模式[root@redis-node1 &node2 & node3 ~] # /etc/init.d/redis_6379 restartStopping ...Redis stoppedStarting Redis server...
4.22.配置slave节点
[root@redis-node2 & 3 ~] # vim /etc/redis/6379.confreplicaof 172 .25.254.100 6379[root@redis-node2 & 3 ~] # /etc/init.d/redis_6379 restartStopping ...Waiting for Redis to shutdown ...Redis stoppedStarting Redis server...
4.23:测试效果
# 在 mastser 节点[root@redis-node1 ~] # redis-cli127 .0.0.1:6379> set name leeOK# 在 slave 节点查看[root@redis-node2 ~] # redis-cli127 .0.0.1:6379> get name"lee"
4.3:主从同步过程
- slave节点发送同步亲求到master节点
- slave节点通过master节点的认证开始进行同步
- master节点会开启bgsave进程发送内存rbd到slave节点,在此过程中是异步操作,也就是说
- master节点仍然可以进行写入动作
- slave节点收到rdb后首先清空自己的所有数据
- slave节点加载rdb并进行数据恢复
- 在master和slave同步过程中master还会开启新的bgsave进程把没有同步的数据进行缓存
- 然后通过自有的replactionfeedslave函数把未通过内存快照发动到slave的数据一条一条写入到
- slave中
五:Redis的哨兵(高可用)
5.1:Redis哨兵
- 每10秒每个sentinel对master和slave执行info
- 发现slave节点
- 确认主从关系
- 每2秒每个sentinel通过master节点的channel交换信息(pub/sub)
- 通过sentinel__:hello频道交互
- 交互对节点的“看法”和自身信息
- 每1秒每个sentinel对其他sentinel和redis执行p
5.2:哨兵的实验过程
# 编辑配置文件[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.confprotected-mode no # 关闭保护模式port 26379 # 监听端口daemonize no # 进入不打如后台pidfile /var/run/redis-sentinel.pid #sentinel 进程 pid 文件loglevel notice # 日志级别sentinel monitor mymaster 172 .25.254.10 6379 2 # 创建 sentinel 监控监控 master 主机, 2 表示必须得到 2 票sentinel down-after-milliseconds mymaster 10000 #master 中断时长, 10 秒连不上视为master 下线sentinel parallel-syncs mymaster 1 # 发生故障转移后,同时开始同步新master 数据的 slave 数量sentinel failover-timeout mymaster 180000 # 整个故障切换的超时时间为 3 分钟#### 复制配置文件到其他阶段[root@redis-node1 redis-7.4.0] # scp /etc/redis/sentinel.confroot@172.25.254.20:/etc/redis/root@172.25.254.20 's password:sentinel.conf100 % 14KB 9 .7MB/s 00 :00[root@redis-node1 redis-7.4.0] # scp /etc/redis/sentinel.confroot@172.25.254.30:/etc/redis/root@172.25.254.30 's password:sentinel.conf
2 启动服务
[root@redis-node1 redis-7.4.0] # redis-sentinel /etc/redis/sentinel.conf39319 :X 05 Aug 2024 20 :53:16.807 # WARNING Memory overcommit must be enabled!Without it, a background save or replication may fail under low memory condition.Being disabled, it can also cause failures without low memory condition, seehttps://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command'sysctl vm.overcommit_memory=1' for this to take effect.39319 :X 05 Aug 2024 20 :53:16.807 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo39319 :X 05 Aug 2024 20 :53:16.807 * Redis version = 7 .4.0, bits = 64 , commit = 00000000 ,modified = 0 , pid = 39319 , just started39319 :X 05 Aug 2024 20 :53:16.807 * Configuration loaded39319 :X 05 Aug 2024 20 :53:16.808 * Increased maximum number of open files to10032 (it was originally set to 1024 ).39319 :X 05 Aug 2024 20 :53:16.808 * monotonic clock: POSIX clock_gettime_.__.- `` __ '' - .__.- `` `. ` _. '' - ._ Redis Community Edition.- `` .- ```. ``` \/ _.,_ '' - ._ 7 .4.0 (00000000/0) 64 bit( ' , .-` | `, ) Running in sentinel mode| `-._` - ...- ` __...-.``-._|'` _.- '| Port: 26379| `-._ ` ._ / _.- ' | PID: 39319`-._ ` - ._ `-./ _.-' _.-'| `-._` - ._ `-.__.-' _.-'_.-'|| `-._` - ._ _.- '_.-' | https://redis.io`-._ ` - ._ `-.__.-'_.-' _.-'| `-._` - ._ `-.__.-' _.-'_.-'|| `-._` - ._ _.- '_.-' |`-._ ` - ._ `-.__.-'_.-' _.-'`-._ ` - .__.- ' _.-'`-._ _.-'`-.__.-'39319 :X 05 Aug 2024 20 :53:16.810 * Sentinel new configuration saved on disk39319 :X 05 Aug 2024 20 :53:16.810 * Sentinel ID ise568add863fd7c132e03f7a6ce2c5ef367d3bdae39319 :X 05 Aug 2024 20 :53:16.810 # +monitor master mymaster 172.25.254.100 6379quorum 239319 :X 05 Aug 2024 20 :53:16.811 * + slave slave 172 .25.254.10:6379172 .25.254.201 6379 @ mymaster 172 .25.254.100 637939319 :X 05 Aug 2024 20 :53:16.812 * Sentinel new configuration saved on disk39319 :X 05 Aug 2024 20 :53:16.812 * + slave slave 172 .25.254.200:6379172 .25.254.200 6379 @ mymaster 172 .25.254.100 637939319 :X 05 Aug 2024 20 :53:16.813 * Sentinel new configuration saved on disk39319 :X 05 Aug 2024 20 :53:41.769 * + sentinel sentinel4fe1dcbe25a801e75d6edfc5b0a8517bfa7992c3 172 .25.254.200 26379 @ mymaster172 .25.254.100 637939319 :X 05 Aug 2024 20 :53:41.771 * Sentinel new configuration saved on disk39319 :X 05 Aug 2024 20 :53:57.227 * + sentinel sentinel83f928aafe317a5f9081eea8fc5c383ff31c55ef 172 .25.254.201 26379 @ mymaster172 .25.254.100 637939319 :X 05 Aug 2024 20 :53:57.229 * Sentinel new configuration saved on disk
Attention!!!!!/etc/redis/sentinel.conf 文件在用哨兵程序调用后会更改其配置文件,如果需要重新做需要删掉文件重新编辑
在开一个 master 节点终端[root@redis-node1 ~]# redis-cli127.0.0.1:6379> SHUTDOWN
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> info replications127.0.0.1:6379> info replication# Replicationrole:masterconnected_slaves:2slave0:ip=172.25.254.201,port=6379,state=online,offset=211455,lag=1slave1:ip=172.25.254.100,port=6379,state=online,offset=211455,lag=1master_failover_state:no-failovermaster_replid:d42fd72f3dfae94c84ca722ad1653417495ef4fdmaster_replid2:290c3407108cc6120086981b7149a6fa377594c4master_repl_offset:211598second_repl_offset:185931repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:150986repl_backlog_histlen:60613
5.3:在整个架构中可能会出现的问题
# 在 matster 中设定[root@redis-node2 ~]# redis-cli127.0.0.1:6379> CONFIG GET min-slaves-to-write1) "min-slaves-to-write"2) "0"127.0.0.1:6379> CONFIG set min-slaves-to-write 2OK127.0.0.1:6379> CONFIG GET min-slaves-to-write1) "min-slaves-to-write"2) "2"# 如果要永久保存写到配置文件中 /etc/redis/
六:Redis Cluster(无中心化设计)
6.1 :Redis Cluster 工作原理
- 所有Redis节点使用(PING机制)互联
- 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
- 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
- redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis
- node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担
- 16384/N个槽位
- Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使 用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点 上,从而有效解决单机瓶颈。
6.2:创建redis cluster的前提
- 每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。
- 每个节点必须开启的参数
- cluster-enabled yes #必须开启集群状态,开启后redis进程会有cluster显示
- cluster-config-file nodes-6380.conf #此文件有redis cluster集群自动创建和维护,不需要任何手动操作
- 所有redis服务器必须没有任何数据
- 先启动为单机redis且没有任何key value
6.3:部署redis cluster
[root@redis-masterx ~] # vim /etc/redis/redis.confmasterauth "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-cli127 .0.0.1:6379> auth 123456OK127 .0.0.1:6379> info# Clustercluster_enabled:1
6.4 :redis-cli --cluster 参数说明
[root@redis-master1 ~] # redis-cli --cluster helpCluster 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 服务器的数据到当前集群-
6.5:创建redis-cluster
[root@redis-master1 ~] # redis-cli --cluster create -a 123456 \> 172 .25.254.10:6379 172 .25.254.20:6379 172 .25.254.30:6379 \> 172 .25.254.110:6379 172 .25.254.120:6379 172 .25.254.130:6379 \> --cluster-replicas 1>>> Performing hash slots allocation on 6 nodes...Master[0] - > Slots 0 - 5460 # 哈希槽分配Master[1] - > Slots 5461 - 10922 Master[2] - > Slots 10923 - 16383Adding replica 172 .25.254.120:6379 to 172 .25.254.10:6379 # 主从分配情况Adding replica 172 .25.254.130:6379 to 172 .25.254.20:6379Adding replica 172 .25.254.110:6379 to 172 .25.254.30:6379M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[0-5460] (5461 slots) masterM: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[5461-10922] (5462 slots) masterM: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[10923-16383] (5461 slots) masterS: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379replicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379replicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379replicates ba504e78f14df5944280f9035543277a0cf5976bCan I set the above configuration? (type 'yes' to accept): yes>>> Performing Cluster Check (using node 172 .25.254.10:6379)M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379slots: (0 slots) slavereplicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7M: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379slots: (0 slots) slavereplicates ba504e78f14df5944280f9035543277a0cf5976bM: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379slots: (0 slots) slavereplicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1[OK] All nodes agree about slots configuration.>>> Check for open slots... # 检查打开的哈希槽位>>> Check slots coverage... # 检查槽位覆盖范围[OK] All 16384 slots covered. # 所有槽位分配完成# 配置文件位置[root@redis-master1 ~] # ll /var/lib/redis/nodes-6379.conf
检测redis集群状态
[root@redis-master1 ~]# redis-cli -a 123456 --cluster info 172.25.254.10:6379
# 查看集群状态Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.172 .25.254.10:6379 (5ab2e93f...) - > 0 keys | 5461 slots | 1 slaves.172 .25.254.20:6379 (ba504e78...) - > 0 keys | 5462 slots | 1 slaves. 172 .25.254.30:6379 (1fcaeb1d...) - > 0 keys | 5461 slots | 1 slaves.[OK] 0 keys in 3 masters.0 .00 keys per slot on average.[root@redis-master1 ~] # redis-cli -a 123456 cluster infoWarning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.cluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:821cluster_stats_messages_pong_sent:801cluster_stats_messages_sent:1622cluster_stats_messages_ping_received:796cluster_stats_messages_pong_received:821cluster_stats_messages_meet_received:5cluster_stats_messages_received:1622[root@redis-master1 ~] # redis-cli -a 123456 --cluster check 172.25.254.10:6379# 检测集群Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.172 .25.254.10:6379 (5ab2e93f...) - > 0 keys | 5461 slots | 1 slaves.172 .25.254.20:6379 (ba504e78...) - > 0 keys | 5462 slots | 1 slaves.172 .25.254.30:6379 (1fcaeb1d...) - > 0 keys | 5461 slots | 1 slaves.[OK] 0 keys in 3 masters.0 .00 keys per slot on average.>>> Performing Cluster Check (using node 172 .25.254.10:6379)M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379slots: (0 slots) slavereplicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7M: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379slots: (0 slots) slavereplicates ba504e78f14df5944280f9035543277a0cf5976bM: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379slots: (0 slots) slavereplicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
[root@redis-master1 ~] # redis-cli -a 123456Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.127 .0.0.1:6379> set key1 value1 # 被分配到 20 的 hash 槽位上(error) MOVED 9189 172 .25.254.20:6379[root@redis-master2 ~] # redis-cli -a 123456Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.127 .0.0.1:6379> set key1 value1OK
6.6:集群扩容
# 添加 master[root@redis-master1 ~] # redis-cli -a 123456 --cluster add-node172.25.254.40:6379 172.25.254.10:6379Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.>>> Adding node 172 .25.254.40:6379 to cluster 172 .25.254.10:6379>>> Performing Cluster Check (using node 172 .25.254.10:6379)M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379slots: (0 slots) slavereplicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7M: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379slots: (0 slots) slavereplicates ba504e78f14df5944280f9035543277a0cf5976bM: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379slots: (0 slots) slavereplicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.>>> Getting functions from cluster>>> Failed retrieving Functions from the cluster, skip this step as Redis versiondo not support function command (error = 'ERR unknown command `FUNCTION`, withargs beginning with: `DUMP`, ' ) >>> Send CLUSTER MEET to node 172 .25.254.40:6379 to make it join the cluster.[OK] New node added correctly.[root@redis-master1 ~] # redis-cli -a 123456 --cluster info 172.25.254.10:6379Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.172 .25.254.10:6379 (5ab2e93f...) - > 0 keys | 5461 slots | 1 slaves.172 .25.254.40:6379 (009571cb...) - > 0 keys | 0 slots | 0 slaves.172 .25.254.20:6379 (ba504e78...) - > 1 keys | 5462 slots | 1 slaves.172 .25.254.30:6379 (1fcaeb1d...) - > 0 keys | 5461 slots | 1 slaves.[OK] 1 keys in 4 masters.0 .00 keys per slot on average.# 分配槽位[root@redis-master1 ~] # redis-cli -a 123456 --cluster reshard172.25.254.10:6379Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.>>> Performing Cluster Check (using node 172 .25.254.10:6379)M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379slots: (0 slots) slavereplicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7M: 009571cb206a89afa6658b60b2d403136056ac09 172 .25.254.40:6379slots: (0 slots) masterM: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379slots: (0 slots) slavereplicates ba504e78f14df5944280f9035543277a0cf5976bM: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379slots: (0 slots) slavereplicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.How many slots do you want to move (from 1 to 16384 )? 4096What is the receiving node ID? 009571cb206a89afa6658b60b2d403136056ac09Please enter all the source node IDs.Type 'all' to use all the nodes as source nodes for the hash slots.Type 'done' once you entered all the source nodes IDs.Source node #1: all# 添加 salve 7.7 clsuter 集群维护添加节点的时候是先添加 node 节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的 Redis node 上的槽位迁移到集群中的其他 Redis node 节点上,然后再将其删除,如果一个 Redis node 节点上的槽位没有被完全迁移,删除该 node 的时候会提示有数据且无法删除。[root@redis-master1 ~] # redis-cli -a 123456 --cluster add-node172.25.254.140:6379 172.25.254.10:6379 --cluster-slave --cluster-master-id009571cb206a89afa6658b60b2d403136056ac09Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.>>> Adding node 172 .25.254.140:6379 to cluster 172 .25.254.10:6379>>> Performing Cluster Check (using node 172 .25.254.10:6379)M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379slots: (0 slots) slavereplicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7M: 009571cb206a89afa6658b60b2d403136056ac09 172 .25.254.40:6379slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) masterM: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379slots: (0 slots) slavereplicates ba504e78f14df5944280f9035543277a0cf5976bM: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379slots: (0 slots) slavereplicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.>>> Send CLUSTER MEET to node 172 .25.254.140:6379 to make it join the cluster.Waiting for the cluster to join>>> Configure node as replica of 172 .25.254.40:6379.[OK] New node added correctly.
6.7:clsuter集群维护
# 移除要下线主机的哈希槽位[root@redis-master2 ~] # redis-cli -a 123456 --cluster reshard 172.25.254.20:6379Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.>>> Performing Cluster Check (using node 172 .25.254.20:6379)M: ba504e78f14df5944280f9035543277a0cf5976b 172 .25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)M: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172 .25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s) M: 009571cb206a89afa6658b60b2d403136056ac09 172 .25.254.40:6379slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master1 additional replica(s)S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172 .25.254.110:6379slots: (0 slots) slavereplicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7S: d458f34fa900d83212c021dc1e65396e490b5495 172 .25.254.120:6379slots: (0 slots) slavereplicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172 .25.254.10:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172 .25.254.130:6379slots: (0 slots) slavereplicates ba504e78f14df5944280f9035543277a0cf5976bS: 86a4a8fb08e70e41b5a30f829deb983d23854ea7 172 .25.254.140:6379slots: (0 slots) slavereplicates 009571cb206a89afa6658b60b2d403136056ac09[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.How many slots do you want to move (from 1 to 16384 )? 4096What is the receiving node ID? 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7Please enter all the source node IDs.Type 'all' to use all the nodes as source nodes for the hash slots.Type 'done' once you entered all the source nodes IDs.Source node #1: 5ab2e93f4f0783983676f7bd118efaacfb202bd1Source node #2: done# 删除 master[root@redis-master2 ~] # redis-cli -a 123456 --cluster del-node172.25.254.120:6379 d458f34fa900d83212c021dc1e65396e490b5495Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.>>> Removing node d458f34fa900d83212c021dc1e65396e490b5495 from cluster172 .25.254.120:6379>>> Sending CLUSTER FORGET messages to the cluster...>>> Sending CLUSTER RESET SOFT to the deleted node .[root@redis-master2 ~] # redis-cli -a 123456 --cluster del-node172.25.254.10:6379 5ab2e93f4f0783983676f7bd118efaacfb202bd1Warning: Using a password with '-a' or '-u' option on the command line interfacemay not be safe.>>> Removing node 5ab2e93f4f0783983676f7bd118efaacfb202bd1 from cluster172 .25.254.10:6379>>> Sending CLUSTER FORGET messages to the cluster...>>> Sending CLUSTER RESET SOFT to the deleted node .
这篇关于全网最最最最详细的企业级NoSql数据库Redis集群的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!