Redis|用 Docker 架設 Redis Cluster

瀏覽人次: 6732
2023-06-11 更新
redis

Redis 版本 6.2.4

docker-compose.yml

透過 docker-compose 模擬六台 server, 3 master, 3 slave,


yml 檔中,最後的 cluster-creator 將六台 Server 組合在一起成為 cluster。

    
version: "3"
services:
  redis1:
    image: redis:latest
    command: redis-server --port 7000 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - "7000:7000"
    healthcheck:
      test: [ "CMD", "redis-cli", "-p", "7000", "cluster", "info" ]
      interval: 1s
      timeout: 3s
      retries: 30
  redis2:
    image: redis:latest
    command: redis-server --port 7001 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - "7001:7001"
    healthcheck:
      test: [ "CMD", "redis-cli", "-p", "7001", "cluster", "info" ]
      interval: 1s
      timeout: 3s
      retries: 30
  redis3:
    image: redis:latest
    command: redis-server --port 7002 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - "7002:7002"
    healthcheck:
      test: [ "CMD", "redis-cli", "-p", "7002", "cluster", "info" ]
      interval: 1s
      timeout: 3s
      retries: 30
  redis4:
    image: redis:latest
    command: redis-server --port 7003 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - "7003:7003"
    healthcheck:
      test: [ "CMD", "redis-cli", "-p", "7003", "cluster", "info" ]
      interval: 1s
      timeout: 3s
      retries: 30
  redis5:
    image: redis:latest
    command: redis-server --port 7004 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - "7004:7004"
    healthcheck:
      test: [ "CMD", "redis-cli", "-p", "7004", "cluster", "info" ]
      interval: 1s
      timeout: 3s
      retries: 30
  redis6:
    image: redis:latest
    command: redis-server --port 7005 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - "7005:7005"
    healthcheck:
      test: [ "CMD", "redis-cli", "-p", "7005", "cluster", "info" ]
      interval: 1s
      timeout: 3s
      retries: 30
  redis-cluster-creator:
    image: redis
    entrypoint: [/bin/sh,-c,'echo "yes" | redis-cli --cluster create redis1:7000 redis2:7001 redis3:7002 redis4:7003 redis5:7004 redis6:7005 --cluster-replicas 1']
    depends_on:
      redis1:
        condition: service_healthy
      redis2:
        condition: service_healthy
      redis3:
        condition: service_healthy
      redis4:
        condition: service_healthy
      redis5:
        condition: service_healthy
      redis6:
        condition: service_healthy

啟動服務

docker compose up -d

確認服務

redis-cli -p 7000 cluster info #確認 cluster_state:ok
redis-cli -p 7000 cluster nodes #查看 cluster node 狀態
redis-cli -p 7000 -c #進入 redis cluster 互動模式,-c 為 cluster mode

有任何問題,或是想看新主題?

聯絡我們

快速跳轉目錄

✦ 集先鋒 Bimap – 企業建置高速穩定的海量日誌分析平台✦

集中不同的結構化資料和非結構化日誌,並進行關聯性的大數據整合,客製化儀表版、自訂事件告警、機器學習等等,以滿足各種大數據的應用場景和解決方案。