This example will demonstrate the creation of a docker-based redis cluster. Due to limited resources, the cluster consists of 3 master nodes and no slave nodes.
Create redis node directory
mkdir -p ~/redis/node1
mkdir -p ~/redis/node1/data
chmod 777 ~/redis/node1/data
Create redis.conf
$ cat redis.conf
#监听端口号,如果所有节点在同一台机器上,每一个节点要用不同的端口
port 6379
#允许的最大连接数
maxclients 1000
#集群模式
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
#aof持久化
appendonly yes
appendfsync everysec
#密码
requirepass 123456
#数据文件路径,这里是docker里的路径
dir /usr/local/etc/redis/data
#允许外网ip访问
protected-mode no
Distribute redis configuration information to 3 nodes
scp -r . node1:/opt/redis
scp -r . node2:/opt/redis
scp -r . node3:/opt/redis
start all nodes
docker run -v /opt/redis:/usr/local/etc/redis -d --net host redis redis-server /usr/local/etc/redis/redis.conf
command description
parameter | illustrate |
-v /opt/redis:/usr/local/etc/redis | Mount the redis configuration directory on the host to docker |
-d | Background process |
--net host | The port of the mapped host will directly listen to port 6379 of the host |
redis | The name of the docker node |
redis-server /usr/local/etc/redis/redis.conf | docker run command |
Create a cluster
docker run -it redis redis-cli --cluster create ${node1-ip}:6379 ${node2-ip}:6379 ${node3-ip}:6379 --cluster-replicas 0 -a 123456
Parameter Description
parameter | illustrate |
-it | Run commands in foreground mode |
--cluster create ${node1-ip}:6379 ${node2-ip}:6379 ${node3-ip}:6379 | node address |
--cluster-replicas 0 | No slave node |
-a 123456 | The password of the node should be the same as the password in the redis.conf file |
This command will let you confirm the slot distribution in the middle, you need to enter yes to continue.
examine
$ redis-cli -c -h 10.0.0.76 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
858637f2406e498684b1e60fcecf4ad840276dc2 10.0.0.113:6379@16379 master - 0 1640576067894 7 connected 5461-10921
0b02a441444b365b4a9d9d26fe5f8c4fa13d110a 10.0.0.76:6379@16379 myself,master - 0 1640576065000 6 connected 0-5460
b02ea6a3c8b88891a357dfcbc8affea134d3e44d 10.0.0.168:6379@16379 master - 0 1640576067000 8 connected 10922-16383
Finish