ismailhabib / docker-kafka

Apache Kafka on Docker
https://registry.hub.docker.com/u/ismailhabib/docker-kafka/
0 stars 0 forks source link

Apache Kafka on Docker

This repository holds a build definition and supporting files for building a Docker image to run Kafka in containers. It is published as an Automated Build on the Docker registry, as ismailhabib/docker-kafka.

This build intends to provide an operator-friendly Kafka deployment suitable for usage in a production Docker environment:

If you find any shortcomings with the build regarding operability, pull requests or feedback via GitHub issues are welcomed.

Usage Quick Start

Here is a minimal-configuration example running the Kafka broker service, then using the container as a client to run the basic producer and consumer example from the Kafka Quick Start:

$ docker run -d --name zookeeper jplock/zookeeper:3.4.6
$ docker run -d --name kafka --link zookeeper:zookeeper ismailhabib/docker-kafka

# Or if you want to run them with Docker Compose
$ docker-compose up

$ ZK_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' zookeeper)
$ KAFKA_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' kafka)

$ docker run --rm ismailhabib/docker-kafka \
>   kafka-topics.sh --create --topic test --replication-factor 1 --partitions 1 --zookeeper $ZK_IP:2181
Created topic "test".

# In separate terminals:
$ docker run --rm --interactive ismailhabib/docker-kafka \
>   kafka-console-producer.sh --topic test --broker-list $KAFKA_IP:9092
<type some messages followed by newline>

$ docker run --rm ismailhabib/docker-kafka \
>   kafka-console-consumer.sh --topic test --from-beginning --zookeeper $ZK_IP:2181

Volumes

The container exposes two volumes that you may wish to bind-mount, or process elsewhere with --volumes-from:

Ports and Linking

The container publishes two ports:

Kafka requires Apache ZooKeeper. You can satisfy the dependency by simply linking another container that exposes ZooKeeper on its standard port of 2181, as shown in the above example, ensuring that you link using an alias of zookeeper.

Alternatively, you may configure a specific address for Kafka to find ZK. See the Configuration section below.

A more complex local development setup

This example shows more configuration options and assumes that you wish to run a development environment with Kafka ports mapped directly to localhost, for instance if you're writing a producer or consumer and want to avoid rebuilding a container for it to run in as you iterate. This requires that localhost is your Docker host, i.e. your workstation runs Linux. If you're using something like boot2docker, substitute the value of boot2docker ip below.

$ mkdir -p kafka-ex/{data,logs} && cd kafka-ex
$ docker run -d --name zookeeper --publish 2181:2181 jplock/zookeeper:3.4.6
$ docker run -d \
    --hostname localhost \
    --name kafka \
    --volume ./data:/data --volume ./logs:/logs \
    --publish 9092:9092 --publish 7203:7203 \
    --env KAFKA_ADVERTISED_HOST_NAME=127.0.0.1 --env ZOOKEEPER_IP=127.0.0.1 \
    ismailhabib/docker-kafka

Configuration

Some parameters of Kafka configuration can be set through environment variables when running the container (docker run -e VAR=value). These are shown here with their default values, if any:

JMX

Remote JMX access can be a bit of a pain to set up. The start script for this container tries to make it as painless as possible, but it's important to understand that if you want to connect a client like VisualVM from outside other Docker containers (e.g. directly from your host OS in development), then you'll need to configure RMI to be addressed as the Docker host IP or hostname. If you have set KAFKA_ADVERTISED_HOST_NAME, that value will be used and is probably what you want. If not (you're only using other containers to talk to Kafka brokers) or you need to override it for some reason, then you can instead set JAVA_RMI_SERVER_HOSTNAME.

For example in practice, if your Docker host is VirtualBox run by Docker Machine, a run command like this should allow you to connect VisualVM from your host OS to $(docker-machine ip):7203:

$ docker run -d --name kafka -p 7203:7203 \
    --link zookeeper:zookeeper \
    --env JAVA_RMI_SERVER_HOSTNAME=$(docker-machine ip) \
    ismailhabib/docker-kafka

Note that it is fussy about port as well---it may not work if the same port number is not used within the container and on the host (any advice for workarounds is welcome).

Finally, please note that by default remote JMX has authentication and SSL turned off (these settings are taken from Kafka's own default start scripts). If you expose the JMX hostname/port from the Docker host in a production environment, you should make make certain that access is locked down appropriately with firewall rules or similar. A more advisable setup in a Docker setting would be to run a metrics collector in another container, and link it to the Kafka container(s).

If you need finer-grained configuration, you can totally control the relevant Java system properties by setting KAFKA_JMX_OPTS yourself---see start.sh.