使用Redis-trib.rb创建redis cluster

一:在所有服务器开启2个实例,分别是端口6379和6380
 
二:使用cluster的集群管理命令redis-trib.rb创建3个主节点
    [root@redis-102 redis-3.0.0]# redis-trib.rb create 172.18.64.102:6379 172.18.64.103:6379 172.18.64.104:6379
    >>> Creating cluster
    Connecting to node 172.18.64.102:6379: OK
    Connecting to node 172.18.64.103:6379: OK
    Connecting to node 172.18.64.104:6379: OK
    >>> Performing hash slots allocation on 3 nodes…
    Using 3 masters:
    172.18.64.104:6379
    172.18.64.103:6379
    172.18.64.102:6379
    M: 3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379
       slots:10923-16383 (5461 slots) master
    M: 50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379
       slots:5461-10922 (5462 slots) master
    M: 9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379
       slots:0-5460 (5461 slots) master
    Can I set the above configuration? (type ‘yes’ to accept): yes    <———————–此处等用户确认,如果没问题就输入yes,redis-trib.rb将创建集群
    >>> 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 172.18.64.102:6379)
    M: 3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379          <————————此处显示了该节点的节点IDs和表示是Master
       slots:10923-16383 (5461 slots) master              <————————此处显示了该节点负责处理的slot数量
    M: 50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379
       slots:5461-10922 (5462 slots) master
    M: 9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379
       slots:0-5460 (5461 slots) master
    [OK] All nodes agree about slots configuration.           <————————表明创建ok
    >>> Check for open slots…
    >>> Check slots coverage…
    [OK] All 16384 slots covered.      <—————此处看见所有的16384个slot分配ok
   
   
三:检查cluster的运行状况
    [root@redis-102 redis-3.0.0]# redis-trib.rb check 172.18.64.102:6379
    Connecting to node 172.18.64.102:6379: OK
    Connecting to node 172.18.64.103:6379: OK
    Connecting to node 172.18.64.104:6379: OK
    >>> Performing Cluster Check (using node 172.18.64.102:6379)
    M: 3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379
       slots:10923-16383 (5461 slots) master
       0 additional replica(s)
    M: 50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379
       slots:5461-10922 (5462 slots) master
       0 additional replica(s)
    M: 9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379
       slots:0-5460 (5461 slots) master
       0 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots…
    >>> Check slots coverage…
    [OK] All 16384 slots covered.
   
四:查看cluster的各节点信息
    [root@redis-102 redis-3.0.0]# redis-cli -c -p 6379 cluster nodes
    50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379 master – 0 1432281835580 2 connected 5461-10922
    9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379 master – 0 1432281834579 3 connected 0-5460
    3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379 myself,master – 0 0 1 connected 10923-16383
   
五:查看cluster的info
    [root@redis-102 redis-3.0.0]# redis-cli -c -p 6379 cluster info
    cluster_state:ok                  <————————表明集群运行ok
    cluster_slots_assigned:16384
    cluster_slots_ok:16384         <————————所有slot都ok
    cluster_slots_pfail:0              <————————预失败的slot数量
    cluster_slots_fail:0                <————————失败的slot数量
    cluster_known_nodes:3         <————————该集群目前包含的节点个数
    cluster_size:3
    cluster_current_epoch:3
    cluster_my_epoch:1
    cluster_stats_messages_sent:848
    cluster_stats_messages_received:848
   
六:使用redis-trib.rb添加节点到集群
    redis-trib.rb add-node new_ip:new_port  existing_host:existing_port –master-id <IDs> –slave
    交互式的添加node
    [root@redis-102 redis-3.0.0]# redis-trib.rb add-node 172.18.64.102:6380 172.18.64.102:6379
    >>> Adding node 172.18.64.102:6380 to cluster 172.18.64.102:6379
    Connecting to node 172.18.64.102:6379: OK
    Connecting to node 172.18.64.103:6379: OK
    Connecting to node 172.18.64.104:6379: OK
    >>> Performing Cluster Check (using node 172.18.64.102:6379)
    M: 3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379
       slots:10923-16383 (5461 slots) master
       0 additional replica(s)
    M: 50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379
       slots:5461-10922 (5462 slots) master
       0 additional replica(s)
    M: 9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379
       slots:0-5460 (5461 slots) master
       0 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 172.18.64.102:6380: OK
    >>> Send CLUSTER MEET to node 172.18.64.102:6380 to make it join the cluster.
    [OK] New node added correctly.              

七:检查新增加的节点
    03d704860b97e40db753fca10729fd0796123b11 172.18.64.102:6380 master – 0 1432282865995 0 connected         <——新加的节点都为master,没有slot
    50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379 master – 0 1432282866997 2 connected 5461-10922
    9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379 master – 0 1432282867999 3 connected 0-5460
    3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379 myself,master – 0 0 1 connected 10923-16383
    依次加入其它新的节点
   
八:把新添加的master节点设置成已经存在主节点的从节点
   
    连接到需要作为从的节点
    命令:
    cluster replicate ‘master IDs’
   
    [root@redis-102 run]# redis-cli -h 172.18.64.102 -p 6380                         
    172.18.64.102:6380> cluster replicate ‘9502e31e8733cb37085d70602b7fcfb6ff8acb27’       <——–172.18.64.104:6379的节点ID
    OK
   
    [root@redis-102 run]# redis-cli -h 172.18.64.103 -p 6380
    172.18.64.103:6380> cluster replicate ‘3ba2e16d18760f6f7bcd688b2feaf7158976fe00’         <——–172.18.64.102:6379的节点ID
    OK
   
    [root@redis-102 run]# redis-cli -h 172.18.64.104 -p 6380
    172.18.64.104:6380> cluster replicate ‘50881204feebc7f4543d479da0ff4c99ed723202’        <——–172.18.64.103:6379的节点ID
    OK
   
九:检查整个集群信息和状态
    172.18.64.104:6380> cluster nodes
    50881204feebc7f4543d479da0ff4c99ed723202 172.18.64.103:6379 master – 0 1432287767155 2 connected 5461-10922
    3ba2e16d18760f6f7bcd688b2feaf7158976fe00 172.18.64.102:6379 master – 0 1432287767655 1 connected 10923-16383
    c33bfc7db866477bf987967bd5a939c14b198a69 172.18.64.103:6380 slave 3ba2e16d18760f6f7bcd688b2feaf7158976fe00 0 1432287768155 7 connected
    e4e35a319c765509befbfc602976f822db0307b0 172.18.64.102:6380 slave 9502e31e8733cb37085d70602b7fcfb6ff8acb27 0 1432287768654 3 connected
    9502e31e8733cb37085d70602b7fcfb6ff8acb27 172.18.64.104:6379 master – 0 1432287769155 3 connected 0-5460
    3649888a8b83bc5ca5279e0323c08ac96cae09f8 172.18.64.104:6380 myself,slave 50881204feebc7f4543d479da0ff4c99ed723202 0 0 8 connected

此条目发表在Redis分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注