http://blog.csdn.net/imxiangzi/article/details/52431729
http://www.2cto.com/kf/201701/586689.html
meet Not all 16384 slots are covered by nodes.
http://www.cnblogs.com/SailorXiao/p/5808872.html
Redis Cluster 理論知識
集群配置 https://blog.csdn.net/u012572955/article/details/53996107
全面剖析Redis Cluster原理和應用
兩台服務器 192.168.67.129 192.168.67.131 port 6379 6380 6381 一共6個節點(集群最少要三個節點即三個主節點,主節點掛了從節點會接管,如果沒有從節點那整個集群就fail了)
192.168.67.129:6379
192.168.67.129:6380
192.168.67.129:6381
192.168.67.131:6379
192.168.67.131:6380
192.168.67.131:6381
這樣就會3主3從 3個主分配3份 slot 一共16384 slots被平均分三份 每個主分開負責一個區域 ,所以三個slots區域任何一個出問題集群都不能用了
slots:0-5460 (5461 slots) master
slots:10923-16383 (5461 slots) master
slots:5461-10922 (5462 slots) master
yum -y install gcc
cd /usr/local
wget http://download.redis.io/releases/redis-3.2.2.tar.gz
tar zxf redis-3.2.2.tar.gz
cd redis-3.2.2
make
make install
cd utils
./install_server.sh 接着下面一路回車
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
修改 /etc/redis/6379.conf
daemonize yes 這樣就可以默認啟動就后台運行
[root@localhost redis]# ls
6379.conf 6380.conf 6381.conf 修改6380和6381相應內容 6379部分全部改成6380和6381就行了,參考如下
daemonize yes //redis后台運行 pidfile /var/run/redis_6380.pid //pidfile文件 port 6380 //端口 cluster-enabled yes //開啟集群 把注釋#去掉 cluster-config-file nodes_6380.conf //集群的配置 配置文件首次啟動自動生成 cluster-node-timeout 5000 //請求超時 設置5秒夠了 appendonly yes
# /usr/local/bin/redis-server /etc/redis/63xx 啟動redis 6379 6380 6381
# ps -ef |grep redis
root 1348 1 0 15:59 ? 00:00:12 /usr/local/bin/redis-server 192.168.67.129:6379 [cluster]
root 1743 1 0 16:01 ? 00:00:10 /usr/local/bin/redis-server 192.168.67.129:6381 [cluster]
root 1747 1 0 16:02 ? 00:00:10 /usr/local/bin/redis-server 192.168.67.129:6380 [cluster]
關閉redis
# pkill redis-server #正常停止Redis服務(一個服務器多個端口的幾個redis-server全部都會被kill),可以通過pkill命令停止所有Redis服務或者使用kill -15 redis-pid停止某一個Redis服務。
用kill停止之后如果無法重新啟動redis,那就cp一個新的配置文件然后再重啟試試,最好別用kill
監控狀態
# redis-cli -p 6379 monitor
OK
以上操作兩台服務器都做了 ,接下來配置集群
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis //等好久而且沒有任何安裝信息,久到你以為安裝失敗了,放着不管就可以了,十分鍾后再說
# sudo gem install redis
Successfully installed redis-3.3.3
1 gem installed
Installing ri documentation for redis-3.3.3...
Installing RDoc documentation for redis-3.3.3...
運行 # redis-trib.rb 沒報錯就行了
[創建集群]# redis-trib.rb create --replicas 1 192.168.67.129:6379 192.168.67.129:6380 192.168.67.129:6381 192.168.67.131:6379 192.168.67.131:6380 192.168.67.131:6381
--replicas 1
表示我們希望為集群中的每個主節點創建一個從節點。
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.67.131:6379
192.168.67.129:6379
192.168.67.131:6380
Adding replica 192.168.67.129:6380 to 192.168.67.131:6379
Adding replica 192.168.67.131:6381 to 192.168.67.129:6379
Adding replica 192.168.67.129:6381 to 192.168.67.131:6380
M: c241982270411fc34cca6fb248c802f92a2bade2 192.168.67.129:6379
slots:5461-10922 (5462 slots) master
S: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
replicates 6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc
S: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
replicates f4a2fb289b66107a046cc308df83ee0ddd59222e
M: 6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc 192.168.67.131:6379
slots:0-5460 (5461 slots) master
M: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots:10923-16383 (5461 slots) master
S: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
replicates c241982270411fc34cca6fb248c802f92a2bade2
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 192.168.67.129:6379)
M: c241982270411fc34cca6fb248c802f92a2bade2 192.168.67.129:6379
slots:5461-10922 (5462 slots) master
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots: (0 slots) master
replicates 6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc
M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots: (0 slots) master
replicates f4a2fb289b66107a046cc308df83ee0ddd59222e
M: 6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc 192.168.67.131:6379
slots:0-5460 (5461 slots) master
M: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots:10923-16383 (5461 slots) master
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots: (0 slots) master
replicates c241982270411fc34cca6fb248c802f92a2bade2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# redis-trib.rb check 192.168.67.129:6379 #查看集群狀態 理論上三個port任意查一個結果都是一樣的
>>> Performing Cluster Check (using node 192.168.67.129:6379)
S: c241982270411fc34cca6fb248c802f92a2bade2 192.168.67.129:6379
slots: (0 slots) slave
replicates 129accd00021097ac0c0379b373c1b19de12b75c
S: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots: (0 slots) slave
replicates f4a2fb289b66107a046cc308df83ee0ddd59222e
M: 6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc 192.168.67.131:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots: (0 slots) slave
replicates 6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:5461-10922 (5462 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.
還以可以這么查
[root@localhost ~]# redis-cli -c -p 6381 cluster nodes
c241982270411fc34cca6fb248c802f92a2bade2 :0 slave,fail,noaddr 129accd00021097ac0c0379b373c1b19de12b75c 1503144886378 1503144886378 7 disconnected
429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380 master - 0 1503151926142 8 connected 0-5460
f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380 slave 341f7501a60b510b94159ef6667776e17ce3893d 0 1503151928162 10 connected
6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc :0 master,fail,noaddr - 1503144886373 1503144886373 4 disconnected
129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381 myself,master - 0 0 7 connected 5461-10922
341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381 master - 0 1503151927654 10 connected 10923-16383
6499aa3e2dc11fba6cb88881132c80f17b2e3497 192.168.67.129:6379 slave 429e81bda0d58db69344eb080d6b8d2ed5c3beed 0 1503151927149 8 connected
# redis-cli -c -p 6380 #進入任何一台的端口 # redis-cli -c -h 192.168.67.131 -p 6381
127.0.0.1:6380> set hw "Hello World"
-> Redirected to slot [3315] located at 192.168.67.131:6379
OK
192.168.67.131:6379> get hw
"Hello World"
# redis-cli -c -p 6381
127.0.0.1:6381> get hw
-> Redirected to slot [3315] located at 192.168.67.131:6379
"Hello World"
pkill后遇到問題
>>> Creating cluster
[ERR] Node 192.168.67.131:6380 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
需要刪除 redis.conf文件所在文件件下的aof、rdb、nodes.conf等文件 rm dump.rdb nodes-6379.conf nodes-6380.conf nodes-6381.conf
清空緩存
127.0.0.1:6381> flushdb
OK
如果還報錯試試pkill之后重啟再啟動集群
重新創建集群的時候遇到問題
[root@localhost ~]# redis-trib.rb create --replicas 1 192.168.67.129:6379 192.168.67.129:6380 192.168.67.129:6381 192.168.67.131:6379 192.168.67.131:6380 192.168.67.131:6381
>>> Creating cluster #只要redis-trib.rb create一次就夠了
[ERR] Node 192.168.67.129:6380 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
[root@localhost ~]# redis-trib.rb check 127.0.0.1:6379 #查6379發現這個節點單獨成了一個master
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: fed9d4de4467764899959f6a779fbdc1fc5f657d 127.0.0.1:6379
slots: (0 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
查其他節點發現 6個節點有三個是主1個從,剩下兩個從說明有問題 都是6379出的問題
[root@localhost ~]# redis-trib.rb check 192.168.67.131:6381
>>> Performing Cluster Check (using node 192.168.67.131:6381)
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots:0-5460 (5461 slots) master
0 additional replica(s)
S: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots: (0 slots) slave
replicates 341f7501a60b510b94159ef6667776e17ce3893d
M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots:10923-16383 (5461 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.
[root@localhost ~]# redis-cli -c -p 6379 cluster nodes
fed9d4de4467764899959f6a779fbdc1fc5f657d 192.168.67.131:6379 myself,master - 0 0 0 connected
[root@localhost ~]# redis-cli -c -p 6380 cluster nodes
c241982270411fc34cca6fb248c802f92a2bade2 :0 slave,fail,noaddr 129accd00021097ac0c0379b373c1b19de12b75c 1503144886358 1503144886358 7 disconnected
341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381 master - 0 1503147664318 10 connected 10923-16383
6a0da105d71714e4c2a9ef1bf8d4e421859bcbdc :0 master,fail,noaddr - 1503144886358 1503144886358 4 disconnected
129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381 master - 0 1503147663810 7 connected 5461-10922
429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380 master - 0 1503147664823 8 connected 0-5460
f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380 myself,slave 341f7501a60b510b94159ef6667776e17ce3893d 0 0 5 connected
[root@localhost ~]# redis-cli -p 6379
127.0.0.1:6379> set zzx zhan
(error) CLUSTERDOWN Hash slot not served
#連接有問題的節點發現無法正常使用 連接6379以外的端口發現set get正常 說明當前集群的兩個6379節點有問題 剩下的4個節點正常,需要把有問題的節點重新加到集群中
參考了這篇http://www.cnblogs.com/guxiong/p/6266890.html里面的meet
[root@localhost ~]# redis-cli -c -h 192.168.67.129 -p 6379 cluster meet 192.168.67.131 6380
OK
[root@localhost ~]# redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 127.0.0.1:6379
slots: (0 slots) slave
replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:5461-10922 (5462 slots) master
0 additional replica(s)
S: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots: (0 slots) slave
replicates 341f7501a60b510b94159ef6667776e17ce3893d
M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots:0-5460 (5461 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.
meet后把一個6379重新加進去了 剩下一個 redis-cli -c -h 192.168.67.131 -p 6379 cluster meet 192.168.67.131 6380 這下全部6個節點都正常了
done!!!
[root@localhost ~]# redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: fed9d4de4467764899959f6a779fbdc1fc5f657d 127.0.0.1:6379
slots: (0 slots) master
0 additional replica(s)
S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 192.168.67.129:6379
slots: (0 slots) slave
replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots: (0 slots) slave
replicates 341f7501a60b510b94159ef6667776e17ce3893d
M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:5461-10922 (5462 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.
發現多了一個 127.0.0.1:6379
[root@localhost ~]# redis-trib.rb del-node 127.0.0.1:6379 'fed9d4de4467764899959f6a779fbdc1fc5f657d'
>>> Removing node fed9d4de4467764899959f6a779fbdc1fc5f657d from cluster 127.0.0.1:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[重啟腳本]# cat restart_redis
#/bin/bash
pkill redis-server
sleep 0.1
for ((i=1;i<=5;i++));do
sum=`ps -ef |grep "redis-server"|grep -v grep|wc -l`
if [ $sum == 0 ];
then
echo "killed redis-server"
rm -rf /etc/redis/dump.rdb /etc/redis/nodes-*.conf
echo "ready to start redis"
#sleep 1
redis-server /etc/redis/6379.conf
redis-server /etc/redis/6380.conf
redis-server /etc/redis/6381.conf
echo "started redis"
ps -ef |grep "redis-server"|grep -v grep
echo "end!!"
exit 0
else
echo "$i sleep"
sleep 1
fi
done
echo "have $sum redis-server not be killed,please check the result "
exit 1
重啟檢查集群發現[WARNING] Node 127.0.0.1:6379 has slots in importing state (1988). 參考http://blog.csdn.net/imxiangzi/article/details/52431729
如發現如下這樣的錯誤:
[WARNING] Node 192.168.0.11:6380 has slots in migrating state (5461).
[WARNING] The following slots are open: 5461
可以使用redis命令取消slots遷移(5461為slot的ID):
cluster setslot 5461 stable
需要注意,須登錄到192.168.0.11:6380上執行redis的setslot子命令。
[root@localhost ~]# redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: fed9d4de4467764899959f6a779fbdc1fc5f657d 127.0.0.1:6379
slots: (0 slots) master
0 additional replica(s)
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots: (0 slots) slave
replicates 341f7501a60b510b94159ef6667776e17ce3893d
M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 192.168.67.129:6379
slots: (0 slots) slave
replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 127.0.0.1:6379 has slots in importing state (1988).
[WARNING] Node 192.168.67.131:6381 has slots in importing state (1988).
[WARNING] Node 192.168.67.129:6381 has slots in importing state (1988).
[WARNING] The following slots are open: 1988
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]# redis-cli -p 6381
127.0.0.1:6381> cluster setslot 1988 stable
OK
127.0.0.1:6381> exit
最后檢查發現有一個主應該設置成從
[root@localhost redis]# redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: cd3f4079f866560cbcf4149f6181dc19e8880b91 127.0.0.1:6379
slots: (0 slots) master
0 additional replica(s)
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots: (0 slots) slave
replicates 341f7501a60b510b94159ef6667776e17ce3893d
M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:5461-10922 (5462 slots) master
0 additional replica(s)
S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 192.168.67.129:6379
slots: (0 slots) slave
replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
注意7006是新增的節點,而7000是已存在的節點(可為master或slave)。如果需要將7006變成某master的slave節點,執行命令:
[root@localhost redis]# redis-cli -p 6379
127.0.0.1:6379> cluster replicate 129accd00021097ac0c0379b373c1b19de12b75c
OK
再檢查,集群正常(注意:不能為一個非空並且分配了slot的主節點繼續添加從節點)
[root@localhost redis]# redis-cli -p 6379
127.0.0.1:6379> set abc wsj
(error) MOVED 7638 192.168.67.131:6381
127.0.0.1:6379> get zzx
(error) MOVED 1988 192.168.67.129:6380
出現MOVED是因為代碼寫錯了 少了-c 應該是redis-cli -c -p 6381
搭建集群比較簡單,搭建好后出現的問題:
1、本來一主一從變成兩個主,就要把其中一個主變成從(登錄到相應節點 cluster replicate ID)
2、集群中少了一個節點,要把節點重新加到集群中( redis-cli -c -h 192.168.67.131 -p 6379 cluster meet 192.168.67.131 6380 )
3、檢查集群狀態發現 "[WARNING] Node 127.0.0.1:6379 has slots in importing state (1988)." (登錄相應節點 cluster setslot 1988 stable)
4、重啟服務器后發現只有6379自動啟動 檢查集群發現6380 6381報錯 因為沒啟動 運行 啟動相應port后再檢查集群就正常起來了,可能會報has slots in importing state。
6個節點增加到8個
復制redis.conf文件,然后啟動redis 這里用6382.conf,再往集群中添加節點
添加節點
redis-trib.rb add-node 192.168.67.131:6382 192.168.67.129:6379 #前面寫要添加的節點,后面一個任意集群中一個節點都行
[root@localhost redis]# redis-trib.rb add-node 192.168.67.131:6382 192.168.67.129:6379 >>> Adding node 192.168.67.131:6382 to cluster 192.168.67.129:6379 >>> Performing Cluster Check (using node 192.168.67.129:6379) S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 192.168.67.129:6379 slots: (0 slots) slave replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 2ee98237ff31b5cad0fbaff9f0faccda50f46707 192.168.67.129:6382 slots: (0 slots) slave replicates fed9d4de4467764899959f6a779fbdc1fc5f657d M: fed9d4de4467764899959f6a779fbdc1fc5f657d 192.168.67.131:6379 slots: (0 slots) master 1 additional replica(s) M: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380 slots: (0 slots) slave replicates 341f7501a60b510b94159ef6667776e17ce3893d M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381 slots:5461-10922 (5462 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. >>> Send CLUSTER MEET to node 192.168.67.131:6382 to make it join the cluster. [OK] New node added correctly.
[root@localhost redis]# redis-cli -c -h 192.168.67.129 -p 6382
192.168.67.129:6382> cluster replicate 129accd00021097ac0c0379b373c1b19de12b75c
OK
192.168.67.129:6382> exit #再設置從節點
設置失敗 提示被設置的主節點非空(不能為一個非空並且分配了slot的主節點繼續添加從節點)
[root@localhost redis]# redis-cli -c -h 192.168.67.131 -p 6379 192.168.67.131:6379> cluster replicate 429e81bda0d58db69344eb080d6b8d2ed5c3beed (error) ERR To set a master the node must be empty and without assigned slots. 192.168.67.131:6379> exit
那就要刪除主節點(429e81bda0d58db69344eb080d6b8d2ed5c3beed)的slot(就是移除slot給其他節點 有多少移動多少)
[root@localhost ~]# redis-trib.rb reshard 127.0.0.1:6382 #這個端口任意一個都行
>>> Performing Cluster Check (using node 127.0.0.1:6382)
S: 2ee98237ff31b5cad0fbaff9f0faccda50f46707 127.0.0.1:6382
slots: (0 slots) slave
replicates 129accd00021097ac0c0379b373c1b19de12b75c
M: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots:0-1987,1989-2729,5461-8191,10923-16383 (10921 slots) master
1 additional replica(s)
M: fed9d4de4467764899959f6a779fbdc1fc5f657d 192.168.67.131:6379
slots:1988 (1 slots) master #把1988給131:6380
1 additional replica(s)
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots: (0 slots) master
1 additional replica(s)
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:2730-5460,8192-10922 (5462 slots) master
1 additional replica(s)
S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 192.168.67.129:6379
slots: (0 slots) slave
replicates fed9d4de4467764899959f6a779fbdc1fc5f657d
S: ad1bae36b72d5db11a505080da5e6ae3b1c56810 192.168.67.131:6382
slots: (0 slots) slave
replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed
S: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots: (0 slots) slave
replicates f4a2fb289b66107a046cc308df83ee0ddd59222e
[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)? 1
What is the receiving node ID? f4a2fb289b66107a046cc308df83ee0ddd59222e
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:fed9d4de4467764899959f6a779fbdc1fc5f657d
Source node #2:done
Ready to move 1 slots.
Source nodes:
M: fed9d4de4467764899959f6a779fbdc1fc5f657d 192.168.67.131:6379
slots:1988 (1 slots) master
1 additional replica(s)
Destination node:
M: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots:0-1987,1989-2729,5461-8191,10923-16383 (10921 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 1988 from fed9d4de4467764899959f6a779fbdc1fc5f657d
Do you want to proceed with the proposed reshard plan (yes/no)? yes
cluster replicate 的時候報錯
自動調整rebalance
[root@localhost ~]# redis-trib.rb rebalance 192.168.67.129:6379 >>> Performing Cluster Check (using node 192.168.67.129:6379) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Rebalancing across 3 nodes. Total weight = 3 Moving 5460 slots from 192.168.67.131:6380 to 192.168.67.131:6379 ################################################ Moving 1 slots from 192.168.67.131:6381 to 192.168.67.131:6379 #
[root@localhost ~]# redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
S: 6499aa3e2dc11fba6cb88881132c80f17b2e3497 127.0.0.1:6379
slots: (0 slots) slave
replicates fed9d4de4467764899959f6a779fbdc1fc5f657d
S: 341f7501a60b510b94159ef6667776e17ce3893d 192.168.67.129:6381
slots: (0 slots) slave
replicates f4a2fb289b66107a046cc308df83ee0ddd59222e
M: fed9d4de4467764899959f6a779fbdc1fc5f657d 192.168.67.131:6379
slots:0-2730,5461-8191 (5462 slots) master
1 additional replica(s)
S: 2ee98237ff31b5cad0fbaff9f0faccda50f46707 192.168.67.129:6382
slots: (0 slots) slave
replicates 129accd00021097ac0c0379b373c1b19de12b75c
M: f4a2fb289b66107a046cc308df83ee0ddd59222e 192.168.67.131:6380
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 429e81bda0d58db69344eb080d6b8d2ed5c3beed 192.168.67.129:6380
slots: (0 slots) master
1 additional replica(s)
S: ad1bae36b72d5db11a505080da5e6ae3b1c56810 192.168.67.131:6382
slots: (0 slots) slave
replicates 429e81bda0d58db69344eb080d6b8d2ed5c3beed
M: 129accd00021097ac0c0379b373c1b19de12b75c 192.168.67.131:6381
slots:2731-5460,8192-10922 (5461 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.
rebalance 之后有一個M沒有slots,還是需要手動調整才行
要把一個主變成從(或者從變主)有兩種方法,第一種就是kill掉主 這樣方法可能會丟失數據 因為主從切換需要時間(
Redis 集群的一致性保證(Redis Cluster consistency guarantees)
Redis 集群不保證強一致性。實踐中,這意味着在特定的條件下,Redis 集群可能會丟掉一些被系統收 到的寫入請求命令。
Redis 集群為什么會丟失寫請求的第一個原因,是因為采用了異步復制。這意味着在寫期間下面的事情 發生了:
你的客戶端向主服務器 B 寫入。
主服務器 B 回復 OK 給你的客戶端。
主服務器 B 傳播寫入操作到其從服務器 B1,B2 和 B3。
)
第二種就是用CLUSTER FAILOVER 命令 這里需要登錄從然后輸入命令 就能把對應的主換成從了,這種方法不會丟數據
獲取所有 redis的key
192.168.67.131:6379> keys *
1) "lqq"
2) "zzx"
3) "0904"
redis-cli 中。 使用命令 info Keyspace 查看存儲數據的相關信息。
通過 keys * 查看所有的 key,想要釋放掉的話可以刪除Key來釋放,
刪除單個:del key
redis 2.8.9安裝
[root@tomcat1 redis-2.8.9]# tar zxf redis-2.8.9.tar.gz
[root@tomcat1 redis-2.8.9]# cd redis-2.8.9
[root@tomcat1 redis-2.8.9]# make
[root@tomcat1 redis-2.8.9]# make install
[root@tomcat1 redis-2.8.9]# cd utils/
[root@tomcat1 utils]# ./install_server.sh 路回車
[root@tomcat1 redis-2.8.9]# ./redis-server redis.conf 直接啟動redis
redis-server:Redis服務器的daemon啟動程序
redis-cli:Redis命令行操作工具。當然,你也可以用telnet根據其純文本協議來操作
redis-benchmark:Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能
redis-check-aof:更新日志檢查
配置文件內容如下
[root@redis1 ~]# grep -v "^#" redis.conf|grep -v "^$"
[root@redis1 redis-3.2.1]# grep -v "^#" redis.conf|grep -v "^$"
主
bind 192.168.1.8 127.0.0.1
daemonize yes
port 6379
從
bind 192.168.1.9 127.0.0.1
port 6389
daemonize yes
slaveof 192.168.1.8 6379