Redis Cluster cluster construction (CentOS7 environment, Redis 6.0.9)

table of Contents

 

1. Introduction

Second, deploy Redis Cluster cluster

1. Compile the Redis file

2. Create 6 new folders and prepare

3. Copy redis execution file and conf

4. Modify the redis.conf file

5. Create a startup file start_all.sh

 6. Create a stop file stop_all.sh

7, start the node

8. Create a cluster

Third, verify that the cluster is working properly

references:


1. Introduction

In the stand-alone version of Redis, there is no communication between each Master, so we generally do Pre-sharding in a Jedis client or an agent such as Codis. According to CAP theory, the stand-alone version of Redis belongs to guarantee CP (Consistency & Partition-Tolerancy) at the expense of A (Availability), which means that Redis can ensure that all users see the same data (consistency, because Redis is not automatically redundant When there is a problem with network communication, the temporarily isolated subsystem can continue to operate (partition tolerance, because there is no direct relationship between Masters, no communication is required), but there is no guarantee that all requests will be possible when some nodes fail Responded (availability, if a Master node hangs up, then the fragmented data on it will be inaccessible). With the Cluster function, Redis has changed from a pure NoSQL memory database to a distributed NoSQL database, and the CAP model has also changed from CP to AP. In other words, through automatic sharding and redundant data, Redis has a truly distributed ability. If a node is down, because the data is backed up on other nodes, other nodes can continue to provide it on top of it. Service guarantees Availability. However, because of this, Redis cannot guarantee its strong consistency. This is also required by the CAP theory, and only two of the three can be chosen.

Second, deploy Redis Cluster cluster

1. Compile the Redis file

See the previous blog post for details

2. Create 6 new folders and prepare

[root@10 opt]# mkdir redis
[root@10 opt]# cd redis
[root@10 redis]# mkdir redis0
[root@10 redis]# mkdir redis1
[root@10 redis]# mkdir redis2
[root@10 redis]# mkdir redis3
[root@10 redis]# mkdir redis4
[root@10 redis]# mkdir redis5

3. Copy redis execution file and conf

[root@10 redis0]# cp ../../redis-6.0.9/bin/* .

[root@10 redis0]# cp ../../redis-6.0.9/etc/* .

[root@10 redis0]# cd ../redis1
[root@10 redis1]# cp ../redis0/* 

[root@10 redis1]# cd ../redis2
[root@10 redis2]# cp ../redis0/* .
[root@10 redis2]# cd ../redis3
[root@10 redis3]# cp ../redis0/* .
[root@10 redis3]# cd ../redis4
[root@10 redis4]# cp ../redis0/* .
[root@10 redis4]# cd ../redis5
[root@10 redis5]# cp ../redis0/* .
[root@10 redis5]# ls
dump.rdb         redis-benchmark  redis-check-rdb  redis.conf      redis-server
mkreleasehdr.sh  redis-check-aof  redis-cli        redis-sentinel  redis-trib.rb

4. Modify the redis.conf file

Modify the following four items (red part)

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 loopback interface address (this means Redis will only be able to
# accept client connections from the same host that it is running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
bind 192.168.56.101

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

 

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

################################ REDIS CLUSTER  ###############################

# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

Note: The port numbers in the 6 folders of the same IP should be different, here is 6379-6484, in actual use, the port numbers of different IPs can be set the same.

5. Create a startup file start_all.sh

In order to facilitate management, the startup file and the termination file are placed in the redis folder, and a copy of the redis-cli file is also copied

cd redis0
./redis-server redis.conf
cd ..
cd redis1
./redis-server redis.conf
cd ..
cd redis2
./redis-server redis.conf
cd ..
cd redis3
./redis-server redis.conf
cd ..
cd redis4
./redis-server redis.conf
cd ..
cd redis5
./redis-server redis.conf
cd ..

 6. Create a stop file stop_all.sh

./redis1/redis-cli -p 6379 shutdown
./redis1/redis-cli -p 6380 shutdown
./redis1/redis-cli -p 6381 shutdown
./redis1/redis-cli -p 6382 shutdown
./redis1/redis-cli -p 6383 shutdown
./redis1/redis-cli -p 6384 shutdown

7, start the node

./start_all.sh executes the startup file, after executing it, you can check the startup status through ps

[root@10 redis]# ./start_all.sh
[root@10 redis]# ps -ef | grep redis
root     14959     1  0 08:18 ?        00:00:07 ./redis-server 192.168.56.101:6379 [cluster]
root     14965     1  0 08:18 ?        00:00:07 ./redis-server 192.168.56.101:6380 [cluster]
root     14971     1  0 08:18 ?        00:00:07 ./redis-server 192.168.56.101:6381 [cluster]
root     14977     1  0 08:18 ?        00:00:07 ./redis-server 192.168.56.101:6382 [cluster]
root     14983     1  0 08:18 ?        00:00:07 ./redis-server 192.168.56.101:6383 [cluster]
root     14989     1  0 08:18 ?        00:00:07 ./redis-server 192.168.56.101:6384 [cluster]
root     18204 10810  0 09:18 pts/1    00:00:00 grep --color=auto redis

8. Create a cluster

Execute ./redis-cli --cluster create 192.168.56.101:6379 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382 192.168.56.101:6383 192.168.56.101:6384 --cluster-replicas 1 command to create a cluster

[root@10 redis]# ./redis-cli --cluster create 192.168.56.101:6379 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382 192.168.56.101:6383 192.168.56.101:6384 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.56.101:6383 to 192.168.56.101:6379
Adding replica 192.168.56.101:6384 to 192.168.56.101:6380
Adding replica 192.168.56.101:6382 to 192.168.56.101:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 522f0dca780073ab668bc80ded5a1489e2febe3e 192.168.56.101:6379
   slots:[0-5460] (5461 slots) master
M: f65edec341cdc39ded3ecd5298c92f3192c8afcd 192.168.56.101:6380
   slots:[5461-10922] (5462 slots) master
M: 1b55615b201a68db64db210381d9a4dbaaf6b17f 192.168.56.101:6381
   slots:[10923-16383] (5461 slots) master
S: 3f9ff65caf28e79864f14f1996e8b5ecd136959a 192.168.56.101:6382
   replicates f65edec341cdc39ded3ecd5298c92f3192c8afcd
S: 5a595b9da405a27a097f9756008b47414e2da1f7 192.168.56.101:6383
   replicates 1b55615b201a68db64db210381d9a4dbaaf6b17f
S: 204bc647266c4c369ebe45d4ba0755c28543494e 192.168.56.101:6384
   replicates 522f0dca780073ab668bc80ded5a1489e2febe3e
Can I set the above configuration? (type 'yes' to accept): 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 192.168.56.101:6379)
M: 522f0dca780073ab668bc80ded5a1489e2febe3e 192.168.56.101:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: f65edec341cdc39ded3ecd5298c92f3192c8afcd 192.168.56.101:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 1b55615b201a68db64db210381d9a4dbaaf6b17f 192.168.56.101:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 204bc647266c4c369ebe45d4ba0755c28543494e 192.168.56.101:6384
   slots: (0 slots) slave
   replicates 522f0dca780073ab668bc80ded5a1489e2febe3e
S: 3f9ff65caf28e79864f14f1996e8b5ecd136959a 192.168.56.101:6382
   slots: (0 slots) slave
   replicates f65edec341cdc39ded3ecd5298c92f3192c8afcd
S: 5a595b9da405a27a097f9756008b47414e2da1f7 192.168.56.101:6383
   slots: (0 slots) slave
   replicates 1b55615b201a68db64db210381d9a4dbaaf6b17f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Note 1: Many on the network use redis-trib.rb to create clusters, but after redis5.0, the support of the ruby ​​script redis-trib.rb is cancelled (the way to add clusters manually through the command line remains unchanged), and the collection is created to redis In -cli, the environment related to ruby ​​installation is avoided. Use the redis-clit parameter --cluster directly instead. The error message is as follows:

[root@10 redis]# ./redis-trib.rb create --replicas 1 192.168.56.101:6379 192.168.56.101:6380 192.168.56.101:6381 192.92.168.56.101:6383 192.168.56.101:6384
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

Example:
redis-cli --cluster create 192.168.56.101:6379 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382 192.168.56.56.101:6384 --cluster-replicas 1

To get help about all subcommands, type:
redis-cli --cluster help

Note 2: When creating a cluster, the connection error is prompted as follows

[root@10 redis]# redis-cli --cluster create 192.168.56.101:6379 192.168.56.101:6380 192.168.56.101:6381 192.168.56.10.101:6383 192.168.56.101:6384 --cluster-replicas 1
Could not connect to Redis at 192.168.56.101:6379: Connection refused

Solution: Check if the bind of the Redis.conf file in step 4 has a bound IP, here is 192.168.56.101

Note 3: The copied executable file reports an error when creating the cluster, the error message is as follows:

[root@10 redis]# ./redis-cli --cluster create 192.168.56.101:6379 192.168.56.101:6380 192.168.56.101:6381 192.168.56.10.101:6383 192.168.56.101:6384 --cluster-replicas 1
[ERR] Node 192.168.56.101:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

Solution: delete appendonly.aof, dump.rdb, node_xxx.conf files in each node, log in to each redis node with redis-cli -c -h -p, execute the following command, and then create a cluster.

[root@10 redis]# ./redis0/redis-cli -c -h 192.168.56.101 -p 6379
192.168.56.101:6379> flushdb
OK
192.168.56.101:6379> cluster reset
OK
192.168.56.101:6379> exit

Third, verify that the cluster is working properly

[root@10 redis]# ./redis1/redis-cli -c -h 192.168.56.101 -p 6379
192.168.56.101:6379> cluster nodes
f65edec341cdc39ded3ecd5298c92f3192c8afcd 192.168.56.101:6380@16380 master - 0 1607262213000 2 connected 5461-10922
1b55615b201a68db64db210381d9a4dbaaf6b17f 192.168.56.101:6381@16381 master - 0 1607262213000 3 connected 10923-16383
204bc647266c4c369ebe45d4ba0755c28543494e 192.168.56.101:6384@16384 slave 522f0dca780073ab668bc80ded5a1489e2febe3e 0 1nnected
3f9ff65caf28e79864f14f1996e8b5ecd136959a 192.168.56.101:6382@16382 slave f65edec341cdc39ded3ecd5298c92f3192c8afcd 0 1nnected
5a595b9da405a27a097f9756008b47414e2da1f7 192.168.56.101:6383@16383 slave 1b55615b201a68db64db210381d9a4dbaaf6b17f 0 1nnected
522f0dca780073ab668bc80ded5a1489e2febe3e 192.168.56.101:6379@16379 myself,master - 0 1607262213000 1 connected 0-5460
192.168.56.101:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1493
cluster_stats_messages_pong_sent:1464
cluster_stats_messages_sent:2957
cluster_stats_messages_ping_received:1459
cluster_stats_messages_pong_received:1493
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2957
 

 

references:

1、https://blog.csdn.net/truelove12358/article/details/79612954

2 、https://www.cnblogs.com/zhoujinyi/p/11606935.html

3、https://blog.csdn.net/miss1181248983/article/details/90056960

 

Guess you like

Origin blog.csdn.net/xlyrh/article/details/110776381