搜索镜像
docker search redis
拉取镜像
docker pull redis:latest
注:镜像Tag可通过Docker Hub查看。
相关配置
- 开启密码验证
requirepass 123456
- 开始数据持久化
appendonly yes
- 开启集群模式
cluster-enabled yes
- 开启主从密码验证
masterauth 123456
运行容器
运行Redis集群节点容器,并开放集群总线端口,集群总线端口为:Redis节点端口+10000。
docker run -d --name Redis-6381 -p 6381:6381 -p 16381:16381 -v /docker/redis/cluster/node1/data:/data redis:latest --port 6381 --requirepass 123456 --appendonly yes --cluster-enabled yes --masterauth 123456
docker run -d --name Redis-6382 -p 6382:6382 -p 16382:16382 -v /docker/redis/cluster/node2/data:/data redis:latest --port 6382 --requirepass 123456 --appendonly yes --cluster-enabled yes --masterauth 123456
docker run -d --name Redis-6383 -p 6383:6383 -p 16383:16383 -v /docker/redis/cluster/node3/data:/data redis:latest --port 6383 --requirepass 123456 --appendonly yes --cluster-enabled yes --masterauth 123456
docker run -d --name Redis-6384 -p 6384:6384 -p 16384:16384 -v /docker/redis/cluster/node4/data:/data redis:latest --port 6384 --requirepass 123456 --appendonly yes --cluster-enabled yes --masterauth 123456
docker run -d --name Redis-6385 -p 6385:6385 -p 16385:16385 -v /docker/redis/cluster/node5/data:/data redis:latest --port 6385 --requirepass 123456 --appendonly yes --cluster-enabled yes --masterauth 123456
docker run -d --name Redis-6386 -p 6386:6386 -p 16386:16386 -v /docker/redis/cluster/node6/data:/data redis:latest --port 6386 --requirepass 123456 --appendonly yes --cluster-enabled yes --masterauth 123456
构建集群
进入任意Redis容器节点,并执行redis-cli --cluster create
命令,创建Redis Cluster集群;
docker exec -it Redis-6381 /bin/bash
redis-cli -a '123456' --cluster create 192.168.31.169:6381 192.168.31.169:6382 192.168.31.169:6383 192.168.31.169:6384 192.168.31.169:6385 192.168.31.169:6386 --cluster-replicas 1
Redis自动分配完成后,输入yes
指令确认配置信息;
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.31.169:6385 to 192.168.31.169:6381
Adding replica 192.168.31.169:6386 to 192.168.31.169:6382
Adding replica 192.168.31.169:6384 to 192.168.31.169:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: bd0f7eeb5f92f39736e29e088cf61e10c30d4a6f 192.168.31.169:6381
slots:[0-5460] (5461 slots) master
M: 5e60384ce1cc43bf1ece9b2b5528c4bbe690d100 192.168.31.169:6382
slots:[5461-10922] (5462 slots) master
M: f5de06c763662af6e9f502dd744f0f9acbc584ce 192.168.31.169:6383
slots:[10923-16383] (5461 slots) master
S: ab91cd519d269f8bced240f8b720fe6a080545d0 192.168.31.169:6384
replicates f5de06c763662af6e9f502dd744f0f9acbc584ce
S: ea3c792cb2348c845dee2f2071f4f0d9e55bd3e1 192.168.31.169:6385
replicates bd0f7eeb5f92f39736e29e088cf61e10c30d4a6f
S: 4fbf0e09cf027501c8e4b45fd922fb44a25e0922 192.168.31.169:6386
replicates 5e60384ce1cc43bf1ece9b2b5528c4bbe690d100
Can I set the above configuration? (type 'yes' to accept): yes
等待Redis节点加入集群,并分配哈希槽完成即可;
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.
>>> Performing Cluster Check (using node 192.168.31.169:6381)
M: bd0f7eeb5f92f39736e29e088cf61e10c30d4a6f 192.168.31.169:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: f5de06c763662af6e9f502dd744f0f9acbc584ce 172.18.0.6:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 4fbf0e09cf027501c8e4b45fd922fb44a25e0922 172.18.0.3:6386
slots: (0 slots) slave
replicates 5e60384ce1cc43bf1ece9b2b5528c4bbe690d100
S: ea3c792cb2348c845dee2f2071f4f0d9e55bd3e1 172.18.0.1:6385
slots: (0 slots) slave
replicates bd0f7eeb5f92f39736e29e088cf61e10c30d4a6f
S: ab91cd519d269f8bced240f8b720fe6a080545d0 172.18.0.4:6384
slots: (0 slots) slave
replicates f5de06c763662af6e9f502dd744f0f9acbc584ce
M: 5e60384ce1cc43bf1ece9b2b5528c4bbe690d100 172.18.0.7:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Docker-Compose
根据Redis集群相关配置,构建配置文件redis.conf
,不同节点端口需调整;
port 6381
requirepass 123456
appendonly yes
cluster-enabled yes
masterauth 123456
编写docker-compose.yml
文件;
version: '3'
services:
redis6381:
image: redis:latest
container_name: Redis-6381
ports:
- 6381:6381
- 16381:16381
volumes:
- /docker/redis/cluster/node1/config:/config
- /docker/redis/cluster/node1/data:/data
command:
["redis-server","/config/redis.conf"]
redis6382:
image: redis:latest
container_name: Redis-6382
ports:
- 6382:6382
- 16382:16382
volumes:
- /docker/redis/cluster/node2/config:/config
- /docker/redis/cluster/node2/data:/data
command:
[ "redis-server","/config/redis.conf" ]
redis6383:
image: redis:latest
container_name: Redis-6383
ports:
- 6383:6383
- 16383:16383
volumes:
- /docker/redis/cluster/node3/config:/config
- /docker/redis/cluster/node3/data:/data
command:
[ "redis-server","/config/redis.conf" ]
redis6384:
image: redis:latest
container_name: Redis-6384
ports:
- 6384:6384
- 16384:16384
volumes:
- /docker/redis/cluster/node4/config:/config
- /docker/redis/cluster/node4/data:/data
command:
[ "redis-server","/config/redis.conf" ]
redis6385:
image: redis:latest
container_name: Redis-6385
ports:
- 6385:6385
- 16385:16385
volumes:
- /docker/redis/cluster/node5/config:/config
- /docker/redis/cluster/node5/data:/data
command:
[ "redis-server","/config/redis.conf" ]
redis6386:
image: redis:latest
container_name: Redis-6386
ports:
- 6386:6386
- 16386:16386
volumes:
- /docker/redis/cluster/node6/config:/config
- /docker/redis/cluster/node6/data:/data
command:
[ "redis-server","/config/redis.conf" ]
执行docker-compose
命令运行容器后,进行构建集群。
docker-compose -p redis_cluster up -d