初识redis-cluster

安装redis

 1 [root@localhost ~]# cd /datas/soft/
 2 [root@localhost soft]# ll redis-5.0.0.tar.gz     // 已经下载的最新版
 3 -rwxrwxrwx 1 www www 1947721 Oct 22 14:21 redis-5.0.0.tar.gz
 4 [root@localhost soft]# tar -xzf redis-5.0.0.tar.gz
 5 [root@localhost soft]# cd redis-5.0.0
 6 [root@localhost redis-5.0.0]# make
 7 [root@localhost redis-5.0.0]# cd src
 8 [root@localhost src]# make install PREFIX=/datas/soft/redis5
 9 [root@localhost src]# mkdir /datas/soft/redis5/etc
10 [root@localhost src]# cp ../redis.conf /datas/soft/redis5/etc/
11 [root@localhost src]# vim redis.conf
12     daemonize yes  // 后台运行
13 [root@localhost src]# cd /datas/soft/redis5/bin/
14 [root@localhost bin]# ./redis-server ../etc/redis.conf
15 16936:C 24 Oct 2018 20:01:42.406 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16 16936:C 24 Oct 2018 20:01:42.406 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=16936, just started
17 16936:C 24 Oct 2018 20:01:42.406 # Configuration loaded
18 [root@localhost bin]# ps auxf | grep redis
19 root      16943  0.0  0.0 103304   872 pts/1    S+   20:02   0:00      |   \_ grep redis                     
20 root      16937  0.1  0.7 152484  7760 ?        Ssl  20:01   0:00 ./redis-server 127.0.0.1:6379
21 [root@localhost bin]# ./redis-cli -p 6379
22 127.0.0.1:6379> set a hahaha6379
23 OK
24 127.0.0.1:6379> get a
25 "hahaha6379"

部署集群

  1 [root@localhost bin]# cd ../etc
  2 [root@localhost etc]# mkdir redis-cluster
  3 [root@localhost etc]# cd redis-cluster
  4 [root@localhost redis-cluster]# mkdir 30001
  5 [root@localhost redis-cluster]# cd 30001
  6 [root@localhost 30001]# cp ../../redis.conf ./
  7 [root@localhost 30001]# vim redis.conf
  8     port 30001  // 绑定端口号
  9     daemonize yes  // 后台运行
 10     pidfile /datas/soft/redis5/etc/redis-cluster/30001/redis.pid  // 修改pid进程文件名
 11     logfile /datas/soft/redis5/etc/redis-cluster/30001/redis.log  // 修改日志文件名称
 12     dir /datas/soft/redis5/etc/redis-cluster/30001/  // 修改数据文件存放地址
 13     cluster-enabled yes  // 启用集群
 14     cluster-config-file nodes-30001.conf  // 配置每个节点的配置文件,同样以端口号为名称
 15     cluster-node-timeout 15000  // 配置集群节点的超时时间,可改可不改
 16     appendonly yes  // 启动AOF增量持久化策略
 17     appendfsync always  // 发生改变就记录日志
 18 [root@localhost redis-cluster]# ll    // 相同步骤配置其他端口
 19 total 24
 20 drwxr-xr-x 2 root root 4096 Oct 24 21:34 30001
 21 drwxr-xr-x 2 root root 4096 Oct 24 21:34 30002
 22 drwxr-xr-x 2 root root 4096 Oct 24 21:35 30003
 23 drwxr-xr-x 2 root root 4096 Oct 24 21:35 30004
 24 drwxr-xr-x 2 root root 4096 Oct 24 21:35 30005
 25 drwxr-xr-x 2 root root 4096 Oct 24 21:35 30006
 26 [root@localhost redis-cluster]# /datas/soft/redis5/bin/redis-server /datas/soft/redis5/etc/redis-cluster/30001/redis.conf      // 启动
 27 [root@localhost redis-cluster]# /datas/soft/redis5/bin/redis-server /datas/soft/redis5/etc/redis-cluster/30002/redis.conf 
 28 [root@localhost redis-cluster]# /datas/soft/redis5/bin/redis-server /datas/soft/redis5/etc/redis-cluster/30003/redis.conf 
 29 [root@localhost redis-cluster]# /datas/soft/redis5/bin/redis-server /datas/soft/redis5/etc/redis-cluster/30004/redis.conf 
 30 [root@localhost redis-cluster]# /datas/soft/redis5/bin/redis-server /datas/soft/redis5/etc/redis-cluster/30005/redis.conf 
 31 [root@localhost redis-cluster]# /datas/soft/redis5/bin/redis-server /datas/soft/redis5/etc/redis-cluster/30006/redis.conf 
 32 [root@localhost redis-cluster]# ps auxf | grep redis
 33 root      17034  0.0  0.0 103304   876 pts/1    S+   21:38   0:00      |   \_ grep redis
 34 root      16937  0.2  1.0 158116 10400 ?        Ssl  20:01   0:13 ./redis-server 127.0.0.1:6379   
 35 root      16997  0.9  0.7 155552  7952 ?        Ssl  21:38   0:00 /datas/soft/redis5/bin/redis-server 127.0.0.1:30001 [cluster]                            
 36 root      17003  0.6  0.7 155552  7952 ?        Ssl  21:38   0:00 /datas/soft/redis5/bin/redis-server 127.0.0.1:30002 [cluster]                            
 37 root      17008  0.6  0.7 155552  7968 ?        Ssl  21:38   0:00 /datas/soft/redis5/bin/redis-server 127.0.0.1:30003 [cluster]                            
 38 root      17013  0.6  1.2 159648 12100 ?        Ssl  21:38   0:00 /datas/soft/redis5/bin/redis-server 127.0.0.1:30004 [cluster]                            
 39 root      17020  0.4  1.2 159648 12120 ?        Ssl  21:38   0:00 /datas/soft/redis5/bin/redis-server 127.0.0.1:30005 [cluster]                            
 40 root      17027  0.7  1.2 159648 12104 ?        Ssl  21:38   0:00 /datas/soft/redis5/bin/redis-server 127.0.0.1:30006 [cluster]                            
 41 [root@localhost redis-cluster]# ll /datas/soft/redis5/etc/redis-cluster/30001/
 42 total 88
 43 -rw-r--r-- 1 root root     0 Oct 22 15:01 appendonly.aof
 44 -rw-r--r-- 1 root root   175 Oct 24 21:38 dump.rdb
 45 -rw-r--r-- 1 root root   787 Oct 24 21:38 nodes-30001.conf
 46 -rwxr-xr-x 1 root root 62271 Oct 24 21:34 redis.conf
 47 -rw-r--r-- 1 root root 10257 Oct 24 21:38 redis.log
 48 -rw-r--r-- 1 root root     6 Oct 24 21:38 redis.pid
 49 
 50 [root@localhost redis-cluster]# cd ../../bin/
 51 [root@localhost bin]# yum install -y ruby
 52 [root@localhost bin]# yum install -y rubygems
 53 [root@localhost bin]# cp /datas/soft/redis-5.0.0/src/redis-trib.rb ./
 54 [root@localhost bin]# ruby redis-trib.rb create --replicas 1 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
 55 redis-trib.rb:6: odd number list for Hash
 56         white: 29,
 57               ^
 58 redis-trib.rb:6: syntax error, unexpected ':', expecting '}'
 59         white: 29,
 60               ^
 61 redis-trib.rb:7: syntax error, unexpected ',', expecting kEND
 62 [root@localhost bin]# ruby -v
 63 ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
 64 [root@localhost bin]# yum remove -y ruby
 65 [root@localhost bin]# yum remove -y rubygems
 66 [root@localhost bin]# cd /datas/soft/
 67 [root@localhost soft]# ll ruby-2.4.5.tar.gz     // ruby最新版
 68 -rwxr-xr-x   1 root root 14232887 Oct 24 19:33 ruby-2.4.5.tar.gz
 69 [root@localhost soft]# tar -xzf ruby-2.4.5.tar.gz
 70 [root@localhost soft]# cd ruby-2.4.5
 71 [root@localhost ruby-2.4.5]# ./configure
 72 [root@localhost ruby-2.4.5]# make
 73 [root@localhost ruby-2.4.5]# make install
 74 [root@localhost ruby-2.4.5]# ruby -v
 75 ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
 76 [root@localhost ruby-2.4.5]# cd /datas/soft/redis5/bin/
 77 [root@localhost bin]# ruby redis-trib.rb create --replicas 1 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
 78 WARNING: redis-trib.rb is not longer available!
 79 You should use redis-cli instead.
 80 
 81 All commands and features belonging to redis-trib.rb have been moved
 82 to redis-cli.
 83 In order to use them you should call redis-cli with the --cluster
 84 option followed by the subcommand name, arguments and options.
 85 
 86 Use the following syntax:
 87 redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
 88 
 89 Example:
 90 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
 91 
 92 To get help about all subcommands, type:
 93 redis-cli --cluster help
 94 
 95 [root@localhost bin]# ./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
 96 >>> Performing hash slots allocation on 6 nodes...
 97 Master[0] -> Slots 0 - 5460
 98 Master[1] -> Slots 5461 - 10922
 99 Master[2] -> Slots 10923 - 16383
100 Adding replica 127.0.0.1:30004 to 127.0.0.1:30001
101 Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
102 Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
103 >>> Trying to optimize slaves allocation for anti-affinity
104 [WARNING] Some slaves are in the same host as their master
105 M: 0ef9bb46603bc2703e2f812316b2d17e4664a285 127.0.0.1:30001
106    slots:[0-5460] (5461 slots) master
107 M: 1cce19c5b8f58666199445ee858b0a699cf523f9 127.0.0.1:30002
108    slots:[5461-10922] (5462 slots) master
109 M: 040afde427d4df0738b4705006309fc171403613 127.0.0.1:30003
110    slots:[10923-16383] (5461 slots) master
111 S: 7bb7a15f22730493cd7c66954a39e4008788c879 127.0.0.1:30004
112    replicates 1cce19c5b8f58666199445ee858b0a699cf523f9
113 S: c08daea6c990cb2732d95ae03c201c94523c0eff 127.0.0.1:30005
114    replicates 040afde427d4df0738b4705006309fc171403613
115 S: 7acc8a1f489ec954f069f998792eea8982653e70 127.0.0.1:30006
116    replicates 0ef9bb46603bc2703e2f812316b2d17e4664a285
117 Can I set the above configuration? (type 'yes' to accept): yes
118 >>> Nodes configuration updated
119 >>> Assign a different config epoch to each node
120 >>> Sending CLUSTER MEET messages to join the cluster
121 Waiting for the cluster to join
122 ..
123 >>> Performing Cluster Check (using node 127.0.0.1:30001)
124 M: 0ef9bb46603bc2703e2f812316b2d17e4664a285 127.0.0.1:30001
125    slots:[0-5460] (5461 slots) master
126    1 additional replica(s)
127 S: 7bb7a15f22730493cd7c66954a39e4008788c879 127.0.0.1:30004
128    slots: (0 slots) slave
129    replicates 1cce19c5b8f58666199445ee858b0a699cf523f9
130 M: 1cce19c5b8f58666199445ee858b0a699cf523f9 127.0.0.1:30002
131    slots:[5461-10922] (5462 slots) master
132    1 additional replica(s)
133 S: 7acc8a1f489ec954f069f998792eea8982653e70 127.0.0.1:30006
134    slots: (0 slots) slave
135    replicates 0ef9bb46603bc2703e2f812316b2d17e4664a285
136 S: c08daea6c990cb2732d95ae03c201c94523c0eff 127.0.0.1:30005
137    slots: (0 slots) slave
138    replicates 040afde427d4df0738b4705006309fc171403613
139 M: 040afde427d4df0738b4705006309fc171403613 127.0.0.1:30003
140    slots:[10923-16383] (5461 slots) master
141    1 additional replica(s)
142 [OK] All nodes agree about slots configuration.
143 >>> Check for open slots...
144 >>> Check slots coverage...
145 [OK] All 16384 slots covered.
146 [root@localhost bin]# ./redis-cli -c -p 30001
147 127.0.0.1:30001> set a hahahaha30001
148 -> Redirected to slot [15495] located at 127.0.0.1:30003
149 OK
150 127.0.0.1:30003> get a
151 "hahahaha30001"
152 127.0.0.1:30003> 
153 [root@localhost bin]# ./redis-cli -c -p 30006
154 127.0.0.1:30006> get a
155 -> Redirected to slot [15495] located at 127.0.0.1:30003
156 "hahahaha30001"
157 127.0.0.1:30003>

参考资料

https://www.cnblogs.com/it-cen/p/4295984.html

https://blog.csdn.net/naixiyi/article/details/51346880

https://www.cnblogs.com/PatrickLiu/p/8458788.html

http://www.runoob.com/ruby/ruby-installation-unix.html

https://blog.csdn.net/vtopqx/article/details/50235737

 

——————————- 2019-08-12 —————————————–

单实例数据迁移到Cluster

https://cloud.tencent.com/developer/article/1433055

https://www.18188.org/articles/2016/04/23/1461374145366.html

    原文作者:redis
    原文地址: https://www.cnblogs.com/chanAndy/p/9851512.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞