Redis相关笔记(5)集群

Redis 相关笔记(5)集群

redis自带分布式集群:

1.找到redis源码目录:

# cd xxx/redis-x-x/utils/create-cluster/

打开create-cluster脚本:

# vi create-cluster
#!/bin/bash

# Settings
BIN_PATH="../../src/"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""

# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.
...

需要修改的只有前面setting部分的几个选项;

Nodes为启动的redis实例总数

REPLICAS为所有主从复制的集群拥有的备机数,一般REPLICAS数+1为NODES数的约数(NODES可以被REPLICAS+1整除)

需要修改的就这两项实验够用了:

启动脚本:

# ./create-cluster start 
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006

启动完毕,创建集群:

# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 9f382b2cd7592e52d872911a6dd460c5712ab3ea 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: 4e7d796d83a731e4833eeac23b912e9eb6dcf7e2 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: 2f44727fddbeab371405bc931a87e8ba0b09ef00 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: d35e982246e825ed03a1f2b39f051bb869ccdd29 127.0.0.1:30004
   replicates 9f382b2cd7592e52d872911a6dd460c5712ab3ea
S: 97bb3516cdf24f12c47bea3d10a1fe49b702d580 127.0.0.1:30005
   replicates 4e7d796d83a731e4833eeac23b912e9eb6dcf7e2
S: 4778e46d088f8bc1a0713a0a0130148b0c84f37e 127.0.0.1:30006
   replicates 2f44727fddbeab371405bc931a87e8ba0b09ef00
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 127.0.0.1:30001)
M: 9f382b2cd7592e52d872911a6dd460c5712ab3ea 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 4e7d796d83a731e4833eeac23b912e9eb6dcf7e2 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d35e982246e825ed03a1f2b39f051bb869ccdd29 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 9f382b2cd7592e52d872911a6dd460c5712ab3ea
S: 4778e46d088f8bc1a0713a0a0130148b0c84f37e 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 2f44727fddbeab371405bc931a87e8ba0b09ef00
S: 97bb3516cdf24f12c47bea3d10a1fe49b702d580 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 4e7d796d83a731e4833eeac23b912e9eb6dcf7e2
M: 2f44727fddbeab371405bc931a87e8ba0b09ef00 127.0.0.1:30003
   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.

创建完成,查看帮助:

# 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
                 --cluster-fix-with-unreachable-masters
  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
                 --cluster-only-masters
                 --cluster-only-replicas
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-from-user <arg>
                 --cluster-from-pass <arg>
                 --cluster-from-askpass
                 --cluster-copy
                 --cluster-replace
  backup         host:port backup_directory
  help           

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

Cluster Manager Options:
  --cluster-yes  Automatic yes to cluster commands prompts

举例:reshard

# redis-cli --cluster reshard 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 9f382b2cd7592e52d872911a6dd460c5712ab3ea 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 4e7d796d83a731e4833eeac23b912e9eb6dcf7e2 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d35e982246e825ed03a1f2b39f051bb869ccdd29 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 9f382b2cd7592e52d872911a6dd460c5712ab3ea
S: 4778e46d088f8bc1a0713a0a0130148b0c84f37e 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 2f44727fddbeab371405bc931a87e8ba0b09ef00
S: 97bb3516cdf24f12c47bea3d10a1fe49b702d580 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 4e7d796d83a731e4833eeac23b912e9eb6dcf7e2
M: 2f44727fddbeab371405bc931a87e8ba0b09ef00 127.0.0.1:30003
   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.
How many slots do you want to move (from 1 to 16384)?

写入你想移动的槽位,

What is the receiving node ID?

写入你想设置的接受槽位的结点:

Source node #1:

写入你想分配槽位的结点:

Source node #2:

可以再写(你给出的所有节点会平均分配槽位个数,总数等于之前设置的槽位个数)完成了就写done,表示总共就是以上写得这些结点给出槽位。

Moving slot 0 from 9f382b2cd7592e52d872911a6dd460c5712ab3ea
...
Moving slot 1999 from 9f382b2cd7592e52d872911a6dd460c5712ab3ea
Do you want to proceed with the proposed reshard plan (yes/no)? 

输入yes:

Moving slot 0 from 127.0.0.1:30001 to 127.0.0.1:30002:
...
Moving slot 1999 from 127.0.0.1:30001 to 127.0.0.1:30002:

完成

停止集群:

# ./create-cluster stop
Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006

清理缓存:

# ./create-cluster clean

完成

posted @ 2021-09-04 23:20  飞杨煎生物  阅读(103)  评论(0)    收藏  举报