Redis cluster construction and simple use
Introduce the installation environment and version
Simulate 6 nodes with two virtual machines and 3 nodes with one machine to create 3 master and 3 salve environments.
Redis uses redis-3.2.4 version.
Both virtual machines are CentOS, one CentOS6.5 (IP: 192.168.31.245), and one CentOS7 (IP: 192.168.31.210).
Installation process
1. Download and unzip
cd
/root/software
wget http:
//download
.redis.io
/releases/redis-3
.2.4.
tar
.gz
tar
-zxvf redis-3.2.4.
tar
.gz
2. Compile and install
cd redis-3.2.4 make && make install
3. Copy redis-trib.rb to / usr / local / bin directory
cd src
cp redis-trib.rb /usr/local/bin/
4. Create a Redis node
First create the redis_cluster directory under the /root/software/redis-3.2.4 directory on the 192.168.31.245 machine;
mkdir redis_cluster
Under the redis_cluster directory, create directories named 7000, 7001, 7002, and copy redis.conf to these three directories
mkdir 7000 7001 7002<br>cp redis.conf redis_cluster/7000 cp redis.conf redis_cluster/7001 cp redis.conf redis_cluster/7002
Modify these three configuration files separately, modify the following
port 7000 // port 7000,7002,7003 bind native ip // default ip is 127.0.0.1 It needs to be changed to the ip that other node machines can access otherwise the corresponding port cannot be accessed when creating the cluster, the cluster cannot be created daemonize yes // redis Run pidfile / var /run/redis_7000.pid in the background // pidfile file corresponds to 7000, 7001, 7002 cluster-enabled yes // open the cluster and comment # remove cluster-config-file nodes_7000.conf // the cluster configuration configuration file is started for the first time Automatically generate 7000,7001,7002 cluster-node-timeout 15000 // Request timeout is 15 seconds by default, you can set appendonly yes //AOF log is turned on if necessary, it will record a log for each write operation
- Then on another machine (192.168.31.210), repeat the above three steps, but change the directory to 7003, 7004, 7005, and the corresponding configuration file can be modified according to this rule.
5. Start each node
Execute redis -server redis_cluster / 7000 / redis.conf redis -server redis_cluster / 7001 / redis.conf redis -server redis_cluster / 7002 / redis.conf on the first machine and execute redis -server redis_cluster / 7003 / redis on the other machine .conf redis -server redis_cluster / 7004 / redis.conf redis -server redis_cluster / 7005 /redis.conf
6. Check the redis startup
## 一机 <br> ps -ef | grep redis root 61020 1 0 02 : 14 ? 00 : 00 : 01 redis-server 127.0 . 0.1 : 7000 [cluster] root 61024 1 0 02 : 14 ? 00 : 00 : 01 redis-server 127.0 . 0.1 : 7001 [cluster] root 61029 1 0 02 : 14 ? 00:00:01 redis-server 127.0.0.1:7002 [cluster] netstat -tnlp | grep redis tcp 0 0 127.0.0.1:17000 0.0.0.0:* LISTEN 61020/redis-server tcp 0 0 127.0.0.1:17001 0.0.0.0:* LISTEN 61024/redis-server tcp 0 0 127.0.0.1:17002 0.0.0.0:* LISTEN 61029/redis-server tcp 0 0 127.0.0.1:7000 0.0.0.0:* LISTEN 61020/redis-server tcp 0 0 127.0.0.1:7001 0.0.0.0:* LISTEN 61024/redis-server tcp 0 0 127.0 . 0.1 : 7002 0.0 . 0.0 : * LISTEN 61029 / redis- server ## Another machine ps -ef | grep redis root 9957 1 0 02 : 32 ? 00 : 00 : 01 redis-server 127.0 . 0.1 : 7003 [cluster] root 9964 1 0 02 : 32 ? 00:00:01 redis-server 127.0.0.1:7004 [cluster] root 9971 1 0 02:32 ? 00:00:01 redis-server 127.0.0.1:7005 [cluster] root 10065 4744 0 02:38 pts/0 00:00:00 grep --color=auto redis netstat -tlnp | grep redis tcp 0 0 127.0.0.1:17003 0.0.0.0:* LISTEN 9957/redis-server 1 tcp 0 0 127.0.0.1:17004 0.0.0.0:* LISTEN 9964/redis-server 1 tcp 0 0 127.0.0.1:17005 0.0.0.0:* LISTEN 9971/redis-server 1 tcp 0 0 127.0.0.1:7003 0.0.0.0:* LISTEN 9957/redis-server 1 tcp 0 0 127.0.0.1:7004 0.0.0.0:* LISTEN 9964/redis-server 1 tcp 0 0 127.0.0.1:7005 0.0.0.0:* LISTEN 9971/redis-server 1
7. Create a cluster
Redis officially provides the redis-trib.rb tool, which is in the src directory of the decompression directory. In the third step, it has been copied to the / usr / local / bin directory and can be used directly on the command line. Use the following command to complete the installation.
redis-trib.rb create --replicas 1 192.168.31.245:7000 192.168.31.245:7001 192.168.31.245:7002 192.168.31.210:7003 192.168.31.210:7004 192.168.31.210:7005
Among them, the first three ip: port are the nodes of the first machine, and the remaining three are the second machine.
Wait, something went wrong. This tool is implemented in ruby, so you need to install ruby. The installation command is as follows:
yum -y
install
ruby ruby-devel rubygems rpm-build
gem
install
redis
[root@localhost src]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2. 提示ruby版本过低,更新ruby版本
After running the redis-trib.rb command, the following prompt will appear:
Just enter yes, and then the following content appears, indicating that the installation was successful.
8. Cluster verification
Connect the node of the 7002 port of the cluster on the first machine, and connect the 7005 node on the other machine. The connection method is redis-cli -h 192.168. 31.245 -c -p 7002, and the parameter -C can be connected to the cluster, because the above redis.conf changed bind to ip address, so the -h parameter cannot be omitted.
Run the set hello world command on the 7005 node , and the results are as follows:
Then on another 7002 port, check the content of key hello, get hello, the execution result is as follows:
This indicates that the cluster is operating normally.
Briefly talk about the principle
Redis cluster was designed with decentralization and middleware in mind, that is to say, each node in the cluster is equal and equal, and each node stores its own data and the entire The status of the cluster. Each node is connected to all other nodes, and these connections remain active, which ensures that we only need to connect to any node in the cluster to obtain data from other nodes.
The Redis cluster does not use traditional consistent hashing to distribute data, but uses another 哈希槽 (hash slot)
method called distribution. Redis cluster assigns 16,384 slots by default. When we set a key, we will use the CRC16
algorithm to get the modulo to get its own slot
, and then divide this key to the nodes in the hash slot range. The specific algorithm is:CRC16(key) % 16384。所以我们在测试的时候看到set 和 get 的时候,直接跳转到了7000端口的节点。
The Redis cluster will store the data in a master node, and then synchronize the data between the master and its corresponding salve. When reading data, it also obtains data from the corresponding master node according to the consistent hash algorithm. Only when a master hangs up will a corresponding salve node be started to act as a master.
It should be noted that the 3个或以上
master node must be required , otherwise it will fail when creating the cluster, and when the number of surviving master nodes is less than half of the total number of nodes, the entire cluster cannot provide services.