搭建 Redis 叢集

AndreSyuu發表於2020-10-04

搭建 Redis 叢集

vim docker-compose.yml

Redis Desktop Manager下載

https://www.jianshu.com/p/6895384d2b9e

version: '3.7'
services:
  master:
    image: redis
    container_name: redis-master
    ports:
      - 6379:6379

  slave1:
    image: redis
    container_name: redis-slave-1
    ports:
      - 6380:6379
    command: redis-server --slaveof redis-master 6379

  slave2:
    image: redis
    container_name: redis-slave-2
    ports:
      - 6381:6379
    command: redis-server --slaveof redis-master 6379

搭建 Sentinel 叢集

我們至少需要建立三個 Sentinel 服務,docker-compose.yml 配置如下:

version: '3.7'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf

  sentinel2:
    image: redis
    container_name: redis-sentinel-2
    ports:
      - 26380:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf

  sentinel3:
    image: redis
    container_name: redis-sentinel-3
    ports:
      - 26381:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

修改 Sentinel 配置檔案

需要三份 sentinel.conf 配置檔案,分別為 sentinel1.confsentinel2.confsentinel3.conf,配置檔案內容相同

port 26379
dir /tmp
# 自定義叢集名,其中 127.0.0.1 為 redis-master 的 ip,6379 為 redis-master 的埠,2 為最小投票數(因為有 3 臺 Sentinel 所以可以設定成 2)
sentinel monitor mymaster 192.168.100.11 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

檢視叢集是否生效

進入 Sentinel 容器,使用 Sentinel API 檢視監控情況:

docker exec -it redis-sentinel-1 /bin/bash
redis-cli -p 26379
sentinel master mymaster
sentinel slaves mymaster

127.0.0.1:26379> sentinel slaves mymaster
1)  1) "name"
    2) "172.19.0.3:6379"
    3) "ip"
    4) "172.19.0.3"
    5) "port"
    6) "6379"
    7) "runid"
    8) ""
    9) "flags"
   10) "s_down,slave"
   11) "link-pending-commands"

相關文章