https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.0.tar.gz
wget https://cache.ruby-china.com/pub/ruby/2.6/ruby-2.6.0.tar.gz

https://gems.ruby-china.com/

[root@redis01 ruby-2.6.0]# wget https://cache.ruby-china.com/pub/ruby/2.6/ruby-2.6.0.tar.gz
[root@redis01 ruby-2.6.0]# tar xf ruby-2.6.0.tar.gz
[root@redis01 ruby-2.6.0]# cd ruby-2.6.0
[root@redis01 ruby-2.6.0]# ./configure —prefix=/opt/ruby-2.6.0
make && make install

创建软连接

  1. [root@redis01 ruby-2.6.0]# ln -s /opt/ruby-2.6.0 /opt/ruby
  2. [root@redis01 ruby-2.6.0]# ll /opt
  3. total 0
  4. drwxr-xr-x. 6 root root 56 Jan 12 16:54 python3
  5. lrwxrwxrwx. 1 root root 7 Jan 14 17:37 redis -> redis-5
  6. drwxr-xr-x. 5 root root 114 Jan 18 00:07 redis-5
  7. lrwxrwxrwx. 1 root root 15 Jan 18 01:26 ruby -> /opt/ruby-2.6.0
  8. drwxr-xr-x. 6 root root 56 Jan 18 01:25 ruby-2.6.0

添加全局环境变量

  1. [root@redis01 ruby-2.6.0]# echo 'PATH="/opt/ruby/bin:$PATH"'>>/etc/profile
  2. [root@redis01 ruby-2.6.0]# source /etc/profile
  3. [root@redis01 ruby-2.6.0]# gem -v
  4. 3.0.1
  5. [root@redis01 ruby-2.6.0]# ruby -v
  6. ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
  7. [root@redis01 ruby-2.6.0]#

[root@redis01 ruby-2.6.0]# vim /root/.gemrc
[sources]
https://mirrors.tuna.tsinghua.edu.cn/rubygems/

[root@redis01 ruby-2.6.0]# gem sources l
Failed to load /root/.gemrc because it doesn’t contain valid YAML hash
CURRENT SOURCES
https://rubygems.org/
[root@redis01 ruby-2.6.0]# gem install redis
Failed to load /root/.gemrc because it doesn’t contain valid YAML hash
Fetching redis-4.1.0.gem
Successfully installed redis-4.1.0
Parsing documentation for redis-4.1.0
Installing ri documentation for redis-4.1.0
Done installing documentation for redis after 0 seconds
1 gem installed
[root@redis01 ruby-2.6.0]#

  1. [root@redis01 ~]# find / -name redis-trib.rb
  2. /root/tools/redis-5.0.3/src/redis-trib.rb
  1. [root@redis01 data]# cd cluster-test/
  2. [root@redis01 cluster-test]# ls
  3. 7000 7002 7004 redis-7000.conf redis-7002.conf redis-7004.conf
  4. 7001 7003 7005 redis-7001.conf redis-7003.conf redis-7005.conf
  5. [root@redis01 cluster-test]# redis-server redis-7000.conf
  6. [root@redis01 cluster-test]# redis-server redis-7000.conf
  7. [root@redis01 cluster-test]# redis-server redis-7001.conf
  8. [root@redis01 cluster-test]# redis-server redis-7002.conf
  9. [root@redis01 cluster-test]# redis-server redis-7003.conf
  10. [root@redis01 cluster-test]# redis-server redis-7004.conf
  11. [root@redis01 cluster-test]# redis-server redis-7005.conf
  12. [root@redis01 cluster-test]# ps -ef |grep redis
  13. root 84433 1 0 16:13 ? 00:00:00 redis-server *:7000 [cluster]
  14. root 84445 1 0 16:13 ? 00:00:00 redis-server *:7001 [cluster]
  15. root 84453 1 0 16:13 ? 00:00:00 redis-server *:7002 [cluster]
  16. root 84462 1 0 16:13 ? 00:00:00 redis-server *:7003 [cluster]
  17. root 84471 1 0 16:13 ? 00:00:00 redis-server *:7004 [cluster]
  18. root 84480 1 0 16:13 ? 00:00:00 redis-server *:7005 [cluster]
  19. root 84490 83509 0 16:13 pts/0 00:00:00 grep --color=auto redis
  20. [root@redis01 cluster-test]# /root/tools/redis-5.0.3/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  21. WARNING: redis-trib.rb is not longer available!
  22. You should use redis-cli instead.
  23. All commands and features belonging to redis-trib.rb have been moved
  24. to redis-cli.
  25. In order to use them you should call redis-cli with the --cluster
  26. option followed by the subcommand name, arguments and options.
  27. Use the following syntax:
  28. redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
  29. Example:
  30. redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
  31. To get help about all subcommands, type:
  32. redis-cli --cluster help

上述的无法使用,原因在于此:(官方说明) 简言之:版本不同导致的,redis 5 已经弃用了之前的方式

The first example, that is, the cluster creation, will be shown using both redis-cli in Redis 5 and redis-trib in Redis 3 and 4. However all the next examples will only use redis-cli, since as you can see the syntax is very similar, and you can trivially change one command line into the other by using redis-trib.rb help to get info about the old syntax. Important: note that you can use Redis 5 redis-cli against Redis 4 clusters without issues if you wish. To create your cluster for Redis 5 with redis-cli simply type:

  1. redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 \
  2. 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
  3. --cluster-replicas 1

Using redis-trib.rb for Redis 4 or 3 type:

  1. ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
  2. 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  1. [root@redis01 cluster-test]# redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
  2. >>> Performing hash slots allocation on 6 nodes...
  3. Master[0] -> Slots 0 - 5460
  4. Master[1] -> Slots 5461 - 10922
  5. Master[2] -> Slots 10923 - 16383
  6. Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
  7. Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
  8. Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
  9. >>> Trying to optimize slaves allocation for anti-affinity
  10. [WARNING] Some slaves are in the same host as their master
  11. M: ebe673291e667448261fa0fa69aaf456ca401810 127.0.0.1:7000
  12. slots:[0-5460] (5461 slots) master
  13. M: 2fbbf2910cbadc908df41fa21a270794046ed0d0 127.0.0.1:7001
  14. slots:[5461-10922] (5462 slots) master
  15. M: b02df74489dc025eab598ec124fe51e62ff47041 127.0.0.1:7002
  16. slots:[10923-16383] (5461 slots) master
  17. S: db36f1d1c12a8dd5f973146af184d926c90ac164 127.0.0.1:7003
  18. replicates 2fbbf2910cbadc908df41fa21a270794046ed0d0
  19. S: 6545a7673f0d947440ae644d0e760e4578ec77c3 127.0.0.1:7004
  20. replicates b02df74489dc025eab598ec124fe51e62ff47041
  21. S: 1881b58b72eab8dc1d335c8c2158ed9097b5094b 127.0.0.1:7005
  22. replicates ebe673291e667448261fa0fa69aaf456ca401810
  23. Can I set the above configuration? (type 'yes' to accept):

该选项--cluster-replicas 1意味着我们希望每个创建的主库都有一个从库

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:7000)
M: ebe673291e667448261fa0fa69aaf456ca401810 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 6545a7673f0d947440ae644d0e760e4578ec77c3 127.0.0.1:7004
   slots: (0 slots) slave
   replicates b02df74489dc025eab598ec124fe51e62ff47041
M: b02df74489dc025eab598ec124fe51e62ff47041 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 2fbbf2910cbadc908df41fa21a270794046ed0d0 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1881b58b72eab8dc1d335c8c2158ed9097b5094b 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ebe673291e667448261fa0fa69aaf456ca401810
S: db36f1d1c12a8dd5f973146af184d926c90ac164 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 2fbbf2910cbadc908df41fa21a270794046ed0d0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试集群存储

[root@redis01 cluster-test]# redis-cli  -p 7000 -c
127.0.0.1:7000> 
127.0.0.1:7000> 
127.0.0.1:7000> 
127.0.0.1:7000> 
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> set addr 123
-> Redirected to slot [12790] located at 127.0.0.1:7002
OK
127.0.0.1:7002>

以下节点解释

127.0.0.1:7000> set addr 123 -> Redirected to slot [12790] located at 127.0.0.1:7002 OK 127.0.0.1:7002>

7000 节点存放数据 仔细看数据已经存放在了 7002 节点 并且已经自动切换到了 7002 节点

redis-cluster 会默认将不同的key,进行CRC16算法,进行分配到不同槽位中

127.0.0.1:7004> set add fas
-> Redirected to slot [1943] located at 127.0.0.1:7000
OK
127.0.0.1:7000>

重新分配虚拟槽

检查节点的集群状态

[root@redis01 cluster-test]# redis-cli --cluster check 127.0.0.1:7000 
127.0.0.1:7000 (ebe67329...) -> 2 keys | 5461 slots | 1 slaves.
127.0.0.1:7002 (b02df744...) -> 1 keys | 5461 slots | 1 slaves.
127.0.0.1:7001 (2fbbf291...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: ebe673291e667448261fa0fa69aaf456ca401810 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 6545a7673f0d947440ae644d0e760e4578ec77c3 127.0.0.1:7004
   slots: (0 slots) slave
   replicates b02df74489dc025eab598ec124fe51e62ff47041
M: b02df74489dc025eab598ec124fe51e62ff47041 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 2fbbf2910cbadc908df41fa21a270794046ed0d0 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1881b58b72eab8dc1d335c8c2158ed9097b5094b 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ebe673291e667448261fa0fa69aaf456ca401810
S: db36f1d1c12a8dd5f973146af184d926c90ac164 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 2fbbf2910cbadc908df41fa21a270794046ed0d0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

参考:
https://redis.io/topics/cluster-tutorial