Ubuntu 22.04 Redis 六節點叢集部署

Ubuntu 22.04 Redis 6-Node Cluster Deployment

本文將詳細介紹如何在 Ubuntu 22.04 環境下部署 Redis 六節點叢集,實現高可用性和資料分片功能。

Redis Cluster 架構概述

Redis Cluster 是 Redis 官方提供的分散式解決方案,提供以下核心功能:

  • 資料分片 (Sharding):將資料自動分散到多個節點,支援水平擴展
  • 高可用性 (High Availability):透過主從複製和自動故障轉移確保服務持續運作
  • 去中心化架構:無需額外的 Proxy 或 Sentinel,節點間透過 Gossip 協議通訊

Hash Slot 機制

Redis Cluster 使用 16384 個 Hash Slot 來分配資料:

1
HASH_SLOT = CRC16(key) mod 16384

每個主節點負責一部分 Hash Slot,當存取某個 Key 時,客戶端會根據 CRC16 演算法計算出對應的 Slot,並將請求發送到負責該 Slot 的節點。

叢集通訊

  • Cluster Bus:節點間使用 Port + 10000 進行叢集內部通訊(例如:6379 使用 16379)
  • Gossip 協議:用於節點狀態同步、故障偵測
  • MOVED/ASK 重定向:當客戶端存取的 Key 不在當前節點時,會返回重定向指令

六節點規劃(3 主 3 從)

節點架構

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
┌─────────────────────────────────────────────────────────────┐
│                    Redis Cluster (6 Nodes)                   │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐       │
│  │   Master 1   │  │   Master 2   │  │   Master 3   │       │
│  │  10.0.0.11   │  │  10.0.0.12   │  │  10.0.0.13   │       │
│  │  Port: 6379  │  │  Port: 6379  │  │  Port: 6379  │       │
│  │ Slots: 0-5460│  │Slots: 5461-  │  │Slots: 10923- │       │
│  │              │  │     10922    │  │     16383    │       │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘       │
│         │                 │                 │                │
│         ▼                 ▼                 ▼                │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐       │
│  │   Slave 1    │  │   Slave 2    │  │   Slave 3    │       │
│  │  10.0.0.14   │  │  10.0.0.15   │  │  10.0.0.16   │       │
│  │  Port: 6379  │  │  Port: 6379  │  │  Port: 6379  │       │
│  │ (replicate   │  │ (replicate   │  │ (replicate   │       │
│  │  Master 1)   │  │  Master 2)   │  │  Master 3)   │       │
│  └──────────────┘  └──────────────┘  └──────────────┘       │
│                                                              │
└─────────────────────────────────────────────────────────────┘

節點清單

角色主機名稱IP 位址Port負責 Slots
Master 1redis-node-0110.0.0.1163790-5460
Master 2redis-node-0210.0.0.1263795461-10922
Master 3redis-node-0310.0.0.13637910923-16383
Slave 1redis-node-0410.0.0.146379- (複製 Master 1)
Slave 2redis-node-0510.0.0.156379- (複製 Master 2)
Slave 3redis-node-0610.0.0.166379- (複製 Master 3)

硬體需求建議

  • CPU:2 核心以上
  • 記憶體:4GB 以上(根據資料量調整)
  • 磁碟:SSD 建議,至少 20GB
  • 網路:1Gbps 內網,低延遲

節點安裝與設定

以下步驟需在所有六個節點上執行。

系統準備

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 更新系統套件
sudo apt update && sudo apt upgrade -y

# 設定主機名稱(每個節點執行對應的命令)
sudo hostnamectl set-hostname redis-node-01  # 在節點 1 執行
sudo hostnamectl set-hostname redis-node-02  # 在節點 2 執行
# ... 以此類推

# 設定 hosts 檔案(所有節點)
sudo tee -a /etc/hosts << EOF
10.0.0.11 redis-node-01
10.0.0.12 redis-node-02
10.0.0.13 redis-node-03
10.0.0.14 redis-node-04
10.0.0.15 redis-node-05
10.0.0.16 redis-node-06
EOF

安裝 Redis

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 安裝 Redis Server
sudo apt install redis-server -y

# 確認 Redis 版本
redis-server --version
# Redis server v=6.0.16 或更高版本

# 停止預設的 Redis 服務
sudo systemctl stop redis-server
sudo systemctl disable redis-server

系統調優

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 設定記憶體 overcommit
echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf

# 停用 Transparent Huge Pages (THP)
echo 'never' | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' | sudo tee /sys/kernel/mm/transparent_hugepage/defrag

# 設定開機時停用 THP
sudo tee /etc/rc.local << 'EOF'
#!/bin/bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
exit 0
EOF
sudo chmod +x /etc/rc.local

# 調整 TCP backlog
echo 'net.core.somaxconn = 65535' | sudo tee -a /etc/sysctl.conf

# 套用系統參數
sudo sysctl -p

建立 Redis Cluster 目錄結構

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 建立 Redis Cluster 專用目錄
sudo mkdir -p /etc/redis/cluster
sudo mkdir -p /var/lib/redis/cluster
sudo mkdir -p /var/log/redis
sudo mkdir -p /var/run/redis

# 設定目錄權限
sudo chown -R redis:redis /var/lib/redis
sudo chown -R redis:redis /var/log/redis
sudo chown -R redis:redis /var/run/redis

Redis Cluster 設定檔

建立 Redis Cluster 設定檔 /etc/redis/cluster/redis-cluster.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
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
sudo tee /etc/redis/cluster/redis-cluster.conf << 'EOF'
# 基本設定
bind 0.0.0.0
port 6379
daemonize no
supervised systemd
pidfile /var/run/redis/redis-cluster.pid
loglevel notice
logfile /var/log/redis/redis-cluster.log

# 資料持久化
dir /var/lib/redis/cluster
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# RDB 快照設定
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb

# 記憶體管理
maxmemory 2gb
maxmemory-policy allkeys-lru

# Cluster 設定
cluster-enabled yes
cluster-config-file /var/lib/redis/cluster/nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 10.0.0.11  # 請依據各節點修改
cluster-announce-port 6379
cluster-announce-bus-port 16379

# 安全設定
requirepass your_strong_password_here
masterauth your_strong_password_here

# 效能調優
tcp-backlog 65535
tcp-keepalive 300
timeout 0

# 複製設定
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
EOF

注意:每個節點需修改 cluster-announce-ip 為該節點的 IP 位址。

建立 Systemd 服務

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo tee /etc/systemd/system/redis-cluster.service << 'EOF'
[Unit]
Description=Redis Cluster Node
After=network.target

[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/cluster/redis-cluster.conf
ExecStop=/usr/bin/redis-cli -a your_strong_password_here shutdown
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65535
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF

# 重新載入 systemd 設定
sudo systemctl daemon-reload

設定防火牆

1
2
3
4
5
6
# 開放 Redis 服務埠
sudo ufw allow 6379/tcp comment 'Redis Cluster'
sudo ufw allow 16379/tcp comment 'Redis Cluster Bus'

# 確認防火牆規則
sudo ufw status

啟動 Redis 節點

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 啟動 Redis Cluster 服務
sudo systemctl start redis-cluster
sudo systemctl enable redis-cluster

# 確認服務狀態
sudo systemctl status redis-cluster

# 測試連線
redis-cli -a your_strong_password_here ping
# 應回應: PONG

叢集建立與初始化

確認所有六個節點都已啟動後,在任一節點上執行叢集建立命令。

建立叢集

1
2
3
4
5
6
7
8
9
# 使用 redis-cli 建立叢集(在任一節點執行)
redis-cli -a your_strong_password_here --cluster create \
  10.0.0.11:6379 \
  10.0.0.12:6379 \
  10.0.0.13:6379 \
  10.0.0.14:6379 \
  10.0.0.15:6379 \
  10.0.0.16:6379 \
  --cluster-replicas 1

執行後會顯示 Slot 分配計畫,輸入 yes 確認:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.15:6379 to 10.0.0.11:6379
Adding replica 10.0.0.16:6379 to 10.0.0.12:6379
Adding replica 10.0.0.14:6379 to 10.0.0.13:6379
>>> Trying to optimize slaves allocation for anti-affinity
...
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 10.0.0.11:6379)
...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

驗證叢集狀態

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 檢查叢集資訊
redis-cli -a your_strong_password_here -c cluster info

# 預期輸出:
# 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
# ...
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 檢查節點資訊
redis-cli -a your_strong_password_here -c cluster nodes

# 預期輸出範例:
# 7c9e... 10.0.0.11:6379@16379 myself,master - 0 0 1 connected 0-5460
# 8d3f... 10.0.0.12:6379@16379 master - 0 1698672000000 2 connected 5461-10922
# 9a2b... 10.0.0.13:6379@16379 master - 0 1698672000000 3 connected 10923-16383
# a4c5... 10.0.0.14:6379@16379 slave 9a2b... 0 1698672000000 3 connected
# b5d6... 10.0.0.15:6379@16379 slave 7c9e... 0 1698672000000 1 connected
# c6e7... 10.0.0.16:6379@16379 slave 8d3f... 0 1698672000000 2 connected

測試叢集讀寫

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 使用 -c 參數啟用叢集模式
redis-cli -a your_strong_password_here -c -h 10.0.0.11 -p 6379

# 寫入資料測試
127.0.0.1:6379> set user:1001 "Alice"
-> Redirected to slot [5765] located at 10.0.0.12:6379
OK

127.0.0.1:6379> set user:1002 "Bob"
-> Redirected to slot [10839] located at 10.0.0.12:6379
OK

127.0.0.1:6379> set user:1003 "Charlie"
-> Redirected to slot [1437] located at 10.0.0.11:6379
OK

# 讀取資料測試
127.0.0.1:6379> get user:1001
-> Redirected to slot [5765] located at 10.0.0.12:6379
"Alice"

Slot 分配與資料分片

查看 Slot 分配

1
2
3
4
5
6
7
8
9
# 查看各節點負責的 Slots
redis-cli -a your_strong_password_here --cluster check 10.0.0.11:6379

# 輸出範例:
# 10.0.0.11:6379 (7c9e...) -> 3 keys | 5461 slots | 1 slaves
# 10.0.0.12:6379 (8d3f...) -> 2 keys | 5462 slots | 1 slaves
# 10.0.0.13:6379 (9a2b...) -> 1 keys | 5461 slots | 1 slaves
# [OK] 6 keys in 3 masters.
# 0.00 keys per slot on average.

Hash Tag 功能

當需要確保相關的 Key 儲存在同一個 Slot 時,可使用 Hash Tag:

1
2
3
4
5
6
7
8
9
# 使用 {} 包含的部分作為 Hash 計算依據
127.0.0.1:6379> set {user:1001}:profile "profile data"
OK
127.0.0.1:6379> set {user:1001}:sessions "session data"
OK
127.0.0.1:6379> set {user:1001}:preferences "preferences data"
OK

# 以上三個 Key 會被分配到同一個 Slot

手動遷移 Slot

若需要手動調整 Slot 分配(例如增減節點後重新平衡):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 使用 reshard 命令
redis-cli -a your_strong_password_here --cluster reshard 10.0.0.11:6379

# 互動式選擇:
# How many slots do you want to move (from 1 to 16384)? 1000
# What is the receiving node ID? <目標節點 ID>
# Please enter all the source node IDs.
#   Type 'all' to use all the nodes as source nodes for the hash slots.
#   Type 'done' once you entered all the source nodes IDs.
# Source node #1: <來源節點 ID>
# Source node #2: done

自動重新平衡

1
2
3
4
5
6
# 自動平衡各節點的 Slot 數量
redis-cli -a your_strong_password_here --cluster rebalance 10.0.0.11:6379

# 使用權重進行平衡
redis-cli -a your_strong_password_here --cluster rebalance 10.0.0.11:6379 \
  --cluster-weight <node-id-1>=1 <node-id-2>=2 <node-id-3>=1

故障轉移測試

模擬主節點故障

1
2
3
4
5
# 在 Master 1 (10.0.0.11) 上模擬故障
redis-cli -a your_strong_password_here -h 10.0.0.11 -p 6379 DEBUG SEGFAULT

# 或直接停止服務
sudo systemctl stop redis-cluster

觀察故障轉移

1
2
3
4
5
# 在其他節點上監控叢集狀態
redis-cli -a your_strong_password_here -h 10.0.0.12 -c cluster nodes

# 觀察原 Slave 是否提升為 Master
# 預期結果:10.0.0.14 (原 Slave) 變成 master,接管 0-5460 slots

手動故障轉移

當需要進行維護時,可以手動觸發故障轉移:

1
2
3
4
5
6
# 在 Slave 節點上執行手動故障轉移
redis-cli -a your_strong_password_here -h 10.0.0.14 -p 6379
127.0.0.1:6379> CLUSTER FAILOVER

# TAKEOVER 模式(強制接管,即使 Master 無回應)
127.0.0.1:6379> CLUSTER FAILOVER TAKEOVER

恢復故障節點

1
2
3
4
5
# 重新啟動故障節點
sudo systemctl start redis-cluster

# 節點會自動重新加入叢集,並成為新 Master 的 Slave
redis-cli -a your_strong_password_here -c cluster nodes

叢集維護操作

新增節點

1
2
3
4
5
6
7
8
9
# 新增 Master 節點
redis-cli -a your_strong_password_here --cluster add-node \
  10.0.0.17:6379 10.0.0.11:6379

# 新增 Slave 節點(指定 Master)
redis-cli -a your_strong_password_here --cluster add-node \
  10.0.0.18:6379 10.0.0.11:6379 \
  --cluster-slave \
  --cluster-master-id <master-node-id>

移除節點

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 首先將 Slave 節點移除
redis-cli -a your_strong_password_here --cluster del-node \
  10.0.0.11:6379 <slave-node-id>

# 移除 Master 節點前,需先遷移其所有 Slots
redis-cli -a your_strong_password_here --cluster reshard 10.0.0.11:6379 \
  --cluster-from <master-node-id> \
  --cluster-to <target-node-id> \
  --cluster-slots 5461 \
  --cluster-yes

# 確認 Slots 已清空後,移除 Master
redis-cli -a your_strong_password_here --cluster del-node \
  10.0.0.11:6379 <master-node-id>

叢集修復

1
2
3
4
5
6
7
8
9
# 檢查叢集問題
redis-cli -a your_strong_password_here --cluster check 10.0.0.11:6379

# 自動修復叢集問題
redis-cli -a your_strong_password_here --cluster fix 10.0.0.11:6379

# 修復遺失的 Slots
redis-cli -a your_strong_password_here --cluster fix 10.0.0.11:6379 \
  --cluster-fix-with-unreachable-masters

資料備份

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 手動觸發 BGSAVE(在各 Master 節點執行)
redis-cli -a your_strong_password_here -h 10.0.0.11 BGSAVE
redis-cli -a your_strong_password_here -h 10.0.0.12 BGSAVE
redis-cli -a your_strong_password_here -h 10.0.0.13 BGSAVE

# 備份 RDB 和 AOF 檔案
for node in 11 12 13; do
  scp redis@10.0.0.$node:/var/lib/redis/cluster/dump.rdb ./backup/node-$node-dump.rdb
  scp redis@10.0.0.$node:/var/lib/redis/cluster/appendonly.aof ./backup/node-$node-appendonly.aof
done

滾動升級

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 依序升級各節點(先升級 Slaves,再升級 Masters)

# 1. 升級 Slave 節點
for slave in 10.0.0.14 10.0.0.15 10.0.0.16; do
  ssh $slave "sudo apt update && sudo apt install redis-server -y"
  ssh $slave "sudo systemctl restart redis-cluster"
  sleep 10  # 等待節點重新加入
done

# 2. 對 Master 進行故障轉移後升級
for master in 10.0.0.11 10.0.0.12 10.0.0.13; do
  # 執行故障轉移
  slave=$(redis-cli -a your_strong_password_here -h $master cluster nodes | grep slave | awk '{print $2}' | cut -d: -f1)
  redis-cli -a your_strong_password_here -h $slave CLUSTER FAILOVER
  sleep 5

  # 升級原 Master(現在是 Slave)
  ssh $master "sudo apt update && sudo apt install redis-server -y"
  ssh $master "sudo systemctl restart redis-cluster"
  sleep 10
done

監控與效能調校

叢集監控腳本

建立監控腳本 /usr/local/bin/redis-cluster-monitor.sh

 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
#!/bin/bash

REDIS_PASSWORD="your_strong_password_here"
REDIS_NODES="10.0.0.11 10.0.0.12 10.0.0.13 10.0.0.14 10.0.0.15 10.0.0.16"

echo "========== Redis Cluster Status =========="
echo "Time: $(date)"
echo ""

# 叢集整體狀態
echo "--- Cluster Info ---"
redis-cli -a $REDIS_PASSWORD -h 10.0.0.11 cluster info | grep -E "cluster_state|cluster_slots|cluster_known_nodes"
echo ""

# 各節點狀態
echo "--- Node Status ---"
for node in $REDIS_NODES; do
  echo "Node: $node"
  info=$(redis-cli -a $REDIS_PASSWORD -h $node INFO server 2>/dev/null | grep -E "redis_version|uptime_in_days")
  memory=$(redis-cli -a $REDIS_PASSWORD -h $node INFO memory 2>/dev/null | grep -E "used_memory_human|maxmemory_human")
  clients=$(redis-cli -a $REDIS_PASSWORD -h $node INFO clients 2>/dev/null | grep connected_clients)

  if [ -z "$info" ]; then
    echo "  Status: UNREACHABLE"
  else
    echo "  $info"
    echo "  $memory"
    echo "  $clients"
  fi
  echo ""
done

# 複製狀態
echo "--- Replication Status ---"
redis-cli -a $REDIS_PASSWORD -h 10.0.0.11 -c cluster nodes | \
  awk '{
    role = ($3 ~ /master/) ? "Master" : "Slave"
    status = ($3 ~ /fail/) ? "FAIL" : "OK"
    printf "%-15s %-8s %-6s %s\n", $2, role, status, $9
  }'
1
2
3
4
5
# 設定執行權限
sudo chmod +x /usr/local/bin/redis-cluster-monitor.sh

# 加入 crontab 定時執行
(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/local/bin/redis-cluster-monitor.sh >> /var/log/redis-cluster-monitor.log 2>&1") | crontab -

使用 redis-cli 監控

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 即時監控命令
redis-cli -a your_strong_password_here -h 10.0.0.11 monitor

# 慢查詢日誌
redis-cli -a your_strong_password_here -h 10.0.0.11 SLOWLOG GET 10

# 記憶體分析
redis-cli -a your_strong_password_here -h 10.0.0.11 MEMORY DOCTOR

# 客戶端列表
redis-cli -a your_strong_password_here -h 10.0.0.11 CLIENT LIST

效能調優參數

在設定檔中加入以下調優參數:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 連線池設定
tcp-backlog 65535
tcp-keepalive 300

# 慢查詢設定
slowlog-log-slower-than 10000
slowlog-max-len 128

# 客戶端輸出緩衝區
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

# Lazy Free(非同步刪除)
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes

# IO 執行緒(Redis 6.0+)
io-threads 4
io-threads-do-reads yes

Prometheus + Grafana 監控整合

安裝 Redis Exporter:

 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
# 下載 Redis Exporter
wget https://github.com/oliver006/redis_exporter/releases/download/v1.55.0/redis_exporter-v1.55.0.linux-amd64.tar.gz
tar xzf redis_exporter-v1.55.0.linux-amd64.tar.gz
sudo mv redis_exporter-v1.55.0.linux-amd64/redis_exporter /usr/local/bin/

# 建立 systemd 服務
sudo tee /etc/systemd/system/redis-exporter.service << 'EOF'
[Unit]
Description=Redis Exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/redis_exporter \
  -redis.addr=redis://10.0.0.11:6379,redis://10.0.0.12:6379,redis://10.0.0.13:6379 \
  -redis.password=your_strong_password_here \
  -is-cluster=true
User=redis
Group=redis
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# 啟動服務
sudo systemctl daemon-reload
sudo systemctl start redis-exporter
sudo systemctl enable redis-exporter

Prometheus 設定:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# prometheus.yml
scrape_configs:
  - job_name: 'redis-cluster'
    static_configs:
      - targets:
        - '10.0.0.11:9121'
        - '10.0.0.12:9121'
        - '10.0.0.13:9121'
        - '10.0.0.14:9121'
        - '10.0.0.15:9121'
        - '10.0.0.16:9121'

常用監控指標

指標說明警告閾值
redis_cluster_state叢集狀態!= 1
redis_cluster_slots_assigned已分配 Slots!= 16384
redis_connected_clients連線客戶端數> 10000
redis_memory_used_bytes記憶體使用量> maxmemory * 0.9
redis_commands_processed_total命令處理數-
redis_keyspace_hits_total快取命中數-
redis_keyspace_misses_total快取未命中數-
redis_master_repl_offset複製偏移量-

常見問題排解

叢集狀態異常

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 問題:cluster_state:fail
# 檢查方法:
redis-cli -a your_strong_password_here --cluster check 10.0.0.11:6379

# 可能原因:
# 1. Slots 未完全覆蓋
# 2. 主節點故障且無可用 Slave
# 3. 網路分區

# 解決方案:
redis-cli -a your_strong_password_here --cluster fix 10.0.0.11:6379

記憶體不足

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 問題:OOM (Out of Memory)
# 檢查方法:
redis-cli -a your_strong_password_here INFO memory

# 解決方案:
# 1. 調整 maxmemory 設定
# 2. 使用適當的 eviction policy
# 3. 清理過期 Key
redis-cli -a your_strong_password_here CONFIG SET maxmemory 4gb
redis-cli -a your_strong_password_here CONFIG SET maxmemory-policy allkeys-lru

複製延遲

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 問題:Slave 複製延遲
# 檢查方法:
redis-cli -a your_strong_password_here -h 10.0.0.14 INFO replication

# 檢查指標:
# master_link_status:up
# master_last_io_seconds_ago:0
# master_sync_in_progress:0

# 解決方案:
# 1. 檢查網路延遲
# 2. 調整 repl-backlog-size
# 3. 確認磁碟 I/O 效能

總結

本文詳細介紹了在 Ubuntu 22.04 上部署 Redis 六節點叢集的完整流程,涵蓋:

  1. 架構規劃:3 主 3 從的高可用架構設計
  2. 安裝設定:系統調優、Redis 安裝與叢集設定
  3. 叢集建立:使用 redis-cli 建立並驗證叢集
  4. 資料分片:Hash Slot 機制與手動 Slot 管理
  5. 故障轉移:自動與手動故障轉移測試
  6. 維護操作:節點增減、備份與滾動升級
  7. 監控調校:效能監控與參數調優

透過此架構,您可以獲得:

  • 高可用性:任一主節點故障時,從節點自動接管
  • 水平擴展:可輕鬆增加節點以提升容量和效能
  • 資料安全:透過持久化和複製機制確保資料不遺失

建議在正式環境部署前,先在測試環境完整演練故障轉移和維護操作,確保團隊熟悉各項操作流程。

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy