美文网首页
使用Docker搭建单机Kafka

使用Docker搭建单机Kafka

作者: 晴海s | 来源:发表于2020-09-09 22:14 被阅读0次

    Docker搭建Zookeeper

    获取zookeeper

    docker pull zookeeper 
    

    运行zookeeper

    docker run -it -p2888:2888 -p2181:2181 --name=zookeeper zookeeper
    

    自此zookeeper搭建完毕

    从控制台返回的信息中,我们可以得知zookeeper的配置文件地址为 /conf/zoo.cfg,我们可以对zookeeper的配置进行自己的定制,这个我们以后有机会一起分析。

    qinghai@localhost my_kata % docker run -it -p2888:2888 -p2181:2181 --name=zookeeper zookeeper 
    ZooKeeper JMX enabled by default
    Using config: /conf/zoo.cfg
    2020-09-09 07:20:37,460 [myid:] - INFO  [main:QuorumPeerConfig@173] - Reading configuration from: /conf/zoo.cfg
    2020-09-09 07:20:37,471 [myid:] - INFO  [main:QuorumPeerConfig@450] - clientPort is not set
    2020-09-09 07:20:37,471 [myid:] - INFO  [main:QuorumPeerConfig@463] - secureClientPort is not set
    2020-09-09 07:20:37,471 [myid:] - INFO  [main:QuorumPeerConfig@479] - observerMasterPort is not set
    2020-09-09 07:20:37,473 [myid:] - INFO  [main:QuorumPeerConfig@496] - metricsProvider.className is org.apache.zookeeper.metrics.impl.DefaultMetricsProvider
    2020-09-09 07:20:37,484 [myid:] - ERROR [main:QuorumPeerConfig@720] - Invalid configuration, only one server specified (ignoring)
    2020-09-09 07:20:37,489 [myid:1] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
    2020-09-09 07:20:37,489 [myid:1] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
    2020-09-09 07:20:37,490 [myid:1] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
    2020-09-09 07:20:37,490 [myid:1] - WARN  [main:QuorumPeerMain@138] - Either no config or no quorum defined in config, running in standalone mode
    2020-09-09 07:20:37,495 [myid:1] - INFO  [main:ManagedUtil@44] - Log4j 1.2 jmx support found and enabled.
    2020-09-09 07:20:37,504 [myid:1] - INFO  [main:QuorumPeerConfig@173] - Reading configuration from: /conf/zoo.cfg
    2020-09-09 07:20:37,504 [myid:1] - INFO  [main:QuorumPeerConfig@450] - clientPort is not set
    2020-09-09 07:20:37,505 [myid:1] - INFO  [main:QuorumPeerConfig@463] - secureClientPort is not set
    2020-09-09 07:20:37,505 [myid:1] - INFO  [main:QuorumPeerConfig@479] - observerMasterPort is not set
    2020-09-09 07:20:37,505 [myid:1] - INFO  [main:QuorumPeerConfig@496] - metricsProvider.className is org.apache.zookeeper.metrics.impl.DefaultMetricsProvider
    2020-09-09 07:20:37,508 [myid:1] - ERROR [main:QuorumPeerConfig@720] - Invalid configuration, only one server specified (ignoring)
    2020-09-09 07:20:37,508 [myid:1] - INFO  [main:ZooKeeperServerMain@122] - Starting server
    2020-09-09 07:20:37,526 [myid:1] - INFO  [main:ServerMetrics@62] - ServerMetrics initialized with provider org.apache.zookeeper.metrics.impl.DefaultMetricsProvider@7a675056
    2020-09-09 07:20:37,529 [myid:1] - INFO  [main:FileTxnSnapLog@124] - zookeeper.snapshot.trust.empty : false
    2020-09-09 07:20:37,544 [myid:1] - INFO  [main:ZookeeperBanner@42] - 
    2020-09-09 07:20:37,545 [myid:1] - INFO  [main:ZookeeperBanner@42] -   ______                  _                                          
    2020-09-09 07:20:37,545 [myid:1] - INFO  [main:ZookeeperBanner@42] -  |___  /                 | |                                         
    2020-09-09 07:20:37,545 [myid:1] - INFO  [main:ZookeeperBanner@42] -     / /    ___     ___   | | __   ___    ___   _ __     ___   _ __   
    2020-09-09 07:20:37,545 [myid:1] - INFO  [main:ZookeeperBanner@42] -    / /    / _ \   / _ \  | |/ /  / _ \  / _ \ | '_ \   / _ \ | '__|
    2020-09-09 07:20:37,545 [myid:1] - INFO  [main:ZookeeperBanner@42] -   / /__  | (_) | | (_) | |   <  |  __/ |  __/ | |_) | |  __/ | |    
    2020-09-09 07:20:37,546 [myid:1] - INFO  [main:ZookeeperBanner@42] -  /_____|  \___/   \___/  |_|\_\  \___|  \___| | .__/   \___| |_|
    2020-09-09 07:20:37,546 [myid:1] - INFO  [main:ZookeeperBanner@42] -                                               | |                     
    2020-09-09 07:20:37,546 [myid:1] - INFO  [main:ZookeeperBanner@42] -                                               |_|                     
    2020-09-09 07:20:37,546 [myid:1] - INFO  [main:ZookeeperBanner@42] - 
    

    Docker搭建kafka

    由于kafka没有官方的docker镜像,所以我们使用docker版本的centos去制作一个kafka的docker镜像

    获取centos的Docker镜像

    docker pull centos
    

    docker hub 官方最新的centos版本是centos8,如果我们要使用centos7可以指定tag来制定我们使用的centos版本,centos8相比较于7的版本,其中之一就是软件仓库管理软件的变化,centos7使用的 yum 更改为centos8中的dnf

    docker pull centos:7
    

    下载完成后,我们运行centos镜像

    docker run it -p9092:9092 --name=kafka centos
    

    同样,我们如果使用centos7的话,可以使用

    docker run it -p9092:9092 --name=kafka centos:7
    

    执行完成后,我们成功的进入了docker容器内部的centos命令行中

    首先,我们更新一下软件列表

    dnf update
    

    更完成后,我们再去下载vim

    dnf install vim
    

    由于kafka需要java环境,我们需要安装openjdk

    dnf install java
    

    安装完成后,运行

    java -version
    

    可以看到返回结果

    [root@0db103c94a5c /]# java -version 
    openjdk version "1.8.0_262"
    OpenJDK Runtime Environment (build 1.8.0_262-b10)
    OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
    

    安装成功

    让我们安装kafka吧

    通过curl下载kafka

    curl -O -L https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.6.0/kafka_2.12-2.6.0.tgz
    

    下载后解压

    tar -zvxf kafka_2.12-2.6.0.tgz
    

    这样我们就获得了一个kafka

    解压完成后,我们来获取zookeeper的ip地址

    使用contro+p+q来退出docker 内部容器控制台,返回值宿主机控制台

    使用docker ps

    qinghai@localhost software % docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                NAMES
    0db103c94a5c        centos              "/bin/bash"              6 hours ago         Up About an hour    0.0.0.0:9092->9092/tcp                                               kafka
    995242c5d83c        zookeeper           "/docker-entrypoint.…"   6 hours ago         Up About an hour    0.0.0.0:2181->2181/tcp, 3888/tcp, 0.0.0.0:2888->2888/tcp, 8080/tcp   zookeeper
    

    我们要查看zookeeper的ip 那我们使用 docker inspect --format='{{.NetworkSettings.IPAddress}} CONTAINER ID

    docker inspect --format='{{.NetworkSettings.IPAddress}} 995242c5d83c
    

    结果

    qinghai@localhost software % docker inspect --format='{{.NetworkSettings.IPAddress}}' zookeeper 
    172.17.0.2
    

    那么我获取到的zookeeper的ip地址为:172.17.0.2

    我们重新回到kafka容器内部

    docker exec -it kafka /bin/bash
    

    进入kafka的安装目录后,我们进入bin目录中

    进入kafka的config的路径中

    编辑server.properties文件

    将zookeeper.connect=localhost:2181更改为zookeeper.connect=172.17.0.2:2181

    启动kafka

    ./kafka-server-start.sh ../config/server.properties
    

    我们会看到启动结果

    上面的部分省略....
    [2020-09-09 13:59:16,767] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
    

    此时,我们使用contro+p+q重新进入kafka容器中

    docker exec -it kafka /bin/bash
    

    进入kafka的安装路径,在进入bin执行

    [root@0db103c94a5c bin]# ./kafka-topics.sh --create --zookeeper 172.17.0.2:2181 --replication-factor 1 --partitions 1 --topic nanjolono
    

    得到了结果,我们创建了一个名字为nanjolono的topic

    Created topic nanjolono.
    

    那么我们来查看是否成功了吧!

    查看创建的topic

    ./kafka-topics.sh --list --zookeeper 172.17.0.2:2181
    

    得到结果

    nanjolono
    

    创建成功!

    发送一些消息

    [root@0db103c94a5c bin]# ./kafka-console-producer.sh --broker-list localhost:9092 --topic nanjolono
    >
    

    如何验证我们是否接收到消息?

    我们再次创建一个shell窗口

    进入kafka容器中

    docker exec -it kafka /bin/bash 
    

    进入kafka的安装目录,进入bin目录

    ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic nanjolono --from-beginning 
    

    此时,我们在生产者控制台输入文字,消费者控制台能够成功接受到,就是成功啦!

    お疲れ様でした!

    感谢你的收看!我们成功的使用docker搭建了一个可以使用的kafka环境。

    相关文章

      网友评论

          本文标题:使用Docker搭建单机Kafka

          本文链接:https://www.haomeiwen.com/subject/pawyektx.html