创建目录
mkdir -p /usr/local/docker-redis/redis-cluster
切换至指定目录
cd /usr/local/docker-redis/redis-cluster/
编写 redis-cluster.tmpl 文件
vi redis-cluster.tmpl
1port ${PORT}
2requirepass 1234
3masterauth 1234
4protected-mode no
5daemonize no
6appendonly yes
7cluster-enabled yes
8cluster-config-file nodes.conf
9cluster-node-timeout 15000
10cluster-announce-ip 192.168.10.10
11cluster-announce-port ${PORT}
12cluster-announce-bus-port 1${PORT}
13
mkdir conf & data path
1
2for port in `seq 6371 6373`; do \
3 mkdir -p ${port}/conf \
4 && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
5 && mkdir -p ${port}/data;\
6done
7
8for port in `seq 6374 6376`; do \
9 mkdir -p ${port}/conf \
10 && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
11 && mkdir -p ${port}/data;\
12done
compose.yaml
1version: "3.8"
2
3services:
4 redis-6371: # 服务名称
5 image: redis # 创建容器时所需的镜像
6 container_name: redis-6371 # 容器名称
7 restart: always # 容器总是重新启动
8 network_mode: "host" # host 网络模式
9 volumes: # 数据卷,目录挂载
10 - /usr/local/docker-redis/redis-cluster/6371/conf/redis.conf:/usr/local/etc/redis/redis.conf
11 - /usr/local/docker-redis/redis-cluster/6371/data:/data
12 command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
13
14 redis-6372:
15 image: redis
16 container_name: redis-6372
17 network_mode: "host"
18 volumes:
19 - /usr/local/docker-redis/redis-cluster/6372/conf/redis.conf:/usr/local/etc/redis/redis.conf
20 - /usr/local/docker-redis/redis-cluster/6372/data:/data
21 command: redis-server /usr/local/etc/redis/redis.conf
22
23 redis-6373:
24 image: redis
25 container_name: redis-6373
26 network_mode: "host"
27 volumes:
28 - /usr/local/docker-redis/redis-cluster/6373/conf/redis.conf:/usr/local/etc/redis/redis.conf
29 - /usr/local/docker-redis/redis-cluster/6373/data:/data
30 command: redis-server /usr/local/etc/redis/redis.conf
31
32 redis-6374:
33 image: redis
34 container_name: redis-6374
35 network_mode: "host"
36 volumes:
37 - /usr/local/docker-redis/redis-cluster/6374/conf/redis.conf:/usr/local/etc/redis/redis.conf
38 - /usr/local/docker-redis/redis-cluster/6374/data:/data
39 command: redis-server /usr/local/etc/redis/redis.conf
40
41 redis-6375:
42 image: redis
43 container_name: redis-6375
44 network_mode: "host"
45 volumes:
46 - /usr/local/docker-redis/redis-cluster/6375/conf/redis.conf:/usr/local/etc/redis/redis.conf
47 - /usr/local/docker-redis/redis-cluster/6375/data:/data
48 command: redis-server /usr/local/etc/redis/redis.conf
49
50 redis-6376:
51 image: redis
52 container_name: redis-6376
53 network_mode: "host"
54 volumes:
55 - /usr/local/docker-redis/redis-cluster/6376/conf/redis.conf:/usr/local/etc/redis/redis.conf
56 - /usr/local/docker-redis/redis-cluster/6376/data:/data
57 command: redis-server /usr/local/etc/redis/redis.conf
58
1docker-compose up -d
2
3redis-cli -a 1234 --cluster create {ip}:6371 {ip}:6372 {ip}:6373 {ip}:6374 {ip}:6375 {ip}:6376 --cluster-replicas 1
4
Install docker compose
1
2curl -SL https://github.com/docker/compose/releases/download/v2.9.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
3