Redis Cluster adopts a centerless structure, each node saves data and the entire cluster state, and each node is connected to all other nodes. Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes. Redis Cluster also provides a degree of availability during partitioning, which is effectively the ability to continue operations if some nodes fail or cannot communicate. However, in the event of a major failure (for example, when most of the masters are unavailable), the cluster will stop functioning. The ability of Redis Cluster to automatically split a dataset across multiple nodes, and continue running when a subset of nodes fails or cannot communicate with the rest of the cluster.
Self-built bridge network, manually assign IP to install
1. Manually create the installation
You need to manually create a network card, specify an IP to start multiple containers, and then enter a container to execute commands to create a cluster.
Refer to the specific installation steps: www.stephen520.cn/blog/10271
2. One-click start of docker-compose and scripts
Based on the above manual installation process, some generated configuration scripts have been written. You can use docker-compose to automatically create a network card with a specified network segment, and automatically create multiple clusters and other functions.
Configuration file template redis-cluster.tmpl.
port 6379
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 172.19.0.1${NO}
cluster-announce-port 6379
cluster-announce-bus-port 16379
Note: Here cluster-announce-ip is the network segment where the network card is automatically created. ${NO} is mainly to generate different IPs according to the following script.
Generate configuration file script generate.sh
Generate 6 instance configuration files, 3 masters and 3 slaves. The IPs are 172.19.0.11, 172.19.0.12, ... , 172.19.0.16
for no in `seq 1 6`; do \
mkdir -p redis-${no}/conf \
&& NO=${no} envsubst < redis-cluster.tmpl > redis-${no}/conf/redis.conf \
&& mkdir -p redis-${no}/data;\
done
docker-compose.yaml
Note: The network segment 172.19.0.0/16 already exists, and the creation fails.
Refer to docker-compose to manually specify the IP www.cnblogs.com/xuanmanstei… docker network prune deletes useless networks and releases network segments.
# 描述 Compose 文件的版本信息
version: "3.8"
# 定义服务,可以多个
services:
redis-cluster:
image: redis:latest
networks:
redis:
ipv4_address: 172.19.0.2
command: redis-cli --cluster create 172.19.0.11:6379 172.19.0.12:6379 172.19.0.13:6379 172.19.0.14:6379 172.19.0.15:6379 172.19.0.16:6379 --cluster-replicas 1 --cluster-yes
depends_on:
- redis-1
- redis-2
- redis-3
- redis-4
- redis-5
- redis-6
redis-1: # 服务名称
image: redis:latest # 创建容器时所需的镜像
container_name: redis-1 # 容器名称
restart: "no" # 容器总是重新启动
networks:
redis:
ipv4_address: 172.19.0.11
ports:
- "6379:6379"
- "16379:16379"
volumes: # 数据卷,目录挂载
- ./etc_rc.local:/etc/rc.local
- ./redis-1/conf/redis.conf:/etc/redis/redis.conf
- ./redis-1/data:/data
command: redis-server /etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
redis-2:
image: redis:latest
container_name: redis-2
networks:
redis:
ipv4_address: 172.19.0.12
ports:
- "6380:6379"
- "16380:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./redis-2/conf/redis.conf:/etc/redis/redis.conf
- ./redis-2/data:/data
command: redis-server /etc/redis/redis.conf
redis-3:
image: redis:latest
container_name: redis-3
networks:
redis:
ipv4_address: 172.19.0.13
ports:
- "6381:6379"
- "16381:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./redis-3/conf/redis.conf:/etc/redis/redis.conf
- ./redis-3/data:/data
command: redis-server /etc/redis/redis.conf
redis-4:
image: redis:latest
container_name: redis-4
networks:
redis:
ipv4_address: 172.19.0.14
ports:
- "6382:6379"
- "16382:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./redis-4/conf/redis.conf:/etc/redis/redis.conf
- ./redis-4/data:/data
command: redis-server /etc/redis/redis.conf
redis-5:
image: redis:latest
container_name: redis-5
networks:
redis:
ipv4_address: 172.19.0.15
ports:
- "6383:6379"
- "16383:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./redis-5/conf/redis.conf:/etc/redis/redis.conf
- ./redis-5/data:/data
command: redis-server /etc/redis/redis.conf
redis-6:
image: redis:latest
container_name: redis-6
networks:
redis:
ipv4_address: 172.19.0.16
ports:
- "6384:6379"
- "16384:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./redis-6/conf/redis.conf:/etc/redis/redis.conf
- ./redis-6/data:/data
command: redis-server /etc/redis/redis.conf
# 自动创建网络,并手动指定IP网段
networks:
redis:
ipam:
config:
- subnet: 172.19.0.0/16
HOST mode, separate IP, different port installation
Reference article:
1. docker-compose and one-click script start
Similar to the above, but there are some differences, and some generated configuration scripts have been written.
The main difference is that the bridge network that comes with docker is used here, and different ports are used to create clusters.
Configuration file template redis-cluster.tmpl.
port 6379
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 172.17.0.1
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
Note: Here cluster-announce-ip is the IP of the bridge network card. use differentcluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT}
Generate configuration file script generate.sh
Generate 6 instance configuration files, 3 masters and 3 slaves. Communication on different ports under 172.17.0.1:6379, 172.17.0.1:6380, ..., 172.17.0.1:6384
for port in `seq 6379 6384`; do \
mkdir -p ${port}/conf \
&& PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
&& mkdir -p ${port}/data;\
done
docker-compose.yaml
# 描述 Compose 文件的版本信息
version: "3.8"
# 定义服务,可以多个
services:
redis-cluster:
image: redis:latest
command: redis-cli --cluster create 172.17.0.1:6379 172.17.0.1:6380 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 --cluster-replicas 1 --cluster-yes
depends_on:
- redis-6379
- redis-6380
- redis-6381
- redis-6382
- redis-6383
- redis-6384
redis-6379: # 服务名称
image: redis:latest # 创建容器时所需的镜像
container_name: redis-6379 # 容器名称
restart: "no" # 容器总是重新启动
ports:
- "6379:6379"
- "16379:16379"
volumes: # 数据卷,目录挂载
- ./etc_rc.local:/etc/rc.local
- ./6379/conf/redis.conf:/etc/redis/redis.conf
- ./6379/data:/data
command: redis-server /etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
redis-6380:
image: redis:latest
container_name: redis-6380
ports:
- "6380:6379"
- "16380:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./6380/conf/redis.conf:/etc/redis/redis.conf
- ./6380/data:/data
command: redis-server /etc/redis/redis.conf
redis-6381:
image: redis:latest
container_name: redis-6381
ports:
- "6381:6379"
- "16381:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./6381/conf/redis.conf:/etc/redis/redis.conf
- ./6381/data:/data
command: redis-server /etc/redis/redis.conf
redis-6382:
image: redis:latest
container_name: redis-6382
ports:
- "6382:6379"
- "16382:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./6382/conf/redis.conf:/etc/redis/redis.conf
- ./6382/data:/data
command: redis-server /etc/redis/redis.conf
redis-6383:
image: redis:latest
container_name: redis-6383
ports:
- "6383:6379"
- "16383:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./6383/conf/redis.conf:/etc/redis/redis.conf
- ./6383/data:/data
command: redis-server /etc/redis/redis.conf
redis-6384:
image: redis:latest
container_name: redis-6384
ports:
- "6384:6379"
- "16384:16379"
volumes:
- ./etc_rc.local:/etc/rc.local
- ./6384/conf/redis.conf:/etc/redis/redis.conf
- ./6384/data:/data
command: redis-server /etc/redis/redis.conf
# 使用已经存在的bridge网络,或者也可以手动创建一个 替换上面的172.17.0.1
# 创建方式:docker network create redis --subnet 172.28.0.0/16
# 当然也有另一种方式,就是用docker-compose自动创建的网络,不过需要手动进入一个容器运行 cluster 创建命令
networks:
persist:
external:
name: bridge
Other references
- How the docker container communicates with the host www.cnblogs.com/koushr/p/14…
- Tips for using docker -compose's network networkszhuanlan.zhihu.com/p/382779160