Linux系統5分鐘快速搭建redis集羣

  • 單個redis安裝
    # 下載redis4.0.10
    wget http://download.redis.io/releases/redis-4.0.10.tar.gz
    # 解壓(解壓後生成目錄redis-4.0.10)
    solid@test1:~/fyj$ tar zxvf redis-4.0.10.tar.gz
    
    # 使用cd命令進入到redis-4.0.10文件夾下
    # 執行make命令
    solid@test1:~/fyj/redis/redis-4.0.10$ make
    
    # 將redis安裝到/home/solid/fyj/redis文件夾下 成功後該文件夾下會生成bin目錄
    solid@test1:~/fyj/redis/redis-4.0.10$ PREFIX=/home/solid/fyj/redis make install
    
    # 安裝成功後 默認安裝文件夾/home/solid/fyj/redis下會生成redis.conf文件 如果沒有則從解壓後的redisredis-4.0.10拷貝出來
    # redis.conf文件內容
    ###
    bind 172.16.10.29
    port 7000
    daemonize yes # 設置是否後臺啓動 Redis,默認no,正常都需要以服務形式啓動 Redis,所以這裏設置爲yes。
    appendonly yes  #開啓aof持久化模式,每次寫操作請求都追加到appendonly.aof文件中
    appendfsync always  #每次有寫操作的時候都同步
    logfile /data/redis/logs/redis.log #redis服務日誌
    pidfile /var/run/redis_7000.pid  #pidfile文件對應7000,7001,7002
    ###
    
    # 啓動redis服務,根據/home/solid/fyj/redis/redis.conf的配置
    solid@test1:~/fyj/redis/bin$ ./redis-server /home/solid/fyj/redis/redis.conf 
    # 測試服務是否開啓成功
    netstat -anp | grep 7000
    
    # 測試redis是否可用
    solid@test1:~/fyj/redis/bin$ ./redis-cli -h 172.16.10.29 -p 7000
    172.16.10.29:7000> set name fyj
    OK
    172.16.10.29:7000> get name
    "fyj"
    172.16.10.29:7000> 
    
    # 關閉redis服務
    solid@test1:~/fyj/redis/bin$ ./redis-cli -h 172.16.10.29 -p 7000 shutdown
    # 測試服務是否關閉
    netstat -anp | grep 7000
    # 通過 netstat 可以看出來端口已經是TIME_WAIT狀態了

     

  • redis集羣
    #同一個服務器創建六個節點,模擬集羣 端口號爲7001-1006
    # 建立如下目錄結構:
    solid@test1:~/fyj/redis/bin$ mkdir -p /home/solid/fyj/redis/redis-cluster/{7001,7002,7003,7004,7005,7006}
    # 分別進入每個端口目錄創建配置文件:
    cd /home/solid/fyj/redis/redis-cluster/7001 && touch redis.conf
    
    # 端口7001-7006與目錄對應
    port 7001   
    #默認ip爲127.0.0.1,需要改爲其他節點機器可訪問的ip,否則創建集羣時無法訪問對應的端口,無法創建集羣
    bind 172.16.10.29 
    #redis後臺運行
    daemonize yes   
    #開啓集羣
    cluster-enabled yes  
    #集羣的配置,配置文件首次啓動自動生成 7001-7006  
    cluster-config-file /home/solid/fyj/redis/conf/nodes_7001.conf  
    #請求超時,默認15秒,可自行設置
    cluster-node-timeout 8000
    #開啓aof持久化模式,每次寫操作請求都追加到appendonly.aof文件中
    appendonly yes  
    #每次有寫操作的時候都同步
    appendfsync always  
    #redis服務日誌
    logfile /home/solid/fyj/redis/logs/redis.log
    #pidfile文件對應7001-7006
    pidfile /var/run/redis_7001.pid  
    
    #redis.conf文件內容
    ###
    port 7001
    bind 172.16.10.29
    daemonize yes
    cluster-enabled yes
    cluster-config-file /home/solid/fyj/redis/conf/nodes_7001.conf
    cluster-node-timeout 8000
    appendonly yes
    appendfsync always
    logfile /home/solid/fyj/redis/logs/redis.log
    pidfile /var/run/redis_7001.pid  
    ###
    
    # 可以在每個服務器上寫一個啓動腳本start-redis.sh:
    for((i=1;i<=6;i++)); 
    do /home/solid/fyj/redis/bin/redis-server /home/solid/fyj/redis/redis-cluster/700$i/redis.conf; 
    done
    
    #關閉服務腳本shutdown-redis.sh
    for((i=1;i<=6;i++));
    do /home/solid/fyj/redis/bin/redis-cli -c -h 172.16.10.29 -p 700$i shutdown;
    done
    
    #如果腳本執行失敗顯示Permission denied,則執行命令chmod 777 start-redis.sh賦予權限
    solid@test1:~/fyj/redis/bin$ ./start-redis.sh 
    #六個服務開啓成功
    
    #關閉六個服務
    solid@test1:~/fyj/redis/bin$ ./shutdown-redis.sh 
    
    #創建集羣
    #注意:在任意一臺上運行 不要在每臺機器上都運行,一臺就夠了 
    #Redis 官方提供了 redis-trib.rb 這個工具,就在解壓目錄的 src 目錄中 
    solid@test1:~/fyj/redis/redis-4.0.10/src$ cd /home/solid/fyj/redis/redis-4.0.10/src
    
    #--replicas  1 代表每個master有一個slave
    ./redis-trib.rb create --replicas 1 172.16.10.29:7001 172.16.10.29:7002 172.16.10.29:7003 172.16.10.29:7004 172.16.10.29:7005 172.16.10.29:7006
    
    #如果遇到一下錯誤
    rubygems: cannot load such file -- redis (LoadError)
    #解決:執行gem install redis命令
    solid@test1:~/fyj/redis/redis-4.0.10/src$ gem install redis
    
    # 如果遇到以下錯誤
    [ERR] Node 172.16.10.29:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
    # 解決:將每個節點進行flushdb
    solid@test1:~/fyj/redis/bin$ ./redis-cli -h 172.16.10.29 -p 7001
    172.16.10.29:7001> flushdb
    OK
    172.16.10.29:7001> quit
    每一個節點都執行一次
    
    # 敲完這個命令後會提示是否按照默認的推薦方式配置集羣主從,一般選yes就行了
    # 推薦了3個masters,3個從節點
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    172.16.10.29:7001
    172.16.10.29:7002
    172.16.10.29:7003
    Adding replica 172.16.10.29:7005 to 172.16.10.29:7001
    Adding replica 172.16.10.29:7006 to 172.16.10.29:7002
    Adding replica 172.16.10.29:7004 to 172.16.10.29:7003
    >>> Trying to optimize slaves allocation for anti-affinity
    [WARNING] Some slaves are in the same host as their master
    M: cee38fbe017b942fdaf4051316916fe4ee11b04f 172.16.10.29:7001
       slots:0-5460,5798 (5462 slots) master
    M: c162a6a6b507eeb669d673f83f85744b6d4c4b58 172.16.10.29:7002
       slots:5461-10922 (5462 slots) master
    M: f0e15582ec68375f521a90c455858d7bfe43f7c6 172.16.10.29:7003
       slots:5798,10923-16383 (5462 slots) master
    S: 17d2aeab147108bb94b9fd0809a74372b42d9ca0 172.16.10.29:7004
       replicates c162a6a6b507eeb669d673f83f85744b6d4c4b58
    S: 882a3f1568c7d973f013c810631b603fff94a63d 172.16.10.29:7005
       replicates f0e15582ec68375f521a90c455858d7bfe43f7c6
    S: ac13426a9c392ece56e71390c85b98d0d7e13e14 172.16.10.29:7006
       replicates cee38fbe017b942fdaf4051316916fe4ee11b04f
    Can I set the above configuration? (type 'yes' to accept): yes
    
    #如果遇到以下錯誤
    ERR Slot 5798 is already busy (Redis::CommandError)
    
    #解決:這是由於之前創建集羣沒有成功,需要將nodes.conf和dir裏面的文件全部刪除。
    find / -name "nodes-7001.conf"
    #將nodes-700*.conf文件刪除,重啓六個redis實例
    
    >>> 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.16.10.29:7001)
    M: 38ecd880fabbb6082e6d167828b827383a1bdb24 172.16.10.29:7001
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    M: ad99c1a5c1887d719edcf20d57501744e2d0558b 172.16.10.29:7002
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: ab0fa7a0c6b807024f6a1411e1ef2565f74be8cb 172.16.10.29:7006
       slots: (0 slots) slave
       replicates 519420f386391cc5fa92f560d427ac632b15f41f
    M: 519420f386391cc5fa92f560d427ac632b15f41f 172.16.10.29:7003
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 99f3233040dadc18cf7f988e35d7ba7a1bcb338f 172.16.10.29:7005
       slots: (0 slots) slave
       replicates ad99c1a5c1887d719edcf20d57501744e2d0558b
    S: fca683c7397a370f574ceaf277a0094a87264309 172.16.10.29:7004
       slots: (0 slots) slave
       replicates 38ecd880fabbb6082e6d167828b827383a1bdb24
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    #至此,集羣創建完成
    
    #參數 -C 可連接到集羣
    solid@test1:~/fyj/redis/bin$ ./redis-cli -c -p 7001 -h 172.16.10.29
    172.16.10.29:7001> 

     

  • 集羣添加一個主節點
    #我們新建兩個服務,按照之前搭建的集羣方式新增倆個節點:(一主一從 master、slave)。
    #Mater:7007 slave:7008
    #創建7007/7008文件夾。拷貝redis.conf文件到對於的7007,7008目錄下 ,再進行修改配置文件。
    #啓動7007和7008倆個服務並查看服務狀態。
    
    #添加一個主節點
    # 步驟一:使用add-node命令:綠色爲新增節點,紅色爲已知存在節點
    solid@test1:~/fyj/redis/redis-4.0.10/src$ ./redis-trib.rb add-node 172.16.10.29:7007 172.16.10.29:7001
    >>> Adding node 172.16.10.29:7007 to cluster 172.16.10.29:7001
    >>> Performing Cluster Check (using node 172.16.10.29:7001)
    M: 38ecd880fabbb6082e6d167828b827383a1bdb24 172.16.10.29:7001
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    M: ad99c1a5c1887d719edcf20d57501744e2d0558b 172.16.10.29:7002
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: ab0fa7a0c6b807024f6a1411e1ef2565f74be8cb 172.16.10.29:7006
       slots: (0 slots) slave
       replicates 519420f386391cc5fa92f560d427ac632b15f41f
    M: 519420f386391cc5fa92f560d427ac632b15f41f 172.16.10.29:7003
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 99f3233040dadc18cf7f988e35d7ba7a1bcb338f 172.16.10.29:7005
       slots: (0 slots) slave
       replicates ad99c1a5c1887d719edcf20d57501744e2d0558b
    S: fca683c7397a370f574ceaf277a0094a87264309 172.16.10.29:7004
       slots: (0 slots) slave
       replicates 38ecd880fabbb6082e6d167828b827383a1bdb24
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 172.16.10.29:7007 to make it join the cluster.
    [OK] New node added correctly.
    
    # 注意:當添加節點成功以後,新增的節點不會有任何數據,因爲他沒有分配任何slot。需要爲新節點手動分配slot。
    
    # 步驟二:reshard命令,分配slot:
    solid@test1:~/fyj/redis/redis-4.0.10/src$ ./redis-trib.rb reshard  
    172.16.10.29:7007
    
    # 提示一 是希望你需要多少個槽移動到新的節點上,可以自己設置,比如200個槽。
    How many slots do you want to move (from 1 to 16384)? 2000
    # 提示二 是你需要把這200個slot槽移動到那個節點上去(需要指定節點id,這裏是7007的節點id),並且下個提示是輸入all爲從所有主節點(7001-7006)中分別抽取相應的槽數(一共爲200個槽到指定的新節點中!,並且會打印執行分片的計劃。)
    What is the receiving node ID? 382634a4025778c040b7213453fd42a709f79e28
    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
    # 提示三 輸入yes確認開始執行分片任務
    Do you want to proceed with the proposed reshard plan (yes/no)? yes

     

  • 集羣添加一個從節點
    # 步驟一:使用add-node命令:綠色爲新增節點,紅色爲已知存在節點
    solid@test1:~/fyj/redis/redis-4.0.10/src$ ./redis-trib.rb add-node 172.16.10.29:7008 172.16.10.29:7001
    # 步驟二:首先需要登錄到7008節點的客戶端,然後使用集羣命令,執行replicate命令來指定當前節點的主節點id爲哪一個。把當前的7008(slave)節點指定到一個主節點下(這裏使用之前創建的7007主節點)。
    solid@test1:~/fyj/redis/bin$ ./redis-cli -c -h 172.16.10.29 -p 7008
    172.16.10.29:7008> cluster replicate 307a85994f80df7e4b5e63467f6bfeba5c61c805
    (7007的id)
    OK

     

  • 集羣刪除一個主節點
    # 如果主節點有從節點,將從節點轉移到其他主節點。如果主節點有slot,先將主節點裏的slot分配到其他可用節點中,然後再刪除節點才行,否則會有數據的丟失。
    # 步驟一:刪除7007(master)節點之前,我們需要先把其全部的數據(slot槽)移動到其他節點上去(目前只能把master的數據遷移到一個節點上,暫時做不了平均分配功能)。
    solid@test1:~/fyj/redis/redis-4.0.10/src$ ./redis-trib.rb reshard  
    172.16.10.29:7007
    
    How many slots do you want to move (from 1 to 16384)? 1999
    (註釋:這裏不會是正好2000個槽)
    What is the receiving node ID? 614d0def75663f2620b6402a017014b57c912dad
    (註釋:這裏是需要把數據移動到哪?7001的主節點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:307a85994f80df7e4b5e63467f6bfeba5c61c805
    (註釋:這裏是需要數據源,也就是我們的7007節點id)
    Source node #2:done
    (註釋:這裏直接輸入done 開始生成遷移計劃)
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    
    (註釋:這裏輸入yes開始遷移)
    # 步驟二:最後我們直接使用del-node命令刪除7007主節點即可(藍色表示7007的節點id)。
    solid@test1:~/fyj/redis/redis-4.0.10/src$ ./redis-trib.rb del-node 172.16.10.29:7007 307a85994f80df7e4b5e63467f6bfeba5c61c805
    >>> Removing node 307a85994f80df7e4b5e63467f6bfeba5c61c805 from cluster 172.16.10.29:7007
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.

     

  • 集羣刪除一個從節點
    # 步驟一:刪除從節點7008,輸入del-node命令,指定刪除節點ip和端口,以及節點id(藍色爲7008節點id),移除了7008 slave節點,前節點的服務進程也會隨之銷燬。
    solid@test1:~/fyj/redis/redis-4.0.10/src$ ./redis-trib.rb del-node 172.16.10.29:7008 a78c8a41f6430b51a7eca1fdb50092c463a8f1ac

     

  • 列出集羣節點
    solid@test1:~/fyj/redis/bin$ ./redis-cli -c -h 172.16.10.29 -p 7008
    172.16.10.29:7008> cluster nodes

     

  • redis.conf詳細配置          https://github.com/linli8/cnblogs/blob/master/redis副本.conf

  • 記得動動小手點個贊哦<大笑臉>

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