Redis 集群安装实验手册(三) - 集群模式

上一篇文章,我们介绍了 Redis 的哨兵模式,本文进一步介绍: Redis Cluster。
Redis Cluster 是 Redis 3.0 开始新增的一种群集模式, 由多个Redis节点组成。不同节点之间数据不重复,每个节点在逻辑上对应多个数据分片。在每个节点内部分又为主、备节点,支持一主多从结构,

实验环境

操作系统: Ubuntu 18.04
Docker: 18.09 ( 安装可参考 在Ubuntu 18.04 中安装 Docker )
Docker-Compose: 1.8.0 (可以执行 sudo apt install docker-compose 进行安装)
Redis-Cli: 3.0.6 (可以执行 sudo apt install redis-tools 进行安装)

实验目标

在主从的基础上,搭建用于监控主从的哨兵。 在本次实验中,将搭建一个具有三个节点的集群。每个节点包含一个主Redis Server和一个从Redis Server,每个 Redis Server运行于 Docker 中,使用主机的不同端口(host网络模式)。

为尽量简化实验的复杂度,使用 Docker 和 Docker-Compose 进行配置,并且不配置存储,使用简单的 host 网络模式。

Redis Server 配置文件

为便于区分,在实验中我们将配置文件的文件名与端口号对应。 6个实例的配置文件分别如下:

  1. node-6581.conf
1
2
3
4
5
6
7
8
port 6581
requirepass redis.123
masterauth redis.123
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file node-6581.conf
  1. node-6582.conf
1
2
3
4
5
6
7
8
port 6582
requirepass redis.123
masterauth redis.123
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file node-6582.conf
  1. node-6583.conf
1
2
3
4
5
6
7
8
port 6583
requirepass redis.123
masterauth redis.123
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file node-6583.conf
  1. node-6584.conf
1
2
3
4
5
6
7
8
port 6584
requirepass redis.123
masterauth redis.123
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file node-6584.conf
  1. node-6585.conf
1
2
3
4
5
6
7
8
port 6585
requirepass redis.123
masterauth redis.123
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file node-6585.conf
  1. node-6586.conf
1
2
3
4
5
6
7
8
port 6586
requirepass redis.123
masterauth redis.123
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file node-6586.conf

Docker-Compose 配置文件

定义好6个 Redis Server 的配置文件后, 现在用 Docker-Compose 把它们的运行组合起来,新建名为: docker-compose.yml 的文件,内容如下:

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
version: '2'

services:
redis-node-6581:
image: redis
container_name: redis-node-6581
network_mode: host
volumes:
- ./node-6581.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-node-6582:
image: redis
container_name: redis-node-6582
network_mode: host
volumes:
- ./node-6582.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-node-6583:
image: redis
container_name: redis-node-6583
network_mode: host
volumes:
- ./node-6583.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-node-6584:
image: redis
container_name: redis-node-6584
network_mode: host
volumes:
- ./node-6584.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-node-6585:
image: redis
container_name: redis-node-6585
network_mode: host
volumes:
- ./node-6585.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-node-6586:
image: redis
container_name: redis-node-6586
network_mode: host
volumes:
- ./node-6586.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]

启动 Redis Cluster 集群

在 docker-compose.yml 所在的目录执行:

1
docker-compose up -d

系统显示:

1
2
3
4
5
6
Creating redis-node-6583
Creating redis-node-6585
Creating redis-node-6582
Creating redis-node-6581
Creating redis-node-6586
Creating redis-node-6584

如果使用 docker ps 命令,可以看到六个个容器被启动了。

1
2
3
4
5
6
7
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
16199a8d6aac redis "docker-entrypoint.s…" 36 seconds ago Up 34 seconds redis-node-6584
7bb837289c60 redis "docker-entrypoint.s…" 36 seconds ago Up 34 seconds redis-node-6586
5a4b7716fb8f redis "docker-entrypoint.s…" 36 seconds ago Up 34 seconds redis-node-6581
4aca3e5f7c25 redis "docker-entrypoint.s…" 36 seconds ago Up 35 seconds redis-node-6582
065b4f298ad6 redis "docker-entrypoint.s…" 36 seconds ago Up 34 seconds redis-node-6585
b9e6106c4ef6 redis "docker-entrypoint.s…" 36 seconds ago Up 35 seconds redis-node-6583

实例启动后,接下来要执行构建集群的命令:

首先,执行下面的命令登入到任何一个 Redis Server 实例中(我们使用 redis-node-6581),

1
docker exec -it redis-node-6581 /bin/bash

看到提示符后,执行下面的命令登入启动 Redis-Cli:

1
redis-cli -p 6581 -a redis.123

然后执行下面的命令构建集群

1
2
3
4
5
6
7
8
redis-cli -p 6581 -a redis.123 --cluster create \
127.0.0.1:6581 \
127.0.0.1:6582 \
127.0.0.1:6583 \
127.0.0.1:6584 \
127.0.0.1:6585 \
127.0.0.1:6586 \
--cluster-replicas 1

系统会提示你是否覆盖配置信息,输入: yes

然后系统输出:

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
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6585 to 127.0.0.1:6581
Adding replica 127.0.0.1:6586 to 127.0.0.1:6582
Adding replica 127.0.0.1:6584 to 127.0.0.1:6583
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 2485951fd0f3066e71ee95d4f974952e7708166c 127.0.0.1:6581
slots:[0-5460] (5461 slots) master
M: 508c944750d5246579b5f30bab85f609050f13f1 127.0.0.1:6582
slots:[5461-10922] (5462 slots) master
M: 3852752a9d80920a9625b130ddedff4937851f58 127.0.0.1:6583
slots:[10923-16383] (5461 slots) master
S: 1401da40aa9c29defe2b155760eff318b958c7af 127.0.0.1:6584
replicates 3852752a9d80920a9625b130ddedff4937851f58
S: 4b41a3778864e153601cbc5096ac8d0864e8a057 127.0.0.1:6585
replicates 2485951fd0f3066e71ee95d4f974952e7708166c
S: 500d8b41097575f159f1e6101c65b3b8b031e2bb 127.0.0.1:6586
replicates 508c944750d5246579b5f30bab85f609050f13f1
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 127.0.0.1:6581)
M: 2485951fd0f3066e71ee95d4f974952e7708166c 127.0.0.1:6581
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 500d8b41097575f159f1e6101c65b3b8b031e2bb 127.0.0.1:6586
slots: (0 slots) slave
replicates 508c944750d5246579b5f30bab85f609050f13f1
S: 4b41a3778864e153601cbc5096ac8d0864e8a057 127.0.0.1:6585
slots: (0 slots) slave
replicates 2485951fd0f3066e71ee95d4f974952e7708166c
S: 1401da40aa9c29defe2b155760eff318b958c7af 127.0.0.1:6584
slots: (0 slots) slave
replicates 3852752a9d80920a9625b130ddedff4937851f58
M: 3852752a9d80920a9625b130ddedff4937851f58 127.0.0.1:6583
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 508c944750d5246579b5f30bab85f609050f13f1 127.0.0.1:6582
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看集群信息

登入任何一个 Redis Server 实例(实验中使用 redis-node-6581),执行以下命令,可以查看集群的相关信息

1
docker exec -it redis-node-6581 /bin/bash

看到提示符后,执行下面的命令登入启动 Redis-Cli:

1
redis-cli -p 6581 -a redis.123

看到提示符后,执行下面的命令:

1
cluster info

查看集群信息,系统显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:511
cluster_stats_messages_pong_sent:558
cluster_stats_messages_sent:1069
cluster_stats_messages_ping_received:553
cluster_stats_messages_pong_received:511
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1069

也可以执行下面的命令查看集群中节点的信息:

1
cluster nodes

系统显示:

1
2
3
4
5
6
500d8b41097575f159f1e6101c65b3b8b031e2bb 127.0.0.1:6586@16586 slave 508c944750d5246579b5f30bab85f609050f13f1 0 1600737423000 2 connected
4b41a3778864e153601cbc5096ac8d0864e8a057 127.0.0.1:6585@16585 slave 2485951fd0f3066e71ee95d4f974952e7708166c 0 1600737424214 1 connected
1401da40aa9c29defe2b155760eff318b958c7af 127.0.0.1:6584@16584 slave 3852752a9d80920a9625b130ddedff4937851f58 0 1600737422212 3 connected
3852752a9d80920a9625b130ddedff4937851f58 127.0.0.1:6583@16583 master - 0 1600737421209 3 connected 10923-16383
2485951fd0f3066e71ee95d4f974952e7708166c 127.0.0.1:6581@16581 myself,master - 0 1600737423000 1 connected 0-5460
508c944750d5246579b5f30bab85f609050f13f1 127.0.0.1:6582@16582 master - 0 1600737423213 2 connected 5461-10922

本文标题:Redis 集群安装实验手册(三) - 集群模式

文章作者:晨星

发布时间:2020年09月22日 - 08:09

最后更新:2020年09月22日 - 09:09

原始链接:https://www.mls-tech.info/redis/redis-setup-cluster/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。