redis cluster安裝以及操作
阿新 • • 發佈:2018-12-25
redis-8000.conf
port 8000
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 15000
daemonize yes
appendonly yes
dir "/usr/local/redis/redis-data/"
appendfilename "appendonly-8000.aof"
logfile "8000.log"
pidfile /var/run/redis-8000.pid
dbfilename "dump-8000.rdb"
bind 192.168.138.128
protected-mode no
配置cluster配置檔案 8000 8001 8002為主機 8003 8004 8005為從機
sed 's/8000/8001/g' redis-8000.conf > redis-8001.conf;
sed 's/8000/8002/g' redis-8000.conf > redis-8002.conf;
sed 's/8000/8003/g' redis-8000.conf > redis-8003.conf;
sed 's/8000/8004/g' redis-8000.conf > redis-8004.conf;
sed 's/8000/8005/g' redis-8000.conf > redis-8005.conf
啟動伺服器
redis-server /etc/config/cluster/redis-8000.conf;
redis-server /etc/config/cluster/redis-8001. conf;
redis-server /etc/config/cluster/redis-8002.conf;
redis-server /etc/config/cluster/redis-8003.conf;
redis-server /etc/config/cluster/redis-8004.conf;
redis-server /etc/config/cluster/redis-8005.conf;
8000分別與其他埠建立關係
redis-cli -c -p 8000 cluster meet 192.168.138.128 8001;
redis-cli -c -p 8000 cluster meet 192.168.138.128 8002;
redis-cli -c -p 8000 cluster meet 192.168.138.128 8003;
redis-cli -c -p 8000 cluster meet 192.168.138.128 8004;
redis-cli -c -p 8000 cluster meet 192.168.138.128 8005;
0-16383slot的分佈在8000 8001 8002主機的不同範圍
start=$1
end=$2
port=$3
for slot in `seq ${start} ${end}`
do
echo "slot:${slot}"
redis-cli -c -p ${port} cluster addslots ${slot}
done
sh addSolts.sh 0 5460 8000
sh addSolts.sh 5461 10922 8001
sh addSolts.sh 10923 16383 8002
檢視cluster狀態資訊
[[email protected] scripts]# redis-cli -c -h 192.168.138.128 -p 8000
192.168.138.128:8000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_sent:1057
cluster_stats_messages_received:982
檢視slots的資訊
192.168.138.128:8000> cluster slots
1) 1) (integer) 5461
2) (integer) 10922
3) 1) "192.168.138.128"
2) (integer) 8001
3) "41ff2c811fa237c423934f4fb60566530d65bcd1"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "192.168.138.128"
2) (integer) 8002
3) "2e43cbd8495bba9bf0d0d5bba6668391fc0c053b"
3) 1) (integer) 0
2) (integer) 5460
3) 1) "192.168.138.128"
2) (integer) 8000
3) "30c231a603cf446ab3e1394b12220999933f8c1c"
使8003 8004 8005成為8000 8001 8002的從
redis-cli -c -p 8003 cluster replicate `redis-cli -c -p 8003 cluster nodes | grep 8000 | awk '{print $1}'`;
redis-cli -c -p 8004 cluster replicate `redis-cli -c -p 8003 cluster nodes | grep 8001 | awk '{print $1}'`;
redis-cli -c -p 8005 cluster replicate `redis-cli -c -p 8003 cluster nodes | grep 8002 | awk '{print $1}'`;