- 准备目录
mkdir rs01/data
mkdir rs02/data
mkdir rs03/data
mkdir rs04/data
mkdir rs05/data
mkdir rs06/data
准备端口配置
rs01: 6379
rs02: 6380
rs03: 6381
rs04: 6382
rs05: 6383
rs06: 6384准备redis.conf , 例如 rs01的. 192.168.1.8 为宿主机器的ip,因在一台机器上,所以设置一样即可。根据实际情况配置
# 服务的对外访问端口
port 6379
# 访问密码
requirepass 123456
# 集群中节点连接时,密码
masterauth 123456
appendonly yes
# 将bind注释掉,不然后面执行会报 节点不是cluster的错误
# [ERR] Node 192.168.1.105:7003 is not configured as a cluster node.
#bind 127.0.0.1 -::1
# 开启集群功能
cluster-enabled yes
# 集群配置文件
cluster-config-file nodes.conf
# 节点心跳失败的超时时间
cluster-node-timeout 5000
# 集群对外访问ip.port
cluster-announce-ip 192.168.1.8
cluster-announce-port 6379
# 集群内部通信接口
cluster-announce-bus-port 16379
# 持久化目录存放位置
dir ./
# 绑定地址
bind 0.0.0.0
# 主从实例的IP地址
replica-announce-ip 192.168.1.8
# 保护模式,关闭则不会校验用户名密码的校验
protected-mode no
# redis后端运行,当为yes时,看不到日志,可以配置日志的输入文件
#daemonize yes
# 日志文件
#logfile /tmp/redis/run.log
- 准备docker-compose.yml
version: '3.1'
services:
redis-cluster:
image: redis:latest
# --cluster-replicas 1 表明每一个master会有一个从节点,形成了1个master:1个对应的从节点
# 无需从节点: --cluster-replicas 0, 即全部为主节点
command: redis-cli --cluster create 192.168.1.8:6379 192.168.1.8:6380 192.168.1.8:6381 192.168.1.8:6382 192.168.1.8:6383 192.168.1.8:6384 --cluster-replicas 1 --cluster-yes -a 123456
depends_on:
- redis-1
- redis-2
- redis-3
- redis-4
- redis-5
- redis-6
# redis1配置
redis-1:
image: redis:latest
container_name: redis-1
restart: always
ports:
- 6379:6379
- 16379:16379
volumes:
- ./rs01/data:/data
- ./rs01/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
rsnet:
ipv4_address: 172.30.0.2
# redis2配置
redis-2:
image: redis:latest
container_name: redis-2
restart: always
ports:
- 6380:6380
- 16380:16380
volumes:
- ./rs02/data:/data
- ./rs02/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
rsnet:
ipv4_address: 172.30.0.3
# redis3配置
redis-3:
image: redis:latest
container_name: redis-3
restart: always
ports:
- 6381:6381
- 16381:16381
volumes:
- ./rs03/data:/data
- ./rs03/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
rsnet:
ipv4_address: 172.30.0.4
# redis4配置
redis-4:
image: redis:latest
container_name: redis-4
restart: always
ports:
- 6382:6382
- 16382:16382
volumes:
- ./rs04/data:/data
- ./rs04/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
rsnet:
ipv4_address: 172.30.0.5
# redis5配置
redis-5:
image: redis:latest
container_name: redis-5
restart: always
ports:
- 6383:6383
- 16383:16383
volumes:
- ./rs05/data:/data
- ./rs05/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
rsnet:
ipv4_address: 172.30.0.6
# redis6配置
redis-6:
image: redis:latest
container_name: redis-6
restart: always
ports:
- 6384:6384
- 16384:16384
volumes:
- ./rs06/data:/data
- ./rs06/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
rsnet:
ipv4_address: 172.30.0.7
networks:
rsnet:
ipam:
driver: default
config:
- subnet: "172.30.0.0/24"
gateway: 172.30.0.1
- 查看集群信息, 可以看到每一个后面都会有1 slaves,说明都有一个从节点
PS D:\docker\redis\redis-cluster> docker exec -it redis-1 redis-cli --cluster info 192.168.1.8:6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.8:6379 (af022877...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.8:6380 (599fdec5...) -> 0 keys | 5462 slots | 1 slaves.
192.168.1.8:6382 (8471802d...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
- 测试主从切换 可以看到主节点 redis-2,则其从节点会升级到主节点,因没有其他节点可成为从节点,则会显示0 slaves
PS D:\docker\redis\redis-cluster> docker stop redis-2
redis-2
PS D:\docker\redis\redis-cluster> docker exec -it redis-1 redis-cli --cluster info 192.168.1.8:6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.8:6380: Connection refused
192.168.1.8:6382 (8471802d...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.8:6383 (ada666d0...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.8:6384 (cf973988...) -> 0 keys | 5462 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
- 待续