码迷,mamicode.com
首页 > 系统相关 > 详细

linux-redis cluster集群(redis5.x)

时间:2020-02-25 00:22:42      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:hash   节点   warning   linu   输入   port   连接   round   dad   

1、查看redis安装目录:

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# ll
total 472
-rw-rw-r-- 1 root root 106874 May 16 2019 00-RELEASENOTES
-rw-rw-r-- 1 root root 53 May 16 2019 BUGS
-rw-rw-r-- 1 root root 2381 May 16 2019 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 May 16 2019 COPYING
drwxrwxr-x 6 root root 4096 Feb 6 11:38 deps
-rw-r--r-- 1 root root 203457 Feb 21 23:31 dump.rdb
-rw-rw-r-- 1 root root 11 May 16 2019 INSTALL
-rw-rw-r-- 1 root root 151 May 16 2019 Makefile
-rw-rw-r-- 1 root root 6888 May 16 2019 MANIFESTO
-rw-rw-r-- 1 root root 20555 May 16 2019 README.md
-rw-rw-r-- 1 root root 61821 Feb 15 13:12 redis.conf
-rwxrwxr-x 1 root root 275 May 16 2019 runtest
-rwxrwxr-x 1 root root 280 May 16 2019 runtest-cluster
-rwxrwxr-x 1 root root 341 May 16 2019 runtest-moduleapi
-rwxrwxr-x 1 root root 281 May 16 2019 runtest-sentinel
-rw-rw-r-- 1 root root 9710 May 16 2019 sentinel.conf
drwxrwxr-x 3 root root 4096 Feb 24 16:07 src
drwxrwxr-x 11 root root 4096 May 16 2019 tests
drwxrwxr-x 8 root root 4096 May 16 2019 utils

2、在当前目录创建redis_cluster

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]mkdir redis_cluster

3、将src目录下所有文件拷贝到redis_cluster/node6379~6384下

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r ./src/ redis_cluster/node6379

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r ./src/ redis_cluster/node6380

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r ./src/ redis_cluster/node6381

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r ./src/ redis_cluster/node6382

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r ./src/ redis_cluster/node6383

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r ./src/ redis_cluster/node6384

4、将redis.conf拷贝到redis_cluster/node6379下

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# cp -r redis.conf ./redis_cluster/node6379

5、修改nodes6379目录下redis.conf文件,以nodes6379为例:

[root@iZwz97y9qoykzzotubitq3Z redis-5.0.5]# vim ./redis_cluster/node6379/redis.conf

进行如下配置:
# bind 127.0.0.1

protected-mode no

port 6379(对应node序号进行更改,6380,6381...)

pidfile /var/run/redis_6379.pid(对应node序号进行更改,6380,6381...)

dbfilename dump6379.rdb (对应node序号进行更改,6380,6381...)

masterauth root123456

requirepass root123456 (注意此处是空格,而不是“=”)

cluster-enabled yes

cluster-config-file nodes-6379.conf (对应node序号进行更改,6380,6381...)

cluster-node-timeout 15000

然后wq!保存退出。

6、将redis_cluster/node6379/redis.conf拷贝到node6380~6084下

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# cp -r node6379/redis.conf  node6380/

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# cp -r node6379/redis.conf  node6381/

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# cp -r node6379/redis.conf  node6382/

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# cp -r node6379/redis.conf  node6383/

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# cp -r node6379/redis.conf  node6384/

7、以此修改redis.conf文件,以node6380为例:

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# vim node6380/redis.conf

输入“ :%s/6379/6380/g”,回车结束,最后wq!保存退出(6379整体替换为6380);

注:node6381~6384 按上述操作整体替换即可。

8、以此启动node6379~6384节点,以node6379为例

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# node6379/redis-server node6379/redis.conf
12051:C 24 Feb 2020 22:32:36.650 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12051:C 24 Feb 2020 22:32:36.650 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12051, just started
12051:C 24 Feb 2020 22:32:36.650 # Configuration loaded
注:启动时,必须加载各自节点下的redis.conf

9、查看各节点的启动状态

[root@iZwz97y9qoykzzotubitq3Z redis_cluster]# ps -aux | grep redis
root 425 0.1 0.1 156456 3304 ? Ssl 20:53 0:06 node6383/redis-server *:6383 [cluster]
root 849 0.1 0.1 156456 3300 ? Ssl 20:54 0:06 node6384/redis-server *:6384 [cluster]
root 31077 0.1 0.1 163112 3332 ? Ssl 20:49 0:07 node6379/redis-server *:6379 [cluster]
root 32046 0.1 0.1 162600 3288 ? Ssl 20:51 0:06 node6380/redis-server *:6380 [cluster]
root 32390 0.1 0.1 156456 3248 ? Ssl 20:52 0:06 node6381/redis-server *:6381 [cluster]
root 32669 0.1 0.1 156456 3344 ? Rsl 20:53 0:06 node6382/redis-server *:6382 [cluster]

10、配置集群

[root@iZwz97y9qoykzzotubitq3Z node6379]# ./redis-cli --cluster create 39.xx.130.xx:6379 39.xx.130.xx:6380 39.xx.130.xx:6381 39.xx.130.xx:6382 39.xx.130.xx:6383 39.xx.130.xx:6384 --cluster-replicas 1 -a root123456

若最终出现下面信息,则说明配置成功!

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

注:redis5版本可以直接使用redis-cli命令配置集群(内部集成ruby)

--cluster-replicas 1:表示主从比例1:1 (一台主机对应有一台从机)

--cluster-replicas 2:表示主从比例1:2 (一台主机对应有两台从机)

-a root123456:配置集群时所需的认证密码 (-a:auth简写,root123456:redis服务器认证密码)

11、测试redis-cluster

[root@iZwz97y9qoykzzotubitq3Z node6379]# ./redis-cli -c -a root123456
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
127.0.0.1:6379> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:6381
OK
127.0.0.1:6381> set hello world
-> Redirected to slot [866] located at 127.0.0.1:6379
OK
127.0.0.1:6379> get foo
-> Redirected to slot [12182] located at 127.0.0.1:6381
"bar"
127.0.0.1:6381> get hello
-> Redirected to slot [866] located at 127.0.0.1:6379
"world"
存值时,根据key进行算法后,将数据存入对应的节点中(每个节点对应有hash槽的范围)

12、查看节点信息

127.0.0.1:6379> cluster nodes
79b4f44b41c07275182241da6325e3ffb354867b 127.0.0.1:6383@16383 slave 1a3fe89294ea018880d59c9c080297f8870bd3c8 0 1582556062000 5 connected
0f6150af547cfc3aaaaff3fabe19421a93862150 127.0.0.1:6381@16381 master - 0 1582556062299 3 connected 10923-16383
0a9350c59941f1ef0f1eb147a971528a926dbfd1 127.0.0.1:6382@16382 slave 0aba59f4fd0341df1dc06bd38d3dade90b7f8ae4 0 1582556064304 4 connected
6c97a66fe6f90e76c65b16e9eef52b0a64b5e588 127.0.0.1:6384@16384 slave 0f6150af547cfc3aaaaff3fabe19421a93862150 0 1582556063000 6 connected
1a3fe89294ea018880d59c9c080297f8870bd3c8 127.0.0.1:6380@16380 master - 0 1582556063302 2 connected 5461-10922
0aba59f4fd0341df1dc06bd38d3dade90b7f8ae4 127.0.0.1:6379@16379 myself,master - 0 1582556060000 1 connected 0-5460

注:说明6379(主)与6382(从)配对

13、模拟将node6379节点停掉

 重新打开一个tab

[root@iZwz97y9qoykzzotubitq3Z ~]# ps -aux | grep redis
root 425 0.1 0.1 156456 3248 ? Ssl 20:53 0:08 node6383/redis-server *:6383 [cluster]
root 849 0.1 0.1 156456 3304 ? Ssl 20:54 0:08 node6384/redis-server *:6384 [cluster]
root 19850 0.0 0.0 24856 1600 pts/0 S+ 22:50 0:00 ./redis-cli -c -a root123456
root 23411 0.0 0.0 112712 964 pts/1 R+ 22:58 0:00 grep --color=auto redis
root 31077 0.1 0.1 163112 3364 ? Ssl 20:49 0:08 node6379/redis-server *:6379 [cluster]
root 32046 0.1 0.1 162600 3288 ? Ssl 20:51 0:08 node6380/redis-server *:6380 [cluster]
root 32390 0.1 0.1 156456 3264 ? Ssl 20:52 0:08 node6381/redis-server *:6381 [cluster]
root 32669 0.1 0.1 156456 3260 ? Ssl 20:53 0:08 node6382/redis-server *:6382 [cluster]

[root@iZwz97y9qoykzzotubitq3Z ~]# kill -9 31077

[root@iZwz97y9qoykzzotubitq3Z ~]# ps -aux | grep redis
root 425 0.1 0.1 156456 3248 ? Ssl 20:53 0:08 node6383/redis-server *:6383 [cluster]
root 849 0.1 0.1 156456 3304 ? Ssl 20:54 0:08 node6384/redis-server *:6384 [cluster]
root 19850 0.0 0.0 24856 1600 pts/0 S+ 22:50 0:00 ./redis-cli -c -a root123456
root 23990 0.0 0.0 112712 960 pts/1 R+ 22:59 0:00 grep --color=auto redis
root 32046 0.1 0.1 162600 3288 ? Rsl 20:51 0:08 node6380/redis-server *:6380 [cluster]
root 32390 0.1 0.1 156456 3264 ? Ssl 20:52 0:08 node6381/redis-server *:6381 [cluster]
root 32669 0.1 0.1 156456 3260 ? Ssl 20:53 0:08 node6382/redis-server *:6382 [cluster]

再连接6381节点:

[root@iZwz97y9qoykzzotubitq3Z node6379]# ./redis-cli -c -p 6381  -a root123456

127.0.0.1:6381> get hello
-> Redirected to slot [866] located at 127.0.0.1:6382
"world"

127.0.0.1:6382> cluster nodes
0a9350c59941f1ef0f1eb147a971528a926dbfd1 127.0.0.1:6382@16382 myself,master - 0 1582557176000 7 connected 0-5460
1a3fe89294ea018880d59c9c080297f8870bd3c8 127.0.0.1:6380@16380 master - 0 1582557180461 2 connected 5461-10922
79b4f44b41c07275182241da6325e3ffb354867b 127.0.0.1:6383@16383 slave 1a3fe89294ea018880d59c9c080297f8870bd3c8 0 1582557179458 5 connected
0f6150af547cfc3aaaaff3fabe19421a93862150 127.0.0.1:6381@16381 master - 0 1582557178000 3 connected 10923-16383
6c97a66fe6f90e76c65b16e9eef52b0a64b5e588 127.0.0.1:6384@16384 slave 0f6150af547cfc3aaaaff3fabe19421a93862150 0 1582557178456 6 connected
0aba59f4fd0341df1dc06bd38d3dade90b7f8ae4 127.0.0.1:6379@16379 master,fail - 1582556349063 1582556347000 1 disconnected

分析:当6379节点还是正常时,get hello应该是重定向到6379节点上的。而现在重定向到6382节点上,

说明之前6382是6379的从节点。但此时6382是主节点了,6379恢复正常后,也只能是从节点了。

127.0.0.1:6381> get hello
-> Redirected to slot [866] located at 127.0.0.1:6379
"world"

 

linux-redis cluster集群(redis5.x)

标签:hash   节点   warning   linu   输入   port   连接   round   dad   

原文地址:https://www.cnblogs.com/yuefeng123/p/12359506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!