21.22 redis集群介绍
21.23/21.24 redis集群搭建配置
21.25 redis集群操作
21.22 redis集群介绍
redis集群是分布式的,写入的数据并不在同一台机器上,因此每台机器上的数据并不一致
。
Redis集群上的所有节点都是一主一从
(可以是多个从),其中从不提供服务,仅作为备用。不支持
同时处理多个键(如mset/mget
),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
支持在线增加、删除节点。客户端可以连任何一个主节点进行读写。
21.23/21.24 redis集群搭建配置
场景设置:
两台机器,分别开启三个Redis服务(端口)
kun03 192.168.80.102
kun04 192.168.80.103
kun03机器上三个端口7000,7002,7004,全部为主
kun04机器上三个端口7001,7003,7005,全部为从
两台机器上都要编译安装redis,然后编辑并复制3个不同的redis.conf,分别设置不同的端口号、dir等参数,还需要增加cluster相关参数,然后分别启动6个redis服务
具体redis配置文件大家到https://coding.net/u/aminglinux/p/yuanke_centos7/git/tree/master/21NOSQL
准备工作
在kun03机器上
编辑7000 7002 7004的配置文件
1 | [root@kun03 ~]# vim /etc/redis_7000.conf |
1 | [root@kun03 ~]# vim /etc/redis_7002.conf |
1 | [root@kun03 ~]# vim /etc/redis_7004.conf |
创建对应的数据目录
1 | [root@kun03 ~]# mkdir /data/redis_data |
启动Redis服务
1 | [root@kun03 ~]# redis-server /etc/redis_7000.conf |
在kun04机器上
编辑7001 7003 7005的配置文件
1 | [root@kun04 ~]# vim /etc/redis_7001.conf |
1 | [root@kun04 ~]# vim /etc/redis_7003.conf |
1 | [root@kun04 ~]# vim /etc/redis_7005.conf |
创建对应的数据目录
1 | [root@kun04 ~]# mkdir /data/redis_data/ |
启动Redis服务
1 | [root@kun04 ~]# redis-server /etc/redis_7001.conf |
现在任意一台机器上安装Ruby2.2 由于yum上的Ruby只有2.0版本,而redis cluster支持ruby2.2的。因此需要安装ruby。这里是把ruby的源码包制作成rpm包来安装ruby的 ,也可以直接编译安装源码包来安装ruby。
步骤
下载对应的包
1 | [root@kun03 ~]# yum -y groupinstall "Development Tools" |
进入root目录下
1 | [root@kun03 ~]# cd /root/ |
创建目录
1 | [root@kun03 ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} |
下载源码包和spec文件到rpmbuild/SOURCES目录下
1 | [root@kun03 ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES |
制作ruby的rpm包
1 | [root@kun03 ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec |
yum安装本地rpm包
1 | [root@kun03 ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm |
下载redis-trib.rb并拷贝到bin目录下
1 | [root@kun03 ~]# gem install redis |
使用redis-trib.rb命令建立集群
1 | [root@kun03 ~]# redis-trib.rb create --replicas 1 192.168.80.102:7000 192.168.80.102:7002 192.168.80.102:7004 192.168.80.103:7001 192.168.80.103:7003 192.168.80.103:7005 |
看到两个OK表示已经集群创建成功
21.25 redis集群操作
可以连接任意一个节点来操作Redis集群
登录集群
格式:redis-cli -c -h IP -p 端口
1
[root@kun03 x86_64]# redis-cli -c -h 192.168.80.102 -p 7000
-h
指定IP-c
使用集群方式登录-p
指定端口
查看集群状态
格式:redis-trib.rb check 任意节点IP:端口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24[root@kun03 x86_64]# redis-trib.rb check 192.168.80.102:7000
>>> Performing Cluster Check (using node 192.168.80.102:7000)
M: 2042a7a6231ea5b6eb42ca3a5bf02abb419126a0 192.168.80.102:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 41866d20d5f0d736b05abaea15d0e08b6bcd9350 192.168.80.103:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 48f4abd0547a0c794c6905c2bd0a314f212b0042 192.168.80.103:7005
slots: (0 slots) slave
replicates 2042a7a6231ea5b6eb42ca3a5bf02abb419126a0
M: c66658b3eea43e01d49367a7f610055a6ec0d5bd 192.168.80.102:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 90c82e347f19415b3692c2cab5a671566573bbd9 192.168.80.102:7004
slots: (0 slots) slave
replicates 41866d20d5f0d736b05abaea15d0e08b6bcd9350
S: 28d00e5717a7eb28dc461c002e733dba94ac9a5e 192.168.80.103:7003
slots: (0 slots) slave
replicates c66658b3eea43e01d49367a7f610055a6ec0d5bd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
前面命令都是进入集群后操作的
列出节点
格式:cluster ndes
1
2
3
4
5
6
7192.168.80.102:7000> CLUSTER NODES
41866d20d5f0d736b05abaea15d0e08b6bcd9350 192.168.80.103:7001@17001 master - 0 1534940719663 4 connected 5461-10922
48f4abd0547a0c794c6905c2bd0a314f212b0042 192.168.80.103:7005@17005 slave 2042a7a6231ea5b6eb42ca3a5bf02abb419126a0 0 1534940715632 6 connected
c66658b3eea43e01d49367a7f610055a6ec0d5bd 192.168.80.102:7002@17002 master - 0 1534940718150 2 connected 10923-16383
90c82e347f19415b3692c2cab5a671566573bbd9 192.168.80.102:7004@17004 slave 41866d20d5f0d736b05abaea15d0e08b6bcd9350 0 1534940717645 4 connected
2042a7a6231ea5b6eb42ca3a5bf02abb419126a0 192.168.80.102:7000@17000 myself,master - 0 1534940715000 1 connected 0-5460
28d00e5717a7eb28dc461c002e733dba94ac9a5e 192.168.80.103:7003@17003 slave c66658b3eea43e01d49367a7f610055a6ec0d5bd 0 1534940718655 5 connected
7005 slave 2042a7a6231ea5b6eb42ca3a5bf02abb419126a0
2042a7a6231ea5b6eb42ca3a5bf02abb419126a0 表示7000的nodeIP,因此7005是7000的从
查看集群信息
格式:cluster info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18192.168.80.102:7000> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1920
cluster_stats_messages_pong_sent:1271
cluster_stats_messages_fail_sent:4
cluster_stats_messages_sent:3195
cluster_stats_messages_ping_received:1271
cluster_stats_messages_pong_received:1372
cluster_stats_messages_fail_received:1
cluster_stats_messages_received:2644
添加新节点
格式:cluster meet 节点IP 端口
1
2
3
4
5
6
7
8192.168.80.102:7000> CLUSTER MEET 192.168.80.102 7006
OK
192.168.80.102:7000> CLUSTER MEET 192.168.80.103 7007
OK
192.168.80.102:7000> CLUSTER NODES ##新添加的节点默认都是master
8b9e3db578088c156fc9733185423880e415a0da 192.168.80.103:7007@17007 master - 0 1534941780821 7 connected
bd436acd42955038a96b2bab9df2dcb70f347439 192.168.80.102:7006@17006 master - 0 1534941781000 0 connected
把当前节点设置为指定节点的从
格式:cluster replicater 主节点的node_id
1
2
3
4
5
6
7[root@kun03 x86_64]# redis-cli -c -h 192.168.80.103 -p 7007 ##进入7007的节点
192.168.80.103:7007> CLUSTER REPLICATE bd436acd42955038a96b2bab9df2dcb70f347439
OK ##指定为7006的node_id的从
192.168.80.103:7007> CLUSTER NODES
8b9e3db578088c156fc9733185423880e415a0da 192.168.80.103:7007@17007 myself,slave bd436acd42955038a96b2bab9df2dcb70f347439 0 1534941891000 7 connected
bd436acd42955038a96b2bab9df2dcb70f347439 192.168.80.102:7006@17006 master - 0 1534941893533 0 connected
移除节点
格式:cluster forget 移除节点node_id
1
2
3
4
5
6
7
8
9192.168.80.103:7007> CLUSTER FORGET bd436acd42955038a96b2bab9df2dcb70f347439
(error) ERR Can't forget my master! ##不能移除为主的节点,可以把该节点设置为其他几点的从后再移除
192.168.80.103:7007> CLUSTER FORGET 8b9e3db578088c156fc9733185423880e415a0da
(error) ERR I tried hard but I can't forget myself... ##不能移除当前节点 ,可以登录其他节点再来移除
[root@kun03 x86_64]# redis-cli -c -h 192.168.80.102 -p 7000 ##登录7000节点来移除7007节点
192.168.80.102:7000> CLUSTER FORGET 8b9e3db578088c156fc9733185423880e415a0da
OK
保存集群配置
格式:cluster saveconfig
1
2192.168.80.102:7000> CLUSTER SAVECONFIG
OK ##默认保存到某个节点的数据目录下.config文件中