Three machines use Docker to deploy Redis clusters
We use Docker to build Redis clusters on three servers, which are convenient, fast and easy to use.
1. Cluster Principle
In the Redis cluster, the nodes are associated with each other, and the transmission speed and bandwidth are optimized through binary protocols. When a node is dead, more than half of the nodes in the cluster fail to check. Therefore, based on the above situation, the number of nodes is generally odd. Generally, in order to ensure security, each node has a backup node. Therefore, the number of master nodes in the smallest cluster is at least 3, and the number of slave nodes is also 3. Such a cluster requires at least 6 nodes. This is especially important when planning a cluster.
There are 16384 hash slots built in the Redis cluster. When it is necessary to store a key/value key-value pair in the Redis cluster, redis will have an algorithm. First use the CRC16 algorithm to calculate a result, and then calculate the remainder of the result for 16384. In this way, each key will have a hash slot numbered between 0-16383. Redis will roughly map the hash slot to different nodes according to the number of nodes. It should be noted that there is no limit on the number of data stored in each hash slot.
2. Cluster Planning
IP and redis node planning of three hosts:
Host machine IP | Redis node planning | Redis node planning |
---|---|---|
172.15.1.11 | 172.15.1.11:7801 | 172.15.1.11:7804 |
172.15.1.12 | 172.15.1.12:7802 | 172.15.1.12:7805 |
172.15.1.13 | 172.15.1.13:7800 | 172.15.1.13:7803 |
3. Cluster deployment
The deployment of the cluster uses Linux shell scripts, and the steps are as follows
A. Create a template file
Log in to three machines, create a redis-cluster folder in the home directory, and write a file. The function of the file is to create a template file for the Redis configuration file.
port ${PORT} protected-mode no cluster-enabled yes cluster-config-file cluster-node-timeout 5000 cluster-announce-ip 172.15.1.11 #The other two IPs are 172.15.1.12 and 172.15.1.13 respectivelycluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly yes
- port ${PORT}: is a custom port number
- protected-mode no: It is to turn off protection mode, otherwise it may not be accessible through the public network.
- cluster-enabled yes: Start the cluster. Must be configured as yes
- cluster-config-file: The name of the cluster node configuration file, which we usually name.
- cluster-node-timeout 5000: Timeout, unit in milliseconds
- cluster-announce-ip 172.15.1.11: cluster host IP
- cluster-announce-port ${PORT}: node map port
- cluster-announce-bus-port 1${PORT}: Node bus port
- appendonly yes: Turn on persistence mode
B. Customize the network
All nodes are in one network, which facilitates communication.
Create a network in Docker. (Three machines are in the docker swarm cluster, creating an overlay network)
docker network create -d overlay --attachable redis_net
C. Create a configuration generation script
Generate conf and data directories under three machines /home/redis-cluster and generate configuration information.
Here is a Linux shell script.
Be careful to authorize it, otherwise the script will run.
for port in 7801 7804 #The ports of the other two units are modified to 7802 7805 and 7800 7803 respectivelydo mkdir -p ./${port}/conf \ && PORT=${port} envsubst < ./ > ./${port}/conf/ \ && mkdir -p ./${port}/data; \ done
Let me explain the script content to you, and loop twice, and store the variable to the port variable each time. Then create the directory loop, and the command is mkdir -p ./{port}/conf\, which means that the port is followed by the directory name below the current directory, and create a subdirectory conf below. The \ after the \ is a connector, indicating the statement behind the connection.
PORT=${port} envsubst < ./ > means copying the value of the loop variable port to PORT, envsubst is assigned to the file. Then copy the file to the port number directory\conf folder, with the name; mkdir -p ./${port}/data; means continuing to create in the port directory\data directory.
D. Execute the creation configuration generation script
Authorization. Make the file executable permissions and execute the file
chmod +x ./ ./
A total of 6 folders are generated on the three machines, from 7800 to 7805. Each folder contains data and conf folders, and there are configuration files in conf.
E. Create container generation script
Write scripts to create Redis containers on three machines.
current_dir=`pwd` for port in 7801 7804 #The ports of the other two units are modified to 7802 7805 and 7800 7803 respectivelydo docker run -dit -p ${port}:${port} -p 1${port}:1${port} \ --privileged=true -v $current_dir/${port}/conf/:/usr/local/etc/redis/ \ --privileged=true -v $current_dir/${port}/data:/data \ --restart always --name redis-${port} --net redis_net \ --sysctl =1024 redis:5.0.5 redis-server /usr/local/etc/redis/ done
- docker run -dit -p ${port}: ${port} -p 1${port}:1${port}, which means to start the container.-d means to start the background; -i means to enable interactive mode. -t represents a pseudo-terminal. These three parameters can be abbreviated as -dit. ${port}: ${port} means mapping the host port to the container port. 1${port}:1${port} means mapping the bus ports of the host and the container.
- –privileged=true -v $current_dir/${port}/conf/:/usr/local/etc/redis/, privileged means authorization, -v means container mount, mount files under the host /home/redis-cluster/${port}/conf/ directory to the /usr/local/etc/redis directory inside the container, and the configuration file inside the container is.
- –privileged=true -v $current_dir/${port}/data:/data, also authorizes mounting the host data directory to the container's data directory.
- –restart always: means that when Docker restarts, the container will automatically restart.
- –name redis-${port} The custom container name of the container is redis-port number
- –net redis_net: means that the network where the container works is redis_net
- –sysctl =1024 means modifying the value of somaxconn. The kernel size is 1024M. In a word, when a program with a large load is often failed due to insufficient memory.
- redis:5.0.5 represents the mirrored version. The container is created at this point.
- redis-server /usr/local/etc/redis/ means to start the Redis server. The configuration file must be specified.
F. Execute container generation script
Authorization. Make the file executable permissions and execute the file
chmod +x ./ ./
A total of 6 containers are generated on three machines, redis-7800 to redis-7805.
Use the command to confirm:
docker ps -a|grep redis-780
G. Configure the redis cluster
Enter any node and start the cluster. Note that redis container startup does not mean cluster startup.
docker exec -it redis-7801 bash redis-cli --cluster create 172.15.1.13:7800 172.15.1.11:7801 172.15.1.12:7802 172.15.1.13:7803 172.15.1.11:7804 172.15.1.12:7805 --cluster-replicas
The cluster is now built.
4. Cluster detection
Verify the cluster status, enter the container, and execute
docker exec -it redis-7801 bash redis-cli -c -p 7801 info replication cluster nodes cluster info
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.