这篇文章将为大家详细讲解有关centos7.6如何安装docker-zookeeper-kafka,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
单机方案:
# 安装zookeeper
docker run -d \
--name zk \
-p 12181:2181 \
-v /etc/localtime:/etc/localtime \
--restart=always \
zookeeper
# 安装kafka
docker run -d \
--name kafka \
-p 9092:9092 \
--link zk:zookeeper \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_ZOOKEEPER_CONNECT=zk:2181 \
-e KAFKA_LISTENERS=PLAINTEXT://:9092 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.1.100:9092 \
--restart=always \
wurstmeister/kafka
# 进入kafka
docker exec -it kafka /bin/bash
# 创建topic
/opt/kafka/bin/kafka-topics.sh --create --zookeeper zk:2181 --replication-factor 1 --partitions 1 --topic my_topic
# 查看topic列表
/opt/kafka/bin/kafka-topics.sh --list --zookeeper zk:2181
# 发送消息
/opt/kafka/bin/kafka-console-producer.sh --broker-list 122.228.113.229:9092 --topic my_topic
# 接受消息
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 122.228.113.229:9092 --topic my_topic --from-beginning
集群方案:
-- 安装docker-compose
curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
-- 赋予执行权限
chmod +x /usr/local/bin/docker-compose
-- 拉取镜像
docker pull zookeeper:3.4
docker pull wurstmeister/kafka
-- 编写docker-compose.yaml
-- 启动服务
docker run -d zookeeper
-- 测试zookeeper
telnet 127.0.0.1 12181
docker exec -it zoo1 bash ./bin/zkServer.sh status
docker exec -it zoo2 bash ./bin/zkServer.sh status
docker exec -it zoo3 bash ./bin/zkServer.sh status
-- 测试kafka
telnet 127.0.0.1 19092
-- 链接kafka1容器
docker exec -it kafka1 /bin/bash
-- 创建topic
$KAFKA_HOME/bin/kafka-topics.sh --create --topic test --zookeeper zoo1:2181 --replication-factor 1 --partitions 1
-- 查看topic
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper zoo1:2181 --describe --topic test
-- 发布消息
$KAFKA_HOME/bin/kafka-console-producer.sh --topic=test --broker-list kafka1:9092
-- 接收消息
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server kafka1:9092 --from-beginning --topic test
mkdir -p /home/docker_zoo_kafka
cat > /home/docker_zoo_kafka/docker-compose.yml <<EOF
version: "2"
services:
zoo1:
image: zookeeper:3.4
container_name: zoo1
restart: always
ports:
- 12181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
volumes:
- /etc/localtime:/etc/localtime
zoo2:
image: zookeeper:3.4
container_name: zoo2
restart: always
ports:
- 22181:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
volumes:
- /etc/localtime:/etc/localtime
zoo3:
image: zookeeper:3.4
container_name: zoo3
restart: always
ports:
- 32181:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
volumes:
- /etc/localtime:/etc/localtime
kafka1:
image: wurstmeister/kafka
container_name: kafka1
restart: always
ports:
- 19092:9092
environment:
KAFKA_HOST_NAME: kafka1
KAFKA_ADVERTISED_HOST_NAME: kafka1
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.19.160.224:19092
KAFKA_ADVERTISED_PORT: 19092
KAFKA_BROKER_ID: 0
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
volumes:
- /etc/localtime:/etc/localtime
links:
- zoo1
- zoo2
- zoo3
kafka2:
image: wurstmeister/kafka
container_name: kafka2
restart: always
ports:
- 29092:9092
environment:
KAFKA_HOST_NAME: kafka2
KAFKA_ADVERTISED_HOST_NAME: kafka2
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.19.160.224:29092
KAFKA_ADVERTISED_PORT: 29092
KAFKA_BROKER_ID: 1
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
volumes:
- /etc/localtime:/etc/localtime
links:
- zoo1
- zoo2
- zoo3
kafka3:
image: wurstmeister/kafka
container_name: kafka3
restart: always
ports:
- 39092:9092
environment:
KAFKA_HOST_NAME: kafka3
KAFKA_ADVERTISED_HOST_NAME: kafka3
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.19.160.224:39092
KAFKA_ADVERTISED_PORT: 39092
KAFKA_BROKER_ID: 2
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
volumes:
- /etc/localtime:/etc/localtime
links:
- zoo1
- zoo2
- zoo3
EOF
说明:172.19.160.224是宿主机IP
关于“centos7.6如何安装docker-zookeeper-kafka”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。