Redis Cluster 公网部署

RT,如果要公网访问服务器的 Redis,要将 redis-cluster 部署在公网上(似乎是废话,但是如果 Redis 服务发现端口是本地的话,就没法公网访问)。

config 中有几个很重要的参数:

# redis-cluster.conf
cluster-announce-ip 116.205.130.21 # 公网ip
cluster-announce-port 8003 # redis service 暴露给外部的端口
cluster-announce-bus-port 18003 #  redis 集群总线端口

其他就不说了,主要是总线端口,它是 cluster 服务发现的端口,如果填的是局域网 ip(192.168.xxx.xxx)那么就无法被外部访问,因为在 Redis 对 key 进行 hash 的时候,如果处理 key 分片不是本 Redis 服务器的话,会提示Redirected to slot [5798] located at 192.168.xxx.xxx:$xxx(重定向到其他 Redis 服务器),无法被公网访问。 所以要将 cluster 服务发现的 ip 设为公网 ip,还要记得在服务器上暴露服务 ip 和服务发现 ip 以及关闭对应的防火墙。

# 8000/redis-cluster.conf
protected-mode no
port 8000
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_8000.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump8000.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
appendonly yes
appendfilename "appendonly8000.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 15000
cluster-announce-ip 116.205.130.21
cluster-announce-port 8000
cluster-announce-bus-port 18000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread 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
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
version: '3.3'
services:
  redis-cluster:
    image: redis:latest
    command: redis-cli --cluster create 116.205.130.21:8000 116.205.130.21:8001 116.205.130.21:8002 116.205.130.21:8003 116.205.130.21:8004 116.205.130.21:8005 --cluster-replicas 1  --cluster-yes
    depends_on:
      - redis-8000
      - redis-8001
      - redis-8002
      - redis-8003
      - redis-8004
      - redis-8005
  redis-8000: # 服务名称
    image: redis:latest # 创建容器时所需的镜像
    container_name: redis-8000 # 容器名称
    ports:
      - 8000:8000
      - 18000:18000
    volumes: # 数据卷,目录挂载
      - ./8000/redis-cluster.conf:/usr/local/etc/redis/redis.conf
      - ./8000/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令

  redis-8001:
    image: redis:latest
    container_name: redis-8001
    ports:
      - 8001:8001
      - 18001:18001
    volumes:
      - ./8001/redis-cluster.conf:/usr/local/etc/redis/redis.conf
      - ./8001/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-8002:
    image: redis:latest
    container_name: redis-8002
    ports:
      - 8002:8002
      - 18002:18002
    volumes:
      - ./8002/redis-cluster.conf:/usr/local/etc/redis/redis.conf
      - ./8002/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-8003:
    image: redis:latest
    container_name: redis-8003
    ports:
      - 8003:8003
      - 18003:18003
    volumes:
      - ./8003/redis-cluster.conf:/usr/local/etc/redis/redis.conf
      - ./8003/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-8004:
    image: redis:latest
    container_name: redis-8004
    ports:
      - 8004:8004
      - 18004:18004
    volumes:
      - ./8004/redis-cluster.conf:/usr/local/etc/redis/redis.conf
      - ./8004/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-8005:
    image: redis:latest
    container_name: redis-8005
    ports:
      - 8005:8005
      - 18005:18005
    volumes:
      - ./8005/redis-cluster.conf:/usr/local/etc/redis/redis.conf
      - ./8005/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf
updatedupdated2024-06-122024-06-12