Redis集群的操作

1、原始集群

1.1    启动Redis集群

[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7003/redis.conf
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7004/redis.conf
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7005/redis.conf
[[email protected] ~]#  /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7006/redis.conf

1.2    查看服务器运行状态

[[email protected] ~]# ps -ef | grep redis
root      2322     1  0 04:48 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7001 [cluster]
root      2326     1  0 04:48 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7002 [cluster]
root      2330     1  0 04:48 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7003 [cluster]
root      2334     1  0 04:48 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7004 [cluster]
root      2339     1  0 04:48 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7005 [cluster]
root      2344     1  0 04:48 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.2.105:7006 [cluster]
root      2349  2299  0 04:49 pts/0    00:00:00 grep redis

1.3    查看集群状态

# -c 表示集群模式

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.2.105 -p 7001
192.168.2.105:7001> cluster nodes
aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001 myself,slave 1b362004cf174d9ecb72c0650117781618c52a70 0 0 1 connected
1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004 master - 0 1477568990462 8 connected 0-5460
987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005 slave f9a0fbd03254b0010bca6782867dfa65f21a2448 0 1477568991974 5 connected
f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002 master - 0 1477568991471 2 connected 5461-10922
cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003 master - 0 1477568990967 3 connected 10923-16383
286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006 slave cabff9be14080ae12bfad0b199ae737639ec57bd 0 1477568990967 6 connected
192.168.2.105:7001> 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:8
cluster_my_epoch:8
cluster_stats_messages_sent:998
cluster_stats_messages_received:351

2、新集群操作

2.1  我们新建俩个服务,按照之前搭建的集群方式新增俩个节点:(一主一从 master、slave)  Master:7007      Slave:7008

步骤一:创建7007/7008文件夹。拷贝redis.conf文件到对于的7007,7008目录下要进行修改配置文件。

             [[email protected] redis-cluster]# mkdir 7007
             [[email protected] redis-cluster]# mkdir 7008
             [[email protected] redis-cluster]# cd 7001
             [[email protected] 7001]# cp redis.conf /usr/local/redis-cluster/7007/
             [[email protected] 7001]# cp redis.conf /usr/local/redis-cluster/7008/
             [[email protected] 7001]# vim/usr/local/redis-cluster/7007/redis.conf
                修改内容如下:
                port:7007
                dir /usr/local/redis-cluster/7007/
                cluster-config-file nodes-7007.conf
             [[email protected] 7001]# vim/usr/local/redis-cluster/7008/redis.conf
                修改内容如下:
                port:7008
                dir /usr/local/redis-cluster/7008/
                cluster-config-file nodes-7008.conf

步骤二:启动7007和7008俩个服务并查看服务状态。

[[email protected] 7001]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7007/redis.conf
[[email protected] 7001]#/usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf
[[email protected] 7001]# ps -el | grep redis

2.2  学习redis-trib命令使用

[[email protected] ~]# cd /usr/local/redis-3.0.0-rc2/src/
[[email protected] src]# ./redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>

  call            host:port command arg arg .. arg
  set-timeout     host:port milliseconds
  del-node        host:port node_id
  fix             host:port
  reshard         host:port
                  --slots <arg>
                  --yes
                  --from <arg>
                  --to <arg>
  help            (show this help)
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  check           host:port
  import          host:port
                  --from <arg>
  create          host1:port1 ... hostN:portN
                  --replicas <arg>

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

1. create:创建一个集群环境host1:port1 ... hostN:portN(集群中的主从节点比例)

2. call:可以执行redis命令

3. add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port

4. del-node:移除一个节点

5. reshard:重新分片

6. check:检查集群状态

3、新增一个主节点7007(master)

 步骤一:使用add-node命令:绿色为新增节点,红色为已知存在节点

/usr/local/redis-3.0.0-rc2/src/redis-trib.rb  add-node
192.168.2.105:7007 192.168.2.105:7001

>>> Adding node 192.168.2.105:7007 to cluster 192.168.2.105:7004
Connecting to node 192.168.2.105:7004: OK
Connecting to node 192.168.2.105:7005: OK
Connecting to node 192.168.2.105:7002: OK
Connecting to node 192.168.2.105:7006: OK
Connecting to node 192.168.2.105:7001: OK
Connecting to node 192.168.2.105:7003: OK
>>> Performing Cluster Check (using node 192.168.2.105:7004)
M: 1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005
   slots: (0 slots) slave
   replicates f9a0fbd03254b0010bca6782867dfa65f21a2448
M: f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006
   slots: (0 slots) slave
   replicates cabff9be14080ae12bfad0b199ae737639ec57bd
S: aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001
   slots: (0 slots) slave
   replicates 1b362004cf174d9ecb72c0650117781618c52a70
M: cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 192.168.2.105:7007: OK
>>> Send CLUSTER MEET to node 192.168.2.105:7007 to make it join the cluster.
[OK] New node added correctly.

 步骤二:查看集群状态:

/usr/local/redis/bin/redis-cli -c -h 192.168.2.105 -p 7001
192.168.2.105:7001> cluster  nodes
192.168.2.105:7001> cluster nodes
aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001 myself,slave 1b362004cf174d9ecb72c0650117781618c52a70 0 0 1 connected
c25aca95364e13b58e57d831126ed77656ba1d76 192.168.2.105:7007 master - 0 1477571648229 9 connected 0-65 5461-5527 10923-10988
1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004 master - 0 1477571648734 8 connected 66-5460
987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005 slave f9a0fbd03254b0010bca6782867dfa65f21a2448 0 1477571649238 5 connected
f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002 master - 0 1477571649238 2 connected 5528-10922
cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003 master - 0 1477571648229 3 connected 10989-16383
286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006 slave cabff9be14080ae12bfad0b199ae737639ec57bd 0 1477571647225 6 connected

注意:当添加节点成功以后,新增的节点不会有任何数据,因为它没有分配任何的slot(hash槽)。我们需要为新节点手工分配slot。

4、为7007分配slot槽

 步骤一:使用redis-trib 命令,找到集群中的任意一个主节点(红色位置表现集群中的任意一个主节点),对其进行重新分片工作。 

[[email protected] 7001]#/usr/local/redis-3.0.0-rc2/src/redis-trib.rb reshard
192.168.2.105:7004

Connecting to node 192.168.2.105:7004: OK
Connecting to node 192.168.2.105:7007: OK
Connecting to node 192.168.2.105:7005: OK
Connecting to node 192.168.2.105:7002: OK
Connecting to node 192.168.2.105:7006: OK
Connecting to node 192.168.2.105:7001: OK
Connecting to node 192.168.2.105:7003: OK
>>> Performing Cluster Check (using node 192.168.2.105:7004)
M: 1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: c25aca95364e13b58e57d831126ed77656ba1d76 192.168.2.105:7007
   slots: (0 slots) master
   0 additional replica(s)
S: 987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005
   slots: (0 slots) slave
   replicates f9a0fbd03254b0010bca6782867dfa65f21a2448
M: f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006
   slots: (0 slots) slave
   replicates cabff9be14080ae12bfad0b199ae737639ec57bd
S: aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001
   slots: (0 slots) slave
   replicates 1b362004cf174d9ecb72c0650117781618c52a70
M: cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

(提示一)

How many slots do youwant to move (from 1 to 16384)?200

(提示二)

What is the receivingnode ID? 382634a4025778c040b7213453fd42a709f79e28

Please enter all thesource node IDs.

Type ‘all‘ to use all the nodes as sourcenodes for the hash slots.

Type ‘done‘ once you entered all the sourcenodes IDs.

Source node #1:all

Ready to move 200slots.

Sourcenodes:

M: 614d0def75663f2620b6402a017014b57c912dad192.168.2.105:7001

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 8aac82b63d42a1989528cd3906579863a5774e77192.168.2.105:7002

slots:5461-10922 (5462 slots) master

1 additional replica(s)

M: 83df08875c7707878756364039df0a4c8658f272192.168.2.105:7003

slots:10923-16383 (5461 slots) master

1 additional replica(s)

Destinationnode:

M: 382634a4025778c040b7213453fd42a709f79e28192.168.2.105:7007

slots: (0 slots) master

0 additional replica(s)

Resharding plan:(分片执行计划日志)

Moving slot 5461 from8aac82b63d42a1989528cd3906579863a5774e77

...

Moving slot 0 from 614d0def75663f2620b6402a017014b57c912dad

...

Moving slot 10923 from83df08875c7707878756364039df0a4c8658f272

...

(提示三)

Do you want toproceed with the proposed reshard plan (yes/no)?yes

Moving slot 65 from 192.168.2.105:7001to 192.168.2.105:7007:

...

Moving slot 10923 from 192.168.2.105:7003to 192.168.2.105:7007:

...

Moving slot 5527 from 192.168.2.105:7002to 192.168.2.105:7007:

...

提示一:是希望你需要多少个槽移动到新的节点上,可以自己设置,比如200个槽。

提示二:是你需要把这200个slot槽移动到那个节点上去(需要指定节点id),并且下个提示是输入all为从所有主节点(7001 7002 7003)中分别抽取响应的槽数(一共为200个槽到指定的新节点中!,并且会打印执行分片的计划。)

提示三:输入yes确认开始执行分片任务。

如上图所示,现在我们的7007已经有slot槽了,也就是说可以在7007上进行读写数据啦!到此为止我们的7007已经加入到集群中啦,并且是主节点(Master)

5、添加从节点(7008)到集群中去

步骤一:还是需要执行add-node命令:

[[email protected] 7001]#     /usr/local/redis-3.0.0-rc2/src/redis-trib.rbadd-node 192.168.2.105:7008 192.168.2.105:7004

         步骤二:我们需要执行replicate命令来指定当前节点(从节点)的主节点id为哪个。

首先需要登录新加的7008节点的客户端,然后使用集群命令进行操作,把当前的7008(slave)节点指定到一个主节点下(这里使用之前创建的7007主节点,红色表示节点id)

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.2.105 -p 7008
192.168.2.105:7008> cluster replicate <strong><span style="color:#ff0000;">c25aca95364e13b58e57d831126ed77656ba1d76</span></strong>
192.168.2.105:7008> OK(提示OK则操作成功)

我们继续看一下当前集群的状态,如下图:我们已经成功的把7008放到7007这个主节点下面了,到此为止我们已经成功的添加完一个从节点了。

6、尝试删除一个节点(7008 slave)

          步骤一:删除从节点7008,输入del-node命令,指定删除节点ip和端口,以及节点id(红色为7008节点id)

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rb  del-node 192.168.2.105:7008bd83784a3eb65264fcdfc38e6c2a8f6f5dc6dd0c

          步骤二:再次查看一下集群状态,如下图所示,我们已经成功的移除了7008 slave节点,另外我们发现移除一个节点以后,当前节点的服务进程也会随之销毁。可以使用ps命令查看当前的服务(ps -ef | grep redis),发现少了一个运行的server,也就是刚移除的7008从节点。

7、删除之前加入的主节点7007,这个步骤会相对比较麻烦一些,因为主节点的里面是有分配了slot槽的,所以我们这里必须先把7007里的slot槽放入到其他的可用主节点中去,然后再进行移除节点操作才行,不然会出现数据丢失问题。

         步骤一:删除7007(master)节点之前,我们需要先把其全部的数据(slot槽)移动到其他节点上去(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能)。

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rb  reshard 192.168.2.105:7007

输出如下:

>>> Performing Cluster Check(using node 192.168.2.105:7007)

M: 382634a4025778c040b7213453fd42a709f79e28192.168.2.105:7007

slots:0-65,5461-5527,10923-10988 (199 slots) master

0 additional replica(s)

S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0192.168.2.105:7006

slots: (0 slots) slave

replicates 83df08875c7707878756364039df0a4c8658f272

S: a69b98937844c6050ee5885266ccccb185a3f36a192.168.2.105:7004

slots: (0 slots) slave

replicates 614d0def75663f2620b6402a017014b57c912dad

M: 614d0def75663f2620b6402a017014b57c912dad192.168.2.105:7001

slots:66-5460 (5395 slots) master

1additional replica(s)

M: 8aac82b63d42a1989528cd3906579863a5774e77192.168.2.105:7002

slots:5528-10922 (5395 slots) master

1additional replica(s)

S: adb99506ddccad332e09258565f2e5f4f456a150192.168.2.105:7005

slots: (0 slots) slave

replicates8aac82b63d42a1989528cd3906579863a5774e77

M: 83df08875c7707878756364039df0a4c8658f272192.168.2.105:7003

slots:10989-16383 (5395 slots) master

1additional replica(s)

[OK] All nodes agree about slotsconfiguration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

How many slots do youwant to move (from 1 to 16384)?199

(注释:这里不会是正好200个槽)

What is the receivingnode ID? 614d0def75663f2620b6402a017014b57c912dad

(注释:这里是需要把数据移动到哪?7001的主节点id)

Please enter all thesource node IDs.

Type ‘all‘ to use all the nodes as sourcenodes for the hash slots.

Type ‘done‘ once you entered all the sourcenodes IDs.

Source node #1:382634a4025778c040b7213453fd42a709f79e28

(注释:这里是需要数据源,也就是我们的7007节点id)

Source node #2:done

(注释:这里直接输入done
开始生成迁移计划)

Ready to move 199 slots.

Source nodes:

M: 382634a4025778c040b7213453fd42a709f79e28192.168.2.105:7007

slots:0-65,5461-5527,10923-10988 (199 slots)master

0 additional replica(s)

Destinationnode:

M: 614d0def75663f2620b6402a017014b57c912dad192.168.2.105:7001

slots:66-5460 (5395 slots) master

1 additional replica(s)

Resharding plan:

Moving slot 0 from 382634a4025778c040b7213453fd42a709f79e28

...

Do you want toproceed with the proposed reshard plan (yes/no)? Yes

(注释:这里输入yes开始迁移)

Moving slot 0 from 192.168.2.105:7007 to 192.168.2.105:7001:

         到此为止我们已经成功的把7007主节点的数据迁移到7001上去了,我们可以看一下现在的集群状态如下图,你会发现7007下面已经没有任何数据(slot)槽了,证明迁移成功!

          步骤二:最后我们直接使用del-node命令删除7007主节点即可(红色表示7007的节点id)。

[[email protected] 7001]# /usr/local/redis-3.0.0-rc2/src/redis-trib.rbdel-node   192.168.2.105:7007   c25aca95364e13b58e57d831126ed77656ba1d76

最后:我们查看集群状态,一切还原为最初始状态啦!OK结束!

192.168.2.105:7001> cluster nodes
aae7ff08b8cc4bb5473044a52f926b6e82c96840 192.168.2.105:7001 myself,slave 1b362004cf174d9ecb72c0650117781618c52a70 0 0 1 connected
1b362004cf174d9ecb72c0650117781618c52a70 192.168.2.105:7004 master - 0 1477574617571 10 connected 0-5527 10923-10988
987b27c96774f9c9bbbbf7bf3ccfe4e4b2e4c3e7 192.168.2.105:7005 slave f9a0fbd03254b0010bca6782867dfa65f21a2448 0 1477574618076 5 connected
f9a0fbd03254b0010bca6782867dfa65f21a2448 192.168.2.105:7002 master - 0 1477574618579 2 connected 5528-10922
cabff9be14080ae12bfad0b199ae737639ec57bd 192.168.2.105:7003 master - 0 1477574617066 3 connected 10989-16383
286b1ed602c9e74f23b8bd8d66db50308b256d3e 192.168.2.105:7006 slave cabff9be14080ae12bfad0b199ae737639ec57bd 0 1477574617066 6 connected
时间: 2024-11-29 03:17:38

Redis集群的操作的相关文章

Linux离线安装redis集群

一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务器处于脱机状态. 二.安装环境及工具 系统:CentOS7 工具:XShell5及Xftp5 安装包: Ruby-2.4.2 Rubygems-2.6.14 Redis-4.0.2(3.x版本才开始支持集群功能) openssl-1.1.0f 三.安装步骤 要搭建一个最简单的Redis集群,我们至少

Prometheus 监控 Redis 集群

Prometheus 监控 Redis cluster,其实套路都是一样的,使用 exporter. exporter 负责采集指标,通过 http 暴露给 Prometheus 拉取.granafa 则通过这些指标绘图展示数据.Prometheus 收集的数据还会根据你设置的告警规则判断是否要发送给 Alertmanager, Alertmanager 则要判断是否要发出告警. Alertmanager 告警分为三个阶段 Inactive 触发告警的规则会被发送到这来. Pending 你设置

Java Spring mvc 操作 Redis 及 Redis 集群

本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 Redis 是什么,能做什么 Redis 是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序集合,位图,hyperloglogs等数据类型.内置复制.Lua脚本.LRU收回.事务以及不同级别磁盘持久化功能,同时通过Redis

java操作redis数据库实例(redis集群)

1.配置redis集群 <?xml version="1.0" encoding="UTF-8"?> <redisCluster> <!--userRoute --> <clusterGroup name="userRoute" selectdb="1"> <server host="10.177.129.16" port="6379"

Redis集群操作手册

一.原始集群(6节点 3主3从): (1)启动集群: [[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf [[email protected] ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf [[email protected] ~]# /usr

java操作redis集群配置[可配置密码]和工具类

java操作redis集群配置[可配置密码]和工具类 <dependency>   <groupId>redis.clients</groupId>   <artifactId>jedis</artifactId>   <version>2.9.0</version>   </dependency>   <dependency>   <groupId>org.apache.commons

redis集群介绍、redis集群搭建配置、redis集群操作

一:redis集群介绍 多个redis节点网络互联,数据共享所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为.支持在线增加.删除节点客户端可以连任何一个主节点进行读写 二:redis集群搭建配置 场景设置:两台机器,分别开启三个Redis服务(端口)A机器上三个端口7000,7002,7004,全部为主B机器上三个端口7001,

Redis 集群管理常见操作

Redis 内置客户端 redis-cli 通过 --cluster 选项集成了多个 Redis 集群管理工具, 这些工具可以执行创建集群.向集群中添加或移除节点.对集群实施重分片以及负载均衡等操作, 它们每一个都非常强大:$ redis-cli --cluster help``` Cluster Manager Commands:create host1:port1 ... hostN:portN--cluster-replicas <arg>check host:portinfo host

redis集群操作:增加和减少节点

一  增加节点: STEP1:  我们新建俩个服务,按照之前搭建的集群方式新增俩个节点:(一主一从master.slave) Master:7007   Slave: 7008 (1)创建7007/7008文件夹.拷贝redis.conf文件到对于的7007,7008目录下要进行修改配置文件. <root@localhost redis-cluster># mkdir 7007 7008 <root@localhost redis-cluster># cp 7001/redis.c