1、一台主机上部署zookeeper集群

下面这个是官方给出的docker-compose的部署方案(这个是都在一台主机上部署的):

version: '3'
services:
    zoo1:
        image: zookeeper
        restart: always
        ports:
            - 2182:2181
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    zoo2:
        image: zookeeper
        restart: always
        ports:
            - 2183:2181
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    zoo3:
        image: zookeeper
        restart: always
        ports:
            - 2184:2181
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181

2、一台主机部署zookeeper集群分析

我们从上面的分析,最主要的就是ZOO_MY_ID和ZOO_SERVERS(更多环境变量配置参考:https://hub.docker.com/_/zookeeper)

  • ZOO_MY_ID:节点ID
  • ZOO_SERVERS:集群节点地址,多个节点之间使用空格隔开

由此可以看出如果不在一个主机上,那么这几个容器之间就得是想通的,为了使其想通,我们运用了Macvlan网络

3、Macvlan网络

macvlan本身是linxu kernel的模块,本质上是一种网卡虚拟化技术。其功能是允许在同一个物理网卡上虚拟出多个网卡,通过不同的MAC地址在数据链路层进行网络数据的转发,一块网卡上配置多个 MAC 地址(即多个 interface),每个interface可以配置自己的IP,Docker的macvlan网络实际上就是使用了Linux提供的macvlan驱动.在物理网络看来,每张虚拟网卡都是一个单独的接口。

4、跨主机环境准备:

编号主机IP容器IP
主机1192.168.1.18710.0.3.71
主机2192.168.1.18810.0.3.72
主机3192.168.1.18910.0.3.73

5、创建macvlan网络

主机1(主机2和主机3也做如下类似操作):

docker network create -d macvlan --subnet=10.0.3.0/24 --gateway=10.0.3.1 -o parent=em2 zookeeper-kafka-net

subnet这里结尾最好是/24,代表255.255.255.0,如果是/16代表255.255.0.0,16这种据说在设置了iptable的情况下会出问题
parent后是网卡名称192.168.1.187的网卡名称,可通过ipconfig查找
请添加图片描述

最后的zookeeper-kafka-net是你创建的macvlan网络名称,可随意写

6、docker-compose.yml文件

主机1:

version: "3"
services:
  zookeeper1:
    container_name: zookeeper1
    image: zookeeper:3.6.2
    networks:
      zookeeper-kafka-net:
        ipv4_address: 10.0.3.71
    ports:
      - "22181:2181"
      - "22888:2888"
      - "23888:3888"
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=10.0.3.71:2888:3888;2181 server.2=10.0.3.72:2888:3888;2181 server.3=10.0.3.73:2888:3888;2181
    restart: always
networks:
  zookeeper-kafka-net:
    external:
      name: zookeeper-kafka-net

主机2:

version: "3"
services:
  zookeeper2:
    container_name: zookeeper2
    image: zookeeper:3.6.2
    networks:
      zookeeper-kafka-net:
        ipv4_address: 10.0.3.72
    ports:
      - "22181:2181"
      - "22888:2888"
      - "23888:3888"
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=10.0.3.71:2888:3888;2181 server.2=10.0.3.72:2888:3888;2181 server.3=10.0.3.73:2888:3888;2181
    restart: always
networks:
  zookeeper-kafka-net:
    external:
      name: zookeeper-kafka-net

主机3:

version: "3"
services:
  zookeeper3:
    container_name: zookeeper3
    image: zookeeper:3.6.2
    networks:
      zookeeper-kafka-net:
        ipv4_address: 10.0.3.73
    ports:
      - "22181:2181"
      - "22888:2888"
      - "23888:3888"
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=10.0.3.71:2888:3888;2181 server.2=10.0.3.72:2888:3888;2181 server.3=10.0.3.73:2888:3888;2181
    restart: always
networks:
  zookeeper-kafka-net:
    external:
      name: zookeeper-kafka-net

参考

51CTO博客作者jerry1111111的原创作品

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐