centos环境下docker-compose部署kafka集群
1.安装docker-compose
使用curl安装docker-compose:
#下载镜像
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#添加可执行权限
chmod +x /usr/local/bin/docker-compose
控制台输入docker-compose –version成功则显示
若验证是否安装成功时出错
进入下面的网址下载目前的最新版:
https://github.com/docker/compose/releases/tag/1.14.0-rc2
网页拉到最下面,下载:
docker-compose-Linux-x86_64
然后将文件上传到 /usr/local/bin/ 文件夹下,然后将其重命名为docker-compose,修改此文件的权限,增加可执行:chmod +x /usr/local/bin/docker-compose
2.下载镜像(默认宿主机已安装docker)
下载wurstmeister/zookeeper、wurstmeister/kafka、sheepkiller/kafka-manager
docker pull wurstmeister/zookeeper
docker pull wurstmeister/kafka
docker pull sheepkiller/kafka-manager
3.添加docker-compose.yml脚本
version: '2'
services:
zoo1:
image: wurstmeister/zookeeper
restart: unless-stopped
hostname: zoo1
ports:
- "2181:2181"
container_name: zookeeper
kafka1:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 192.168.0.119 ## 修改:宿主机IP
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.119:9092 ## 修改:宿主机IP
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
KAFKA_ADVERTISED_PORT: 9092
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zoo1
container_name: kafka1
kafka2:
image: wurstmeister/kafka
ports:
- "9093:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 192.168.0.119 ## 修改:宿主机IP
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.119:9093 ## 修改:宿主机IP
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
KAFKA_ADVERTISED_PORT: 9093
KAFKA_BROKER_ID: 2
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zoo1
container_name: kafka2
kafka-manager:
image: sheepkiller/kafka-manager ## 镜像:开源的web管理kafka集群的界面
environment:
ZK_HOSTS: 192.168.0.119 ## 修改:宿主机IP
ports:
- "9000:9000" ## 暴露端口
4.运行docker-compose
在docker-compose.yml脚本目录下输入docker-compose up -d
5.显示kafka集群信息
通过kafka-manager查看kafka集群信息,topic信息,新增topic等操作
浏览器输入192.168.0.119:9000,然后添加cluster,其中zk地址填写zoo1:2181
6.测试kafka生产者,消费者
控制台创建生产者,消费者,模拟kafka消费
#创建topic
docker exec kafka1 \
kafka-topics.sh \
--create --topic topic001 \
--partitions 1 \
--zookeeper zookeeper:2181 \
--replication-factor 1
#获取topic列表
docker exec kafka1 \
kafka-topics.sh --list \
--zookeeper zookeeper:2181
#进入kafka消费者
docker exec kafka1 \
kafka-console-consumer.sh \
--topic topic001 \
--bootstrap-server kafka1:9092,kafka2:9092
进入kafka生产者
docker exec -it kafka1 \
kafka-console-producer.sh \
--topic topic001 \
--broker-list kafka1:9092,kafka2:9092
测试效果
生产者
消费者