Docker builds a redis cluster and sets a password

1. Download the mirror

If you have not installed docker, you can refer to the previous article " Centos7 install docker " to install

docker pull redis:5.0.5

After the download is successful, the interface will be shown as below:

2. Create 6 Redis containers

Create 6 Redis containers:
  • redis-node1: 6379
  • redis-node2: 6380
  • redis-node3: 6381
  • redis-node4: 6382
  • redis-node5: 6383
  • redis-node6: 6384

The execution command is as follows:

docker create --name redis-node1 --net host -v /data/redis-data/node1:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-1.conf --port 6379 docker create --name redis-node2 --net host -v /data/redis-data/node2:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-2.conf --port 6380 docker create --name redis-node3 --net host -v /data/redis-data/node3:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-3.conf --port 6381 docker create --name redis-node4 --net host -v /data/redis-data/node4:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-4.conf --port 6382 docker create --name redis-node5 --net host -v /data/redis-data/node5:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-5.conf --port 6383 docker create --name redis-node6 --net host -v /data/redis-data/node6:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-6.conf --port 6384

Sub-parameter explanation:

--cluster-enabled: whether to start the cluster, the value is selected from: Yes, NO
--cluster-config-profile .conf File: Specifies the node information generated automatically
--cluster-node-timeout milliseconds: the configuration node connection timeout
- -appendonly: Whether to enable persistence, optional values: yes, no

Three, start the Redis container

docker start redis-node1 redis-node2 redis-node3 redis-node4 redis-node5 redis-node6

Check whether the container has started successfully after startup

You can see that all 6 containers have been started successfully.

Fourth, set up a Redis cluster

Enter any Redis instance:

# 这里以 redis-node1 实例为例docker exec -it redis-node1 /bin/bash # 组建集群192.168.4.201为当前物理机的ip地址redis-cli --cluster create 192.168.4.201:6379 192.168.4.201:6380 192.168.4.201:6381 192.168.4.201:6382 192.168.4.201:6383 192.168.4.201:6384 --cluster-replicas 1 

Enter yes in the middle

After the creation is successful, check the cluster node information through redis-cli:

At this point, the redis cluster has been successfully built.

Five, set a password

 config set requirepass 'password'   // 设置密码config set masterauth 'password'	// 设置从节点连接主节点的密码

Six, verify the cluster

Connect each node of the cluster through the tool, set the value on node one, and view it on the other 5 nodes

You can see that the data has all been synchronized to each node.