环境
目标
搭建如下图分片+主从集群。
因为Redis 7.2 docker
镜像里面没有配置文件,所以需要去redis
官网下载一个复制里面的redis.conf
博主这里用的是7.2.3
版本的redis.conf
,这个文件就在解压后第一层文件夹里。
然后构建如下文件夹结构。
sharding/
├── docker-compose.yaml
├── master1
│ └── conf
│ └── redis.conf
├── master2
│ └── conf
│ └── redis.conf
├── master3
│ └── conf
│ └── redis.conf
├── replica1
│ └── conf
│ └── redis.conf
├── replica2
│ └── conf
│ └── redis.conf
└── replica3
└── conf
└── redis.conf
对每个redis.conf
都做以下修改。分片集群的redis
主从的redis.conf
目前都是一样的。
port 6379
# 开启集群功能
cluster-enabled yes
# 集群的配置文件名称,不需要我们创建,由redis自己维护
cluster-config-file /data/nodes.conf
# 节点心跳失败的超时时间
cluster-node-timeout 5000
# 持久化文件存放目录
dir /data
# 绑定地址
bind 0.0.0.0
# 让redis后台运行
daemonize no
# 保护模式
protected-mode no
# 数据库数量
databases 1
# 日志
logfile /data/run.log
version: '3.8'
networks:
redis-sharding:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.30.2.0/24
services:
master1:
container_name: master1
image: redis:7.2
volumes:
- ./master1/conf:/usr/local/etc/redis
ports:
- "7001:6379"
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
redis-sharding:
ipv4_address: 172.30.2.11
master2:
container_name: master2
image: redis:7.2
volumes:
- ./master2/conf:/usr/local/etc/redis
ports:
- "7002:6379"
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
networks:
redis-sharding:
ipv4_address: 172.30.2.12
master3:
container_name: master3
image: redis:7.2
volumes:
- ./master3/conf:/usr/local/etc/redis
ports:
- "7003:6379"
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
networks:
redis-sharding:
ipv4_address: 172.30.2.13
replica1:
container_name: replica1
image: redis:7.2
volumes:
- ./replica1/conf:/usr/local/etc/redis
ports:
- "8001:6379"
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
networks:
redis-sharding:
ipv4_address: 172.30.2.21
replica2:
container_name: replica2
image: redis:7.2
volumes:
- ./replica2/conf:/usr/local/etc/redis
ports:
- "8002:6379"
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
networks:
redis-sharding:
ipv4_address: 172.30.2.22
replica3:
container_name: replica3
image: redis:7.2
volumes:
- ./replica3/conf:/usr/local/etc/redis
ports:
- "8003:6379"
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
networks:
redis-sharding:
ipv4_address: 172.30.2.23
需要注意以下几点
bridge
子网并限定了范围,如果该范围已经被使用,请更换。data
进行-v
挂载,如果要挂载,请注意宿主机对应文件夹权限问题。随后运行
docker-compose -p redis-sharding up -d
接下来所有命令都在master1
容器的命令行执行
这个命令会创建了一个集群,包括三个主节点和三个从节点,每个主节点分配一个从节点作为副本,前3个ip
为主节点,后3个为从节点,主节点的从节点随机分配。
redis-cli --cluster create 172.30.2.11:6379 172.30.2.12:6379 172.30.2.13:6379 172.30.2.21:6379 172.30.2.22:6379 172.30.2.23:6379 --cluster-replicas 1
如果希望手动指定主从关系,看下面,否则你可以跳过这一章节了。
redis-cli --cluster create 172.30.2.11:6379 172.30.2.12:6379 172.30.2.13:6379 --cluster-replicas 0
查看3个主节点的ID
redis-cli -h 172.30.2.11 -p 6379 cluster nodes
下面3个命令会将3个从节点加入集群中,其中172.30.2.11
可以是三个主节点的任意一个。
redis-cli -h 172.30.2.21 -p 6379 cluster meet 172.30.2.11 6379
redis-cli -h 172.30.2.22 -p 6379 cluster meet 172.30.2.11 6379
redis-cli -h 172.30.2.23 -p 6379 cluster meet 172.30.2.11 6379
然后为每个从节点指定主节点。
redis-cli -h 172.30.2.21 -p 6379 cluster replicate <master-ID>
redis-cli -h 172.30.2.22 -p 6379 cluster replicate <master-ID>
redis-cli -h 172.30.2.23 -p 6379 cluster replicate <master-ID>
可以通过以下命令查看集群中每个节点的id、角色、ip、port、插槽范围
等信息
redis-cli -h 172.30.2.11 -p 6379 cluster nodes
往集群存入4个键值
redis-cli -c -h 172.30.2.11 -p 6379 set key1 value1
redis-cli -c -h 172.30.2.11 -p 6379 set key2 value2
redis-cli -c -h 172.30.2.11 -p 6379 set key3 value3
redis-cli -c -h 172.30.2.11 -p 6379 set key4 value4
查看每个主节点现有的键值,会发现每个节点只有一部分键值。
redis-cli -h 172.30.2.11 -p 6379 --scan
redis-cli -h 172.30.2.12 -p 6379 --scan
redis-cli -h 172.30.2.13 -p 6379 --scan