一.首先安装docker,在Linux系统依次输入以下命令。
1.sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2.sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.sudo yum makecache fast
4.sudo yum -y install docker-ce
5.sudo service docker start
然后配置docker镜像加速,依次输入以下命令(可不配)
1.sudo mkdir -p /etc/docker
2.sudo tee /etc/docker/daemon.conf <<-'EOF'
{
"registry-mirrors": ["https://ung2thfc.mirror.aliyuncs.com"]
}
EOF
3.sudo systemctl daemon-reload
4.sudo systemctl restart docker
二.安装docker-compose
1.下载docker-compose二进制文件安装
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
2.添加可执行权限
sudo chmod +x /usr/local/bin/docker-compose
测试安装结果
docker-compose --version
三.安装kafka
创建docker-compose-test.yml文件
vim docker-compose-test.yml
version: "3.3"
services:
zookeeper:
image: zookeeper:3.5.5
restart: always
container_name: zookeeper
ports:
- "2181:2181"
expose:
- "2181"
environment:
- ZOO_MY_ID=1
kafka:
image: wurstmeister/kafka:2.12-2.2.1
restart: always
container_name: kafka
environment:
- KAFKA_BROKER_ID=1
- KAFKA_LISTENERS=PLAINTEXT://kafka:9090
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_MESSAGE_MAX_BYTES=2000000
ports:
- "9090:9090"
depends_on:
- zookeeper
将 docker-compose-test.yml
放入你的目录,运行部署脚本
docker-compose -f docker-compose-test.yml up -d
查看两者的日志,检查是否正常启动
docker logs -f zookeeper
docker logs -f kafka
查看一下,Zookeeper 中注册的 Broker 信息
docker exec -it zookeeper bash bin/zkCli.sh
ls /brokers/ids
get /brokers/ids/1
输入 quit
退出
配置宿主机对 kafka
映射
编辑/etc/hosts,添加宿主机ip kafka
vim /etc/hosts
创建Topic ,没有输出,说明创建 Topic 成功!
docker run -it --rm --network host wurstmeister/kafka:2.12-2.2.1 bash /opt/kafka/bin/kafka-topics.sh --bootstrap-server kafka:9090 --create --topic logsTopic --partitions 1 --replication-factor 1
生产一条测试消息
docker run -it --rm --network host wurstmeister/kafka:2.12-2.2.1 bash /opt/kafka/bin/kafka-console-producer.sh --broker-list kafka:9090 --topic logsTopic
输入回车,发送消息; ctrl + c 断开连接
创建消费者,消费消息测试
docker run -it --rm --network host wurstmeister/kafka:2.12-2.2.1 bash /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server kafka:9090 --topic logsTopic --from-beginning
消费成功!