欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

初识redis-cluster

程序员文章站 2022-11-15 12:06:39
安装redis 部署集群 参考资料 https://www.cnblogs.com/it-cen/p/4295984.html https://blog.csdn.net/naixiyi/article/details/51346880 https://www.cnblogs.com/Patrick ......

安装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/patrickliu/p/8458788.html