Redis-集群
Posted 暗里着迷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis-集群相关的知识,希望对你有一定的参考价值。
搭建redis集群有两种方式,原理一样,一个是通过redis提供的脚本文件来启动默认的集群实例,另一个是手动配置。
一.默认脚本启动
在redis的安装的utils/create-cluster目录下,有个create-cluster执行文件,查看文件我们只需要改两个地方:
#!/bin/bash # Settings PORT=30000 TIMEOUT=2000 NODES=6 REPLICAS=1 # You may want to put the above config parameters into config.sh in order to # override the defaults without modifying this script. if [ -a config.sh ] then source "config.sh" fi # Computed vars ENDPORT=$((PORT+NODES)) if [ "$1" == "start" ] then while [ $((PORT < ENDPORT)) != "0" ]; do PORT=$((PORT+1)) echo "Starting $PORT" ../../src/redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes done exit 0 fi if [ "$1" == "create" ] then HOSTS="" while [ $((PORT < ENDPORT)) != "0" ]; do PORT=$((PORT+1)) HOSTS="$HOSTS 127.0.0.1:$PORT" done ../../src/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS exit 0 fi if [ "$1" == "stop" ] then while [ $((PORT < ENDPORT)) != "0" ]; do PORT=$((PORT+1)) echo "Stopping $PORT" ../../src/redis-cli -p $PORT shutdown nosave done exit 0 fi if [ "$1" == "watch" ] then PORT=$((PORT+1)) while [ 1 ]; do clear date ../../src/redis-cli -p $PORT cluster nodes | head -30 sleep 1 done exit 0 fi if [ "$1" == "tail" ] then INSTANCE=$2 PORT=$((PORT+INSTANCE)) tail -f ${PORT}.log exit 0 fi if [ "$1" == "call" ] then while [ $((PORT < ENDPORT)) != "0" ]; do PORT=$((PORT+1)) ../../src/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9 done exit 0 fi if [ "$1" == "clean" ] then rm -rf *.log rm -rf appendonly*.aof rm -rf dump*.rdb rm -rf nodes*.conf exit 0 fi if [ "$1" == "clean-logs" ] then rm -rf *.log exit 0 fi echo "Usage: $0 [start|create|stop|watch|tail|clean]" echo "start -- Launch Redis Cluster instances." echo "create -- Create a cluster using redis-cli --cluster create." echo "stop -- Stop Redis Cluster instances." echo "watch -- Show CLUSTER NODES output (first 30 lines) of first node." echo "tail <id> -- Run tail -f of instance at base port + ID." echo "clean -- Remove all instances data, logs, configs." echo "clean-logs -- Remove just instances logs."
NODES=6 6表示启动6个redis实例 REPLICAS=1 1表示副本的数量,6个实例,那么就是3主3从
1.启动实例,
默认将启动6个实例,端口从30001~30006
./create-cluster start
2.分配槽,redis默认有16384个槽,执行此命令,然后输入yes,3个主节点将均分这些槽
./create-cluster create
3.清除集群,执行后,当前路径下默认生成的一些配置文件就都被删除了
./create-cluster stop
./create-cluster clean
二.手动执行命令创建集群(推荐)
1. 首先我们要准备6个新的redis实例,并修改好对应配置文件中的内容:
cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 2000 appendonly yes appendfilename appendonly-6379.aof dbfilename dump-6379.rdb logfile 6379.log daemonize yes 端口号自己的定义,aof要不要开根据自己业务来,后台运行。
2. 启动这些实例:
redis-server 30001.conf
redis-server 30002.conf
...
3. redis-cli 客户端提供了创建集群的命令,通过如下命令查看:
redis-cli --cluster help
Cluster Manager Commands: create host1:port1 ... hostN:portN --cluster-replicas <arg> check host:port --cluster-search-multiple-owners info host:port fix host:port --cluster-search-multiple-owners reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> --cluster-replace rebalance host:port --cluster-weight <node1=w1...nodeN=wN> --cluster-use-empty-masters --cluster-timeout <arg> --cluster-simulate --cluster-pipeline <arg> --cluster-threshold <arg> --cluster-replace add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id <arg> del-node host:port node_id call host:port command arg arg .. arg set-timeout host:port milliseconds import host:port --cluster-from <arg> --cluster-copy --cluster-replace help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
4.加入集群,3主3从:
redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1
三.介绍下常用的集群操作命令:
1. reshard 重新分片。redis集群默认将16384个槽均分了,如果想将某一个主节点下面的N个槽的数据移至另一个主节点下(新增主节点或已有主节点重新均衡槽数):
redis-cli --cluster reshard 127.0.0.1:30001
127.0.0.1:30001 没有固定限制,只要是redis集群中的某个主节点的ip+port即可,执行完会提示:
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 58eb27ff6459a3375e9eaab29bc1438edc622c51 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 963652d83d92be777539c777cbde0475e98c53bd 127.0.0.1:30005
slots: (0 slots) slave
replicates a45cc489e63539924101ff4c504203f2ef2d5683
M: a45cc489e63539924101ff4c504203f2ef2d5683 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 727ef5c4243f9b47e7c7f6bdedbe5f1d850631a9 127.0.0.1:30006
slots: (0 slots) slave
replicates 58eb27ff6459a3375e9eaab29bc1438edc622c51
M: 788b2f2855d6b760f53d9894c4c9ba5162345a1f 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f0414dc3845188e6c977615fda959e59d14103ef 127.0.0.1:30004
slots: (0 slots) slave
replicates 788b2f2855d6b760f53d9894c4c9ba5162345a1f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2. How many slots do you want to move (from 1 to 16384)?
提示输入你想移动的槽的个数,
比如输入:2000
3. What is the receiving node ID?
提示输入你想用哪个主节点来接收这2000个槽位
比如输入:58eb27ff6459a3375e9eaab29bc1438edc622c51
4. Source node #1:
提示输入2000个槽位由哪个节点提供,想从多个主节点拆槽位,就输入一个后回车继续输入下一个,输入完成后,输入 done 回车
5. Do you want to proceed with the proposed reshard plan (yes/no)?
提示你是否要执行这个分片计划,输入:yes
6. 再通过redis-cli --cluster check 127.0.0.1:30001 命令查看下重分片后的槽位信息:
M: 58eb27ff6459a3375e9eaab29bc1438edc622c51 127.0.0.1:30001 slots:[0-5460],[10923-12922] (7461 slots) master 1 additional replica(s) S: 963652d83d92be777539c777cbde0475e98c53bd 127.0.0.1:30005 slots: (0 slots) slave replicates a45cc489e63539924101ff4c504203f2ef2d5683 M: a45cc489e63539924101ff4c504203f2ef2d5683 127.0.0.1:30003 slots:[12923-16383] (3461 slots) master 1 additional replica(s) S: 727ef5c4243f9b47e7c7f6bdedbe5f1d850631a9 127.0.0.1:30006 slots: (0 slots) slave replicates 58eb27ff6459a3375e9eaab29bc1438edc622c51 M: 788b2f2855d6b760f53d9894c4c9ba5162345a1f 127.0.0.1:30002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: f0414dc3845188e6c977615fda959e59d14103ef 127.0.0.1:30004 slots: (0 slots) slave replicates 788b2f2855d6b760f53d9894c4c9ba5162345a1f 可以看出,节点58eb27ff6459a3375e9eaab29bc1438edc622c51多出了槽位[10923-12922],a45cc489e63539924101ff4c504203f2ef2d5683 少了2000个槽位
7. add-node,添加的节点可以试从节点,也可以试从节点,具体看添加时后面接的参数,根据help命令,如下:
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
(1)首先我们要准备一个新的redis实例,并修改好配置文件中的内容,如:6379.conf,启动后,将此实例添加到redis集群中,下面试添加一个主节点:
redis-cli --cluster add-node 127.0.0.1:6379 127.0.0.1:30001 --cluster-master-id 58eb27ff6459a3375e9eaab29bc1438edc622c51
(2)如果想在某个主节点下挂在从节点:
redis-cli --cluster add-node 127.0.0.1:6380 127.0.0.1:6379 --cluster-slave
8. del-node,比较简单:
redis-cli --cluster del-node 127.0.0.1:6380 node_id
集群操作的其他命令,可以自己尝试通过help命令,去实操一把。
以上是关于Redis-集群的主要内容,如果未能解决你的问题,请参考以下文章