Table of Contents
1. Brief description
2. Steps to create a cluster
2.1. Create a directory
2.2. Download the source code and unzip and compile it
3. Create 6 redis configuration files
4, start the node
5. Start the cluster
6. Cluster operation
6.1. Shut down the cluster
6.2, Restart the cluster
6.3. Use script File startup cluster
7. Execute
Home Database Redis How to build and use redis5 cluster under Centos7

How to build and use redis5 cluster under Centos7

Jun 01, 2023 am 11:37 AM
redis centos7

1. Brief description

There should be at least three nodes in the cluster, and each node has a backup node. 6 servers are required.

If conditions are limited, you can build a pseudo-distributed cluster. The following steps are to build a redis cluster with 6 nodes on a Linux server.

2. Steps to create a cluster

2.1. Create a directory

New directory: mkdir /usr/local /redis-cluster

2.2. Download the source code and unzip and compile it

wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make
make install prefix=/usr/local/redis
Copy after login

3. Create 6 redis configuration files

The six configuration files cannot be in the same directory. Here we define them as follows:

/root/software/redis/redis-cluster-conf/7001/redis.conf
/ root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-cluster-conf/7003/redis.conf
/root/software/redis/redis- cluster-conf/7004/redis.conf
/root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-cluster-conf/7006/redis. conf

Some operation commands are for reference only:

cp redis.conf /usr/local/redis/bin
cd /usr/local/redis/
cp -r bin ../redis-cluster/redis01
cd /usr/local/redis-cluster/redis01
rm dump.rdb #删除快照
vim redis.conf
Copy after login

The content of the configuration file is:

port 7001 #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile /var/run/redis_7001.pid
bind 172.20.10.7 #127.0.0.1改为本机ip地址,可用ifconfig查看ip
Copy after login

The port and pidfile need to be adjusted according to the folder. .

Create the remaining 5 instances:

[root@master redis-cluster]# cp -r redis01/ redis02
[root@master redis-cluster]# cp -r redis01/ redis03
[root@master redis-cluster]# cp -r redis01/ redis04
[root@master redis-cluster]# cp -r redis01/ redis05
[root@master redis-cluster]# cp -r redis01/ redis06
Copy after login

Modify the port and pidfile under redis.conf of redis02 ~ redis06 respectively

4, start the node

Enter the redis01, redis02,...redis06 directories respectively and execute: ./redis-server ./redis.conf

Create a batch file and start six redis

at the same time

vim startall.sh

Add the following content:

cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..
Copy after login

Then execute chmod u x start-all.shwillstart -all.shBecome an executable file

Start in the current directory: ./startall.sh

View:ps aux|grep redis

<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/887/227/168559062896660.jpg" class="lazy" alt="How to build and use redis5 cluster under Centos7" />

5. Start the cluster

Because we are using version 5.0.0 To build a cluster with redis, you only need to copy the redis-cli file in the compiled redis directory to the redis-cluster directory. (After redis version 5.0, it is started directly in C language)

/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 172.20. 10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas 1

After starting, you can see the success message, as follows:

>>> performing hash slots allocation on 6 nodes...
master[0] -> slots 0 - 5460
master[1] -> slots 5461 - 10922
master[2] -> slots 10923 - 16383
adding replica 172.20.10.7:7004 to 172.20.10.7:7001
adding replica 172.20.10.7:7005 to 172.20.10.7:7002
adding replica 172.20.10.7:7006 to 172.20.10.7:7003
>>> trying to optimize slaves allocation for anti-affinity
[warning] some slaves are in the same host as their master
m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001
 slots:[0-5460] (5461 slots) master
m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002
 slots:[5461-10922] (5462 slots) master
m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003
 slots:[10923-16383] (5461 slots) master
s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004
 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0
s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005
 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26
s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006
 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413
can i set the above configuration? (type &#39;yes&#39; to accept):yes
Copy after login

Enter yes and press Enter

>>> 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 172.20.10.7:7001)
m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001
 slots:[0-5460] (5461 slots) master
 1 additional replica(s)
m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002
 slots:[5461-10922] (5462 slots) master
 1 additional replica(s)
s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004
 slots: (0 slots) slave
 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0
m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003
 slots:[10923-16383] (5461 slots) master
 1 additional replica(s)
s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005
 slots: (0 slots) slave
 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26
s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006
 slots: (0 slots) slave
 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.
Copy after login

At this point, the reids5 cluster is completed.

6. Cluster operation

6.1. Shut down the cluster

Method 1:

Provided by redis5 Tools for shutting down the cluster are in the following directory:

/root/redis-5.0.0/utils/create-cluster

Open this file and modify the port to our own , as shown below:

How to build and use redis5 cluster under Centos7

The port prot is set to 7000, nodes is 6, and the tool will automatically accumulate 1 to generate six nodes 7001-7006 for operation.

Look down and modify the path and add the IP address. If not added, it will default to the local 127.0.0.1

How to build and use redis5 cluster under Centos7

After modification, execute the following command to shut down the cluster:

/root/redis-5.0.0/utils/create-cluster/create-cluster stop

Method 2:

create-cluster directory Write a script file: vim shutdown.sh
The content is as follows:

/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7001 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7002 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7003 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7004 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7005 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7006 shutdown
Copy after login

Then execute chmod u x shutdown.sh to turn shutdown.sh into an executable file

Start in the current directory: ./shutdown.sh

View: ps aux|grep redis

Official:/usr/local/redis-cluster/redis-cli -a xxx -c - h 192.168.5.100 -p 8001

Tips: -a access server password, -c indicates cluster mode, -h specifies the ip address, -p specifies the port number

6.2, Restart the cluster

/root/redis-5.0.0/utils/create-cluster/create-cluster start

6.3. Use script File startup cluster

vim startall.sh Append the following content: (Remember to change your own IP address)

/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 172.20.10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas
Copy after login

Start: ./startall.sh

7. Execute

redis01/redis-cli -h 192.168.25.153 -p 7002 -c

## in the test cluster

redis-cluster directory #Where -c indicates connecting to redis in cluster mode, -h specifies the ip address, and -p specifies the port number

cluster nodes Query cluster node information

cluster info Query cluster status information

The above is the detailed content of How to build and use redis5 cluster under Centos7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles