1. 程式人生 > >使用docker-compose快速構建叢集示例(一)

使用docker-compose快速構建叢集示例(一)

一、zookeeper叢集

docker-compose檔案:

    version: '3.1'
    services:
      zoo1:
        image: zookeeper
        hostname: zoo1
        container_name: zoo1
        ports:
          - 2181:2181
        environment:
          ZOO_MY_ID: 1
          ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
      zoo2:
        image: zookeeper
        restart: always
        hostname: zoo2
        container_name: zoo2
        ports:
          - 2182:2181
        environment:
          ZOO_MY_ID: 2
          ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
      zoo3:
        image: zookeeper
        restart: always
        hostname: zoo3
        container_name: zoo3
        ports:
          - 2183:2181
        environment:
          ZOO_MY_ID: 3
          ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
  • 在這裡ZOO_MY_ID必須唯一
  • ZOO_SERVERS用來配置服務的broker

二、redis cluster

1、建立redis資料夾

在對應的資料夾下分別建立7001-7006的資料夾與docker-compose.yml,這裡名字資料夾代表當前redis節點的埠號,如圖所示:

2、建立redis.conf檔案

具體redis-cluster示例大家可以參考官網,那麼分別配置redis.conf檔案

    port 7001
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    appendonly yes

port與資料夾名對應

3、docker-compose.yml檔案


    version: '3.1'
    services:
      redis-node1:
        image: redis
        hostname: redis-node1
        network_mode: host
        container_name: redis-node1
        entrypoint: redis-server /usr/local/etc/redis/redis.conf
        ports:
          - 7001:7001
        env_file:
          - .env
        volumes:
          - "${PROJECT_HOME}/7001/redis.conf:/usr/local/etc/redis/redis.conf"
      redis-node2:
        image: redis
        hostname: redis-node2
        network_mode: host
        container_name: redis-node2
        entrypoint: redis-server /usr/local/etc/redis/redis.conf
        ports:
          - 7002:7002
        env_file:
          - .env
        volumes:
          - "${PROJECT_HOME}/7002/redis.conf:/usr/local/etc/redis/redis.conf"
      redis-node3:
        image: redis
        hostname: redis-node3
        network_mode: host
        container_name: redis-node3
        entrypoint: redis-server /usr/local/etc/redis/redis.conf
        ports:
          - 7003:7003
        env_file:
          - .env
        volumes:
          - "${PROJECT_HOME}/7003/redis.conf:/usr/local/etc/redis/redis.conf"
      redis-node4:
        image: redis
        hostname: redis-node4
        network_mode: host
        container_name: redis-node4
        entrypoint: redis-server /usr/local/etc/redis/redis.conf
        ports:
          - 7004:7004
        env_file:
          - .env
        volumes:
          - "${PROJECT_HOME}/7004/redis.conf:/usr/local/etc/redis/redis.conf"
      redis-node5:
        image: redis
        hostname: redis-node5
        network_mode: host
        container_name: redis-node5
        entrypoint: redis-server /usr/local/etc/redis/redis.conf
        ports:
          - 7005:7005
        env_file:
          - .env
        volumes:
          - "${PROJECT_HOME}/7005/redis.conf:/usr/local/etc/redis/redis.conf"
      redis-node6:
        image: redis
        hostname: redis-node6
        network_mode: host
        container_name: redis-node6
        entrypoint: redis-server /usr/local/etc/redis/redis.conf
        ports:
          - 7006:7006
        env_file:
          - .env
        volumes:
          - "${PROJECT_HOME}/7006/redis.conf:/usr/local/etc/redis/redis.conf"

這裡注意以下幾點:

  1. network_mode設定為host,否則在建立叢集時,會一直等待而不會建立成功
  2. volumes掛載點必須覆蓋容器內部配置,大家也可以考慮掛載redis持久化的資料資料夾

配置好後分別執行:

    $ docker-compose create
    $ docker-compose start

4、啟動叢集

在這裡部署在阿里雲伺服器上
執行命令如下:


    $ docker run -it  inem0o/redis-trib create --replicas 1  公網IP:7001 公網IP:7002 公網IP:7003  公網IP:7004 公網IP:7005 公網IP:7006 

執行後如圖所示:


即可說明成功,然後你就訪問主節點試試資料是否同步吧!