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

1、zookeeper集羣

docker-compose文件:node

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

2、redis cluster

一、建立redis文件夾

在對應的文件夾下分別建立7001-7006的文件夾與docker-compose.yml,這裏名字文件夾表明當前redis節點的端口號,如圖所示:redis

二、建立redis.conf文件

具體redis-cluster示例你們能夠參考官網,那麼分別配置redis.conf文件docker

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

port與文件夾名對應bash

三、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持久化的數據文件夾

配置好後分別運行:app

$ docker-compose create
    $ docker-compose start

四、啓動集羣

在這裏部署在阿里雲服務器上
運行命令以下:阿里雲

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

運行後如圖所示:3d


便可說明成功,而後你就訪問主節點試試數據是否同步吧!rest

相關文章
相關標籤/搜索