#### 安装
```bash
make
make test
make install PREFIX=/opt/redis
```

#### 集群配置
```text
daemonize yes
注释所有save行关闭持久化
pidfile /opt/redis-cluster/redis01/redis_6381.pid
cluster-enabled yes
```

#### 安装ruby
```bash
sudo apt-get install ruby

# 配置ruby gem镜像
# https://ruby.taobao.org/

gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
gem sources -l
gem install redis
```

#### 启动集群
```text
# 启动6个redis实例
# 6381, 6382, 6383, 6384, 6385, 6387

./redis-trib.rb create --replicas 1 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 127.0.0.1:6386

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6381
127.0.0.1:6382
127.0.0.1:6383
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
Adding replica 127.0.0.1:6386 to 127.0.0.1:6383
M: 7e79d845f28c110941588cee29a1289c5101ec25 127.0.0.1:6381
   slots:0-5460 (5461 slots) master
M: 7557e4c955a744e10107b22db577f11c02bf2557 127.0.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e 127.0.0.1:6383
   slots:10923-16383 (5461 slots) master
S: 52290394b0d6ed69bbe3beefb43c1e119afb7552 127.0.0.1:6384
   replicates 7e79d845f28c110941588cee29a1289c5101ec25
S: 36a3e428a17af7f99aad4e441f9c2593649777e7 127.0.0.1:6385
   replicates 7557e4c955a744e10107b22db577f11c02bf2557
S: ac11d910bca88f3ddbcfad9b64d3a0bbfd3c527b 127.0.0.1:6386
   replicates 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e

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:6381)
M: 7e79d845f28c110941588cee29a1289c5101ec25 127.0.0.1:6381
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 52290394b0d6ed69bbe3beefb43c1e119afb7552 127.0.0.1:6384
   slots: (0 slots) slave
   replicates 7e79d845f28c110941588cee29a1289c5101ec25
M: 7557e4c955a744e10107b22db577f11c02bf2557 127.0.0.1:6382
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ac11d910bca88f3ddbcfad9b64d3a0bbfd3c527b 127.0.0.1:6386
   slots: (0 slots) slave
   replicates 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e
S: 36a3e428a17af7f99aad4e441f9c2593649777e7 127.0.0.1:6385
   slots: (0 slots) slave
   replicates 7557e4c955a744e10107b22db577f11c02bf2557
M: 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e 127.0.0.1:6383
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)

# 连接集群
./redis-cli -h 127.0.0.1 -p 6381 -c

# 关闭集群
./redis-cli -h 127.0.0.1 -p 6381 shutdown
```

#### 集群添加节点
```text
# 首先启动一个127.0.0.1:6387的实例

# 添加节点到集群
./redis-trib.rb add-node 127.0.0.1:6387 127.0.0.1:6381

# 给新节点分配哈希slot
./redis-trib.rb reshard 127.0.0.1:6381

>>> Performing Cluster Check (using node 127.0.0.1:6381)
M: 7e79d845f28c110941588cee29a1289c5101ec25 127.0.0.1:6381
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 52290394b0d6ed69bbe3beefb43c1e119afb7552 127.0.0.1:6384
   slots: (0 slots) slave
   replicates 7e79d845f28c110941588cee29a1289c5101ec25
M: 7557e4c955a744e10107b22db577f11c02bf2557 127.0.0.1:6382
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ac11d910bca88f3ddbcfad9b64d3a0bbfd3c527b 127.0.0.1:6386
   slots: (0 slots) slave
   replicates 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e
S: 36a3e428a17af7f99aad4e441f9c2593649777e7 127.0.0.1:6385
   slots: (0 slots) slave
   replicates 7557e4c955a744e10107b22db577f11c02bf2557
M: 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e 127.0.0.1:6383
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: e02cdb5f5878afed4ec8ced049729043ccfb7a31 127.0.0.1:6387
   slots: (0 slots) master
   0 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)? 1000

What is the receiving node ID? e02cdb5f5878afed4ec8ced049729043ccfb7a31
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
  
Source node #1:all

Ready to move 1000 slots.
  Source nodes:
    M: 7e79d845f28c110941588cee29a1289c5101ec25 127.0.0.1:6381
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
    M: 7557e4c955a744e10107b22db577f11c02bf2557 127.0.0.1:6382
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
    M: 4edaf15c3e986a8743a0ea9bcfa3ee4ba2cd6d3e 127.0.0.1:6383
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
  Destination node:
    M: e02cdb5f5878afed4ec8ced049729043ccfb7a31 127.0.0.1:6387
   slots: (0 slots) master
   0 additional replica(s)
  Resharding plan:
  .
  .
  .
```

#### 添加slave节点
```text
./redis-trib.rb add-node --slave 127.0.0.1:6388 127.0.0.1:6387
```

#### 集群移除节点
```text
# 删除slave节点
# 在从节点中，我们没有分配哈希槽，所以删除很简单，我们直接执行
# 有两个参数ip：port  和节点的id
./redis-trib.rb del-node 127.0.0.1:6388 65ee465423c925326a5137668541151b4c37d2d9 

# 删除主节点
# 在删除主节点的时候，因为在主节点中存放着数据，所以我们在删除之前，要把这些数据迁移走，
# 并且把该节点上的哈希槽分配到其他主节点上。

./redis-trib.rb reshard 127.0.0.1:6387
# 问我们有多少个哈希槽要移走，因为我们这个节点上有1000 个所以我们这里输入1000

# 这样期间会询问我们是否要从新分配，我们输入yes。
# 然后就看到6387上的所有节点都被移动到了6381.这样我们就操作完了。
# 然后再执行
./redis-trib.rb del-node 127.0.0.1:6387 61f786c40bcc170006a440abd7dc773e6dd15a19
```