1. Environment preparation
Linux system : CentOS7-2009
Three hosts :
192.168.64.70 端口7000-7002
192.168.64.71 端口7000-7002
192.168.64.72 端口7000-7002
redis:redis-5.0.13
Redis download address: http://download.redis.io/releases/
After downloading, upload it to each Linux server. The author put it in the path of /usr/local/
2. Redis cluster preparation configuration
After uploading the redis compressed package, you can start building, perform the following operations
# 进入redis上传目录
cd /usr/local/
# 解压安装包
tar -zxvf redis-5.0.13.tar.gz
# 重命名redis目录
mv redis-5.0.13 redis
# 进入redis目录
cd redis/
# 编译(这里需要安装gcc,由于CentOs7自带gcc,因此可以直接编译)
make
# 安装(同样需要安装gcc,由于CentOs7自带gcc,因此可以直接编译)
make install
The successful compilation interface is as follows:
The successful installation interface is as follows:
If the above interface does not appear, the compilation and installation failed, and you need to check gcc
Next execute the command
# 创建cluster目录
mkdir cluster
# 进入cluster目录
cd cluster/
# 创建7000,7001,7002目录
mkdir 7000 7001 7002
# 返回上一级目录
cd ../
# 分别把该目录下的redis.conf配置文件复制到7000-7002目录下
cp redis.conf cluster/7000
cp redis.conf cluster/7001
cp redis.conf cluster/7002
Edit the configuration file redis.conf
- Annotate the local bind IP address
- Disable protected mode
-
Modify the port number
-
start background start
- Modify the pid file
- Modify the persistent file path
- Set memory optimization strategy
- Disable AOF mode
- Open cluster configuration
- Open the cluster configuration file
- Modify the cluster timeout
After editing a configuration file, copy the file directly to other folders
cd /usr/local/redis
cp cluster/7000/redis.conf cluster/7001
cp cluster/7000/redis.conf cluster/7002
And change the 7000 part of the above configuration to 7001 and 7002 respectively
The redis cluster environment of such a machine is ready, because the configuration of other machines is the same. So just copy the configured configuration file of the first machine.
The copy command is as follows
# 复制192.168.64.70机器的配置文件到192.168.64.71,192.168.64.72机器
scp -r /usr/local/redis/cluster [email protected]:/usr/local/redis
# -r递归复制目录 复制的时候会弹出输入密码,输入即可
scp -r /usr/local/redis/cluster [email protected]:/usr/local/redis
The replication results are as follows:
start the redis cluster:
- Create a startup script in the redis cluster installation directory, the command is as follows
# 进入redis集群安装目录
cd /usr/local/redis/cluster/
# 创建启动脚本
vim start.sh
# 添加以下命令后保存
#!/bin/sh
redis-server 7000/redis.conf &
redis-server 7001/redis.conf &
redis-server 7002/redis.conf &
- Create a shutdown script
# 创建关闭脚本
vim shutdown.sh
# 添加以下命令后保存
#!/bin/sh
redis-cli -p 7000 shutdown &
redis-cli -p 7001 shutdown &
redis-cli -p 7002 shutdown &
3. Start redis cluster
# 启动脚本
sh start.sh
# 查看运行状态
ps -ef |grep redis
Similarly, we edited one machine and copied the script directly to the other two machines
scp /usr/local/redis/cluster/start.sh [email protected]:/usr/local/redis/cluster
scp /usr/local/redis/cluster/start.sh [email protected]:/usr/local/redis/cluster
scp /usr/local/redis/cluster/shutdown.sh [email protected]:/usr/local/redis/cluster
scp /usr/local/redis/cluster/shutdown.sh [email protected]:/usr/local/redis/cluster
3. Redis configuration cluster master and slave nodes
After the above configuration is completed, you can start to configure the master-slave node.
There are often commands on the Internet to create a cluster.
redis-cli --cluster create --cluster-replicas 1 192.168.64.70:7000 192.168.64.71:7000 192.168.64.72:7000 192.168.64.70:7001 192.168.64.71:7001 192.168.64.72:7001 192.168.64.70:7002 192.168.64.71:7002 192.168.64.72:7002
The general format of this command is as follows:
redis-cli --cluster create --cluster-replicas 每个主节点的从节点数量 机器ip+端口号(多台机器用逗号隔开)
In this way, the system will randomly assign master nodes according to the number of machines, and try to make the number of slave nodes of each master node reach the number of input commands
But if we want to manually enter the command to specify the master-slave relationship.
How to specify, according to the redis cluster master-slave node sharding operation principle, if a master node goes down, the entire shard will collapse, but the remaining nodes in the cluster mode can promote one of the slave nodes of the node to be the master node through election; and we have three Machine, in order to ensure that a certain machine is down, the cluster can still use the master-slave node allocation idea as follows:
master1 slave1_1 slave1_2
192.168.64.70:7000 192.168.64.71:7001 192.168.64.72:7002
master2 slave2_1 slave2_2
192.168.64.71:7000 192.168.64.72:7001 192.168.64.70:7002
master3 slave3_1 slave3_2
192.168.64.72:7000 192.168.64.70:7001 192.168.64.71:7002
The idea of executing the specified command is as follows
- First create a cluster with three master nodes and no slave nodes
- Use the command to add nodes to add slave nodes, so that their master nodes can be specified when adding, and the master-slave correspondence relationship can be established
The specific commands are as follows
- Create a master node with the following command:
redis-cli --cluster create --cluster-replicas 0 192.168.64.70:7000 192.168.64.71:7000 192.168.64.72:7000
- Add slave nodes:
redis-cli --cluster add-node 192.168.64.71:7001 192.168.64.70:7000 --cluster-slave --cluster-master-id *******
in:
- slave means to add a slave node
- cluster-master-id which master node to add to, the id is *****
- 192.168.64.71:7001 slave node to be added
- 192.168.64.70:7000 Any node in the original cluster
The cluster-master-id can be viewed with the following command:
# 进入任意一个redis节点
redis-cli -p 7000
# 查看集群信息
cluster nodes
Therefore, the complete command to create the master-slave node is as follows (note that the following command cluster-master-id is my machine, you need to query the actual cluster-master-id of your machine according to the query cluster-master-id command above):
# 创建具有三个主节点的集群,没有从节点
redis-cli --cluster create --cluster-replicas 0 192.168.64.70:7000 192.168.64.71:7000 192.168.64.72:7000
# 给主节点192.168.64.70:7000增加从节点
redis-cli --cluster add-node 192.168.64.71:7001 192.168.64.70:7000 --cluster-slave --cluster-master-id 8e45923f9c05948ca54cb0c70724a62805fd4585
redis-cli --cluster add-node 192.168.64.72:7002 192.168.64.70:7000 --cluster-slave --cluster-master-id 8e45923f9c05948ca54cb0c70724a62805fd4585
# 给主节点192.168.64.71:7000增加从节点
redis-cli --cluster add-node 192.168.64.72:7001 192.168.64.71:7000 --cluster-slave --cluster-master-id eee597ad3a6a4c9b6d360be4dc4e9c98460d6f4f
redis-cli --cluster add-node 192.168.64.70:7002 192.168.64.71:7000 --cluster-slave --cluster-master-id eee597ad3a6a4c9b6d360be4dc4e9c98460d6f4f
# 给主节点192.168.64.72:7000增加从节点
redis-cli --cluster add-node 192.168.64.70:7001 192.168.64.72:7000 --cluster-slave --cluster-master-id c1817049021a287cd56a058b75c8e754a7c4d449
redis-cli --cluster add-node 192.168.64.71:7002 192.168.64.72:7000 --cluster-slave --cluster-master-id c1817049021a287cd56a058b75c8e754a7c4d449