Step 1: Install Redis
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
Step two: modify the configuration, create a node
we now engage in six nodes, the three main Thrice,
Port regulations are 7001,7002,7003,7004,7005,7006
Redis_cluster we first create a new directory in the root directory, then create six directories under the directory,
7001,7002,7003,7004,7005,7006 respectively, for the presence of redis profile;
Here we want to use redis clusters, first redis modify configuration files redis.conf
mkdir redis_cluster New Directory
[root@localhost ~]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
First copy the configuration file to the 7001 directory
[root@localhost redis_cluster]# cd
[root@localhost ~]# cp redis-5.0.0/redis.conf redis_cluster/7001/
We modify this configuration file
force redis_cluster / 7001 / redis.conf
Modify several
port 7001 // six-node configuration files are 7001-7006
daemonize yes // redis background
pidfile /var/run/redis_7001.pid // pidfile file corresponding to 7001-7006
cluster-enabled yes // open clusters
cluster-config-file nodes_7001.conf // save the node configuration, automatically create, automatically updates the corresponding 7001-7006
cluster-node-timeout 5000 // cluster timeout, the node over this time did not respond to conclude that downtime
appendonly yes // storage, aof, save writes logged.
// The following can not write
# if the password is set, master and slave need to configure the following two parameters:
masterauth "sfsxxx" # connection master password
requirepass "jadsfx" # own password
general configuration example
port 7001 #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes_7001.conf #集群的配置 配置文件首次启动自动生成
cluster-node-timeout 5000 #超时时间 5秒
appendonly yes #aof日志开启 它会每次写操作都记录一条日志
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile /var/run/redis_7001.pid
After modification in 7001, we are arranged at 7001 and 7002-7006 configuration can be copied to the corresponding modify;
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/
[root@localhost ~]# vi redis_cluster/7002/redis.conf
[root@localhost ~]# vi redis_cluster/7003/redis.conf
[root@localhost ~]# vi redis_cluster/7004/redis.conf
[root@localhost ~]# vi redis_cluster/7005/redis.conf
[root@localhost ~]# vi redis_cluster/7006/redis.conf
Edit profiles 5 behind, the port, pidfile, cluster-config-file modifications to respectively;
The third step: Start redis six nodes
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf
Start six nodes
[Root @ localhost ~] # ps -ef | grip Redis
Find Next redis process
Step Four: Create the cluster
redis redis-trib.rb the official tool, the first step had already been in the room next to the bin;
However, prior to use to install ruby, and ruby connection and redis
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
redis-trib.rb create --replicas 1 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 (redis3.0版本用这个)
redis-cli --cluster create 127.0.0.1:7000 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 --cluster-replicas 1(redis5.0版本)
Results are as follows:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005
replicates d61e66e49e669b99d801f22f6461172696fdd1c9
S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
Can I set the above configuration? (type 'yes' to accept):
See the results of operation from the primary node is the node from 700,170,027,003 700,470,057,006 are
7001 slots are assigned to the hash 0-5460
7002 assigned to the hash tank is 5461-10922
7003 slots are assigned to the hash 10923-16383
Finally, ask if we accept the above settings, enter yes on acceptance, we enter yes
Then show:
>>> 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 127.0.0.1:7001)
M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
slots: (0 slots) slave
replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005
slots: (0 slots) slave
replicates d61e66e49e669b99d801f22f6461172696fdd1c9
M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
slots: (0 slots) slave
replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
The Create a hash slot configuration, as well as the success of the cluster can be used;
if there are mistakes:
. [ERR] the Node 127.0.0.1:7001 IS not empty the Either at The nodealready Knows OTHER Nodes (the Check with CLUSTER NODES) or in the contains some Key database 0.
solution is as follows:
1), the next need to add the node aof, rdb and other local backup file deletion;
2), while the new Node cluster configuration file deletion, namely: redis.conf you delete a file inside the cluster-config-file is located;
3), add a new node again or if an error, then register a new Node, / redis-cli-hx -p clearing the database:
127.0.0.1:7001> flushdb # empty the current database
. If the error [ERR] Not all 16384 slots appear are covered by nodes of the
solution is as follows:
Redis-trib.rb FIX 127.0.0.1:7001(3.0 version)
Redis-TRIB. rb check 127.0.0.1:7000(3.0 version)
Redis CLI-FIX 127.0.0.1:7001(5.0 --cluster version)
Redis-CLI --cluster Check 127.0.0.1:7001(5.0 version)
as long as the input node of the cluster to any i.e. It can automatically check all relevant nodes. You can look to see whether the corresponding output of each Master has slots, uneven distribution if it can use the following manner redistribution.
Multi-node cluster to build
a environment set up
under the same local area network to create two virtual machines or other machines in a local area network.
Step two: modify the configuration, create node
First we create three nodes in the virtual machine 192.168.0.5, port respectively 7001,7002,7003
Redis_cluster we first create a new directory in the root directory, and then create three directories under the directory,
7001,7002,7003, respectively, used to keep redis profile;
Here we want to use redis clusters, first redis modify configuration files redis.conf
mkdir redis_cluster New Directory
[root@localhost ~]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002 7003
First copy the configuration file to the 7001 directory
[root@localhost redis_cluster]# cd
[root@localhost ~]# cp redis-5.5.0/redis.conf redis_cluster/7001/
We modify this configuration file
force redis_cluster / 7001 / redis.conf
Modify several
port 7001 // six-node configuration files are 7001-7003
bind 192.168.0.5 // ip default ip to 127.0.0.1 needs to be changed to other nodes machine-accessible or not accessible when you create a cluster, and the cluster has a single difference
daemonize yes // redis background
pidfile /var/run/redis_7001.pid // pidfile file corresponding to 7001-7003
cluster-enabled yes // open clusters
cluster-config-file nodes_7001.conf // save the node configuration, automatically create, automatically updates the corresponding 7001-7003
cluster-node-timeout 5000 // cluster timeout, the node over this time did not respond to conclude that downtime
appendonly yes // storage, aof, save writes logged
After modification in 7001, we are arranged at 7001 and 7002-7003 configuration can be copied to the corresponding modify;
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/
[root@localhost ~]# vi redis_cluster/7002/redis.conf
[root@localhost ~]# vi redis_cluster/7003/redis.conf
[root@localhost ~]# vi redis_cluster/7004/redis.conf
5 behind the edit profile, the port, pidfile, cluster-config- file modifications to respectively;
Similarly 192.168.0.6 machine, but also engage in a directory redis_cluster, 7004,7005,7006 then create a directory,
the third step: start two machines six nodes
to check whether appendonly.aof and dump.rdp and node-7001-7006 in the conf file is deleted before the start, to avoid problems
192.168.0.5 machine
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
[root@localhost ~]# ps -ef | grep redis
root 2242 1 0 19:55 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7001 [cluster]
root 2252 1 0 19:59 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7002 [cluster]
root 2256 1 0 19:59 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7003 [cluster]
root 2260 2214 0 19:59 pts/0 00:00:00 grep --color=auto redis
192.168.0.6 machine
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf
[root@localhost ~]# ps -ef | grep redis
root 2347 1 0 20:31 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7004 [cluster]
root 2351 1 0 20:31 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7005 [cluster]
root 2355 1 0 20:31 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7006 [cluster]
root 2363 2270 0 20:32 pts/0 00:00:00 grep --color=auto redis
Description start all OK
Step four: Set up a firewall, open port cluster
Our two machines directly off the firewall
systemctl stop firewalld.service
Step 5: Create clusters
192.168.0.5 machine as a terminal cluster control
redis redis-trib.rb the official tool, the first step had already been in the room next to the bin;
However, prior to use to install ruby, and ruby connection and redis
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
Start creating machine 05
redis-cli --cluster create 192.168.0.5:7001 192.168.0.5:7002 192.168.0.5:7003 192.168.0.6:7004 192.168.0.6:7005 192.168.0.6:7006 --cluster-replicas 1
See the results of operation from the primary node is the node from 700,170,047,002 700,570,037,006 are
7001 slots are assigned to the hash 0-5460
7004 assigned to the hash tank is 5461-10922
7002 slots are assigned to the hash 10923-16383
Finally, ask if we accept the above settings, enter yes on acceptance, we enter yes
then:
The Create a hash successfully configured slot, and the cluster can be used;
Step Six: cluster test data
We connect any node, and then add a key:
redis redis-cli is the default client tools, plus `-c` parameters at startup, -p
specify the port, you can connect to the cluster.
Here we have to add -h to specify the machine IP
Any node connected to the port:
[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.5 -c -p 7002
192.168.0.5:7002>
It is connected to node 7002
192.168.1.5:7002> set xxx 'fadfa'
-> Redirected to slot [4038] located at 192.168.0.5:7001
OK
I said before Redis Cluster value allocation rules, so when the allocation key, it will use CRC16 ( 'my_name')% 16384 algorithm to calculate, which node will put this key, assigned here to the 4038slot allocated to 7001 (0- 5460) on the node. So have:
Redirected to slot [4038] located at 192.168.0.5:7001
We, are available from other cluster nodes to the data
[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.6 -c -p 7005
192.168.0.6:7005>
192.168.0.6:7005> get xxx
-> Redirected to slot [4038] located at 192.168.0.5:7001
"ADF"
192.168.0.5:7001>