Redis5.0客户端redis-cli管理cluster尝试

  |   3 评论   |   15,005 浏览

    Redis5.0发布了,其中更新了一个新的特性,把Redis的集群管理从Ruby(redis-trib.rb)移植到了C语言代码,直接使用redis-cli就可以管理集群,今天我们就尝试一下。

    1、直接从官网下载最新的Redis5.0包,下载地址 Redis 5.0.0 is the latest stable version.,然后编译安装下。

    2、创建三个文件夹
    mkdir -p /data/redis/cluster/7000
    mkdir -p /data/redis/cluster/7001
    mkdir -p /data/redis/cluster/7002
    把第一步编译好的文件,为了简单只需要把 redis.conf,redis-server,redis-cli 这三个文件copy到7000,7001,7002文件夹中。同时编辑各自的redis.conf文件,修改对应端口。 这些就作为cluster的三个节点。由于我们是为了演示使用redis-cli创建集群的过程,就直接三个master节点,不用对应的slave的节点了。

    3、分别启动7000、7001、7002三个节点
    cd /data/redis/cluster/7000
    ./redis-server redis.conf

    cd /data/redis/cluster/7001
    ./redis-server redis.conf

    cd /data/redis/cluster/7002
    ./redis-server redis.conf

    然后确认redis节点是否正常启动
    [root@shdreams cluster]# ps -ef|grep redis
    root 30248 1 0 03:16 ? 00:00:00 ./redis-server 127.0.0.1:7000 [cluster]
    root 30255 1 0 03:18 ? 00:00:00 ./redis-server 127.0.0.1:7001 [cluster]
    root 30260 1 0 03:18 ? 00:00:00 ./redis-server 127.0.0.1:7002 [cluster]

    4、接下来就是创建集群了,首先我们先看下 redis-cli 都提供了哪些操作集群的命令
    [root@shdreams 7000]# ./redis-cli –cluster help
    Cluster Manager Commands:
    create host1:port1 … hostN:portN
    –cluster-replicas
    check host:port
    info host:port
    fix host:port
    reshard host:port
    –cluster-from
    –cluster-to
    –cluster-slots
    –cluster-yes
    –cluster-timeout
    –cluster-pipeline
    rebalance host:port
    –cluster-weight <node1=w1…nodeN=wN>
    –cluster-use-empty-masters
    –cluster-timeout
    –cluster-simulate
    –cluster-pipeline
    –cluster-threshold
    add-node new_host:new_port existing_host:existing_port
    –cluster-slave
    –cluster-master-id
    del-node host:port node_id
    call host:port command arg arg .. arg
    set-timeout host:port milliseconds
    import host:port
    –cluster-from
    –cluster-copy
    –cluster-replace
    help

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

    OK,我们就创建集群吧~
    我们键入命令 ./redis-cli –cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 回车就会出现如下信息

    Performing hash slots allocation on 3 nodes…
    Master[0] -> Slots 0 - 5460
    Master[1] -> Slots 5461 - 10922
    Master[2] -> Slots 10923 - 16383
    M: a7b511330bffe28357cd21d6ee543e59f0a38dea 127.0.0.1:7000
    slots:[0-5460](5461 slots) master
    M: a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 127.0.0.1:7001
    slots:[5461-10922](5462 slots) master
    M: 91d2903fa575b83b6dc9e6b7dac74c923e2882e9 127.0.0.1:7002
    slots:[10923-16383](5461 slots) master
    Can I set the above configuration? (type ‘yes’ to accept):

    这里是提示我们确认节点相关信息。
    M表示master;后面一长字符串是节点的RunID,在一个redis集群中RunID是唯一的,启动的时候系统自动创建的,不需要人工干预;slots表示此节点分配的hash槽
    这里,就直接键入 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:7000)
    M: a7b511330bffe28357cd21d6ee543e59f0a38dea 127.0.0.1:7000
    slots:[0-5460](5461 slots) master
    M: 91d2903fa575b83b6dc9e6b7dac74c923e2882e9 127.0.0.1:7002
    slots:[10923-16383](5461 slots) master
    M: a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 127.0.0.1:7001
    slots:[5461-10922](5462 slots) master
    [OK] All nodes agree about slots configuration.
    Check for open slots…
    Check slots coverage…
    [OK] All 16384 slots covered.

    如果出现以上信息就表示集群创建OK了

    5、验证下集群信息
    我们进入集群 ./redis-cli -h 127.0.0.1 -p 7000 -c

    127.0.0.1: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:3
    cluster_size:3
    cluster_current_epoch:3
    cluster_my_epoch:1
    cluster_stats_messages_ping_sent:254
    cluster_stats_messages_pong_sent:286
    cluster_stats_messages_sent:540
    cluster_stats_messages_ping_received:284
    cluster_stats_messages_pong_received:254
    cluster_stats_messages_meet_received:2
    cluster_stats_messages_received:540

    127.0.0.1:7000> cluster nodes
    91d2903fa575b83b6dc9e6b7dac74c923e2882e9 127.0.0.1:7002@17002 master - 0 1539934393000 3 connected 10923-16383
    a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 127.0.0.1:7001@17001 master - 0 1539934393933 2 connected 5461-10922
    a7b511330bffe28357cd21d6ee543e59f0a38dea 127.0.0.1:7000@17000 myself,master - 0 1539934390000 1 connected 0-5460

    通过以上信息,确认目前reids集群是正常的。

    6、尝试下redis-cli cluster的其他命令

    把7000节点的10个slots移到7001节点上,键入命令如下,
    ./redis-cli –cluster reshard 127.0.0.1:7000 –cluster-from a7b511330bffe28357cd21d6ee543e59f0a38dea –cluster-to a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 –cluster-slots 10 –cluster-yes

    Performing Cluster Check (using node 127.0.0.1:7000)
    M: a7b511330bffe28357cd21d6ee543e59f0a38dea 127.0.0.1:7000
    slots:[0-5460](5461 slots) master
    M: 91d2903fa575b83b6dc9e6b7dac74c923e2882e9 127.0.0.1:7002
    slots:[10923-16383](5461 slots) master
    M: a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 127.0.0.1:7001
    slots:[5461-10922](5462 slots) master
    [OK] All nodes agree about slots configuration.
    Check for open slots…
    Check slots coverage…
    [OK] All 16384 slots covered.

    Ready to move 10 slots.
    Source nodes:
    M: a7b511330bffe28357cd21d6ee543e59f0a38dea 127.0.0.1:7000
    slots:[0-5460](5461 slots) master
    Destination node:
    M: a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 127.0.0.1:7001
    slots:[5461-10922](5462 slots) master
    Resharding plan:
    Moving slot 0 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 1 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 2 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 3 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 4 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 5 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 6 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 7 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 8 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 9 from a7b511330bffe28357cd21d6ee543e59f0a38dea
    Moving slot 0 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 1 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 2 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 3 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 4 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 5 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 6 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 7 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 8 from 127.0.0.1:7000 to 127.0.0.1:7001:
    Moving slot 9 from 127.0.0.1:7000 to 127.0.0.1:7001:

    以上显示已经移动成功了,我们可以再看下slots的分布情况
    [root@shdreams 7000]# ./redis-cli -h 127.0.0.1 -p 7000 -c cluster nodes
    91d2903fa575b83b6dc9e6b7dac74c923e2882e9 127.0.0.1:7002@17002 master - 0 1539934886013 3 connected 10923-16383
    a7ab1aa24c9030d1fb42bbac3ad72c15bf683ef4 127.0.0.1:7001@17001 master - 0 1539934885011 4 connected 0-9 5461-10922
    a7b511330bffe28357cd21d6ee543e59f0a38dea 127.0.0.1:7000@17000 myself,master - 0 1539934884000 1 connected 10-5460
    发现slots已经转移了。

    当然我们也可以添加节点,删除节点试验,这里就不做了。
    通过以上尝试,发现通过redis-cli来操作redis集群确实方便多了。

    评论

    • 啊啊啊 回复»

      :smile:

    • 111 回复»

      11111111:sob:

    • pz 回复»

      试着加个 password 创建试试看

    发表评论

    validate