redis集群备忘式教程

redis

redis-trib 用法:

Usage: redis-trib <command> <options> <arguments ...>

  create       host1:port1 ... hostN:portN
               --replicas <arg>
  check        host:port
  info         host:port
  fix          host:port
               --timeout <arg>
  reshard      host:port
               --from <arg>
               --to <arg>
               --slots <arg>
               --yes
               --timeout <arg>
               --pipeline <arg>
  rebalance    host:port
               --weight <arg>
               --auto-weights
               --use-empty-masters
               --timeout <arg>
               --simulate
               --pipeline <arg>
               --threshold <arg>
  add-node     new_host:new_port existing_host:existing_port
               --slave
               --master-id <arg>
  del-node     host:port node_id
 set-timeout   host:port milliseconds
 call          host:port command arg arg .. arg
  import       host:port
               --from <arg>
               --copy
               --replace
  help         (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

#初始化:
./redis-trib.rb create --replicas 1 127.0.0.1:6391 127.0.0.1:6392 127.0.0.1:6393 127.0.0.1:6394 127.0.0.1:6395 127.0.0.1:6396

#状态检查:

redis-trib.rb check 127.0.0.1:6379

#redis-cli 用法:

redis-cli 3.2.9

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>        Server hostname (default: 127.0.0.1).
  -p <port>            Server port (default: 6379).
  -s <socket>          Server socket (overrides hostname and port).
  -a <password>        Password to use when connecting to the server.
  -r <repeat>          Execute specified command N times.
  -i <interval>        When -r is used, waits <interval> seconds per command.
                       It is possible to specify sub-second times like -i 0.1.
  -n <db>              Database number.
  -x                   Read last argument from STDIN.
  -d <delimiter>       Multi-bulk delimiter in for raw formatting (default: \n).
  -c                   Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw                Use raw formatting for replies (default when STDOUT is
                       not a tty).
  --no-raw             Force formatted output even when STDOUT is not a tty.
  --csv                Output in CSV format.
  --stat               Print rolling stats about server: mem, clients, ...
  --latency            Enter a special mode continuously sampling latency.
  --latency-history    Like --latency but tracking latency changes over time.
                       Default time interval is 15 sec. Change it using -i.
  --latency-dist       Shows latency as a spectrum, requires xterm 256 colors.
                       Default time interval is 1 sec. Change it using -i.
  --lru-test <keys>    Simulate a cache workload with an 80-20 distribution.
  --slave              Simulate a slave showing commands received from the master.
  --rdb <filename>     Transfer an RDB dump from remote server to local file.
  --pipe               Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n>   In --pipe mode, abort with error if after sending all data.
                       no reply is received within <n> seconds.
                       Default timeout: 30. Use 0 to wait forever.
  --bigkeys            Sample Redis keys looking for big keys.
  --scan               List all keys using the SCAN command.
  --pattern <pat>      Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                       The test will run for the specified amount of seconds.
  --eval <file>        Send an EVAL command using the Lua script at <file>.
  --ldb                Used with --eval enable the Redis Lua debugger.
  --ldb-sync-mode      Like --ldb but uses the synchronous Lua debugger, in
                       this mode the server is blocked and script changes are
                       are not rolled back from the server memory.
  --help               Output this help and exit.
  --version            Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands
and settings.

#连接集群:
redis-cli -c -h 127.0.0.1 -p 6391

#添加节点:(假设进程已启动)
./redis-trib.rb add-node 127.0.0.1:6397 127.0.0.1:6390 #添加一个节点(默认为主节点)

./redis-trib.rb check 127.0.0.1:6390 #检查状态,为的是获取新节点的id

./redis-trib.rb add-node --slave --master-id 63ec1e7d57b3e19c6e1fa07768673001cfa0d30f 127.0.0.1:8380 127.0.0.1:6379 #为刚添加的直接点添加从节点,注意--master-id

./redis-trib.rb add-node --slave 127.0.0.1:6398 127.0.0.1:6390 #从节点的另一种添加方式,注:这种方式是随机找到从节点较少的主节点,为此节点添加从节点,所以具有随机性

#删除一个节点:
./redis-trib.rb del-node 172.16.1.1:6390 12decfd0d975ef0dcd151d0ff232da7a2269a1c2 #从节点可以直接删除,但主节点需要将slots移到其它节点,然后再删除

#重新分片:
./redis-trib.rb reshard 127.0.0.1:6390 #交互式进行,需要手动指定为新节点分配多少slots

./redis-trib.rb reshard <host>:<port> --from <node-id> --to <node-id> --slots <args> --yes #非交互式

另附:
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。
CLUSTER RESET reset
CLUSTER SAVECONFIG 强制节点保存集群当前状态到磁盘上。
CLUSTER SLOTS 获得slot在节点上的映射关系

CLUSTER MEET ip port 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
CLUSTER FORGET node_id 从集群中移除 node_id 指定的节点。
CLUSTER REPLICATE node_id 将当前节点设置为 node_id 指定的节点的从节点。
CLUSTER SLAVES node_id 列出该slave节点的master节点

CLUSTER ADDSLOTS slot [slot …] 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS slot [slot …] 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT slot NODE node_id 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽,然后再进行指派。
CLUSTER SETSLOT slot MIGRATING node_id 将本节点的槽 slot 迁移到 node_id 指定的节点中。
CLUSTER SETSLOT slot IMPORTING node_id 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER SETSLOT slot STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。

CLUSTER KEYSLOT key 计算键 key 应该被放置在哪个槽上。
CLUSTER COUNTKEYSINSLOT slot 返回槽 slot 目前包含的键值对数量。
CLUSTER GETKEYSINSLOT slot count 返回 count 个 slot 槽中的键

READONLY 在集群中的salve节点开启只读模式
READWRITE 禁止读取请求跳转到集群中的salve节点上

参考:
https://my.oschina.net/guol/blog/506193
http://www.redis.cn/topics/cluster-tutorial.html

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注