Redis_集群(cluster)
文章目录
- 简介
- 作用
- 集群结构设计
- 数据存储设计
- 集群内部通讯设计
- Cluster集群结构搭建
- 集群设置和获取数据
- 集群主从下线与主从切换
简介
集群就是使用网络将若干台计算机联通起来,并提供统一的管理方式,使其对外呈现单机的服务效果
作用
- 分散单台服务器的访问压力,实现负载均衡
- 分散单台服务器的存储压力,实现可扩展性
- 降低单台服务器宕机带来的业务灾难
集群结构设计
数据存储设计
- 将所有的存储空间计划切割成16384份,每台主机保存一部分,每份代表的是一个存储空间,称作槽,不是一个key的保存空间。
- 将key按照
CRC16(key) %16384
计算出的结果放到对应的存储空间
集群内部通讯设计
- 各个数据库相互通信,保存各个库中槽的编号数据
- 一次命中,直接返回
- 一次未命中,告知具体位置
Cluster集群结构搭建
现拟搭建一个三主三从集群的例子。
修改配置文件,以其中一个配置文件为例,其他配置文件相应复制修改
[root@localhost conf]# vim redis-6379.conf
port 6379如果打印日志就把以下注释掉
daemonize no
logfile “6379.log”
loglevel verbose
dir /redis-5.0.7/data
dbfilename dump-6379.rdb
rdbcompression yes
rdbchecksum yes
stop-writes-on-bgsave-error yes
save 10 2
appendonly yes
appendfsync everysec
appendfilename appendonly-6379.aof
auto-aof-rewrite-min-size 10mb
auto-aof-rewrite-percentage 100
bind 127.0.0.1
databases 16maxclients 0
timeout 300
添加节点
cluster-enabled yes
cluster配置文件名,该文件属于自动生成,仅用于快速查找文件并查询文件内容
cluster-config-file node-6379.conf
节点服务响应超时时间,用于判定该节点是否下线或切换为从节点
cluster-node-timeout 10000
master连接的slave最小数量
cluster-migration-barrier
通过sed命令复制修改其他配置文件
[root@localhost conf]# sed “s/6379/6380/g” redis-6379.conf > redis-6380.conf
…将三主三从6个redis服务启动
[root@localhost conf]# ps -ef|grep “redis-“
root 6751 6723 0 06:30 pts/0 00:00:00 ./src/redis-server 127.0.0.1:6379 [cluster]
root 6773 6500 0 06:35 pts/1 00:00:00 ./src/redis-server 127.0.0.1:6380 [cluster]
root 6777 6517 0 06:35 pts/2 00:00:00 ./src/redis-server 127.0.0.1:6381 [cluster]
root 6781 6531 0 06:36 pts/3 00:00:00 ./src/redis-server 127.0.0.1:6382 [cluster]
root 6785 6547 0 06:36 pts/4 00:00:00 ./src/redis-server 127.0.0.1:6383 [cluster]
root 6789 6576 0 06:36 pts/5 00:00:00 ./src/redis-server 127.0.0.1:6384 [cluster]
root 6794 6597 0 06:36 pts/6 00:00:00 grep —color=auto redis-通过src目录下的redis-trib.rb工具创建集群,第一次使用需要安装ruby和gem
会在/etc/yum.repos.d/目录下多出一个CentOS-SCLo-scl-rh.repo源
[root@localhost ~]# yum install centos-release-scl-rh
[root@localhost ~]# yum install rh-ruby26 -y
[root@localhost ~]# scl enable rh-ruby23 bash[root@localhost ~]# ruby -v
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-linux]
[root@localhost ~]# gem -v
3.0.3开始创建集群
[root@localhost src]# ./redis-trib.rb create —replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
我使用的Redis版本是5.0.7,使用redis-trib.rb时会提示相关命令已经转移到redis-cli下,如下:
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the —cluster
option followed by the subcommand name, arguments and options.Use the following syntax:
redis-cli —cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]Example:
redis-cli —cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 —cluster-replicas 1To get help about all subcommands, type:
redis-cli —cluster help然后用redis-cli的指令来创建集群
—cluster create创建集群,后面跟的是所有redis服务的IP和port
—cluster-replicas 每个master有几个slave
redis-cli —cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 —cluster-replicas 1
如果遇到以下错误提示:
[ERR] Node 127.0.0.1:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
是因为该redis服务节点下存在数据,首先将data目录中rdb和aof的备份文件删除,已经之前配置的node.conf删除,之后通过客户端连接该redis服务,通过flushdb
命令清空数据库
然后在执行创建集群指令,会收到以下主从信息:
Performing hash slots allocation on 6 nodes…
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 80218584ed58b9dee0f49056f2b85a40517d73d8 127.0.0.1:6379
slots:[0-5460],[5798] (5461 slots) master
M: 6f0f2dab6c4deff95cccb25c11e6fec93c92abf8 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
M: 0588ad00d3b592255460a5d11c93d2e520f42b9c 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
S: 286f7480f8580bcac69975962b9ed231370aa8d9 127.0.0.1:6382
replicates 80218584ed58b9dee0f49056f2b85a40517d73d8
S: 690c42e70fb4474a22996f5c7733849847a66829 127.0.0.1:6383
replicates 6f0f2dab6c4deff95cccb25c11e6fec93c92abf8
S: ea1f287770c0849627b81119eaf8d18f97f1c98f 127.0.0.1:6384
replicates 0588ad00d3b592255460a5d11c93d2e520f42b9c
Can I set the above configuration? (type ‘yes’ to accept):输入yes,稍等会收到创建成功的信息
Can I set the above configuration? (type ‘yes’ to accept): yes
Nodes configuration updated
Assign a different config epoch to each node
Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
……
Performing Cluster Check (using node 127.0.0.1:6379)
M: 80218584ed58b9dee0f49056f2b85a40517d73d8 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 6f0f2dab6c4deff95cccb25c11e6fec93c92abf8 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 690c42e70fb4474a22996f5c7733849847a66829 127.0.0.1:6383
slots: (0 slots) slave
replicates 80218584ed58b9dee0f49056f2b85a40517d73d8
S: ea1f287770c0849627b81119eaf8d18f97f1c98f 127.0.0.1:6384
slots: (0 slots) slave
replicates 6f0f2dab6c4deff95cccb25c11e6fec93c92abf8
M: 0588ad00d3b592255460a5d11c93d2e520f42b9c 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 286f7480f8580bcac69975962b9ed231370aa8d9 127.0.0.1:6382
slots: (0 slots) slave
replicates 0588ad00d3b592255460a5d11c93d2e520f42b9c
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
集群设置和获取数据
如果按照单机redis的客户端连接指令去操作会报错
[root@localhost redis-5.0.7]# ./src/redis-cli
127.0.0.1:6379> set name zhs
(error) MOVED 5798 127.0.0.1:6380
需要在客户端连接的时候加上-c
指令,用来操作集群
[root@localhost redis-5.0.7]# ./src/redis-cli -c
127.0.0.1:6379> set name zhs
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK
127.0.0.1:6380> get name
"zhs"
集群主从下线与主从切换
- slave下线,可重连。
- master下线,在预设时间内master未上线,slave变为master,master再上线时变为slave
还没有评论,来说两句吧...