Redis集群搭建-原生命令
Redis Cluster 原生命令搭建
1.节点配置文件
vim redis-cluster-7000.conf
port 7000
daemonize yes
dir "/usr/local/redis-4.0.11/db"
logfile /usr/local/redis-4.0.11/log/log-redis-cluster-7000.log
dbfilename dump-cluster-node-7000.rdb
#指定本地数据库路径
dir /usr/local/redis-4.0.11/db/
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
以端口号作为区分在本地配置6个节点
可使用sed 全局替换端口号即可
sed 's/7000/7001' redis-cluster-7000.conf > redis-cluster-7001.conf
2.开启节点
redis-server redis-7000.conf
redis-server redis-7001.conf
redis-server redis-7002.conf
redis-server redis-7003.conf
redis-server redis-7004.conf
redis-server redis-7005.conf
3.meet (让节点之间组成全连通)
client-port> cluster meet ip port
例如:
redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7001(即让7000节点和7001组成无向强连通图)
再加入其他4个节点
redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7002
redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7003
redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7004
redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7005
4.分配槽
cluster addslots slot [slot.... ]
例如:给7000节点分配 0 号槽
redis-cli -h 127.0.0.1 -p 7000 cluster addslots 0
为了更加方便的分配槽写一个shell -- vim addslots.sh
start=$1
end=$2
port=$3
for slot in `seq ${start} ${end}`
do
echo "slot: ${slot}"
redis-cli -p ${port} cluster addslots ${slot}
done
执行 addslots.sh 即可给7000节点添加101个槽
sh addslots.sh 0 100 7000
依此给7001 7002 添加槽
sh addslots.sh 5462 10922
sh addslots.sh 10923 16383
5.设置主从
client > cluster replicate node-id
其中node-id通过 client> cluster nodes 查看
例如127.0.0.1:7000 : cluster nodes
2dd8bd28414c1ea1a8579f419fdbc9efb9320cd3 127.0.0.1:7000@17000 myself,master - 0 1590715527000 1 connected 0-5461
4d66669a30b37b848b74427495ba3b2a54245c37 127.0.0.1:7005@17005 slave 71215b7b9c9ccb133cd10c4349b41dc228746df9 0 1590715529000 5 connected
71215b7b9c9ccb133cd10c4349b41dc228746df9 127.0.0.1:7002@17002 master - 0 1590715530614 4 connected 10923-16383
d82ac478758b7ed8b6feb14c37c78372a6308929 127.0.0.1:7004@17004 slave 67f63146d974477594c1cab97ead0e29d2ccac22 0 1590715527581 2 connected
630edf075aa5ba191e5d48a97b72e412562ab013 127.0.0.1:7003@17003 slave 2dd8bd28414c1ea1a8579f419fdbc9efb9320cd3 0 1590715529602 3 connected
67f63146d974477594c1cab97ead0e29d2ccac22 127.0.0.1:7001@17001 master - 0 1590715529000 2 connected 5462-10922
一行中第一个为node-id
设置7003为7000节点的slave、7004为7001节点的slave、7005为7002节点的slave
redis-cli -h 127.0.0.1 -p 7003 cluster replicate ${node-id-7000}
redis-cli -h 127.0.0.1 -p 7004 cluster replicate ${node-id-7001}
redis-cli -h 127.0.0.1 -p 7005 cluster replicate ${node-id-7002}
Client > cluster info 可以查看当前的集群信息
在7000节点中set hello world
Client-7000> set hello world
可在其他节点查看