Redis original single multi-node cluster structures

Before the clustering process must take the suffix aof, rdb of redis data storage file

Delete (or backup to a different folder, if not the / root / under it), no

Cluster will fail.

Step 1: Install Redis (redis installation)

Reids installation package there is a cluster tool, to be copied to / usr / local / bin to go

cp redis-3.2.9/src/redis-trib.rb /usr/local/bin

Step two: modify the configuration, create node

We now engage in six nodes, the three main Thrice,

Port regulations are 7001,7002,7003,7004,7005,7006

Redis_cluster we first create a new directory in the root directory, then create six directories under the directory,

7001,7002,7003,7004,7005,7006 respectively, for the presence of redis profile;

Here we want to use redis clusters, first redis modify configuration files redis.conf

mkdir redis_cluster New Directory

[root@localhost ~]# cd redis_cluster/

[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006

[root@localhost redis_cluster]# ll

0 Total amount

drwxr-xr-x. 2 root root 6 7月  27 17:18 7001

drwxr-xr-x. 2 root root 6 7月  27 17:18 7002

drwxr-xr-x. 2 root root 6 7月  27 17:18 7003

drwxr-xr-x. 2 root root 6 7月  27 17:18 7004

drwxr-xr-x. 2 root root 6 7月  27 17:18 7005

drwxr-xr-x. 2 root root 6 7月  27 17:18 7006

[root@localhost redis_cluster]# 

First copy the configuration file to the 7001 directory

[root@localhost redis_cluster]# cd

[root@localhost ~]# cp redis-3.2.9/redis.conf redis_cluster/7001/

We modify this configuration file

force redis_cluster / 7001 / redis.conf

Modify several

port 7001 // six-node configuration files are 7001-7006

daemonize yes // redis background

pidfile /var/run/redis_7001.pid // pidfile file corresponding to 7001-7006

cluster-enabled yes // open clusters

cluster-config-file nodes_7001.conf // save the node configuration, automatically create, automatically updates the corresponding 7001-7006

cluster-node-timeout 5000 // cluster timeout, the node over this time did not respond to conclude that downtime

appendonly yes // storage, aof, save writes logged

After modification in 7001, we are arranged at 7001 and 7002-7006 configuration can be copied to the corresponding modify;

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/

[root@localhost ~]# vi redis_cluster/7002/redis.conf 

[root@localhost ~]# vi redis_cluster/7003/redis.conf 

[root@localhost ~]# vi redis_cluster/7004/redis.conf 

[root@localhost ~]# vi redis_cluster/7005/redis.conf 

[root@localhost ~]# vi redis_cluster/7006/redis.conf 

Edit profiles 5 behind, the port, pidfile, cluster-config-file modifications to respectively;

The third step: Start redis six nodes

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

Start six nodes

[Root @ localhost ~] # ps -ef | grip Redis  

Find Next redis process

root       9501      1  0 17:38 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9512      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]

root       9516      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9524      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]

root       9528      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

Description have launched successful

Step Four: Create the cluster

redis redis-trib.rb the official tool, the first step had already been placed under the bin;

However, prior to use to install ruby, and ruby ​​connection and redis

yum -y install ruby ruby-devel rubygems rpm-build

Installation errors may occur redis requires Ruby version> = 2.2.2 issue here

solution

gem install redis

Create a cluster

[root@localhost ~]# redis-trib.rb create --replicas 1  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 127.0.0.1:7006

>>> Creating cluster

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

Using 3 masters:

127.0.0.1:7001

127.0.0.1:7002

127.0.0.1:7003

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

Adding replica 127.0.0.1:7006 to 127.0.0.1:7003

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   replicates d61e66e49e669b99d801f22f6461172696fdd1c9

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

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

See the results of operation from the primary node is the node from 700,170,027,003 700,470,057,006 are 

7001 slots are assigned to the hash 0-5460

7002 assigned to the hash tank is 5461-10922

7003 slots are assigned to the hash 10923-16383

Finally, ask if we accept the above settings, enter yes on acceptance, we enter yes

Then show:

>>> 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:7001)

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   slots: (0 slots) slave

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   slots: (0 slots) slave

   replicates d61e66e49e669b99d801f22f6461172696fdd1c9

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   slots: (0 slots) slave

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

Display configuration hash slot, and create a cluster, you can use;

Step five: Test the cluster data

We connect any node, and then add a key:

redis redis-cli is the default client tools, plus `-c` parameters when starting, `-p` designated port, you can connect to the cluster. 

Any node connected to the port:

[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7002

127.0.0.1:7002> 

We connect 7002

127.0.0.1:7002> set xxx  'fdafda'

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

OK

I said before Redis Cluster value allocation rules, so when the allocation key, it will use CRC16 ( 'my_name')% 16384 algorithm to calculate, which node will put this key, assigned here to the 4038slot allocated to 7001 (0- 5460) on the node. So have:

Redirected to slot [4038] located at 127.0.0.1:7001

We, are available from other cluster nodes to the data

127.0.0.1:7001> exit

[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7005

127.0.0.1:7005> get xxx

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

"fdafda"

127.0.0.1:7001> 

Step Six: cluster downtime test

If we get rid of a node, such as the master node 7002

[Root @ localhost ~] # ps -ef | grip Redis

root       9501      1  0 17:38 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9512      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]

root       9516      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9524      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]

root       9528      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

root       9601   2186  0 18:12 pts/0    00:00:00 grep --color=auto redis

[root@localhost ~]# kill -9 9512

[Root @ localhost ~] # ps -ef | grip Redis

root       9501      1  0 17:38 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9516      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9524      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]

root       9528      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

root       9603   2186  0 18:12 pts/0    00:00:00 grep --color=auto redis

[root@localhost ~]# 

Then look at the cluster of cases

redis-trib.rb check 127.0.0.1:7001

>>> Performing Cluster Check (using node 127.0.0.1:7001)

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   slots: (0 slots) slave

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

M: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   slots:5461-10922 (5462 slots) master

   0 additional replica(s)

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   slots: (0 slots) slave

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

We found that 7005 could have been from the node, as the master of his corresponding hung up, it automatically becomes the master node master, all will have a final explanation

 All 16384 slots covered all hash slots can be covered; Cluster can be used normally;

If we have to get rid of 7005, give it a try

[root@localhost ~]# kill -9 9524

[Root @ localhost ~] # ps -ef | grip Redis

root       9501      1  0 17:38 ?        00:00:03 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9516      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:03 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9528      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

root       9610   2186  0 18:16 pts/0    00:00:00 grep --color=auto redis

[root@localhost ~]# 

View clustered cases

redis-trib.rb check 127.0.0.1:7001

Here we find something went wrong, because the master and slave nodes are linked so there did not have to allocate a portion of hash slots, the last sentence

[ERR] Not all 16384 slots are covered by nodes without security coverage.;

You can not properly use the cluster;


 

Published 51 original articles · won praise 9 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_39891030/article/details/89351955