This time we will talk about how to set up a Redis cluster. The official introduction version is three main and three from the form, this time we will demonstrate, and look at some of the features of the cluster.

(1) Install the VM

Source: github.com/limingios/n… You can check out the previous article, Vagrant’s introduction to Docker [Intermediate].

  • The environment

192.168.79.100 192.168.79.101 192.168.79.102 Each server has one active server and one slave server

The installation package is stored in /root/soft/

System type The IP address The node role CPU Memory Hostname
Centos7 192.168.79.100 redis-master-1 2 2G redis-master-1
Centos7 192.168.79.101 redis-master-2 2 2G redis-master-2
Centos7 192.168.79.102 redis-master-3 2 2G redis-master-3

2. Deployment

  • The preparatory work

Soft is created in the root directory for all three hosts

mkdir soft
Copy the code

  • Download the 79.100 machine
cdSoft wget http://download.redis.io/releases/redis-3.2.9.tar.gz tar XVF redis - 3.2.9. Tar. GzcdRedis - 3.2.9Copy the code

  • 79.100 Machine installation
Make install PREFIX = / root/soft/redis - 3.2.9Copy the code
  • 79.100 Machine configuration
cd/ root/soft/redis - 3.2.9Create cluster configuration folder
mkdir cluster-conf
cd cluster-conf
Create cluster port folder
mkdir 7001 
mkdir 7002
cd7001 cp/root/soft/redis - 3.2.9 / redis. Conf., /cd. / 7002 cp/root/soft/redis - 3.2.9 / redis. Conf., /Copy the code

79.100 Machine change 7001 redis.conf, find esc key, enter/name

pwd
vi redis.conf
Copy the code
  1. Bind 127.0.0.1 Modifies bind 0.0.0.0
  2. Port 6379 Change port 7001
  3. Logfile “” Modify logfile “/root/soft/redis-3.2.9/cluster-conf/7001/redis.log”
  4. Dir./ Modify dir /root/sof/redis-3.2.9 /cluster-conf/7001
  5. Appendonly no Modify appendonly yes
  6. Add to end of file

cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000

79.100 Redis. conf on machine 7002, find esc key, enter/name

cd/ root/soft/redis - 3.2.9 / cluster - conf / 7002pwd
vi redis.conf
Copy the code
  1. Bind 127.0.0.1 Modifies bind 0.0.0.0
  2. Port 6379 Modify port 7002
  3. Logfile “” Modify logfile “/root/soft/redis-3.2.9/cluster-conf/7002/redis.log”
  4. Dir./ Modify dir /root/sof/redis-3.2.9/cluster-conf/7002
  5. Appendonly no Modify appendonly yes
  6. Add to end of file

cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000

  • Configuration instructions

1. Port number.

port

2. The log file is specified.

logfile

3. Data directory, where database writes occur. RDB and Aof files are also written in this directory.

dir

4. Determine whether to enable the cluster

cluster-enabled

5. Name of the cluster configuration file. Each node has a cluster-related configuration file that stores cluster information persistently. This file does not need to be manually configured, it is generated and updated by Redis, each Redis cluster node needs a separate configuration file, make sure it does not conflict with the name of the configuration file on the system where the instance is running

cluster-config-file nodes.conf

6. Timeout threshold for node interconnection. Number of timeout milliseconds of cluster nodes

cluster-node-timeout

7. By default, Redis uses RDB persistence, which is sufficient for many applications. However, if Redis breaks down, the data may be lost for several minutes. Therefore, the data can be persisted according to the save policy. Append Only File is another persistence method, which can provide better persistence features. Every time Redis writes data to appendonly. Aof after receiving it, Redis reads the data from this file into memory, ignoring the RDB file.

appendonly

  • SCP is copied to host 101,102
cd/ root/soft SCP - r redis - 3.2.9 / [email protected]: / root/soft/SCP - r redis - 3.2.9 / [email protected]: / root/soft /Copy the code

  • Start the 100101102
# 100 host/ root/soft/redis - 3.2.9 / bin/redis server/root/soft/redis - 3.2.9 / cluster - the conf / 7001 / redis. Conf / root/soft/redis - 3.2.9 / bin/redis server/root/soft/redis - 3.2.9 / cluster - the conf / 7002 / redis. Conf# 101 host/ root/soft/redis - 3.2.9 / bin/redis server/root/soft/redis - 3.2.9 / cluster - the conf / 7001 / redis. Conf / root/soft/redis - 3.2.9 / bin/redis server/root/soft/redis - 3.2.9 / cluster - the conf / 7002 / redis. Conf# 102 host/ root/soft/redis - 3.2.9 / bin/redis server/root/soft/redis - 3.2.9 / cluster - the conf / 7001 / redis. Conf / root/soft/redis - 3.2.9 / bin/redis server/root/soft/redis - 3.2.9 / cluster - the conf / 7002 / redis. ConfCopy the code

  • Create the cluster

100,101,102 Machine execution, install Ruby 2.4.1

yum -y install curl

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB 

curl -sSL https://get.rvm.io | bash -s stable

find / -name rvm -print

source /usr/localRVM /scripts/ RVM RVM install 2.4.1 RVM use 2.4.1 --default gem install redisCopy the code

100 Machine execution

cd/root/soft/ redis-3.2.9/src. /redis-trib.rb create --replicas 1 192.168.79.100:7001 192.168.79.101:7001 192.168.79.102:7001 192.168.79.100:7002 192.168.79.101:7002 192.168.79.102:7002# enter yes
Copy the code

Redis-trib. rb is a redis cluster management tool.

Integrated in redis source code SRC directory.

The name of the role
call Run commands on all nodes in the cluster
set-timeout Set the timeout period for heartbeat connections between cluster nodes
del-node Example Delete a node from a cluster
reshard Online Slot Migration
check Check the cluster
import Import external Redis data into the cluster
add-node Add a new node to the cluster
create Create the cluster
info Viewing Cluster Information
fix Repair of the cluster
rebalance Balance the number of slots on cluster nodes
  • Viewing connection Effects

There are three masters and three slaves.

cd/root/soft/ redis-3.2.9/src. /redis-cli -c -p 7001Copy the code

  • Check the cluster
cd/root/soft/ redis-3.2.9/src. /redis-cli -c -p 7001 cluster nodesCopy the code
  • The cluster breaks down and restarts
cd/ root/soft/redis - 3.2.9 / SRC/redis - trib. Rb fix 192.168.79.100:7001Copy the code

Shard mode, a cluster has 16,383 slots, according to the main relative average way to allocate.

(3) Cluster management

Add a node:./redis-trib.rb add-node IP :port IP :port

The first parameter is the address of the new node, and the second parameter is the IP address and port of any existing node.Copy the code

Remove node: Redis-trib del-node IP :port

The first parameter is the address of any node, and the second is the address of the node you want to remove. Change the master node clusterreplicate master-node-id of a secondary node

1. If the primary node is down, there is no problem. 2. If the secondary node is down, it must be accessible. 3. Both of them are dead (one master and one slave), they cannot be used

Select 1 cannot be used to directly select the database. But it has the concept of slots, cluster keyslot WST. You can see that this key is in that slot.

Theory: Redis has a maximum number of nodes in a cluster of 16,384 and a maximum number of slots of 16,384. A cluster has 16384 slots. Like microblog, Taobao, their cluster has a common: N cluster, cluster is not in the same machine room.

PS: The redis cluster was basically finished. It took me 1 hour to write the article, but it took me 2 hours to set up the environment. The Internet speed at home was too unstable. Just like our normal situation may be able to withstand the application, once the amount of sudden situation may not be able to withstand, through the great benefits of cluster, sharing pressure, disaster tolerance, the fire is high.