Redis 3.0 集羣安裝


1. 安裝實例 :準備三臺虛擬機 : 
    192.168.118.136
    192.168.118.147 
    192.168.118.148
2. 在每個虛擬機上部署兩個redis :
    192.168.118.136:7000
    192.168.118.136:7001
    192.168.118.147:7000
    192.168.118.147:7001
    192.168.118.148:7000
    192.168.118.148:7001
3. 環境必須 
    yum -y install gcc
    yum -y install gcc-c++
    yum -y install ruby
    yum -y install rubygem*
    yum -y install ruby-rdoc
    yum -y install ruby-devel rubygems rpm-build
    gem install redis  // 安裝redis 的第三方接口 , 不安裝貌似不行的樣子
4 安裝redis 集羣
        # tar xvzf redis-3.0.0-beta1.tar.gz
        # mv redis-3.0.0-beta1 redis
    編譯 redis
        # cd ./redis/src
        # make
    編譯後 , 將生成的文件全部移動到 bin文件下面 , 方便使用
        # mkdir ../bin
        # mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server redis-trib.rb ../bin
        # cd ..
    將 bin 下文件 分別 scp 到 各臺機器上 
    並分別在各臺機器上 複製出一份
    分別修改配置文件 (此配置爲最小可運行配置)
    # vi redis.conf
        port 7000
        cluster-enabled yes 
        cluster-config-file nodes.conf
        cluster-node-timeout 5000
        appendonly yes
    分別運行6個redis
        # ./redis-server ./redis.conf
    在任意redis下輸入命令 , 運行集羣 
        # ./redis-trib.rb create --replicas 1 192.168.118.136:7000 192.168.118.136:7001 192.168.118.147:7000 192.168.118.147:7001 192.168.118.148:7000 192.168.118.148:7001

5 添加新節點
    192.168.118.147:7003
    192.168.118.148:7003
    準備好 兩個redis 
    並啓動
    # ./redis-server redis.conf

    添加新master 節點
    輸入命令redis-trib.rb add-node [需要添加節點] [原集羣節點] 
    # ./redis-trib.rb add-node 192.168.118.147:7003 192.168.118.148:7000  

    添加 192.168.118.148:7003 節點作爲副本
    # ./redis-trib.rb add-node --slave 192.168.118.148:7003 192.168.118.148:7000
    或者
    # ./redis-trib.rb add-node --slave --master-id 4841d83e2b037c49bb078fff4b00385bf4927071 192.168.118.148:7003 192.168.118.136:7000
    此處的 4841d83e2b037c49bb078fff4b00385bf4927071 爲剛剛添加的節點 192.168.118.147:7003 的 Node id 
    
    節點ID(node id) 可在 任意redis bin目錄下查看nodes.conf中有記錄 還有很多方式 下面有使用到 
    
    輸入命令 查看集羣節點
    # ./redis-cli -h 192.168.118.136 -p 7001
    192.168.118.136:7001> cluster nodes  //查看節點是否加入到集羣中
    192.168.118.136:7001> cluster nodes
        8cde04688d4b6df9df3d26fa4effc28643a2ee83 192.168.118.147:7001 slave eceba2187dbc166d7d8ec3554994df9f0267e960 0 1411059157653 4 connected
        ccf4a4a11cef9ad1cfe39e55d5ef5859b5c41d45 192.168.118.136:7001 myself,slave c632f8ff6ae350346dcf8a8db185720fe68ad8dd 0 0 2 connected
        f98fad39402031ec0b7e9452685d02c5336e88f6 192.168.118.148:7003 slave 694534c188782b1b0212c8a6d0959ecf8f9608a6 0 1411059158663 0 connected
        0f4f40267218d35628d57c2b7b47b5eef642de19 192.168.118.148:7001 master - 0 1411059156649 7 connected 0-5460
        ca512f21d80237fcb27f2ade91d4c1bebc6b5f5e 192.168.118.136:7000 slave 0f4f40267218d35628d57c2b7b47b5eef642de19 0 1411059158157 7 connected
        c632f8ff6ae350346dcf8a8db185720fe68ad8dd 192.168.118.148:7000 master - 0 1411059158158 5 connected 5461-10922
        eceba2187dbc166d7d8ec3554994df9f0267e960 192.168.118.147:7000 master - 0 1411059157653 3 connected 10923-16383
        694534c188782b1b0212c8a6d0959ecf8f9608a6 192.168.118.147:7003 master - 0 1411059157150 0 connected

    可知 新添加的節點沒有分配到 槽 
    下面開始給新節點分配 hash 槽 
    # ./redis-trib.rb reshard [任意節點地址:port]
        How many slots do you want to move (from 1 to 16384)? 4096  //由於這又4隊主從節點16384/4  [按照情況分配數量]
        .......
        What is the receiving node ID? 4841d83e2b037c49bb078fff4b00385bf4927071  //注意此處的ID 
        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: all //也可以是某個節點的ID
         ......
        Do you want to proceed with the proposed reshard plan (yes/no)? yes  //此處輸入yes 執行操作,輸入no 取消分配   
     
    分配好之後輸入命令 檢查 集羣狀態   
    # ./redis-trib.rb check [任意節點地址:port]
    [root@master addm]# ./redis-trib.rb check 192.168.118.148:7001
        Connecting to node 192.168.118.136:7000: OK
        Connecting to node 192.168.118.147:7003: OK
        Connecting to node 192.168.118.148:7001: OK
        Connecting to node 192.168.118.136:7001: OK
        Connecting to node 192.168.118.148:7000: OK
        Connecting to node 192.168.118.147:7001: OK
        Connecting to node 192.168.118.147:7000: OK
        >>> Performing Cluster Check (using node 192.168.118.136:7000)
        M: 6fb72d5d6f14c67840348c0d14767f2541ae128a 192.168.118.136:7000
           slots:12288-16383 (4096 slots) master
           1 additional replica(s)
        M: 4841d83e2b037c49bb078fff4b00385bf4927071 192.168.118.147:7003
           slots:0-1364,5461-6826,10923-12287 (4096 slots) master
           0 additional replica(s)
        S: 142ef0fd250be201a4f682163245a98d58d230c3 192.168.118.148:7001
           slots: (0 slots) slave
           replicates cde6ec4b98a8e2c5d4020a5e545a7b95c533737d
        S: efce5470e3de0a2ba08211c7fb0d1a860051ba4a 192.168.118.136:7001
           slots: (0 slots) slave
           replicates 6fb72d5d6f14c67840348c0d14767f2541ae128a
        M: d11986f2ccfad296c503684ac372ece29c587f14 192.168.118.148:7000
           slots:1365-5460 (4096 slots) master
           1 additional replica(s)
        S: 0cc4699066987d99391b0faccddf25e8be44c62c 192.168.118.147:7001
           slots: (0 slots) slave
           replicates d11986f2ccfad296c503684ac372ece29c587f14
        M: cde6ec4b98a8e2c5d4020a5e545a7b95c533737d 192.168.118.147:7000
           slots:6827-10922 (4096 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.

    結果中 
    M: 4841d83e2b037c49bb078fff4b00385bf4927071 192.168.118.147:7003
       slots:0-1364,5461-6826,10923-12287 (4096 slots) master
     
6 集羣相關命令 
    集羣 
    CLUSTER INFO 打印集羣的信息
    CLUSTER NODES 列出集羣當前已知的所有節點(node),以及這些節點的相關信息。
    節點
    CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點添加到集羣當中,讓它成爲集羣的一份子。
    CLUSTER FORGET <node_id> 從集羣中移除 node_id 指定的節點。
    CLUSTER REPLICATE <node_id> 將當前節點設置爲 node_id 指定的節點的從節點。
    CLUSTER SAVECONFIG 將節點的配置文件保存到硬盤裏面。
    槽(slot)
    CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。
    CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。
    CLUSTER FLUSHSLOTS 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。
    CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。
    CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。
    CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中導入槽 slot 到本節點。
    CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的導入(import)或者遷移(migrate)。
    鍵
    CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。
    CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。
    CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。  


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章