Redis集群中新增節點(master、slave)(實驗)
# vim /usr/local/redis-3.0.6-6379/redis.conf
daemonize yes //開啟後臺運行
pidfile /var/run/redis_6379.pid //pid文件
port 6379 //端口
bind 192.168.2.100 //默認127.0.0.1,需要改為其他節點可訪問的地址
logfile "/usr/local/redis-3.0.6-6379/redis_6379.log" //log文件路徑
dir /usr/local/redis-3.0.6-6379/ //RDB文件路徑
appendonly yes //開啟AOF持久化
cluster-enabled yes //開啟集群
cluster-config-file nodes-6379.conf //集群配置文件
cluster-node-timeout 15000 //請求超時,默認15秒
啟動新增master節點:
# redis-server redis.conf
安裝ruby環境:(redis-trib.rb命令,需要在ruby環境中執行)
# yum -y install ruby ruby-devel rubygems rpm-build
# gem install redis
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
1 gem installed
可能會遇到的報錯:
ERROR: Could not find a valid gem 'redis' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/latest_specs.4.8.gz)
手動下載:https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
執行:# gem install -l ./redis-3.2.1.gem
把master節點加入集群中:
# redis-trib.rb add-node 192.168.2.202:6379 192.168.2.100:6379
//第一個ip:port 新節點
第二個ip:port 集群中的任意節點
查看集群所有節點信息:(可以看到,新增節點已加入集群,但是沒有分配hash slot)
# redis-trib.rb check 192.168.2.202:6379
>>> Performing Cluster Check (using node 192.168.2.202:6379)
M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379
slots: (0 slots) master
0 additional replica(s)
... ...
給新增master節點分配slot:
# redis-trib.rb reshard 192.168.2.202:6379
>>> Performing Cluster Check (using node 192.168.2.202:6379)
M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379
slots: (0 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)? 1000
//想要移動多少個hash slot?(分配給新節點)
What is the receiving node ID? 8326ff0be199fa0d4db74f0ebcc58f27e65991b4
//接受的節點ID是什麽?(新加節點的ID)
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
//如果從所有節點移動,輸入all
Type 'done' once you entered all the source nodes IDs.
//輸入指定節點ID,done結尾
Source node #1:all
... ...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
//是否滿意hash slot移動計劃?
查看集群節點的信息:(確認新增節點hash slot分配正確)
# redis-trib.rb check 192.168.2.202:6379
>>> Performing Cluster Check (using node 192.168.2.202:6379)
M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379
slots:0-332,5461-5794,10923-11255 (1000 slots) master
0 additional replica(s)
... ...
啟動slave節點:(配置和上面一樣)
# redis-server /usr/local/redis-3.0.6-6380/redis.conf
將slave節點加入集群:
# redis-trib.rb add-node --slave 192.168.2.202:6380 192.168.2.100:6379
//加入--slave參數,表示加入的是slave
第一個IP是新增的slave
第二個IP是節點中的任意節點
查看集群信息:(可以看到新加slave屬於192.168.2.202:6379的從節點)
# redis-trib.rb check 192.168.2.100:6379
>>> Performing Cluster Check (using node 192.168.2.100:6379)
M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379
slots:0-332,5461-5794,10923-11255 (1000 slots) master
1 additional replica(s)
S: e4dc23dc67418bf66c6c63655110612cb9516aff 192.168.2.202:6380
slots: (0 slots) slave
replicates 8326ff0be199fa0d4db74f0ebcc58f27e65991b4
... ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Redis集群中新增節點(master、slave)(實驗)