Implementation of docker redis5.0 cluster Cluster Construction

  • 2021-07-22 12:15:50
  • OfStack

System environment: ubuntu16. 04LTS

In this paper, six docker containers are used to build a single-machine cluster test. If the actual environment is multiple, the number of containers, port numbers and cluster ip addresses can be modified correspondingly. Each machine can do the same according to the following steps.

Pull the official image of redis


docker pull redis:5.0

Create configuration files and data directories

Create Directory


mkdir ~/redis_cluster
cd ~/redis_cluster

Create a new template file sudo vim redis_cluster. tmpl and fill in the following:


# redis Port 
port ${PORT}

#  Turn off protection mode 
protected-mode no

#  Start the cluster 
cluster-enabled yes

#  Cluster node configuration 
cluster-config-file nodes.conf

#  Timeout 
cluster-node-timeout 5000

#  Cluster node IP host Mode is host IP
cluster-announce-ip 10.10.100.197

#  Cluster node port  7000 - 7005
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}

#  Open  appendonly  Backup mode 
appendonly yes

#  Backup per second 
appendfsync everysec

#  Right aof Do you want to synchronize files when they are compressed 
no-appendfsync-on-rewrite no

#  When at present aof File size exceeds 1 On the second rewrite aof File size 100% Is overridden again when the 
auto-aof-rewrite-percentage 100

#  Before rewriting AOF Minimum file size   Default  64mb
auto-aof-rewrite-min-size 5120mb

#  Turn off Snapshot Backup 
save ""

Create configuration files and data directories in batches, and the terminal runs the following commands:


for port in `seq 7000 7005`; do \
 mkdir -p ./${port}/conf \
 && PORT=${port} envsubst < ./redis_cluster.tmpl > ./${port}/conf/redis.conf \
 && mkdir -p ./${port}/data; \
done

Batch start of redis container

The ip address of the container adopts host mode:


for port in `seq 7000 7005`; do \
 docker run -d -it --memory=1G \
 -v ~/redis_cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
 -v ~/redis_cluster/${port}/data:/data \
 --restart always --name redis-${port} --net host \
 --sysctl net.core.somaxconn=1024 redis:5.0 redis-server /usr/local/etc/redis/redis.conf; \
done

Here-memeory = 1G is to limit the memory size of a single docker container to 1G, which will be killed by the process. This warning may appear at runtime... Memory limited without swap... and can be ignored. If you don't need to limit memory, you can remove the-memeory parameter.

Create a cluster

Just enter one of the containers:


docker exec -it redis-7000 bash

After entering, execute the following command to create the cluster:


redis-cli --cluster create 10.10.100.197:7000 10.10.100.197:7001 10.10.100.197:7002 10.10.100.197:7003 10.10.100.197:7004 10.10.100.197:7005 --cluster-replicas 1

Install the redis-cli command (skip this step if you already have one):


sudo apt install redis-tools

After entering yes, the cluster is created, exit exits docker, and then logs in to one of the nodes to verify the availability of the cluster:


redis-cli -c -p 7000

Enter cluster nodes to view cluster status


127.0.0.1:7000> cluster nodes
06851aa134d50096d82a434eced9194233b5204e 10.10.100.197:7003@17003 slave 8b33f273386c9504ef8bd10b005e24825b3b9596 0 1567671901000 4 connected
a42297b65f7101fc9e4941ef8a0e65080d1b6338 10.10.100.197:7005@17005 slave 0aa20378d14e3ef0859815196cbafa67e1001d0e 0 1567671901581 6 connected
e7b6a35b1e92f94c225c507ea19f7f0318f0d1c3 10.10.100.197:7002@17002 master - 0 1567671902582 3 connected 10923-16383
0aa20378d14e3ef0859815196cbafa67e1001d0e 10.10.100.197:7000@17000 myself,master - 0 1567671901000 1 connected 0-5460
8b33f273386c9504ef8bd10b005e24825b3b9596 10.10.100.197:7001@17001 master - 0 1567671902383 2 connected 5461-10922
fe355eed99100197f43d1216d1de82643dd496a5 10.10.100.197:7004@17004 slave e7b6a35b1e92f94c225c507ea19f7f0318f0d1c3 0 1567671901380 5 connected

Set the cluster password

Why don't you write the password in the above steps when you create configuration files in batches with template files?

No matter in redis5. x version or the previous redis version using ruby to create clusters, there is no password parameter configuration in redis-cli-cluster create to create clusters, so we need to set passwords after creating clusters.

We use config set to set the same password for each node (redis does not need to be restarted, and it is still valid after restarting). Before that, add w permission to all redis configuration files, otherwise the password cannot be saved to the file.

Note that the current path is still in ~/redis_cluster/:


mkdir ~/redis_cluster
cd ~/redis_cluster
0

Let's use one as an example:

Log in to 1 node:


mkdir ~/redis_cluster
cd ~/redis_cluster
1

Set password:


mkdir ~/redis_cluster
cd ~/redis_cluster
2

The latter ones can perform the same operation.

Simple test of cluster writing data

Log in to one cluster node at will:


redis-cli -c -p 7003 -a 123456

Write data:


mkdir ~/redis_cluster
cd ~/redis_cluster
4

As you can see, any node in the cluster writes data and can read it at any other node.

At this point, the redis cluster has been built.

Other matters needing attention

External network access to redis may require firewall to open the corresponding port; If you need to delete containers, you can do it in batches:

mkdir ~/redis_cluster
cd ~/redis_cluster
5

Related articles: