基于Docker搭建zookeeper+Kafka单机环境

镜像准备

# zookeeper镜像
docker pull wurstmeister/zookeeper
# kafka镜像
docker pull wurstmeister/kafka
# kafka管理镜像
docker pull sheepkiller/kafka-manager

配置信息

# zoo.cfg 
clientPort=2181
dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=3
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=30

# kafka
//配置分组id
KAFKA_BROKER_ID=1
//开启自动创建主题
KAFKA_AUTO_CREATE_TOPICS_ENABLE=true
//连接zookeeper
KAFKA_ZOOKEEPER_CONNECT=公网ip:2181
//默认内存,调小一点不然启动报错内存溢出
KAFKA_HEAP_OPTS="-Xmx256M -Xms128M"
//配置外网ip访问kafka
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://公网ip:9092

创建文件路径

mkdir -p /root/zookeeper/data
mkdir -p /root/zookeeper/datalog
mkdir -p /root/zookeeper/conf/   # zoo.cfg文件路径

mkdir -p /root/kafka/logs/kafka-logs

整合启动脚本

# !/bin/bash

echo "zookeeper is starting...."
docker run  --name zookeeper -p 2181:2181 \
--restart always \
-v /root/zookeeper/data:/data \
-v /root/zookeeper/datalog:/datalog  \
-v /root/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-e JVMFLAGS="-Xmx256m" \
-d wurstmeister/zookeeper:latest

echo "kafka is starting...."
docker run -d --restart=always --name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=1 \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_HEAP_OPTS="-Xmx256M -Xms128M" \
-e KAFKA_ZOOKEEPER_CONNECT=公网ip:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://公网ip:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
-v /etc/localtime:/etc/localtime \
-v /root/kafka/logs:/kafka/logs \
-d wurstmeister/kafka:latest 

echo "kafka-manager is starting...."
docker run -d --name kafka-manager -p 9000:9000 \
--link zookeeper:zookeeper \
--link kafka:kafka \
--env ZK_HOSTS=zookeeper:2181 \
--restart=always sheepkiller/kafka-manager

echo "Programs startup completed"
docker ps -a                  

kafka-map图形化管理工具(好用)

图形化管理工具
访问地址:http://服务器IP:9001/
DEFAULT_USERNAME:默认账号admin
DEFAULT_PASSWORD:默认密码admin

docker run -d --name kafka-map \
    -p 9001:8080 \
    -v /opt/kafka-map/data:/usr/local/kafka-map/data \
    -e DEFAULT_USERNAME=admin \
    -e DEFAULT_PASSWORD=admin \
    --restart always dushixiang/kafka-map:latest

你可能感兴趣的:(Docker,Kafka,Zookeeper,kafka)