亲宝软件园·资讯

展开

Docker Apache Kafka环境

梦想画家 人气:0

Docker是最受欢迎的容器引擎之一,软件行业用于创建、打包、部署应用。本教程学习如何使用Docker搭建Apache Kafka。

安装单个节点

安装单节点Kafka代理需能够满足本地开发需求,我们首先学习单节点安装。

docker-compose.yml 配置

使用Apache Kafka服务,首先需要启动Zookeeper 服务。这可以通过在docker-compose.yml 文件配置依赖实现,但要确保Zookeeper服务总是在Kafka服务之前启动,反之zookeeper最后停止。

首先创建docker-compose.yml文件,增加两个服务:zookeeper和kafka:

version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181
  
  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 29092:29092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

Zookeeper服务监听2181端口为Kafka提供服务,但对于运行在本机的客户端,暴露的端口为22181.
类似的,kafka通过端口29092暴露给主机应用,但实际上在容器中的端口9092。有KAFKA_ADVERTISED_LISTENERS属性来配置。

下面通过使用命令启动容器、开启kafka服务:

$ docker-compose up -d
Creating network "kafka_default" with the default driver
Creating kafka_zookeeper_1 ... done
Creating kafka_kafka_1     ... done

下面通过nc命令严重两个服务正在监听各自的端口:

$ nc -z localhost 22181
Connection to localhost port 22181 [tcp/*] succeeded!
$ nc -z localhost 29092
Connection to localhost port 29092 [tcp/*] succeeded!

另外,也可以检查日志,当容器正在运行时验证kafka服务是否启动:

$ docker-compose logs kafka | grep -i started

可以看到Kafka服务已经启动。

最后可以使用Kafka Tool Gui 工具连接已安装的Kafka服务:

在这里插入图片描述

安装集群环境

生产环境需要更稳定的环境,下面扩展 docker-compose.yml文件创建多节点集群环境:

docker-compose.yml配置

集群安装需要zookeeper和Kafka有多个实例,下面为zookeeper和kafka增加多个服务:

---
version: '2'
services:
  zookeeper-1:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181

  zookeeper-2:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 32181:2181
  
  kafka-1:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper-1
      - zookeeper-2

    ports:
      - 29092:29092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-1:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  kafka-2:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper-1
      - zookeeper-2
    ports:
      - 39092:39092
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-2:9092,PLAINTEXT_HOST://localhost:39092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

必须要确保kafka服务名称唯一,通过KAFKA_BROKER_ID 属性设置。而且每个服务必须暴露唯一主机端口。虽然zookeeper-1 和 zookeeper-2 监听2181端口,但对于主机端口分别为 22181 和 32181。类似的,kafka-1 和 kafka-2 services对应主机端口分别为 29092 和 39092。

启动kafka集群

现在通过docker-compose命令启动集群:

$ docker-compose up -d
Creating network "kafka_default" with the default driver
Creating kafka_zookeeper-1_1 ... done
Creating kafka_zookeeper-2_1 ... done
Creating kafka_kafka-2_1     ... done
Creating kafka_kafka-1_1     ... done

启动完成,使用Kafka Tool连接集群,地址通过逗号分割只读多个zookeeper地址和端口:

总结

本文使用Docker技术创建单个Kafka节点和Kafka集群环境,并且使用可视化工具连接服务查看代理服务器的配置信息。

加载全部内容

相关教程
猜你喜欢
用户评论