docker-compose redis cluster bridge

docker compose 集群 network: bridge

建议使用 network: host.

由于 Redis 集群不支持网络转发,因此 Docker 搭建 Redis 集群需要注意网络的设置。搭建一个子网是没问题的,集群可以跑起来可以用,但是宿主机是无法使用集群的,只能在子网内部使用

prepare

集群搭建

由于 Redis 集群不支持网络转发,因此 Docker 搭建 Redis 集群需要注意网络的设置。搭建一个子网是没问题的,集群可以跑起来可以用,但是宿主机是无法使用集群的,只能在子网内部使用,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 先到 6380 映射的机器上创建 “hello”
zohar@ZOHAR-LAPTOP:~$ redis-cli -c -p 6380
127.0.0.1:6380> ping
PONG
127.0.0.1:6380> set hello world
OK
127.0.0.1:6380> exit

# 再到 6381 映射的机器上取 “hello”
zohar@ZOHAR-LAPTOP:~$ redis-cli -c -p 6381
127.0.0.1:6381> ping
PONG
127.0.0.1:6381> get hello
-> Redirected to slot [866] located at 172.26.0.101:6379
>


# 客户端卡住了

由于 hello 这个 key 已经在 6380 上被写入了,我们切到 6381 机器上查询的时候,6381 会让我们的客户的自动跳转到存储该 key 的节点上。
但是因为我们定义的是内网的集群,所以跳转用的是内网的 IP 与地址,外网无法连接,所以客户端就无法使用了。
所以创建 Docker 创建 Redis 集群直接使用宿主机网络最好,即使用 “host” 模式。
由于基于 WSL2 的 Docker Host 模式存在问题,因此基于 Host 模式的我直接在 Debian 真机上操作,基于 Bridge 模式的我使用 WSL2 操作。

prepare

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
76
77
78
79
80
81
82
83
84
85


cd /root/apps/bridge-cluster

# 删除node1 ~ node6/data 下的文件
rm -rf node{1,2,3,4,5,6}/data/*
## init
rm -rf node*
ls

mkdir node1
cd node1
mkdir data
touch redis.conf

#然后复制
cd /root/apps/bridge-cluster
cp -r node1 node2
cp -r node1 node3
cp -r node1 node4
cp -r node1 node5
cp -r node1 node6

echo '
port 6379
protected-mode no
daemonize no

##########cluster###########
cluster-enabled yes
cluster-node-timeout 15000

' > /root/apps/bridge-cluster/node1/redis.conf

echo '
port 6379
protected-mode no
daemonize no

##########cluster###########
cluster-enabled yes
cluster-node-timeout 15000
' > /root/apps/bridge-cluster/node2/redis.conf

echo '
port 6379
protected-mode no
daemonize no

##########cluster###########
cluster-enabled yes
cluster-node-timeout 15000
' > /root/apps/bridge-cluster/node3/redis.conf

echo '
port 6379
protected-mode no
daemonize no

##########cluster###########
cluster-enabled yes
cluster-node-timeout 15000
' > /root/apps/bridge-cluster/node4/redis.conf

echo '
port 6379
protected-mode no
daemonize no

##########cluster###########
cluster-enabled yes
cluster-node-timeout 15000
' > /root/apps/bridge-cluster/node5/redis.conf

echo '
port 6379
protected-mode no
daemonize no

##########cluster###########
cluster-enabled yes
cluster-node-timeout 15000
' > /root/apps/bridge-cluster/node6/redis.conf


  • 目录结构
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    ├── docker-compose.yaml
    ├── node1
    │ ├── data
    │ └── redis.conf
    ├── node2
    │ ├── data
    │ └── redis.conf
    ├── node3
    │ ├── data
    │ └── redis.conf
    ├── node4
    │ ├── data
    │ └── redis.conf
    ├── node5
    │ ├── data
    │ └── redis.conf
    └── node6
    ├── data
    └── redis.conf

    docker-compose.yaml

    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    version: "3"

    networks:
    redis-cluster:
    driver: bridge
    ipam:
    driver: default
    config:
    - subnet: 172.26.0.0/24

    services:
    node1:
    image: redis
    container_name: redis-cluster-node-1
    ports:
    - "6371:6379"
    volumes:
    - "./node1/redis.conf:/etc/redis.conf"
    - "./node1/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
    redis-cluster:
    ipv4_address: 172.26.0.101

    node2:
    image: redis
    container_name: redis-cluster-node-2
    ports:
    - "6372:6379"
    volumes:
    - "./node2/redis.conf:/etc/redis.conf"
    - "./node2/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
    redis-cluster:
    ipv4_address: 172.26.0.102

    node3:
    image: redis
    container_name: redis-cluster-node-3
    ports:
    - "6373:6379"
    volumes:
    - "./node3/redis.conf:/etc/redis.conf"
    - "./node3/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
    redis-cluster:
    ipv4_address: 172.26.0.103

    node4:
    image: redis
    container_name: redis-cluster-node-4
    ports:
    - "6374:6379"
    volumes:
    - "./node4/redis.conf:/etc/redis.conf"
    - "./node4/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
    redis-cluster:
    ipv4_address: 172.26.0.104

    node5:
    image: redis
    container_name: redis-cluster-node-5
    ports:
    - "6375:6379"
    volumes:
    - "./node5/redis.conf:/etc/redis.conf"
    - "./node5/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
    redis-cluster:
    ipv4_address: 172.26.0.105

    node6:
    image: redis
    container_name: redis-cluster-node-6
    ports:
    - "6376:6379"
    volumes:
    - "./node6/redis.conf:/etc/redis.conf"
    - "./node6/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
    redis-cluster:
    ipv4_address: 172.26.0.106

启动集群:

1
docker-compose up -d

进入实例内部启动集群:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

docker-compose up -d

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

redis-cli --cluster create 172.26.0.101:6379 172.26.0.102:6379 172.26.0.103:6379 172.26.0.104:6379 172.26.0.105:6379 172.26.0.106:6379 --cluster-replicas 1


redis-cli --cluster create \
192.168.171.136:6371 \
192.168.171.136:6372 \
192.168.171.136:6373 \
192.168.171.136:6374 \
192.168.171.136:6375 \
192.168.171.136:6376 \
--cluster-replicas 1

查询节点信息:

1
2
3
redis-cli -c

cluster nodes

ps命令在docker容器不存在

1
2
3
4
5
6
7
8
apt-get update && apt-get install procps -y
apt install net-tools
apt-get install iputils-ping -y

apt-get install openbsd-inetd -y
#安装 openbsd-inetd,如果已经安装过了,直接执行下面的步骤。
apt-get install telnetd -y
#安装 telnetd

验证集群

  • 容器内网:
1
2
3
4
5
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> set test redsi
-> Redirected to slot [6918] located at 172.26.0.102:6379
OK
  • 宿主机:
    1
    2
    3
    4
    5
    6
    7
    redis-cli -c -p 6371
    127.0.0.1:6371> get hello
    "world"
    127.0.0.1:6371> get test
    -> Redirected to slot [6918] located at 172.26.0.102:6379

    # 卡住

无法进行服务切换。

参考文章

评论