目录结构
redis.conf主要参数
每个配置文件都要修改对应的端口
bind 0.0.0.0
protected-mode no
#每个配置文件都要修改端口
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
#日志文件路径
#logfile "/mydata/master_redis/log/6379.log"
databases 16
always-show-logo yes
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
masterauth "admin"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
#slaveof ip1 6379
#slave-announce-ip ip1
#slave-announce-port 6379
#配置密码
requirepass "admin"
#配置集群相关
cluster-enabled yes
cluster-config-file node.conf
cluster-node-timeout 5000
#开启aof
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
aof-rewrite-incremental-fsync yes
docker-compose.yml
version: '3.1'
services:
cluster_redis_6379:
image: redis:latest
container_name: cluster_redis_6379
# network_mode: "host"
restart: always
ports:
- 6379:6379
- 16379:16379
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data_6379:/data
- ./conf/redis_6379.conf:/etc/redis/redis.conf
cluster_redis_6380:
image: redis:latest
container_name: cluster_redis_6380
# network_mode: "host"
restart: always
ports:
- 6380:6380
- 16380:16380
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data_6380:/data
- ./conf/redis_6380.conf:/etc/redis/redis.conf
cluster_redis_6381:
image: redis:latest
container_name: cluster_redis_6381
# network_mode: "host"
restart: always
ports:
- 6381:6381
- 16381:16381
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data_6381:/data
- ./conf/redis_6381.conf:/etc/redis/redis.conf
cluster_redis_6382:
image: redis:latest
container_name: cluster_redis_6382
# network_mode: "host"
restart: always
ports:
- 6382:6382
- 16382:16382
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data_6382:/data
- ./conf/redis_6382.conf:/etc/redis/redis.conf
cluster_redis_6383:
image: redis:latest
container_name: cluster_redis_6383
# network_mode: "host"
restart: always
ports:
- 6383:6383
- 16383:16383
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data_6383:/data
- ./conf/redis_6383.conf:/etc/redis/redis.conf
cluster_redis_6384:
image: redis:latest
container_name: cluster_redis_6384
# network_mode: "host"
restart: always
ports:
- 6384:6384
- 16384:16384
command: redis-server /etc/redis/redis.conf
volumes:
- ./data/data_6384:/data
- ./conf/redis_6384.conf:/etc/redis/redis.conf
容器内端口不能都是是6379,集群总线接口是端口号+10000
端口号与network_mode: "host"不能一起配置,出现下述问题“主机”网络模式与端口绑定不兼容
问题,具体看github,有对应解释。
ERROR: for cluster_redis_6379 "host" network_mode is
incompatible with port_bindings
https://github.com/docker/compose/issues/8326
启动redis容器
docker-compose up -d
进入容器
docker exec -it cluster_redis_6379 bash
搭建集群
redis-cli -a admin --cluster create \
47.120.0.38:6379 \
47.120.0.38:6380 \
47.120.0.38:6381 \
47.120.0.38:6382 \
47.120.0.38:6383 \
47.120.0.38:6384 \
--cluster-replicas 1 #一个主节点分配一个从节点
集群节点分配结果
集群槽位分配结果
遇到的问题
如果一直卡在Waiting for the cluster to join
,那一般是端口问题,看看集群总线端口是否开启,我在这遇到问题,修改了redis.conf中的端口,将容器中端口都是6379改成不同的,并且在docker-compose.yml的port中,加上各个节点的总线端口好了。
查看集群所有节点的关系
cluster nodes
查看单个节点信息
info replication
集群数据库数量问题
Response received : -ERR SELECT is not allowed in cluster mode
在集群模式下这个配置是不起作用的,集群客户端是不支持多数据库db的,只有一个数据库默认是SELECT 0;
在某个控制台里,比如6379,如果存放的key值对应的槽位不在6379的槽位之中,是不能存放的
127.0.0.1:6379> set 222 222
(error) MOVED 7715 47.120.0.38:6380