redis clusters require at least three master node, we are here to build three master node, and then set up a master to each slave node, redis total of six nodes, where one machine (can deploy multiple machines, modify ip address can be a) deploy 6 redis example, three leading and three from the step of building a cluster as follows:
Step 1: Create a folder redis-cluster at a first machine / usr / local / redis, and then create the six clip files thereunder as follows:
mkdir -p /usr/local/redis/redis-cluster
Enter redis-cluster directory, create a folder 6
mkdir 7001 7002 7003 7004 7005 7006
Step two: before redis.conf configuration file copy to 7001, the following modifications:
cp /usr/local/redis/redis-5.0.5/redis.conf /usr/local/redis/redis-cluster/7001
Description: redis-5.0.5 source package is unpacked
Modify the following configuration:
. 1 to daemonize Yes Redis background // 2 Port 7001 (port number are set for each of the machine) . 3 PidFile /var/run/redis_7001.pid // ... 7006 PidFile file corresponding 7000,7002,7003 . 4 Cluster- enabled yes // Enable the cluster to remove the comment # 5 cluster Nodes-config-file-7001.conf // cluster configuration starts automatically generate configuration file for the first time ... 7006 7001,7002,7003 6 cluster-the Node-timeout 5000 // request timeout setting 5 seconds is enough . 7 appendOnly Yes // log AOF necessary to turn on, each time it writes a log is recorded . 8 bind 127.0.0.1 (ip remove bind bound access information) . 9 protected-MODE NO ( protected mode off) 10 #dir / usr / local / redis / redis-cluster / 7001 / (specify the data file location, you must specify a different directory location, or you'll lose data if the configuration does not exist, add their own)
If you want to set a password needs to be increased as follows:
11 requirepass xxx (redis set an access password) 12 masterauth xxx (between cluster nodes to access the password setting is consistent with the above)
The third step: the modified configuration file, copy to 8002-8006, modify item 2,3,5 in the port number can be replaced with a batch:
First copy:
1 cp /usr/local/redis/redis-cluster/7001/redis.conf /usr/local/redis/redis-cluster/7002 2 cp /usr/local/redis/redis-cluster/7001/redis.conf /usr/local/redis/redis-cluster/7003 3 cp /usr/local/redis/redis-cluster/7001/redis.conf /usr/local/redis/redis-cluster/7004 4 cp /usr/local/redis/redis-cluster/7001/redis.conf /usr/local/redis/redis-cluster/7005 5 cp /usr/local/redis/redis-cluster/7001/redis.conf /usr/local/redis/redis-cluster/7006
Then respectively corresponding to the directory to perform vim redis.conf
% S / String source / destination string / g
:%s/7001/7002/g
:%s/7001/7003/g
:%s/7001/7004/g
:%s/7001/7005/g
:%s/7001/7006/g
Replace 7002 to 7001
Replace and consistent global directory, such as directory is 7003 replaced 7003
Step four: six were started redis example, then check whether a successful start
1 /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7001/redis.conf 2 /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7002/redis.conf 3 /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7003/redis.conf 4 /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7004/redis.conf 5 /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7005/redis.conf 6 /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7006/redis.conf
Check whether a successful start
Use ps-ef | grep redis look at whether to start a successful six nodes
ps -ef | grep Redis
Step 5: Create the entire cluster with redis redis-cli (redis5 previous version of the cluster is to rely ruby script redis-trib.rb achieve)
Excuting an order
/usr/local/redis/bin/redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1
Description: Create a ruby do not need a redis 5.x cluster environment
--cluster-replicas 1 命令的意思是创建master的时候同时创建一个slave
It creates a cluster 3master 3slaver
Enter yes
This shows that clusters are seen to create a finished
Step Seven: Verifying the cluster:
1) can be connected to any one of the client:
./redis-cli -c -a xxx -h 192.168.5.100 -p 7001
Tip: -a password to access the server, -c represent cluster model, specify the ip address and port number (if there is no password, -a xxx can be omitted)
Execute this command:
Open port 7001 client set a value
./redis-cli -c -p 7001
The exit node, enter port 7002:
We found that 7002 port can also be value
2) to verify: cluster info (view cluster information), cluster nodes (see list of nodes)
3) Data verifies
4) Shut down the cluster will need to shut down one by one, using the command :( template)
/usr/local/redis/src/redis-cli -a xxx -c -h 192.168.0.60 -p 8001 shutdown
Execute the following command:
/usr/local/redis/bin/redis-cli -c -p 7001 shutdown
Description 7001 is off
Tip: After building a cluster, they can still make the main operation, the data from the backup copy.
reference:
https://www.jianshu.com/p/8045b92fafb2
https://blog.csdn.net/qq_36514588/article/details/83856795
https://blog.csdn.net/woyixinyiyi/article/details/87967911
https://blog.csdn.net/hoopopo_wll/article/details/84925563
https://my.oschina.net/ruoli/blog/2252393 (redis5 提供了关闭集群的工具)
在这里非常感谢以上作者的劳动果实。