Redis 3.0.2集羣搭建以及相關問題彙總

Redis3 正式支持了 cluster,是爲了解決構建redis集羣時的諸多不便

(1)像操做單個redis同樣操做key,不用操心key在哪一個節點上
(2)在線動態添加、刪除redis節點,不用中止服務
(3)根據節點性能,動態分配不一樣數量的key到不一樣redis節點java

(4)每一個節點都配有slave,並自動監測和切換node

 

Redis3 cluster 中增長了一個重要概念:redis

hash slot(哈希槽),共有16384個slot
key不是直接放入redis中了,而是放入slot中,再把slot放入redis中ruby

根據keyhash值計算其所在的slot,再根據slot和redis節點的對應關係,找到此slot所在的節點,而後到這個節點對key進行操做服務器

例如現有3個redis節點,每一個節點各負責一半數量的slot,16384/3 = 5461個
如今想添加一個redis節點,能夠從 節點 1、節點 2、節點 3 中各拿出一部分的slot,移到新節點
每一個redis節點中的slot數量能夠不一致,根據節點性能適當分配
 
進入集羣須要用redis -c -p 7000    -c是enable cluster   p指定進入端口
 
搭建集羣遇到的問題:
因爲教程裏面給出的集羣的地址是127.0.0.1:7000、......,在java程序訪問的時候是沒法獲取節點信息的,因此我須要從新搭建集羣,而後停掉全部節點,從新搭建的時候,報出了以下錯誤,可知是由於有些配置文件以及數據沒有刪除。將其所有刪除乾淨便可。
[ERR] Node 172.168.63.202:7001 is not empty.
Either the nodealready knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
find / -name dump.rdb 
rm -f /usr/redis/cluster/7002/redis7002/src/dump.rdb 
rm -f /usr/redis/cluster/7004/redis7004/src/dump.rdb
rm -f /usr/redis/cluster/7005/redis7005/src/dump.rdb
rm -f /usr/redis/cluster/7001/redis7001/src/dump.rdb
rm -f /usr/redis/cluster/7000/redis7000/src/dump.rdb
rm -f /usr/redis/cluster/7003/redis7003/src/dump.rdb
rm -f /usr/redis/master/redis-3.0.7/src/dump.rdb
find / -name dump.rdb
 
find / -name appendonly.aof
rm -f /usr/redis/cluster/7002/redis7002/src/appendonly.aof
rm -f /usr/redis/cluster/7004/redis7004/src/appendonly.aof
rm -f /usr/redis/cluster/7005/redis7005/src/appendonly.aof
rm -f /usr/redis/cluster/7001/redis7001/src/appendonly.aof
rm -f /usr/redis/cluster/7000/redis7000/src/appendonly.aof
rm -f /usr/redis/cluster/7003/redis7003/src/appendonly.aof
find / -name appendonly.aof
 
find / -name nodes.conf
rm -f /usr/redis/cluster/7002/redis7002/src/nodes.conf
rm -f /usr/redis/cluster/7004/redis7004/src/nodes.conf
rm -f /usr/redis/cluster/7005/redis7005/src/nodes.conf
rm -f /usr/redis/cluster/7001/redis7001/src/nodes.conf
rm -f /usr/redis/cluster/7000/redis7000/src/nodes.conf
rm -f /usr/redis/cluster/7003/redis7003/src/nodes.conf
find / -name nodes.conf
 
#建立集羣
#!/bin/sh
cd /usr/redis/cluster/7000/redis7000/src/
./redis-server ../redis.conf
cd /usr/redis/cluster/7001/redis7001/src/
./redis-server ../redis.conf
cd /usr/redis/cluster/7002/redis7002/src/
./redis-server ../redis.conf
cd /usr/redis/cluster/7003/redis7003/src/
./redis-server ../redis.conf
cd /usr/redis/cluster/7004/redis7004/src/
./redis-server ../redis.conf
cd /usr/redis/cluster/7005/redis7005/src/
./redis-server ../redis.conf
cd /usr/redis/cluster/7000/redis7000/src/
./redis-trib.rb create --replicas 1 192.168.10.102:7000 192.168.10.102:7001 192.168.10.102:7002 192.168.10.102:7003 192.168.10.102:7004 192.168.10.102:7005
 

#集羣啓動關閉:
#!/bin/sh
/usr/redis/cluster/7000/redis7000/src/redis-cli -p 7000 shutdown
/usr/redis/cluster/7001/redis7001/src/redis-cli -p 7001 shutdown
/usr/redis/cluster/7002/redis7002/src/redis-cli -p 7002 shutdown
/usr/redis/cluster/7003/redis7003/src/redis-cli -p 7003 shutdown
/usr/redis/cluster/7004/redis7004/src/redis-cli -p 7004 shutdown
/usr/redis/cluster/7005/redis7005/src/redis-cli -p 7005 shutdown
if [ $(ps -ef | grep -c redis | grep -cv grep) -gt 1 ]
then echo "集羣關機失敗,請檢查節點"
else
 echo "集羣所有關閉,ok"
fi
 

//集羣搭建教程 http://blog.csdn.net/yubinpll9110/article/details/51373890app

 

 這裏建立6個redis節點,其中三個爲主節點,三個爲從節點。

    redis和端口對應關係:性能

    主服務器ui

    127.0.0.1:7000spa

    127.0.0.1:7001.net

    127.0.0.1:7002

    從服務器:
    127.0.0.1:7003
    127.0.0.1:7004
    127.0.0.1:7005

    步驟:
    1,下載redis。官網下載3.0.0版本,以前幾的版本不支持集羣模式

    下載地址:http://download.redis.io/releases/redis-3.0.0.tar.gz
    2:上傳服務器,解壓,編譯
    tar -zxvf redis-3.0.0-rc2.tar.gz
    mv redis-3.0.0-rc2.tar.gz redis3.0
    cd /usr/local/redis3.0
    make & make install


    3:建立集羣須要的目錄並拷貝redis
    mkdir -p /usr/local/cluster
    mkdir -p /usr/local/cluster/7000
    mkdir -p /usr/local/cluster/7001
    mkdir -p /usr/local/cluster/7002
    mkdir -p /usr/local/cluster/7003
    mkdir -p /usr/local/cluster/7004
    mkdir -p /usr/local/cluster/7005
    cp -rf /usr/local/redis3.0/* /usr/local/cluster/7000/
    cp -rf /usr/local/redis3.0/* /usr/local/cluster/7001/
    cp -rf /usr/local/redis3.0/* /usr/local/cluster/7002/
    cp -rf /usr/local/redis3.0/* /usr/local/cluster/7003/
    cp -rf /usr/local/redis3.0/* /usr/local/cluster/7004/
    cp -rf /usr/local/redis3.0/* /usr/local/cluster/7005/


    4:修改配置文件redis.conf

    vi /usr/local/cluster/7000/redis.conf

    ##修改配置文件中的下面選項
    port 7000
    daemonize yes
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    appendonly yes


    #一樣再對其它配置文件進行修改
    vi /usr/local/cluster/7001/redis.conf
    vi /usr/local/cluster/7002/redis.conf
    vi /usr/local/cluster/7003/redis.conf
    vi /usr/local/cluster/7004/redis.conf
    vi /usr/local/cluster/7005/redis.conf
    ##注意:不一樣的目錄配置不一樣的redis.conf中的port


    5:啓動6個redis

    cd /usr/local/cluster/7000/src
    redis-server ../redis.conf
    cd /usr/local/cluster/7001/src
    redis-server ../redis.conf
    cd /usr/local/cluster/7002/src
    redis-server ../redis.conf
    cd /usr/local/cluster/7003/src
    redis-server ../redis.conf
    cd /usr/local/cluster/7004/src
    redis-server ../redis.conf
    cd /usr/local/cluster/7005/src
    redis-server ../redis.conf

    ##啓動以後使用命令查看redis的啓動狀況ps -ef|grep redis


    6,建立redis集羣
    cd /usr/local/redis3.0/src
    ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

    6.1執行上面的命令的時候會報錯,由於是執行的的腳本,須要
    錯誤內容:/usr/bin/env: ruby: No such file or directory
    因此須要安裝ruby的環境,這裏推薦使用yum install ruby


    yum install ruby


    6.2而後再執行第步的建立集羣命令,還會報錯,提示缺乏rubygems組件
    錯誤內容:
    ./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
    from ./redis-trib.rb:24
    yum install rubygems


    6.3再次執行第步的命令,還會報錯,提示不能加載redis,是由於缺乏redis的接口
    錯誤內容:
    /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from ./redis-trib.rb:25


    gem install redis
    這裏可能沒法安裝,由於沒法鏈接gem服務器:
    [@zw_22_90 src]# gem install redis --version 3.0.0
    ERROR: Could not find a valid gem 'redis' (= 3.0.0) in any repository
    ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)

如何使用?
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org
# 請確保只有 ruby.taobao.org
$ gem install rails
    6.4 再次執行第步的命令,正常執行     輸入yes,而後配置完成。     [@zw_22_90 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005     >>> Creating cluster     Connecting to node 127.0.0.1:7000: OK     Connecting to node 127.0.0.1:7001: OK     Connecting to node 127.0.0.1:7002: OK     Connecting to node 127.0.0.1:7003: OK     Connecting to node 127.0.0.1:7004: OK     Connecting to node 127.0.0.1:7005: OK     >>> Performing hash slots allocation on 6 nodes...     Using 3 masters:     127.0.0.1:7000     127.0.0.1:7001     127.0.0.1:7002     Adding replica 127.0.0.1:7003 to 127.0.0.1:7000     Adding replica 127.0.0.1:7004 to 127.0.0.1:7001     Adding replica 127.0.0.1:7005 to 127.0.0.1:7002     M: 2022f24d581b4a7c3342e3245c32927cbd5ec16d 127.0.0.1:7000     slots:0-5460 (5461 slots) master     M: 37b7008f80f8c21a698da8cb1f1b32db8c0c415c 127.0.0.1:7001     slots:5461-10922 (5462 slots) master     M: ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd 127.0.0.1:7002     slots:10923-16383 (5461 slots) master     S: b5b76d70bbb0dbf3e7df8a38f1259e95e2054721 127.0.0.1:7003     replicates 2022f24d581b4a7c3342e3245c32927cbd5ec16d     S: 6881f8fef9c25da486f320ebf2ead39c1502db4c 127.0.0.1:7004     replicates 37b7008f80f8c21a698da8cb1f1b32db8c0c415c     S: f090526d32cced97731eef2a2e1722a7bac7d9ea 127.0.0.1:7005     replicates ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd     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:7000)     M: 2022f24d581b4a7c3342e3245c32927cbd5ec16d 127.0.0.1:7000     slots:0-5460 (5461 slots) master     M: 37b7008f80f8c21a698da8cb1f1b32db8c0c415c 127.0.0.1:7001     slots:5461-10922 (5462 slots) master     M: ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd 127.0.0.1:7002     slots:10923-16383 (5461 slots) master     M: b5b76d70bbb0dbf3e7df8a38f1259e95e2054721 127.0.0.1:7003     slots: (0 slots) master     replicates 2022f24d581b4a7c3342e3245c32927cbd5ec16d     M: 6881f8fef9c25da486f320ebf2ead39c1502db4c 127.0.0.1:7004     slots: (0 slots) master     replicates 37b7008f80f8c21a698da8cb1f1b32db8c0c415c     M: f090526d32cced97731eef2a2e1722a7bac7d9ea 127.0.0.1:7005     slots: (0 slots) master     replicates ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd     [OK] All nodes agree about slots configuration.     >>> Check for open slots...     >>> Check slots coverage...     [OK] All 16384 slots covered.     至此redis集羣即搭建成功!     7,redis-cli命令進入集羣環境     [@zw_22_90 src]# redis-cli -c -p 7000     127.0.0.1:7000> quit;
相關文章
相關標籤/搜索