安裝Redis和Redis Cluster

安裝Redis

1、下載Redis,由於現在最新的Redis版本已經到了3.0.4,所以測試使用的版本就是3.0.4.
    下載地址:http://redis.io/

2、下載的文件名爲:redis-3.0.3.tar.gz,使用下面命令安裝redis:

    ytc:local ytc$ cd /usr/local/
    ytc:local ytc$ mv ~/Downloads/redis-3.0.3.tar.gz .
    ytc:local ytc$ tar xzf redis-3.0.3.tar.gz
    ytc:local ytc$ cd redis-3.0.3
    ytc:redis-3.0.3 ytc$ make && make install
    ytc:local ytc$ cd /usr/local/
    ytc:local ytc$ mv redis-3.0.3 redis

    修改/etc/bashrc,在文件末尾增加一行:
        PATH=$PATH:/usr/local/redis/src
    
    如上Redis安裝完成。


安裝Redis Cluster

    安裝Redis完成後,嘗試搭建Redis Cluster:
    
    ytc:local ytc$ cd /opt/
    ytc:opt ytc$ mkdir redisDir
    ytc:opt ytc$ cd redisDir
    ytc:redisDir ytc$ mkdir 9001
    ytc:redisDir ytc$ mkdir 9002
    ytc:redisDir ytc$ mkdir 9003
    ytc:redisDir ytc$ mkdir 9004


在900*各個目錄下新建文件config.conf,文件內容如下:

    daemonize yes
    port 9001
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    appendonly yes

    第二行的端口號數字與目錄名一致。


    ytc:redisDir ytc$ cd 9001
    ytc:9001 ytc$ redis-server config.conf
    ytc:9001 ytc$ cd ../9002
    ytc:9002 ytc$ redis-server config.conf
    ytc:9002 ytc$ cd ../9003
    ytc:9003 ytc$ redis-server config.conf
    ytc:9003 ytc$ cd ../9004
    ytc:9004 ytc$ redis-server config.conf 


    使用“ps -fax”命令查看進程,可以看到如下4個進程,它們就是剛剛啓動的4個Redis實例:
    
    501 16287     1   0  3:46PM ??         0:00.05 redis-server *:9001 [cluster]
    501 16289     1   0  3:46PM ??         0:00.04 redis-server *:9002 [cluster]
    501 16291     1   0  3:47PM ??         0:00.04 redis-server *:9003 [cluster]
    501 16293     1   0  3:47PM ??         0:00.04 redis-server *:9004 [cluster]



    
報上面錯誤說明Ruby未安裝Redis部分,使用gem安裝redis部分:
    
    ytc:9004 ytc$ gem install redis
    ERROR:  While executing gem ... (Gem::Exception)
    Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
    
gem源找不到Redis,修改源:
    
    ytc:9004 ytc$ gem sources
    *** CURRENT SOURCES ***

    https://rubygems.org/
    ytc:9004 ytc$ gem sources --remove https://rubygems.org/
    https://rubygems.org/ removed from sources
    ytc:9004 ytc$ gem sources -a http://ruby.taobao.org/
    http://ruby.taobao.org/ added to sources
    ytc:9004 ytc$ gem install redis
    Fetching: redis-3.2.1.gem (100%)
    Successfully installed redis-3.2.1
    Parsing documentation for redis-3.2.1
    Installing ri documentation for redis-3.2.1
    Done installing documentation for redis after 1 seconds
    1 gem installed
    ytc:9004 ytc$
    
然後重新構建Redis集羣:
    
    ytc:9004 ytc$ redis-trib.rb create --replicas 0 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003
    >>> Creating cluster
    Connecting to node 127.0.0.1:9001: OK
    Connecting to node 127.0.0.1:9002: OK
    Connecting to node 127.0.0.1:9003: OK
    >>> Performing hash slots allocation on 3 nodes...
    Using 3 masters:
    127.0.0.1:9001
    127.0.0.1:9002
    127.0.0.1:9003
    M: 6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001
       slots:0-5460 (5461 slots) master
    M: 61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002
       slots:5461-10922 (5462 slots) master
    M: 60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003
       slots:10923-16383 (5461 slots) master
    Can I set the above configuration? (type 'yes' to accept): 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:9001)
    M: 6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001
       slots:0-5460 (5461 slots) master
    M: 61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002
       slots:5461-10922 (5462 slots) master
    M: 60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003
       slots:10923-16383 (5461 slots) master
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    ytc:9004 ytc$
    
構建成功,登錄集羣時需要使用“-c”參數:
    
    ytc:9004 ytc$ redis-cli -c -p 9001
    127.0.0.1:9001> 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_sent:180
    cluster_stats_messages_received:180
    127.0.0.1:9001>
    127.0.0.1:9001>
    127.0.0.1:9001> cluster nodes
    60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003 master - 0 1441958332495 3 connected 10923-16383
    6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001 myself,master - 0 0 1 connected 0-5460
    61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002 master - 0 1441958331990 2 connected 5461-10922
    127.0.0.1:9001>
    
可以看出集羣中有3個node,每個node覆蓋了大概1/3的slot,下面我們增加一個node,並且重新將所有的slots平均分佈到4個node上。
   

增加新node:
    127.0.0.1:9001> cluster meet 127.0.0.1 9004
    OK
    127.0.0.1:9001>
    
爲新node分配節點:
    
    ytc:9004 ytc$ redis-trib.rb -reshard 127.0.0.1:9004
    Unknown redis-trib subcommand '-reshard'
    ytc:9004 ytc$ redis-trib.rb --reshard 127.0.0.1:9004
    Unknown redis-trib subcommand '--reshard'
    ytc:9004 ytc$ redis-trib.rb reshard 127.0.0.1:9004
    Connecting to node 127.0.0.1:9004: OK
    Connecting to node 127.0.0.1:9002: OK
    Connecting to node 127.0.0.1:9003: OK
    Connecting to node 127.0.0.1:9001: OK
    >>> Performing Cluster Check (using node 127.0.0.1:9004)
    M: 7b8fee600f898662bc3f065747f590c986c10a0f 127.0.0.1:9004
       slots: (0 slots) master
       0 additional replica(s)
    M: 61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002
       slots:5461-10922 (5462 slots) master
       0 additional replica(s)
    M: 60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003
       slots:10923-16383 (5461 slots) master
       0 additional replica(s)
    M: 6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001
       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.
    How many slots do you want to move (from 1 to 16384)? 4000
    What is the receiving node ID? 7b8fee600f898662bc3f065747f590c986c10a0f
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    Source node #1:61dfcda57e67a89e31af0d49bf81c8cb1e09beb7
    Source node #2:60c3b50d8464ca40acef55aa33293044a608baa0
    Source node #3:6df75f479092037a21ec556431e7251ad89fdf88
    Source node #4:done
        Moving slot 1329 from 6df75f479092037a21ec556431e7251ad89fdf88
        Moving slot 1330 from 6df75f479092037a21ec556431e7251ad89fdf88
        …………
        Moving slot 1331 from 6df75f479092037a21ec556431e7251ad89fdf88
        Moving slot 1332 from 6df75f479092037a21ec556431e7251ad89fdf88
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    Moving slot 1330 from 127.0.0.1:9001 to 127.0.0.1:9004:
    Moving slot 1331 from 127.0.0.1:9001 to 127.0.0.1:9004:
    Moving slot 1332 from 127.0.0.1:9001 to 127.0.0.1:9004:
    ytc:9004 ytc$
    
上面是指從9001、9002、9003中取出4000個slots放到9004上。

執行完成後,4個node的slot分佈情況爲:
    
    ytc:9004 ytc$ redis-cli -c -p 9001
    127.0.0.1:9001> cluster nodes
    60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003 master - 0 1441959130090 3 connected 12256-16383
    6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001 myself,master - 0 0 1 connected 1333-5460
    61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002 master - 0 1441959132106 2 connected 6795-10922
    7b8fee600f898662bc3f065747f590c986c10a0f 127.0.0.1:9004 master - 0 1441959131098 4 connected 0-1332 5461-6794 10923-12255
    127.0.0.1:9001>
   

發佈了43 篇原創文章 · 獲贊 22 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章