redis-cluster集群单机搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-cluster集群单机搭建相关的知识,希望对你有一定的参考价值。

闲来无事自己做的关于redis-cluster集群的测试

redis的安装

下载安装包(这里使用的版本是:redis-3.2.9.tar.gz)

cd /home/download

tar -zxvf redis-3.2.9.tar.gz

mv redis-3.2.9 redis

创建所需要的以端口为区别的文件夹

mkdir cluster 

cd cluster && mkdir 7000 7001 7002 7003 7004 7005

cd /home/download/redis

cp redis.conf /home/download/cluster/7000

修改7000里面的配置文件

vim /home/download/cluster/7000/redis.conf

修改以下几项

port 7000

daemonize yes

cluster-enabled yes

cluster-config-file nodes-7000.conf

cluster-node-timeout 5000

appendonly yes

分别复制到其余的文件夹下面,修改端口号和文件名称一样

安装集群管理插件

redis-3.2.1.gem 网上下载一个  我这不是最新版本的

yum install ruby rubygems -y

gem install -l redis-3.2.1.gem

分别启动6个redis

cd /home/download/redis/src

./redis-server /home/download/cluster/7000/redis.conf

./redis-server /home/download/cluster/7001/redis.conf

./redis-server /home/download/cluster/7002/redis.conf

./redis-server /home/download/cluster/7003/redis.conf

./redis-server /home/download/cluster/7004/redis.conf

./redis-server /home/download/cluster/7005/redis.conf

检查启动情况

pa-ef | grep redis

root      49131      1  0 Jul29 ?        00:01:03 ./redis-server 127.0.0.1:7000 [cluster]

root      49136      1  0 Jul29 ?        00:01:03 ./redis-server 127.0.0.1:7001 [cluster]

root      49140      1  0 Jul29 ?        00:01:03 ./redis-server 127.0.0.1:7002 [cluster]

root      49257      1  0 Jul29 ?        00:01:03 ./redis-server 127.0.0.1:7003 [cluster]

root      49261      1  0 Jul29 ?        00:01:03 ./redis-server 127.0.0.1:7004 [cluster]

root      49265      1  0 Jul29 ?        00:01:03 ./redis-server 127.0.0.1:7005 [cluster]

root      76733  75739  0 20:49 pts/2    00:00:00 grep --color=auto redis-server

创建集群

./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

(上面的‘1’表示为主的创建一个slave的)

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:7000

127.0.0.1:7001

127.0.0.1:7002

Adding replica 127.0.0.1:7003 to 127.0.0.1:7000

Adding replica 127.0.0.1:7004 to 127.0.0.1:7001

Adding replica 127.0.0.1:7005 to 127.0.0.1:7002

M: a08504e2272ddcb9383baf4258bd016d2161d260 127.0.0.1:7000

   slots:0-5460 (5461 slots) master

M: c5ec423721e193f1abb5926194189068d71f2966 127.0.0.1:7001

   slots:5461-10922 (5462 slots) master

M: e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 127.0.0.1:7002

   slots:10923-16383 (5461 slots) master

S: 37e0303c6eb605df80c10de82c4bd8d2f85ae80f 127.0.0.1:7003

   replicates a08504e2272ddcb9383baf4258bd016d2161d260

S: 71518be19ddd2a2ae53fe0f88112897654eb56de 127.0.0.1:7004

   replicates c5ec423721e193f1abb5926194189068d71f2966

S: a46dd45983767cc4e50d60baf99e2a9f304d740f 127.0.0.1:7005

   replicates e5db2e4845b7869dcbfbbc45bdf6ef22f1187867

Can I set the above configuration? (type ‘yes‘ to accept): yes

会打印出以上内容,输入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: a08504e2272ddcb9383baf4258bd016d2161d260 127.0.0.1:7000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: c5ec423721e193f1abb5926194189068d71f2966 127.0.0.1:7001

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: 71518be19ddd2a2ae53fe0f88112897654eb56de 127.0.0.1:7004

   slots: (0 slots) slave

   replicates c5ec423721e193f1abb5926194189068d71f2966

S: a46dd45983767cc4e50d60baf99e2a9f304d740f 127.0.0.1:7005

   slots: (0 slots) slave

   replicates e5db2e4845b7869dcbfbbc45bdf6ef22f1187867

M: e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 127.0.0.1:7002

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 37e0303c6eb605df80c10de82c4bd8d2f85ae80f 127.0.0.1:7003

   slots: (0 slots) slave

   replicates a08504e2272ddcb9383baf4258bd016d2161d260

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

致此,搭建成功

测试可用redis-cli -c -p 7001

127.0.0.1:7001> set name ceshi

OK

127.0.0.1:7001> get name

"ceshi"

查看主从信息

[[email protected] src]# redis-cli -p 7000 cluster nodes 

c5ec423721e193f1abb5926194189068d71f2966 127.0.0.1:7001 master - 0 1501473285629 2 connected 5461-10922

71518be19ddd2a2ae53fe0f88112897654eb56de 127.0.0.1:7004 slave c5ec423721e193f1abb5926194189068d71f2966 0 1501473285120 5 connected

a08504e2272ddcb9383baf4258bd016d2161d260 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460

a46dd45983767cc4e50d60baf99e2a9f304d740f 127.0.0.1:7005 slave e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 0 1501473284107 6 connected

e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 127.0.0.1:7002 master - 0 1501473282077 3 connected 10923-16383

37e0303c6eb605df80c10de82c4bd8d2f85ae80f 127.0.0.1:7003 slave a08504e2272ddcb9383baf4258bd016d2161d260 0 1501473283091 4 connected

[[email protected] src]# redis-cli -c -p 7005  

127.0.0.1:7005> get name

-> Redirected to slot [5798] located at 127.0.0.1:7001

"ceshi"

127.0.0.1:7001> exit

[[email protected] src]# redis-cli -c -p 7002  

127.0.0.1:7002> get name

-> Redirected to slot [5798] located at 127.0.0.1:7001

"ceshi"

127.0.0.1:7001> exit

[[email protected] src]# 

断点测试

去掉7000redis

[[email protected] src]# ./redis-cli -c -p 7004

127.0.0.1:7004> get name

"ceshi"

127.0.0.1:7004> 

可以取到值,再次查看状态信息

[[email protected] src]# ./redis-cli -p 7004 cluster nodes

71518be19ddd2a2ae53fe0f88112897654eb56de 127.0.0.1:7004 myself,master - 0 0 7 connected 5461-10922

c5ec423721e193f1abb5926194189068d71f2966 127.0.0.1:7001 slave 71518be19ddd2a2ae53fe0f88112897654eb56de 0 1501473934812 7 connected

37e0303c6eb605df80c10de82c4bd8d2f85ae80f 127.0.0.1:7003 master - 0 1501473937837 8 connected 0-5460

e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 127.0.0.1:7002 master - 0 1501473936828 3 connected 10923-16383

a08504e2272ddcb9383baf4258bd016d2161d260 127.0.0.1:7000 master,fail - 1501473837708 1501473836698 1 disconnected

a46dd45983767cc4e50d60baf99e2a9f304d740f 127.0.0.1:7005 slave e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 0 1501473938843 6 connected

再次启动7000redis

./redis-server /usr/local/src/cluster/7000/redis.conf 

[[email protected] src]# ./redis-cli -p 7004 cluster nodes

71518be19ddd2a2ae53fe0f88112897654eb56de 127.0.0.1:7004 myself,master - 0 0 7 connected 5461-10922

c5ec423721e193f1abb5926194189068d71f2966 127.0.0.1:7001 slave 71518be19ddd2a2ae53fe0f88112897654eb56de 0 1501474156543 7 connected

37e0303c6eb605df80c10de82c4bd8d2f85ae80f 127.0.0.1:7003 master - 0 1501474159068 8 connected 0-5460

e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 127.0.0.1:7002 master - 0 1501474156038 3 connected 10923-16383

a08504e2272ddcb9383baf4258bd016d2161d260 127.0.0.1:7000 slave 37e0303c6eb605df80c10de82c4bd8d2f85ae80f 0 1501474157049 8 connected

a46dd45983767cc4e50d60baf99e2a9f304d740f 127.0.0.1:7005 slave e5db2e4845b7869dcbfbbc45bdf6ef22f1187867 0 1501474158057 6 connected

可以看到已经自动成为slave了



以上是关于redis-cluster集群单机搭建的主要内容,如果未能解决你的问题,请参考以下文章

Linux详细搭建Redis-Cluster集群以及使用

redis-cluster集群搭建

redis系列:搭建redis-cluster集群

redis-cluster集群搭建

docker-compose搭建redis-cluster集群

redis集群redis-cluster搭建