Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

First, install the standalone Redis

Installation and configuration:

Service IP Redis installation directory
192.168.211.107 / usr/local/soft/redis – 6.2.4

Step 1: Create a data directory

Create different data directories

CD /usr/local/sof/redis-6.2.4/mkdir redis-cluster CD redis-cluster/ mkdir 6319 6329 6339 6349 6359 6369Copy the code

Step 2: Modify the configuration file

Copy the Redis configuration file redis.conf to the first folder you created

Cp/usr/local/soft/redis - 6.2.4 / redis. Conf/usr/local/soft/redis - 6.2.4 / redis cluster / 6319Copy the code

Modifying a Configuration File

CD/usr/local/soft/redis - 6.2.4 / redis cluster / 6319 vim redis. ConfCopy the code

The following configuration file configuration items if you do not understand, you can go to my standalone version of the Redis instance to view, search these configurations can exit the edit mode using/XXX (oblique bar + partial characters) to search, or directly from the server to modify

Port 6319 protected-mode no daemonize yes dir "/usr/local/soft/redis-6.2.4/redis-cluster/6319/" cluster-enabled yes cluster-config-file nodes-6319.conf cluster-node-timeout 15000 appendonly yes pidfile "/var/run/redis_6319.pid"Copy the code

The following configuration needs to be added for the extranet cluster

# cluster-announce-ip xx.xx.xx.xx # node mapping port cluster-announce-port ${port} # node bus port cluster-announce-bus-port ${PORT}Copy the code

Copy the configuration files to the remaining five created directories

CD /usr/local/sof/redis-6.2.4/redis-cluster/6319/cp redis.conf.. /6329/ cp redis.conf .. /6339/ cp redis.conf .. /6349/ cp redis.conf .. /6359/ cp redis.conf .. / 6369 /Copy the code

Sed -i ‘s/ Old string/new string /’/XXX /xx.xx

CD /usr/local/soft/redis-6.2.4/redis-cluster/ sed -i 's/6319/6329/g' 6329/redis. Conf sed -i 's/6319/6339/g' 6339/redis.conf sed -i 's/6319/6349/g' 6349/redis.conf sed -i 's/6319/6359/g' 6359/redis.conf sed -i 's/6319/6369/g' 6369/redis.confCopy the code

Step 3: Start the node

Start six Redis nodes

./src/redis-server redis-cluster/6319/redis.conf
./src/redis-server redis-cluster/6329/redis.conf
./src/redis-server redis-cluster/6339/redis.conf
./src/redis-server redis-cluster/6349/redis.conf
./src/redis-server redis-cluster/6359/redis.conf
./src/redis-server redis-cluster/6369/redis.conf

ps -ef|grep redis
Copy the code

Step 4: Create a cluster

Start the cluster using an absolute IP address

CD /usr/local/sof/redis-6.2.4/src-redis-cli --cluster create 192.168.211.107:6319 192.168.211.107:6329 192.168.211.107:6339 192.168.211.107:6349 192.168.211.107:6359 192.168.211.107:6369 --cluster-replicas 1Copy the code

Redis allocates 3 master and 3 slave nodes to the 6 nodes, which we confirm directly by yes The slot allocation diagram, recorded here, is useful for subsequent tests

node IP Scope of slot
Master[0]
192.168.211.107:6319 Slots 0 – 5460
Master[1] 192.168.211.107:6329 Slots 5461 – 10922
Master[2] 192.168.211.107:6339 Slots 10923 – 16383

The cluster is created.

Step 5: Test the cluster

Insert keys in batches to test whether cluster nodes can properly create scripts based on key distribution

CD/usr/local/soft/redis - 6.2.4 / redis cluster/vim batchKeyInsert. ShCopy the code

** Redis -cli -h {host} -p {port} {command} is a client connection to execute the command

Redis -cli -h 192.168.211.107 -p 6319 -c -x set name$I >>redis.log

**-c **

Used when connecting cluster nodes. This option prevents moved and Ask exceptions

**-x **

Represents reading data from standard input as the last argument to the command

#! /bin/bash for((i=0; i<100000; I++) do echo - en "on, I love the Java" | redis - 192.168.211.107 cli - h - p - c - 6319 x the set name $I > > redis. The log is doneCopy the code

File Permissions

chmod +x batchKeyInsert.sh
Copy the code

Executing the script (takes a while)

./batchKeyInsert.sh
Copy the code

Access the three primary nodes, connect to the client, and view the data distribution of the nodes

CD /usr/local/sof/redis-6.2.4/src redis-cli -p 6319 dbsizeCopy the code

The node data is evenly distributed and the cluster deployment is successful.