Docker搭建Zookeeper
獲取zookeeper
docker pull zookeeper
運(yùn)行zookeeper
docker run -it -p2888:2888 -p2181:2181 --name=zookeeper zookeeper
自此zookeeper搭建完畢
從控制臺返回的信息中咐容,我們可以得知zookeeper的配置文件地址為 /conf/zoo.cfg功偿,我們可以對zookeeper的配置進(jìn)行自己的定制,這個(gè)我們以后有機(jī)會一起分析。
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去制作一個(gè)kafka的docker鏡像
獲取centos的Docker鏡像
docker pull centos
注
docker hub 官方最新的centos版本是centos8,如果我們要使用centos7可以指定tag來制定我們使用的centos版本,centos8相比較于7的版本酣栈,其中之一就是軟件倉庫管理軟件的變化,centos7使用的 yum 更改為centos8中的dnf
docker pull centos:7
下載完成后汹押,我們運(yùn)行centos鏡像
docker run it -p9092:9092 --name=kafka centos
同樣矿筝,我們?nèi)绻褂胏entos7的話,可以使用
docker run it -p9092:9092 --name=kafka centos:7
執(zhí)行完成后鲸阻,我們成功的進(jìn)入了docker容器內(nèi)部的centos命令行中
首先跋涣,我們更新一下軟件列表
dnf update
更完成后缨睡,我們再去下載vim
dnf install vim
由于kafka需要java環(huán)境,我們需要安裝openjdk
dnf install java
安裝完成后陈辱,運(yùn)行
java -version
可以看到返回結(jié)果
[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
這樣我們就獲得了一個(gè)kafka
解壓完成后奖年,我們來獲取zookeeper的ip地址
使用contro+p+q來退出docker 內(nèi)部容器控制臺,返回值宿主機(jī)控制臺
使用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
結(jié)果
qinghai@localhost software % docker inspect --format='{{.NetworkSettings.IPAddress}}' zookeeper
172.17.0.2
那么我獲取到的zookeeper的ip地址為:172.17.0.2
我們重新回到kafka容器內(nèi)部
docker exec -it kafka /bin/bash
進(jìn)入kafka的安裝目錄后沛贪,我們進(jìn)入bin目錄中
進(jìn)入kafka的config的路徑中
編輯server.properties文件
將zookeeper.connect=localhost:2181更改為zookeeper.connect=172.17.0.2:2181
啟動kafka
./kafka-server-start.sh ../config/server.properties
我們會看到啟動結(jié)果
上面的部分省略....
[2020-09-09 13:59:16,767] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
此時(shí)陋守,我們使用contro+p+q重新進(jìn)入kafka容器中
docker exec -it kafka /bin/bash
進(jìn)入kafka的安裝路徑,在進(jìn)入bin執(zhí)行
[root@0db103c94a5c bin]# ./kafka-topics.sh --create --zookeeper 172.17.0.2:2181 --replication-factor 1 --partitions 1 --topic nanjolono
得到了結(jié)果利赋,我們創(chuàng)建了一個(gè)名字為nanjolono的topic
Created topic nanjolono.
那么我們來查看是否成功了吧水评!
查看創(chuàng)建的topic
./kafka-topics.sh --list --zookeeper 172.17.0.2:2181
得到結(jié)果
nanjolono
創(chuàng)建成功!
發(fā)送一些消息
[root@0db103c94a5c bin]# ./kafka-console-producer.sh --broker-list localhost:9092 --topic nanjolono
>
如何驗(yàn)證我們是否接收到消息媚送?
我們再次創(chuàng)建一個(gè)shell窗口
進(jìn)入kafka容器中
docker exec -it kafka /bin/bash
進(jìn)入kafka的安裝目錄中燥,進(jìn)入bin目錄
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic nanjolono --from-beginning
此時(shí),我們在生產(chǎn)者控制臺輸入文字塘偎,消費(fèi)者控制臺能夠成功接受到疗涉,就是成功啦!
お疲れ様でした!
感謝你的收看吟秩!我們成功的使用docker搭建了一個(gè)可以使用的kafka環(huán)境咱扣。