参考文献

redis.conf配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 连接端口
port 6371
# 允许其他机器访问redis服务
bind 0.0.0.0
# 数据目录
dir /data
# 日志文件, 默认输出到控制台
logfile /logs/nodes-6371.log
appendonly yes
protected-mode no
# 密码
requirepass holelin..

# 开启cluster集群模式
cluster-enabled yes
# 集群配置信息文件名, 记录了每个节点的地址、角色(master/slave)、slot等信息, 由redis自己生成, 位于数据目录下
cluster-config-file nodes-6371.conf
# 集群节点连接超时时间,每个节点都会检查其它节点是否挂了
cluster-node-timeout 5000
# 从节点需要配置主节点的密码, 不然主从同步时从节点无法从主节点同步到数据
masterauth holelin..
# 宿主机的IP
cluster-announce-ip 192.168.11.216
# docker所在的宿主机映射端口
cluster-announce-port 6371
# docker所在的宿主机总线映射端口
cluster-announce-bus-port 16371

目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 创建目录结构
mkdir -p redis-cluster/redis-637{1..6}/{conf,data}
# 复制配置文件
for i in {1..6}; do cp redis.conf redis-cluster/redis-637${i}/conf; done
# 修改配置文件
# 批量修改
for i in {1..6}; do
# 修改端口
sed -i "s/port 6371/port 637${i}/" redis-cluster/redis-637${i}/conf/redis.conf;
# 修改cluster-announce-port
sed -i "s/cluster-announce-port 6371/cluster-announce-port 637${i}/" redis-cluster/redis-637${i}/conf/redis.conf;
# 修改cluster-announce-bus-port
sed -i "s/cluster-announce-bus-port 16371/cluster-announce-bus-port 1637${i}/" redis-cluster/redis-637${i}/conf/redis.conf;
# 修改cluster-config-file
sed -i "s/cluster-config-file nodes-6371.conf/cluster-config-file nodes-637${i}.conf/" redis-cluster/redis-637${i}/conf/redis.conf;
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
redis-cluster/
├── redis-6371
│   ├── conf
│   │   └── redis.conf
│   └── data
│   ├── appendonlydir [error opening dir]
│   └── nodes-6371.conf
├── redis-6372
│   ├── conf
│   │   └── redis.conf
│   └── data
│   ├── appendonlydir [error opening dir]
│   └── nodes-6372.conf
├── redis-6373
│   ├── conf
│   │   └── redis.conf
│   └── data
│   ├── appendonlydir [error opening dir]
│   └── nodes-6373.conf
├── redis-6374
│   ├── conf
│   │   └── redis.conf
│   └── data
│   ├── appendonlydir [error opening dir]
│   ├── dump.rdb
│   └── nodes-6374.conf
├── redis-6375
│   ├── conf
│   │   └── redis.conf
│   └── data
│   ├── appendonlydir [error opening dir]
│   ├── dump.rdb
│   └── nodes-6375.conf
├── redis-6376
│   ├── conf
│   │   └── redis.conf
│   └── data
│   ├── appendonlydir [error opening dir]
│   ├── dump.rdb
│   └── nodes-6376.conf
└── redis-cluster.yaml

docker-compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
version: "3"
services:
redis-6371:
image: redis:7.2.4
container_name: redis-6371
restart: always
volumes:
- ./redis-6371/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6371/data:/data
ports:
- 6371:6371
- 16371:16371
command:
redis-server /usr/local/etc/redis/redis.conf

redis-6372:
image: redis:7.2.4
container_name: redis-6372
volumes:
- ./redis-6372/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6372/data:/data
ports:
- 6372:6372
- 16372:16372
command:
redis-server /usr/local/etc/redis/redis.conf

redis-6373:
image: redis:7.2.4
container_name: redis-6373
volumes:
- ./redis-6373/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6373/data:/data
ports:
- 6373:6373
- 16373:16373
command:
redis-server /usr/local/etc/redis/redis.conf

redis-6374:
image: redis:7.2.4
container_name: redis-6374
restart: always
volumes:
- ./redis-6374/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6374/data:/data
ports:
- 6374:6374
- 16374:16374
command:
redis-server /usr/local/etc/redis/redis.conf

redis-6375:
image: redis:7.2.4
container_name: redis-6375
volumes:
- ./redis-6375/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6375/data:/data
ports:
- 6375:6375
- 16375:16375
command:
redis-server /usr/local/etc/redis/redis.conf

redis-6376:
image: redis:7.2.4
container_name: redis-6376
volumes:
- ./redis-6376/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6376/data:/data
ports:
- 6376:6376
- 16376:16376
command:
redis-server /usr/local/etc/redis/redis.conf
  • 编写完成docker-compose.yaml文件后,使用docker-compose -f redis-cluster.yaml up -d启动容器

创建集群

  • 进入任意容器,此处进入redis-6371容器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# docker exec -it redis-6371 bash
# redis-cli -a holelin.. --cluster create 192.168.11.216:6371 192.168.11.216:6372 192.168.11.216:6373 192.168.11.216:6374 192.168.11.216:6375 192.168.11.216:6376 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.11.216:6375 to 192.168.11.216:6371
Adding replica 192.168.11.216:6376 to 192.168.11.216:6372
Adding replica 192.168.11.216:6374 to 192.168.11.216:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 985b1aa88ec8bd0f97c70e4b9e42d9a5adf0dff7 192.168.11.216:6371
slots:[0-5460] (5461 slots) master
M: 26658fcd193363310b2bfa0dfaea8bf1f144f94d 192.168.11.216:6372
slots:[5461-10922] (5462 slots) master
M: a2687c2f2781940a9d3b6dc3b4810851cad52959 192.168.11.216:6373
slots:[10923-16383] (5461 slots) master
S: 0d7f6fe5260fa7668a9df1e814d18e4e67cb0022 192.168.11.216:6374
replicates a2687c2f2781940a9d3b6dc3b4810851cad52959
S: c16a4ae51fa4fe3674c01155302f5758ca6cd86f 192.168.11.216:6375
replicates 985b1aa88ec8bd0f97c70e4b9e42d9a5adf0dff7
S: 3e3713b3bcda56ca322a0b8d4ace45d16ed47b98 192.168.11.216:6376
replicates 26658fcd193363310b2bfa0dfaea8bf1f144f94d
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.11.216:6371)
M: 985b1aa88ec8bd0f97c70e4b9e42d9a5adf0dff7 192.168.11.216:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: a2687c2f2781940a9d3b6dc3b4810851cad52959 192.168.11.216:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 0d7f6fe5260fa7668a9df1e814d18e4e67cb0022 192.168.11.216:6374
slots: (0 slots) slave
replicates a2687c2f2781940a9d3b6dc3b4810851cad52959
M: 26658fcd193363310b2bfa0dfaea8bf1f144f94d 192.168.11.216:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 3e3713b3bcda56ca322a0b8d4ace45d16ed47b98 192.168.11.216:6376
slots: (0 slots) slave
replicates 26658fcd193363310b2bfa0dfaea8bf1f144f94d
S: c16a4ae51fa4fe3674c01155302f5758ca6cd86f 192.168.11.216:6375
slots: (0 slots) slave
replicates 985b1aa88ec8bd0f97c70e4b9e42d9a5adf0dff7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 看到上面的输出即为 Cluster 集群配置完成.且为 3 主 3 从.