目錄
- 安裝單個(gè)節(jié)點(diǎn)
- docker-compose.yml 配置
- 安裝集群環(huán)境
- docker-compose.yml配置
- 啟動(dòng)kafka集群
- 總結(jié)
Docker是最受歡迎的容器引擎之一,軟件行業(yè)用于創(chuàng)建、打包、部署應(yīng)用。本教程學(xué)習(xí)如何使用Docker搭建Apache Kafka。
安裝單個(gè)節(jié)點(diǎn)
安裝單節(jié)點(diǎn)Kafka代理需能夠滿足本地開(kāi)發(fā)需求,我們首先學(xué)習(xí)單節(jié)點(diǎn)安裝。
docker-compose.yml 配置
使用Apache Kafka服務(wù),首先需要啟動(dòng)Zookeeper 服務(wù)。這可以通過(guò)在docker-compose.yml 文件配置依賴(lài)實(shí)現(xiàn),但要確保Zookeeper服務(wù)總是在Kafka服務(wù)之前啟動(dòng),反之zookeeper最后停止。
首先創(chuàng)建docker-compose.yml文件,增加兩個(gè)服務(wù):zookeeper和kafka:
version: '2' services: zookeeper: image: confluentinc/cp-zookeeper:latest environment: ZOOKEEPER_CLIENT_PORT: 2181 ZOOKEEPER_TICK_TIME: 2000 ports: - 22181:2181 kafka: image: confluentinc/cp-kafka:latest depends_on: - zookeeper ports: - 29092:29092 environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
Zookeeper服務(wù)監(jiān)聽(tīng)2181端口為Kafka提供服務(wù),但對(duì)于運(yùn)行在本機(jī)的客戶(hù)端,暴露的端口為22181.
類(lèi)似的,kafka通過(guò)端口29092暴露給主機(jī)應(yīng)用,但實(shí)際上在容器中的端口9092。有KAFKA_ADVERTISED_LISTENERS屬性來(lái)配置。
下面通過(guò)使用命令啟動(dòng)容器、開(kāi)啟kafka服務(wù):
$ docker-compose up -d Creating network "kafka_default" with the default driver Creating kafka_zookeeper_1 ... done Creating kafka_kafka_1 ... done
下面通過(guò)nc命令嚴(yán)重兩個(gè)服務(wù)正在監(jiān)聽(tīng)各自的端口:
$ nc -z localhost 22181 Connection to localhost port 22181 [tcp/*] succeeded! $ nc -z localhost 29092 Connection to localhost port 29092 [tcp/*] succeeded!
另外,也可以檢查日志,當(dāng)容器正在運(yùn)行時(shí)驗(yàn)證kafka服務(wù)是否啟動(dòng):
$ docker-compose logs kafka | grep -i started
可以看到Kafka服務(wù)已經(jīng)啟動(dòng)。
最后可以使用Kafka Tool Gui 工具連接已安裝的Kafka服務(wù):
安裝集群環(huán)境
生產(chǎn)環(huán)境需要更穩(wěn)定的環(huán)境,下面擴(kuò)展 docker-compose.yml文件創(chuàng)建多節(jié)點(diǎn)集群環(huán)境:
docker-compose.yml配置
集群安裝需要zookeeper和Kafka有多個(gè)實(shí)例,下面為zookeeper和kafka增加多個(gè)服務(wù):
--- version: '2' services: zookeeper-1: image: confluentinc/cp-zookeeper:latest environment: ZOOKEEPER_CLIENT_PORT: 2181 ZOOKEEPER_TICK_TIME: 2000 ports: - 22181:2181 zookeeper-2: image: confluentinc/cp-zookeeper:latest environment: ZOOKEEPER_CLIENT_PORT: 2181 ZOOKEEPER_TICK_TIME: 2000 ports: - 32181:2181 kafka-1: image: confluentinc/cp-kafka:latest depends_on: - zookeeper-1 - zookeeper-2 ports: - 29092:29092 environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-1:9092,PLAINTEXT_HOST://localhost:29092 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 kafka-2: image: confluentinc/cp-kafka:latest depends_on: - zookeeper-1 - zookeeper-2 ports: - 39092:39092 environment: KAFKA_BROKER_ID: 2 KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-2:9092,PLAINTEXT_HOST://localhost:39092 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
必須要確保kafka服務(wù)名稱(chēng)唯一,通過(guò)KAFKA_BROKER_ID 屬性設(shè)置。而且每個(gè)服務(wù)必須暴露唯一主機(jī)端口。雖然zookeeper-1 和 zookeeper-2 監(jiān)聽(tīng)2181端口,但對(duì)于主機(jī)端口分別為 22181 和 32181。類(lèi)似的,kafka-1 和 kafka-2 services對(duì)應(yīng)主機(jī)端口分別為 29092 和 39092。
啟動(dòng)kafka集群
現(xiàn)在通過(guò)docker-compose命令啟動(dòng)集群:
$ docker-compose up -d Creating network "kafka_default" with the default driver Creating kafka_zookeeper-1_1 ... done Creating kafka_zookeeper-2_1 ... done Creating kafka_kafka-2_1 ... done Creating kafka_kafka-1_1 ... done
啟動(dòng)完成,使用Kafka Tool連接集群,地址通過(guò)逗號(hào)分割只讀多個(gè)zookeeper地址和端口:
總結(jié)
本文使用Docker技術(shù)創(chuàng)建單個(gè)Kafka節(jié)點(diǎn)和Kafka集群環(huán)境,并且使用可視化工具連接服務(wù)查看代理服務(wù)器的配置信息。