Hướng dẫn thiết lập Redis Sentinel

Redis Sentinel là một mô hình nâng cao so với cấu trúc master-slave đơn giản, cung cấp khả năng giám sát tự động, phát hiện lỗi và tự động chuyển đổi master khi cần thiết. Bài viết này hướng dẫn cách triển khai Sentinel bằng Docker Compose.

Cấu trúc Redis Master-Slave

Trước khi triển khai Sentinel, chúng ta cần thiết lập một cluster Redis master-slave. Dưới đây là các cấu hình cần thiết.

File cấu hình Redis Master (redis-master.conf)

# Bỏ bind để cho phép kết nối từ bên ngoài
# Port lắng nghe
port 6379

# Không hiển thị logo khi khởi động
always-show-logo no

# Cấu hình xác thực mật khẩu
requirepass mypassword123
protected-mode no 
appendonly yes

# Vô hiệu hóa lệnh KEYS để bảo mật
# Lệnh KEYS * có thể liệt kê tất cả key, gây rủi ro bảo mật
# Ngoài ra, lệnh KEYS sẽ block database, gây ảnh hưởng hiệu năng
rename-command KEYS ""

# Mật khẩu cho replication
masterauth "mypassword123"

File cấu hình Redis Slave 1 (redis-slave1.conf)

# Port lắng nghe
port 6380
always-show-logo no
requirepass mypassword123
protected-mode no 
appendonly yes
rename-command KEYS ""

# Kết nối đến master
replicaof 192.168.1.100 6379

# Mật khẩu để kết nối đến master
masterauth "mypassword123"

File cấu hình Redis Slave 2 (redis-slave2.conf)

# Port lắng nghe
port 6381
always-show-logo no
requirepass mypassword123
protected-mode no 
appendonly yes
rename-command KEYS ""

# Kết nối đến master
replicaof 192.168.1.100 6379

# Mật khẩu để kết nối đến master
masterauth "mypassword123"

File docker-compose cho Redis Master-Slave

version: '3'

services:
  # Container cho master node
  redis-master:
    image: redis
    container_name: redis-master
    restart: always
    network_mode: bridge
    ports:
     - 6379:6379
    environment:
      TZ: "Asia/Ho_Chi_Minh"
    volumes:
      - ./redis-master.conf:/usr/local/etc/redis/redis.conf
      - ./data/master:/data 
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  
  # Container cho slave node 1
  redis-slave-1:
    image: redis
    container_name: redis-slave-1
    restart: always
    network_mode: bridge
    ports:
     - 6380:6380
    environment:
      TZ: "Asia/Ho_Chi_Minh"
    volumes:
      - ./redis-slave1.conf:/usr/local/etc/redis/redis.conf
      - ./data/slave-1:/data 
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  
  # Container cho slave node 2
  redis-slave-2:
    image: redis
    container_name: redis-slave-2
    restart: always
    network_mode: bridge
    ports:
     - 6381:6381
    environment:
      TZ: "Asia/Ho_Chi_Minh"
    volumes:
      - ./redis-slave2.conf:/usr/local/etc/redis/redis.conf
      - ./data/slave-2:/data 
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]

Cấu hình Redis Sentinel

Sau khi thiết lập master-slave, chúng ta có thể triển khai Sentinel để giám sát cluster và tự động chuyển đổi master khi cần.

File cấu hình Sentinel 1 (sentinel-1.conf)

# Port cho Sentinel
port 26379

protected-mode no 
# Cấu hình giám sát master với tên 'redis-cluster'
sentinel monitor redis-cluster 192.168.1.100 6379 2
# Mật khẩu để kết nối đến master
sentinel auth-pass redis-cluster mypassword123
# Thời gian chờ trước khi đánh dấu master là down (30 giây)
sentinel down-after-milliseconds redis-cluster 30000

File cấu hình Sentinel 2 (sentinel-2.conf)

# Port cho Sentinel
port 26380

sentinel monitor redis-cluster 192.168.1.100 6379 2
sentinel auth-pass redis-cluster mypassword123
protected-mode no 
# Thời gian chờ trước khi đánh dấu master là down (30 giây)
sentinel down-after-milliseconds redis-cluster 30000

File cấu hình Sentinel 3 (sentinel-3.conf)

# Port cho Sentinel
port 26381

sentinel monitor redis-cluster 192.168.1.100 6379 2
sentinel auth-pass redis-cluster mypassword123
protected-mode no 
# Thời gian chờ trước khi đánh dấu master là down (30 giây)
sentinel down-after-milliseconds redis-cluster 30000

File docker-compose cho Redis Sentinel

version: '3'

services:
  sentinel-1:
    image: redis
    container_name: redis-sentinel-1
    restart: always
    network_mode: bridge
    ports:
     - 26379:26379
    volumes:
      - ./sentinel-1.conf:/usr/local/etc/redis/sentinel.conf
    environment:
      TZ: "Asia/Ho_Chi_Minh" 
    command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
  
  sentinel-2:
    image: redis
    container_name: redis-sentinel-2
    restart: always
    network_mode: bridge
    ports:
     - 26380:26380
    volumes:
      - ./sentinel-2.conf:/usr/local/etc/redis/sentinel.conf
    environment:
      TZ: "Asia/Ho_Chi_Minh" 
    command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
  
  sentinel-3:
    image: redis
    container_name: redis-sentinel-3
    restart: always
    network_mode: bridge
    ports:
     - 26381:26381
    volumes:
      - ./sentinel-3.conf:/usr/local/etc/redis/sentinel.conf
    environment:
      TZ: "Asia/Ho_Chi_Minh"
    command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]

Để khởi động hệ thống, sử dụng lệnh docker-compose up -d. Để dừng và xóa các container, sử dụng lệnh docker-compose down.

Thẻ: Redis sentinel docker-compose master-slave high-availability

Đăng vào ngày 9 tháng 7 lúc 17:48