数据库运维
记录DBA学习成长历程

Redis cluster 分布式集群

redis cluster分布式集群,类似于MySQL的mycat,提供分布式高可用解决方案

1、在多分片节点中,将16384个槽位,均匀分布到多个分片节点中
2、存数据时,将key做crc16(key),然后和16384进行取模,得出槽位值(0-16383之间)
3、根据计算得出的槽位值,找到相对应的分片节点的主节点,存储到相应槽位上
4、如果客户端当时连接的节点不是将来要存储的分片节点,分片集群会将客户端连接切换至真正存储节点进行数据存储
在搭建集群时,会为每一个分片的主节点,对应一个从节点,实现slaveof的功能,同时当主节点down,实现类似于sentinel的自动failover的功能。
1、redis会有多组分片构成(3组)
2、redis cluster 使用固定个数的slot存储数据(一共16384slot)
3、每组分片分得1/3 slot个数(0-5500 5501-11000 11001-16383)
4、基于CRC16(key) % 16384 ====>值 (槽位号)。

规划、搭建过程

6个redis实例,一般会放到3台硬件服务器
注:在企业规划中,一个分片的两个分到不同的物理机,防止硬件主机宕机造成的整个分片数据丢失。
端口号:7000-7005

安装集群插件

保证redis3.x版本以上可以使用redis集群,不然只能命令逐步安装
1.EPEL源安装ruby支持
yum install ruby rubygems -y
2.使用国内源
gem sources -l
gem sources -a http://mirrors.aliyun.com/rubygems/ 
gem sources --remove https://rubygems.org/
gem sources -l
gem install redis -v 3.3.3

集群节点准备

mkdir /data/700{0..5}
cat > /data/7000/redis.conf <<EOF
port 7000
daemonize yes
pidfile /data/7000/redis.pid
loglevel notice
logfile "/data/7000/redis.log"
dbfilename dump.rdb
dir /data/7000
protected-mode no
cluster-enabled yes ##把节点的集群功能打开
cluster-config-file nodes.conf ##集群节点配置文件
cluster-node-timeout 5000 ##集群节点探测的超时时间
appendonly yes
EOF

cat >> /data/7001/redis.conf <<EOF
port 7001
daemonize yes
pidfile /data/7001/redis.pid
loglevel notice
logfile "/data/7001/redis.log"
dbfilename dump.rdb
dir /data/7001
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat >> /data/7002/redis.conf <<EOF
port 7002
daemonize yes
pidfile /data/7002/redis.pid
loglevel notice
logfile "/data/7002/redis.log"
dbfilename dump.rdb
dir /data/7002
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat >> /data/7003/redis.conf <<EOF
port 7003
daemonize yes
pidfile /data/7003/redis.pid
loglevel notice
logfile "/data/7003/redis.log"
dbfilename dump.rdb
dir /data/7003
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat >> /data/7004/redis.conf <<EOF
port 7004
daemonize yes
pidfile /data/7004/redis.pid
loglevel notice
logfile "/data/7004/redis.log"
dbfilename dump.rdb
dir /data/7004
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat >> /data/7005/redis.conf <<EOF
port 7005
daemonize yes
pidfile /data/7005/redis.pid
loglevel notice
logfile "/data/7005/redis.log"
dbfilename dump.rdb
dir /data/7005
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

启动节点

redis-server /data/7000/redis.conf 
redis-server /data/7001/redis.conf 
redis-server /data/7002/redis.conf 
redis-server /data/7003/redis.conf 
redis-server /data/7004/redis.conf 
redis-server /data/7005/redis.conf 
[root@db01 ~]# ps -ef |grep redis
root 8854 1 0 03:56 ? 00:00:00 redis-server *:7000 [cluster] 
root 8858 1 0 03:56 ? 00:00:00 redis-server *:7001 [cluster] 
root 8860 1 0 03:56 ? 00:00:00 redis-server *:7002 [cluster] 
root 8864 1 0 03:56 ? 00:00:00 redis-server *:7003 [cluster] 
root 8866 1 0 03:56 ? 00:00:00 redis-server *:7004 [cluster] 
root 8874 1 0 03:56 ? 00:00:00 redis-server *:7005 [cluster]

将节点加入集群管理

redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
前3个节点作为主,后三个节点作为从,7000-7003,7001-7004,7002-7005分别对应,对应的节点不能放在一台物理硬件上

集群状态查看

集群主节点状态
redis-cli -p 7000 cluster nodes | grep master
f4c42c1222bbb3840ee5da2c6be7ad302e752bf8 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
90de3ccbf42cf01051dfd8f2b8278a5beb1a87eb 127.0.0.1:7001 master - 0 1603875722226 2 connected 5461-10922
68bebbbe1b89109ad9f642bcc38bf35317d04e00 127.0.0.1:7002 master - 0 1603875723735 3 connected 10923-16383
集群从节点状态
redis-cli -p 7000 cluster nodes | grep slave

集群节点管理

增加新的节点

mkdir /data/7006
mkdir /data/7007
cat > /data/7006/redis.conf <<EOF
port 7006
daemonize yes
pidfile /data/7006/redis.pid
loglevel notice
logfile "/data/7006/redis.log"
dbfilename dump.rdb
dir /data/7006
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat > /data/7007/redis.conf <<EOF
port 7007
daemonize yes
pidfile /data/7007/redis.pid
loglevel notice
logfile "/data/7007/redis.log"
dbfilename dump.rdb
dir /data/7007
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

redis-server /data/7006/redis.conf 
redis-server /data/7007/redis.conf

添加主节点

redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

转移slot(重新分片)

[root@db01 26380]# redis-cli -p 7000 cluster nodes | grep master
f4c42c1222bbb3840ee5da2c6be7ad302e752bf8 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
90de3ccbf42cf01051dfd8f2b8278a5beb1a87eb 127.0.0.1:7001 master - 0 1603876110294 2 connected 5461-10922
957aeba9a7d03ea374ec60dd8ce01ec9f5863c07 127.0.0.1:7006 master - 0 1603876110798 0 connected
68bebbbe1b89109ad9f642bcc38bf35317d04e00 127.0.0.1:7002 master - 0 1603876110294 3 connected 10923-16383
[root@db01 26380]# redis-trib.rb reshard 127.0.0.1:7000
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 957aeba9a7d03ea374ec60dd8ce01ec9f5863c07
Source node #1:all
[root@db01 26380]# redis-cli -p 7000 cluster nodes | grep master
f4c42c1222bbb3840ee5da2c6be7ad302e752bf8 127.0.0.1:7000 myself,master - 0 0 1 connected 1365-5460
90de3ccbf42cf01051dfd8f2b8278a5beb1a87eb 127.0.0.1:7001 master - 0 1603876416728 2 connected 6827-10922
957aeba9a7d03ea374ec60dd8ce01ec9f5863c07 127.0.0.1:7006 master - 0 1603876416224 7 connected 0-1364 5461-6826 10923-12287
68bebbbe1b89109ad9f642bcc38bf35317d04e00 127.0.0.1:7002 master - 0 1603876416728 3 connected 12288-16383

添加一个从节点

redis-trib.rb add-node --slave --master-id 957aeba9a7d03ea374ec60dd8ce01ec9f5863c07 127.0.0.1:7007 127.0.0.1:7000
[root@db01 26380]# redis-cli -p 7000 cluster nodes | grep slave
a7f9a75dfa7a68da43698bd9f650042e9db7fa5d 127.0.0.1:7007 slave 957aeba9a7d03ea374ec60dd8ce01ec9f5863c07 0 1603876618032 7 connected
b1ec963cfd6d4ad21c58a1deb877154ffaabee5b 127.0.0.1:7003 slave f4c42c1222bbb3840ee5da2c6be7ad302e752bf8 0 1603876618536 4 connected
567e69c5e75a82fda70c353988f65736fab80e73 127.0.0.1:7004 slave 90de3ccbf42cf01051dfd8f2b8278a5beb1a87eb 0 1603876618536 5 connected
5665cb0a6b8d2f7dd11cd321a545b1c74bafa845 127.0.0.1:7005 slave 68bebbbe1b89109ad9f642bcc38bf35317d04e00 0 1603876617528 6 connected

删除节点

将需要删除节点slot移动走

分三次挪走7706节点上的槽位
redis-trib.rb reshard 127.0.0.1:7000
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? f4c42c1222bbb3840ee5da2c6be7ad302e752bf8
Please 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:957aeba9a7d03ea374ec60dd8ce01ec9f5863c07
Source node #2:done
------------------------------------------------
[root@db01 26380]# redis-trib.rb reshard 127.0.0.1:7000
How many slots do you want to move (from 1 to 16384)? 1366
What is the receiving node ID? 90de3ccbf42cf01051dfd8f2b8278a5beb1a87eb
Please 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:957aeba9a7d03ea374ec60dd8ce01ec9f5863c07
Source node #2:done
------------------------------------------------
[root@db01 26380]# redis-trib.rb reshard 127.0.0.1:7000
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? 68bebbbe1b89109ad9f642bcc38bf35317d04e00
Please 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:957aeba9a7d03ea374ec60dd8ce01ec9f5863c07
Source node #2:done

删除一个节点

删除master节点之前首先要使用reshard移除master的全部slot,然后再删除当前节点
删除主节点
redis-trib.rb del-node 127.0.0.1:7006 957aeba9a7d03ea374ec60dd8ce01ec9f5863c07
删除从节点
redis-trib.rb del-node 127.0.0.1:7007 a7f9a75dfa7a68da43698bd9f650042e9db7fa5d

设置redis最大内存

config set maxmemory 102400000
赞(0)
MySQL学习笔记 » Redis cluster 分布式集群