Kafka 101

unpublished draft
KafkaC#.Net

Overview#

Docker setup#

Using separate image.
# If network is not yet created
docker network create -d bridge kafka-net

# Get Zookeeper
docker run -d -–net=kafka-net -–name=zookeeper --hostname=zookeeper -p 2181:2181 -e ZOOKEEPER_CLIENT_PORT=2181 -e ZOOKEEPER_TICK_TIME=2000 -e ZOOKEEPER_SYNC_LIMIT=2 confluentinc/cp-zookeeper

# Get Kafka broker
# bootstrap.servers was locate on localhost:9092 ì accessing from the outside.
docker run -d -–net=kafka-net -–name=kafka --hostname=kafka-broker -p 9092:9092 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka-broker:29092,PLAINTEXT_HOST://localhost:9092 -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 -e KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL=http://schema-registry:8081 -e CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS=kafka-broker:29092 -e CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS=1 -e CONFLUENT_METRICS_ENABLE=true -e CONFLUENT_SUPPORT_CUSTOMER_ID=anonymous confluentinc/cp-kafka

# Get Schema-registry
docker run -d -–net=kafka-net -–name=kafka-schema-registry --hostname=schema-registry -p 9501:8081 -e SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS=kafka-broker:29092 -e SCHEMA_REGISTRY_LISTENERS=http://0.0.0.0:8081 -e SCHEMA_REGISTRY_HOST_NAME=schema-registry confluentinc/cp-schema-registry

# Get Kafka-ui
# Located on localhost:9500
docker run -d -–net=kafka-net -p 9500:8080 --name=kafka-ui -e KAFKA_CLUSTERS_0_NAME=local -e KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka-broker:29092 -e KAFKA_CLUSTERS_0_ZOOKEEPER=zookeeper:2181 -e KAFKA_CLUSTERS_0_SCHEMAREGISTRY=schema-registry:8081 provectuslabs/kafka-ui:latest
Using docker compose.
---
version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.0.0
    hostname: zookeeper
    container_name: zookeeper
    ports:
      - '2181:2181'
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-kafka:7.0.0
    hostname: kafka-broker
    container_name: kafka
    depends_on:
      - zookeeper
    ports:
      - '9092:9092'
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-broker:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://schema-registry:8081
      CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: kafka-broker:29092
      CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
      CONFLUENT_METRICS_ENABLE: 'true'
      CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'

  schema-registry:
    image: confluentinc/cp-schema-registry:7.0.0
    hostname: schema-registry
    container_name: schema-registry
    depends_on:
      - broker
    ports:
      - '9501:8081'
    environment:
      SCHEMA_REGISTRY_HOST_NAME: schema-registry
      SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'kafka-broker:29092'
      KAFKA_CLUSTERS_0_ZOOKEEPER: http://0.0.0.0:8081

  kafka-ui:
    image: provectuslabs/kafka-ui:latest
    container_name: kafka-ui
    depends_on:
      - zookeeper
      - broker
      - schema-registry
    ports:
      - '9500:8080'
    environment:
      KAFKA_CLUSTERS_0_NAME: local
      SCHEMA_REGISTRY_LISTENERS: zookeeper:2181
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka-broker:29092
      KAFKA_CLUSTERS_0_SCHEMAREGISTRY: schema-registry:8081

Some little note#

Some useful documentation#

Dotnet-avro tool using example
# Ensure Chr.Avro.Cli tool was install
dotnet tool install Chr.Avro.Cli --global

# For inheritance class, might have to provide more assemblies, don't be suprise if the class cannot be generated!
# The tool doens't ensure properties order.
# Sample command - current context: The folder contail AvroSpecific.dll
dotnet avro create -a AvroSpecific.dll -t Confluent.Kafka.Examples.AvroSpecific.MyTestingClass

Khanh Nguyen

Web developer & .Net lover