Home > Database > Redis > body text

How to build and use redis5 cluster under Centos7

WBOY
Release: 2023-06-01 11:37:06
forward
1562 people have browsed it

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="https://img.php.cn/upload/article/000/887/227/168559062896660.jpg" 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!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!