Redis集群
程序员文章站
2022-05-22 07:52:02
...
Redis分布式存储的高可用集群
配置集群思路
- 配置集群环境(为redis服务器搭建redis环境)
- 启用每一台redis服务器的集群功能
- 配置管理主机 192.168.4.57
- 创建集群
- 查看集群信息
- 客户端访问集群中的主机存取数据
- 管理集群
- 添加新的服务器
- 主服务器 : 清空欲加入节点的服务器redis数据 管理服务器添加节点,划分slots
- 从服务器 : 清空欲加入节点的服务器redis数据 管理服务器直接添加
- 移除服务器
- 从服务器 : 直接移除
- 主服务器 : 清空slots 移除节点
- 注 : 主服务器被移除后, 其从服务器将随机分配给从服务器最少的有slots范围的主服务器节点
- 添加新的服务器
- 集群存储数据的工作原理
Redis集群存储数据的工作原理
Redis在创建集群时, 会将16384个slot平均的分配给各个主服务器
比如此处集群有三台主服务器, 则三台服务器的分别的slot范围为 0-5460 5461-10922 10923-16383
当客户端连接集群存储数据时, 集群会将keyname进行hash运算然后对16384进行取余运算
得到的结果在哪个slot范围, 会连接到对应的服务器,然后将数据存储到那台服务器中
在取数据时, 同样对keyname进行hash运算然后对16384进行取余运算
根据结果连接到对应的服务器, 然后读取数据
集群环境
-
拓扑结构
- Redis服务器 : 6台
- 管理主机 : 1台
- 客户端 : 1台
-
IP规划
主机名 | IP地址 | 端口规划 |
---|---|---|
client | 192.168.4.50 | 无 |
redisA | 192.168.4.51 | 6351 |
redisB | 192.168.4.52 | 6352 |
redisC | 192.168.4.53 | 6353 |
redisD | 192.168.4.54 | 6354 |
redisE | 192.168.4.55 | 6355 |
redisF | 192.168.4.56 | 6356 |
MGM | 192.168.4.57 | 无 |
创建集群
集群环境搭建
- 使用脚本配置6台redis服务器
#!/bin/bash
# 批量部署Redis及简单配置脚本
# 将相应软件包放置在192.168.4.51主机上
IP=192.168.4.
# 由于"$"为特殊字符, 因此无论在sed还是ssh远程控制都需要进行转义, 因此需要二次转义
# 此处采用一个变量存储 经转义的$ 字符
# ssh远程控制时 则取出变量里的值 然后sed命令执行带变量里的值(含转义字符)的命令 即实现了二次转义
a='\$CLIEXEC'
# 生成**
ssh-****** -f "test_rsa" -N ""
for i in {51..55}
do
# 建立51 到其他主机的免密连接, 并将相应软件包传给其他主机
ssh-copy-id aaa@qq.com$IP$i
scp -r redis/ aaa@qq.com$IP$i:
done
for i in {51..55}
do
# 安装Redis软件
ssh $IP$i "yum -y install gcc;cd redis;tar -zxf redis-4.0.8.tar.gz;cd redis-4.0.8/;make && make install;cd utils/;./install_server.sh"
# 停止Redis服务
ssh $IP$i "/etc/init.d/redis_6379 stop"
# 修改redis主配置文件 的IP参数
ssh $IP$i "sed -i \"70cbind 192.168.4.$i\" /etc/redis/6379.conf"
# 修改redis主配置文件 的端口
ssh $IP$i "sed -i \"93cport 63$i\" /etc/redis/6379.conf"
# 开启集群功能
ssh $IP$i "sed -i \"815s/#//\" /etc/redis/6379.conf"
# 指定存储集群信息的配置文件
ssh $IP$i "sed -i \"823s/#//\" /etc/redis/6379.conf"
# 设置集群节点通信超时时间为5000 (单位毫秒)
ssh $IP$i "sed -i \"829ccluster-node-timeout 5000\" /etc/redis/6379.conf"
# 清空数据
ssh $IP$i "rm -rf /var/lib/redis/6379/*"
# 修改redis控制脚本, 使能够关闭改了IP与端口后的Redis服务
ssh $IP$i "sed -i \"43c$a -h 192.168.4.$i -p 63$i shutdown\" /etc/init.d/redis_6379"
# 启动Redis服务
ssh $IP$i "/etc/init.d/redis_6379 start"
done
- 查看服务信息
# 在redisA上验证即可
for i in {51..56}
do
ssh 192.168.4.$i <<EOF
ss -ntulp | grep redis-server
ls /var/lib/redis/6379/nodes-6379.conf #查看集群信息文件
EOF
done
# 在六台redis服务器上查看集群信息
redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
- 部署管理主机
- 部署ruby脚本运行环境
- 创建管理集群脚本redis-trib.rb
# 在MGM服务器上配置管理主机
# 提取准备好redis相应软件
# rubygems提供gem命令 用来安装gem后缀的软件
yum -y install rubygems ruby
cd redis
gem install redis-3.2.1.gem
mkdir /root/bin # 创建命令检索目录
tar -zxvf redis-4.0.8.tar.gz
cp redis-4.0.8/src/redis-trib.rb /root/bin/ # 创建管理集群脚本
chmod +x /root/bin/redis-trib.rb
redis-trib.rb help # 查看命令帮助
- redis-trib脚本
- 用法 : redis-trib.rb <arguments…>
命令 | 描述 |
---|---|
create | 创建集群 |
check | 检查集群 |
info | 查看集群信息 |
reshard | 重新分片 |
del-node | 删除主机 |
add-node --slave | 添加slave主机 |
add-node | 添加master主机 |
rebalance | 平均分配hash slots |
创建集群
- 创建集群
# 在MGM服务器上进行创建
# 定义从服务器个数
# 给每台redis主服务器配一台从服务器
# 管理主机默认从列出来的主机中选出 3 个主服务器(默认前三个)
# --replicas n 定义每个主服务器有n个从服务器
redis-trib.rb create --replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters: # 选举三个主服务器
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351 # 为每个主服务器分配一个从服务器
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 25a9a8778a9176b41cbeb6c20f4d7b874253a9ba 192.168.4.51:6351
slots:0-5460 (5461 slots) master # 主服务器分配16384个slots( 槽) 的范围 主服务器都有槽
M: 752769b4fc263ea25237484f7510398eab324ec4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: ac22464a362b018023d0b39ceb6bf54d58a9c651 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: dd8b9f228f174dadb0ddfba0885c040ff3dafa36 192.168.4.54:6354 # 54从53 处复制数据
replicates ac22464a362b018023d0b39ceb6bf54d58a9c651
S: 27e9094b2322cd951ff31b7fab814cd1ab41cdcb 192.168.4.55:6355
replicates 25a9a8778a9176b41cbeb6c20f4d7b874253a9ba
S: a319c2efc9087b871159f14bf6824c99eab08fe1 192.168.4.56:6356
replicates 752769b4fc263ea25237484f7510398eab324ec4
Can I set the above configuration? (type 'yes' to accept): yes # 这里输入yes
... ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 查看集群信息
# 统计信息
redis-trib.rb info 192.168.4.51:6351
192.168.4.51:6351 (25a9a877...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (ac22464a...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (752769b4...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
# 详细信息
redis-trib.rb check 192.168.4.51:6351
测试集群
- 客户端访问集群
- 为客户端client安装redis软件提供redis-cli命令
- 命令 : redis-cli -c -h IP地址 -p 端口号
- 选项 : -c 集群模式
# 所有的读写操作都是在主服务器上操作 , 从服务器只提供同步数据
# 客户端client测试
redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> set num1 1
OK
192.168.4.51:6351> set num2 2 # 数据被分布式的存储在服务器上
-> Redirected to slot [8983] located at 192.168.4.52:6352
OK
192.168.4.52:6352> set num3 3
-> Redirected to slot [13110] located at 192.168.4.53:6353
OK
192.168.4.53:6353> get num1 # 在任何地方获取数据均可
-> Redirected to slot [4980] located at 192.168.4.51:6351
"1"
192.168.4.51:6351> keys *
1) "v1"
2) "num1"
192.168.4.51:6351>
管理集群
测试故障切换
- 停止master主机的redis服务
- master 宕机后对应的slave自动被选举为master
- 原master启动后 会自动配置为当前master的slave
# 模拟关闭redisA主机的redis服务
/etc/init.d/redis_6379 stop
# 管理主机MGM 查看
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.52:6352
192.168.4.52:6352 (752769b4...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (27e9094b...) -> 3 keys | 5461 slots | 0 slaves.
192.168.4.53:6353 (ac22464a...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 3 masters.
0.00 keys per slot on average.
# 51的从服务器55转成主服务器
[aaa@qq.com ~]# redis-trib.rb check 192.168.4.52:6352
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 752769b4fc263ea25237484f7510398eab324ec4 192.168.4.52:6352 # 主服务器
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 27e9094b2322cd951ff31b7fab814cd1ab41cdcb 192.168.4.55:6355 # 主服务器
slots:0-5460 (5461 slots) master
0 additional replica(s)
S: a319c2efc9087b871159f14bf6824c99eab08fe1 192.168.4.56:6356
slots: (0 slots) slave
replicates 752769b4fc263ea25237484f7510398eab324ec4
M: ac22464a362b018023d0b39ceb6bf54d58a9c651 192.168.4.53:6353 # 主服务器
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: dd8b9f228f174dadb0ddfba0885c040ff3dafa36 192.168.4.54:6354
slots: (0 slots) slave
replicates ac22464a362b018023d0b39ceb6bf54d58a9c651
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 恢复redisA的Redis服务
[aaa@qq.com ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[aaa@qq.com ~]# ss -ntulp | grep redis-server
tcp LISTEN 0 128 192.168.4.51:16351 *:* users:(("redis-server",pid=1266,fd=8))
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1266,fd=6))
# 管理主机查看集群信息
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.52:6352
192.168.4.52:6352 (752769b4...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (27e9094b...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (ac22464a...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 3 masters.
0.00 keys per slot on average.
# 51 已自动称为 主服务器55 的从服务器
[aaa@qq.com ~]# redis-trib.rb check 192.168.4.52:6352
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 752769b4fc263ea25237484f7510398eab324ec4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 27e9094b2322cd951ff31b7fab814cd1ab41cdcb 192.168.4.55:6355
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: a319c2efc9087b871159f14bf6824c99eab08fe1 192.168.4.56:6356
slots: (0 slots) slave
replicates 752769b4fc263ea25237484f7510398eab324ec4
S: 25a9a8778a9176b41cbeb6c20f4d7b874253a9ba 192.168.4.51:6351
slots: (0 slots) slave
replicates 27e9094b2322cd951ff31b7fab814cd1ab41cdcb
M: ac22464a362b018023d0b39ceb6bf54d58a9c651 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: dd8b9f228f174dadb0ddfba0885c040ff3dafa36 192.168.4.54:6354
slots: (0 slots) slave
replicates ac22464a362b018023d0b39ceb6bf54d58a9c651
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
添加新redis服务器
- 准备新redis服务器
- 准备两台服务器(IP分别为redisG : 192.168.4.58 redisH : 192.168.4.59)
- 在两台服务器上搭建redis服务
- 修改redis配置文件开启集群
# 此处以redisG为例
yum -y install gcc
cd redis
tar -zxf redis-4.0.8.tar.gz
cd redis-4.0.8/
make && make install
./utils/install_server.sh
/etc/init.d/redis_6379 stop
sed -i "70cbind 192.168.4.58" /etc/redis/6379.conf
sed -i "93cport 6358" /etc/redis/6379.conf
# 开启集群功能
sed -i "815s/#//" /etc/redis/6379.conf
# 指定存储集群信息的配置文件
sed -i "823s/#//" /etc/redis/6379.conf
# 设置集群节点通信超时时间为5000 (单位毫秒)
sed -i "829ccluster-node-timeout 5000" /etc/redis/6379.conf
# 清空数据
rm -rf /var/lib/redis/6379/*
# 修改redis控制脚本, 使能够关闭改了IP与端口后的Redis服务
sed -i "43c$CLIEXEC' -h 192.168.4.58 -p 63$i shutdown" /etc/init.d/redis_6379
# 启动Redis服务
/etc/init.d/redis_6379 start
- 在MGM主机上为集群添加服务器
- 添加master主机
# 在管理主机,添加master角色主机
# 添加时不指定主机角色, 默认新主机被选为master
# 添加的master主机, 需手动分配slots(槽)
# 192.168.4.51:6351 为集群中任意一IP地址 目的是指定集群
[aaa@qq.com ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.51:6351
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.58:6358
192.168.4.58:6358 (7b4e968c...) -> 0 keys | 0 slots | 0 slaves. # 新建的为被分配slots
192.168.4.52:6352 (752769b4...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (27e9094b...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (ac22464a...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
- 重新分片
- 移除hash槽个数
- 指定接收hash槽主机ID
- 移除hash槽主机ID
- 同意配置
# 重新分片 192.168.4.53:6353 是指定集群
[aaa@qq.com ~]# redis-trib.rb reshard 192.168.4.53:6353
How many slots do you want to move (from 1 to 16384)? 4096 # 要移除的slots个数
What is the receiving node ID? 7b4e968c21dfa931c6441cbe16b3eeb69e4d0c3a # 指定接收slots的主机ID
Source node #1:all # 从所有主服务器移除slots
Do you want to proceed with the proposed reshard plan (yes/no)? yes # 同意配置
# 查看集群信息
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.53:6353
192.168.4.53:6353 (ac22464a...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (7b4e968c...) -> 1 keys | 4096 slots | 0 slaves.
192.168.4.52:6352 (752769b4...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (27e9094b...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
- 添加slave服务器
- 添加slave主机
- 格式 : redis-trib.rb add-node --slave [–master-id id值] ip地址:端口 192.168.4.51:6351
- 如果不指定主节点的id的话, 会把新节点随机添加为从节点最少的主库 192.168.4.51:6351 是指定添加到哪个集群
[aaa@qq.com ~]# redis-trib.rb add-node --slave 192.168.4.59:6359 192.168.4.51:6351
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.58:6358
192.168.4.58:6358 (7b4e968c...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.52:6352 (752769b4...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (27e9094b...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (ac22464a...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
移除redis服务器
- 移除slave主机
- 从服务器没有slots槽, 直接移除即可
- 移除时指定从服务器id值
# 查看集群详细信息
[aaa@qq.com ~]# redis-trib.rb check 192.168.4.58:6358
>>> Performing Cluster Check (using node 192.168.4.58:6358)
M: 7b4e968c21dfa931c6441cbe16b3eeb69e4d0c3a 192.168.4.58:6358
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
M: 752769b4fc263ea25237484f7510398eab324ec4 192.168.4.52:6352
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: a319c2efc9087b871159f14bf6824c99eab08fe1 192.168.4.56:6356
slots: (0 slots) slave
replicates 752769b4fc263ea25237484f7510398eab324ec4
S: dd8b9f228f174dadb0ddfba0885c040ff3dafa36 192.168.4.54:6354
slots: (0 slots) slave
replicates ac22464a362b018023d0b39ceb6bf54d58a9c651
M: 27e9094b2322cd951ff31b7fab814cd1ab41cdcb 192.168.4.55:6355
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: ac22464a362b018023d0b39ceb6bf54d58a9c651 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: dd3b908c28f5d7321c8de6ee121a6812d56444e7 192.168.4.59:6359
slots: (0 slots) slave
replicates 7b4e968c21dfa931c6441cbe16b3eeb69e4d0c3a
S: 25a9a8778a9176b41cbeb6c20f4d7b874253a9ba 192.168.4.51:6351
slots: (0 slots) slave
replicates 27e9094b2322cd951ff31b7fab814cd1ab41cdcb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 59从服务器id dd3b908c28f5d7321c8de6ee121a6812d56444e7
[aaa@qq.com ~]# redis-trib.rb del-node 192.168.4.51:6351 dd3b908c28f5d7321c8de6ee121a6812d56444e7
>>> Removing node dd3b908c28f5d7321c8de6ee121a6812d56444e7 from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
# 查询集群信息
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.58:6358
192.168.4.58:6358 (7b4e968c...) -> 1 keys | 4096 slots | 0 slaves.
192.168.4.52:6352 (752769b4...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (27e9094b...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (ac22464a...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
- 移动master服务器
- 释放占用的slots槽
- 移除主机
# 释放占用的slots槽
[aaa@qq.com ~]# redis-trib.rb reshard 192.168.4.53:6353
How many slots do you want to move (from 1 to 16384)? 4096 # 要移除的slots个数
What is the receiving node ID? 752769b4fc263ea25237484f7510398eab324ec4 # 指定接收slots的主机ID 随便写个主服务器ID
Source node #1:7b4e968c21dfa931c6441cbe16b3eeb69e4d0c3a # 从58 处移除slots
Source node #1:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes # 同意配置
# 移除主机
[aaa@qq.com ~]# redis-trib.rb del-node 192.168.4.51:6351 7b4e968c21dfa931c6441cbe16b3eeb69e4d0c3a
>>> Removing node 7b4e968c21dfa931c6441cbe16b3eeb69e4d0c3a from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
# 查看集群信息
[aaa@qq.com ~]# redis-trib.rb info 192.168.4.55:6355
192.168.4.55:6355 (27e9094b...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (ac22464a...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.52:6352 (752769b4...) -> 3 keys | 8192 slots | 1 slaves.
[OK] 7 keys in 3 masters.
0.00 keys per slot on average.
上一篇: 高性能Web站点技巧原理
下一篇: Redis初探(6)——Redis集群