Docker 安装kafka

作者:xcbeyond
疯狂源自梦想,技术成就辉煌!微信公众号:《程序猿技术大咖》号主,专注后端开发多年,拥有丰富的研发经验,乐于技术输出、分享,现阶段从事微服务架构项目的研发工作,涉及架构设计、技术选型、业务研发等工作。对于Java、微服务、数据库、Docker有深入了解,并有大量的调优经验。

1、安装zookeeper
(1)下载zookeeper镜像。

[docker@xcbeyond ~]$ docker search zookeeper
NAME                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
zookeeper                           Apache ZooKeeper is an open-source server ...   460       [OK]       
jplock/zookeeper                    Builds a docker image for Zookeeper versio...   161                  [OK]
mesoscloud/zookeeper                ZooKeeper                                       73                   [OK]
wurstmeister/zookeeper                                                              57                   [OK]
confluentinc/cp-zookeeper           Official Confluent Docker Image for Zookeeper   30                   
mbabineau/zookeeper-exhibitor                                                       23                   [OK]
digitalwonderland/zookeeper         Latest Zookeeper - clusterable                  18                   [OK]
confluent/zookeeper                                                                 12                   [OK]
tobilg/zookeeper-webui              Docker image for using `zk-web` as ZooKeep...   10                   [OK]
debezium/zookeeper                  Zookeeper image required when running the ...   8                    [OK]
hyperledger/fabric-zookeeper        Fabric Zookeeper docker image for Hyperled...   8                    
springxd/zookeeper                  A Docker image that can run a ZooKeeper se...   6                    [OK]
elevy/zookeeper                     ZooKeeper configured to execute an ensembl...   6                    [OK]
31z4/zookeeper                      Dockerized Apache Zookeeper.                    5                    [OK]
thefactory/zookeeper-exhibitor      Exhibitor-managed ZooKeeper with S3 backup...   5                    [OK]
bitnami/zookeeper                   ZooKeeper is a centralized service for dis...   4                    [OK]
paulbrown/zookeeper                 Zookeeper on Kubernetes (PetSet)                1                    [OK]
openshift/zookeeper-346-fedora20    ZooKeeper 3.4.6 with replication support        1                    
harisekhon/zookeeper                Apache ZooKeeper (tags 3.3 - 3.4)               1                    [OK]
ciscocloud/zookeeper                                                                1                    [OK]
duffqiu/zookeeper-cli                                                               0                    [OK]
jamiecressey89/marathon-zookeeper   Zookeeper image that uses Marathon's API f...   0                    [OK]
phenompeople/zookeeper              Apache ZooKeeper is an open-source server ...   0                    [OK]
strimzi/zookeeper                                                                   0                    
midonet/zookeeper                   Dockerfile for a Zookeeper server.              0                    [OK]
具体下载哪个版本,可在https://hub.docker.com上搜索相应的版本,在这里选择mesoscloud/zookeeper:3.4.8版本。

[docker@xcbeyond ~]$ docker pull mesoscloud/zookeeper:3.4.8
3.4.8: Pulling from mesoscloud/zookeeper
469cfcc7a4b3: Pull complete
0cb794d55c09: Pull complete
49b42dca18f7: Pull complete
1734fcc70024: Pull complete
07fc6b833d94: Pull complete
Digest: sha256:9aa170a4c4aa2f3bc8242f5e3dd9df3b9206ecd99697926f26976e58f96d83cc
Status: Downloaded newer image for mesoscloud/zookeeper:3.4.8
(2)创建zookeeper目录,用于存放zookeeper数据及日志。

[docker@xcbeyond ~]$ mkdir zookeeper
[docker@xcbeyond ~]$ ll
total 16
……
drwxrwxr-x 2 docker docker 4096 Sep  3 22:46 zookeeper
[docker@xcbeyond ~]$ cd zookeeper/
(3)运行并启动zookeeper容器

docker run -p 2181:2181 --name zookeeper -v $PWD/conf:/opt/zookeeper/conf -v $PWD/data:/tmp/zookeeper -d mesoscloud/zookeeper:3.4.8
--------------------------------------------------------------------------------
[docker@docker zookeeper]$ docker run -p 2181:2181 --name zookeeper -v $PWD/conf:/opt/zookeeper/conf -v $PWD/data:/tmp/zookeeper -d mesoscloud/zookeeper:3.4.8
73f52be038de1a77f4c0e836f9eae77e273ec1bff19f9f69def210498660e610
[docker@docker zookeeper]$ docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED              STATUS              PORTS                    NAMES
73f52be038de        mesoscloud/zookeeper:3.4.8   "/usr/local/bin/du..."   About a minute ago   Up About a minute   0.0.0.0:2181->2181/tcp   zookeeper
 

2、安装kafka
(1)下载kafka镜像。

[docker@xcbeyond ~]$ docker search kafka
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
wurstmeister/kafka               Multi-Broker Apache Kafka Image                 663                  [OK]
spotify/kafka                    A simple docker image with both Kafka and ...   311                  [OK]
sheepkiller/kafka-manager        kafka-manager                                   126                  [OK]
ches/kafka                       Apache Kafka. Tagged versions. JMX. Cluste...   106                  [OK]
confluentinc/cp-kafka            Official Confluent Docker Image for Kafka ...   78                   
confluentinc/cp-kafka-connect    Official Confluent Docker Image for Kafka ...   44                   
landoop/kafka-topics-ui          UI for viewing Kafka Topics config and dat...   21                   [OK]
hlebalbau/kafka-manager          Kafka Manager Docker Images Build.              18                   [OK]
hyperledger/fabric-kafka         Fabric Kafka docker image for Hyperledger ...   12                   
digitalwonderland/kafka          Latest Kafka - clusterable                      12                   [OK]
debezium/kafka                   Kafka image required when running the Debe...   11                   [OK]
bitnami/kafka                    Apache Kafka is a distributed streaming pl...   10                   [OK]
cgswong/confluent-kafka          Confluent Platform Kafka                        9                    [OK]
solsson/kafka                    http://kafka.apache.org/documentation.html...   9                    [OK]
danielqsj/kafka-exporter         Kafka exporter for Prometheus                   7                    [OK]
landoop/kafka-connect-ui         Web based UI for Kafka Connect.                 7                    [OK]
cloudtrackinc/kubernetes-kafka   Apache Kafka to Kubernetes Replication Con...   5                    [OK]
caiqinzhou/logspout-kafka        Logspout adapter for writing Docker contai...   3                    [OK]
mesosphere/kafka-client          Kafka client                                    3                    [OK]
phenompeople/kafka               Apache Kafka is a distributed streaming pl...   2                    [OK]
openwhisk/kafkaprovider          OpenWhisk trigger provider services for ka...   2                    [OK]
coco/coco-kafka-bridge           Kafka Bridge listens to a kafka proxy and ...   1                    [OK]
vungle/logstash-kafka-es         A simple Logstash image to ship json logs ...   1                    [OK]
zenreach/kafka-connect           Zenreach's Kafka Connect Docker Image           0                    
amplifymedia/kafka               A fork of wurstmeister/kafka.                   0    
具体下载哪个版本,可在https://hub.docker.com上搜索相应的版本,在这里选择wurstmeister/kafka:2.11-2.0.0版本。

[docker@xcbeyond ~]$ docker pull wurstmeister/kafka:2.11-2.0.0
2.11-2.0.0: Pulling from wurstmeister/kafka
8e3ba11ec2a2: Pull complete
311ad0da4533: Pull complete
391a6a6b3651: Pull complete
a475bdab8855: Pull complete
15709c44ef71: Pull complete
a663224093f6: Pull complete
Digest: sha256:2e3ff64e70ea983530f590282f36991c0a1b105350510f53cc3d1a0279b83c28
Status: Downloaded newer image for wurstmeister/kafka:2.11-2.0.0
 (2)运行并启动kafka容器

[docker@docker kafka]$ docker run  -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.1.200:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.1.200:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -d wurstmeister/kafka:2.11-2.0.0
36f3a86b9fe3eb20c19e5666b68ed2616033e647a5c3607e41ad985405ebf206
[docker@docker kafka]$ docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
36f3a86b9fe3        wurstmeister/kafka:2.11-2.0.0   "start-kafka.sh"         9 seconds ago       Up 7 seconds        0.0.0.0:9092->9092/tcp   kafka
73f52be038de        mesoscloud/zookeeper:3.4.8      "/usr/local/bin/du..."   31 minutes ago      Up 9 minutes        0.0.0.0:2181->2181/tcp   zookeeper
下述 两个参数需改为宿主机器的IP地址,如果不这么设置,可能会导致在别的机器上访问不到kafka。

KAFKA_ZOOKEEPER_CONNECT=192.168.1.200:2181
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.1.200:9092