原创

[Docker] zookeeper+kafka+springboot实践1

某郑姓同学最近在对接该系统,之前没有用linux实践过,这次刚好借这个机会学习下。

  • 环境

系统 Manjaro 20.2 Nibia 内核 Kernel: x86_64 Linux 5.9.8-2-MANJARO

Docker:

Client:
 Version:           19.03.13-ce
 API version:       1.40
 Go version:        go1.15.2
 Git commit:        4484c46d9d
 Built:             Sat Sep 26 12:04:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false

docker-compose:

[ya@ya-PC zookeep]$ docker-compose version
docker-compose version 1.27.4, build unknown
docker-py version: 4.3.1
CPython version: 3.8.6
OpenSSL version: OpenSSL 1.1.1h  22 Sep 2020

防火墙:关闭

  • docker网络
sudo docker network create --driver bridge --subnet 172.69.0.0/25 --gateway 172.69.0.1  kafka_zoo
  • etc/hosts修改
sudo sh -c 'echo "172.69.0.11 broker1" >> /etc/hosts'
sudo sh -c 'echo "172.69.0.12 broker2" >> /etc/hosts'
sudo sh -c 'echo "172.69.0.13 broker3" >> /etc/hosts'
sudo cat /etc/hosts
  • 创建 docker-compose.yml 随便找个目录,以~/kafka为例子
mkdir ~/kafka
cd ~/kafka
echo '' > docker-compose.yml

将底下的代码复制到docker-compose.yml

version: '2'
services:
  zoo1:
    image: zookeeper:3.4.14
    restart: always
    hostname: zoo1
    container_name: zoo1
    ports:
      - "2181:2181"
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
    networks:
      default:
        ipv4_address: 172.69.0.21
  zoo2:
    image: zookeeper:3.4.14
    restart: always
    hostname: zoo2
    container_name: zoo2
    ports:
      - "2182:2181"
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
    networks:
      default:
        ipv4_address: 172.69.0.22
  zoo3:
    image: zookeeper:3.4.14
    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=zoo3:2888:3888
    networks:
      default:
        ipv4_address: 172.69.0.23
  broker1:
    image: wurstmeister/kafka:2.12-2.5.0
    restart: always
    hostname: broker1
    container_name: broker1
    ports:
      - "9091:9091"
    external_links:
      - zoo1
      - zoo2
      - zoo3
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ADVERTISED_HOST_NAME: broker1
      KAFKA_ADVERTISED_PORT: 9091
      KAFKA_HOST_NAME: broker1
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_LISTENERS: PLAINTEXT://broker1:9091
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker1:9091
      JMX_PORT: 9988
    networks:
      default:
        ipv4_address: 172.69.0.11
  broker2:
    image: wurstmeister/kafka:2.12-2.5.0
    restart: always
    hostname: broker2
    container_name: broker2
    ports:
      - "9092:9092"
    external_links:
      - zoo1
      - zoo2
      - zoo3
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_ADVERTISED_HOST_NAME: broker2
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_HOST_NAME: broker2
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_LISTENERS: PLAINTEXT://broker2:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker2:9092
      JMX_PORT: 9988
    networks:
      default:
        ipv4_address: 172.69.0.12
  broker3:
    image: wurstmeister/kafka:2.12-2.5.0
    restart: always
    hostname: broker3
    container_name: broker3
    ports:
      - "9093:9093"
    external_links:
      - zoo1
      - zoo2
      - zoo3
    environment:
      KAFKA_BROKER_ID: 3
      KAFKA_ADVERTISED_HOST_NAME: broker3
      KAFKA_ADVERTISED_PORT: 9093
      KAFKA_HOST_NAME: broker3
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_LISTENERS: PLAINTEXT://broker3:9093
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker3:9093
      JMX_PORT: 9988
    networks:
      default:
        ipv4_address: 172.69.0.13
networks:
  default:
    external:
      name: kafka_zoo
  • 后台启动并运行所有容器
cd ~/kafka
sudo docker-compose up -d

如果你之前没有运行过容易,最开始是需要拉docker镜像的。 稍等一会,就会自动创建容器,并且运行了kafka集群以及zookeeper集群。 可以使用sudo docker ps查看所有运行的容器。

(话外:可以使用lazydocker管理容器,很方便)

下一篇在springboot中集成。

参考: docker-compose 部署全部 sudo重定向权限不够问题及思考

温馨提示:
本文最后更新于 2021年01月07日,已超过 505 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
本文目录