第一步:查看本地镜像、检索Kafka镜像和下载Kafka 镜像

# 查看本地镜像

docker images

# 检索Kafka镜像

docker search kafka

# 下载Kafka 镜像指定版本

docker pull wurstmeister/kafka:latest

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              0e901e68141f        2 months ago        142MB
mysql               5.7                 2a0961b7de03        2 months ago        462MB
minio/minio         latest              e31e0721a96b        7 months ago        406MB
zookeeper           latest              36c607e7b14d        7 months ago        278MB
rabbitmq            management          6c3c2a225947        7 months ago        253MB
elasticsearch       7.6.2               f29a1ee41030        2 years ago         791MB
delron/fastdfs      latest              8487e86fc6ee        4 years ago         464MB
[root@localhost ~]# docker search kafka
NAME                                         DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
bitnami/kafka                                Apache Kafka is a distributed streaming plat…   520                                     [OK]
bitnami/kafka-exporter                                                                       8
ubuntu/kafka                                 Apache Kafka, a distributed event streaming …   7
bitnami/kafka-trigger-controller             Source for this controller is in the kubeles…   5
ibmcom/kafka                                 Docker Image for IBM Cloud Private-CE (Commu…   5
[root@localhost ~]# docker search wurstmeister/kafka
NAME                 DESCRIPTION                       STARS               OFFICIAL            AUTOMATED
wurstmeister/kafka   Multi-Broker Apache Kafka Image   1511                                    [OK]
[root@localhost ~]# docker pull wurstmeister/kafka:latest
latest: Pulling from wurstmeister/kafka
540db60ca938: Pull complete
f0698009749d: Pull complete
d67ee08425e3: Pull complete
1a56bfced4ac: Pull complete
dccb9e5a402a: Pull complete
Digest: sha256:4916aa312512d255a6d82bed2dc5fbee29df717fd9efbdfd673fc81c6ce03a5f
Status: Downloaded newer image for wurstmeister/kafka:latest

第二步:启动Zookeeper

Docker 安装ZooKeeper 请参考:Docker安装ZooKeeper

第三步:启动Kafka

docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_ZOOKEEPER_CONNECT=192.168.43.10:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.43.10:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 wurstmeister/kafka 


[root@localhost ~]# docker run -d --name kafka \
> -p 9092:9092 \
> -e KAFKA_BROKER_ID=0 \
> -e KAFKA_ZOOKEEPER_CONNECT=192.168.43.10:2181 \
> -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.43.10:9092 \
> -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 wurstmeister/kafka
06a66409d7ced3540f02fb0f793a0ece1e103464a17037b1ae855d8f8e9e7b56
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                                                                                                         NAMES
06a66409d7ce        wurstmeister/kafka   "start-kafka.sh"         3 minutes ago       Up 3 minutes        0.0.0.0:9092->9092/tcp                                                                                        kafka
1c3d38b948ba        zookeeper            "/docker-entrypoint.…"   10 hours ago        Up 10 hours         2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp                                                          zookeeper
62233dfad35b        minio/minio          "/usr/bin/docker-ent…"   23 hours ago        Up 23 hours                                                                                                                       minio-server
4b0032878886        6c3c2a225947         "docker-entrypoint.s…"   5 days ago          Up 4 days           4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbitmq

第五步:验证ZooKeeper 和Kafka 容器状态

# 进入ZooKeeper 容器内部

docker exec -it 1c3d38b948ba /bin/bash

# 运行zkCli 客户端指令

zkCli.sh -server 192.168.43.10

[root@localhost ~]# docker exec -it 1c3d38b948ba /bin/bash
root@1c3d38b948ba:/apache-zookeeper-3.7.0-bin# pwd
/apache-zookeeper-3.7.0-bin            
root@1c3d38b948ba:/apache-zookeeper-3.7.0-bin# cd bin/
root@1c3d38b948ba:/apache-zookeeper-3.7.0-bin/bin# zkCli.sh -server 192.168.43.10:2181
[zk: 192.168.43.10:2181(CONNECTING) 0] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
[zk: 192.168.43.10:2181(CONNECTED) 1] ls /zookeeper
[config, quota]
[zk: 192.168.43.10:2181(CONNECTED) 2] quit

WATCHER::

WatchedEvent state:Closed type:None path:null

# 进入Kafka 容器内部

docker exec -it 06a66409d7ce /bin/sh

[root@localhost ~]# docker exec -it 06a66409d7ce /bin/sh
/ # ps -ef
PID   USER     TIME  COMMAND
    1 root      0:30 /usr/lib/jvm/zulu8-ca/bin/java -Xmx1G -Xms1G -server -XX:+
  520 root      0:00 /bin/sh
  526 root      0:00 ps -ef

第六步:Kafka 创建Topic 流程步骤

#进入容器
docker exec -it ${CONTAINER ID} /bin/bash
cd /opt/kafka_2.13-2.8.1/bin
#单机方式:创建一个主题
bin/kafka-topics.sh --create --zookeeper 192.168.43.10:2181 --replication-factor 1 --partitions 1 --topic mykafka
#运行一个生产者
bin/kafka-console-producer.sh --broker-list 192.168.43.10:9092 --topic mykafka
#运行一个消费者
bin/kafka-console-consumer.sh --zookeeper 192.168.43.10:2181 --topic mykafka --from-beginning
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐