Because Kafka relies on ZooKeeper, you need to use Docker to install both ZooKeeper and Kafka. MacOS Docker cannot communicate directly between container and host via IP, so special attention needs to be paid to IP-related Settings during installation. When container need access to the host IP, need to use the docker. For. MAC. Host. Internal or host. Docker. Internal instead.

#1. Pull the mirror
docker pull wurstmeister/zookeeper
docker pull wurstmeister/kafka
#2. Start the zookeeper
docker run -d --name zookeeper -p2181:2181 wurstmeister/zoopeeker
#3. Start the kafkadocker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=docker.for.mac.host.internal:2181/kafka -e KAFKA_ADVERTISED_LISTENERS = PLAINTEXT: / / docker. For. MAC. Host. Internal: 9092 - e KAFKA_LISTENERS = PLAINTEXT: / / 0.0.0.0:9092 wurstmeister/kafkaCopy the code
  • -d Sets the background running
  • –name Zookeeper Specifies the container alias
  • -p 2181:2181 Binds the container port to the host port
  • The -e parameter can be used to set the docker environment variables:
    • KAFKA_BROKER_ID=0 In a Kafka cluster, each Kafka has a BROKER_ID to distinguish itself
    • KAFKA_ZOOKEEPER_CONNECT={host-ip}:{zookeeper-port}/kafka Specifies the kafka path for zooKeeper management
    • Note KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://{host-ip}:9092
    • Note KAFKA_LISTENERS=PLAINTEXT:// 0.0.0.00:9092

Test kafka production and consumption

docker exec -it kafka bash
cd /opt/kafka_xxx/bin
#Run the Kafka producer
./kafka-console-producer.sh --broker-list localhost:9092 --topic first-topic
#Send Hello after >
>Hello
#Open a new terminal and start the Kafka consumer
cd /opt/kafka_xxx/bin
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first-topic --from-beginning
Copy the code

Docker exec: Executes commands in a running container. -I keeps STDIN open even without attachment, and -t assigns a dummy terminal.

Docker kill/stop Docker stop: sends a sigterm signal to the docker, runs it, performs some operations within a certain period of time, and then closes it. Docker kill: Kill the container directly by sending sigkill.

Docker run/start Docker run: Starts a container from the image. Docker start: Runs the stopped container.

Docker ps -a: docker start