使用docker-compose部署zk集群和kafka集群
2022-01-27 本文已影响0人
Shaw_Young
一、环境介绍
使用VM创建四台虚拟机
https://www.jianshu.com/nb/32214413
IP | 说明 |
---|---|
192.168.235.3 | zookeeper、kafka |
192.168.235.4 | zookeeper、kafka |
192.168.235.5 | zookeeper、kafka |
192.168.235.6 | kafka-manager |
二、安装docker
https://www.jianshu.com/p/2a0406c17880
三、安装docker-compose
https://www.jianshu.com/p/8e5a211a9872
四、部署zookeeper集群
三台服务器准备挂载目录
192.168.235.3
[root@zkKafka1 ~]# mkdir /usr/local/zookeeper
[root@zkKafka1 ~]# cd /usr/local/zookeeper/
[root@zkKafka1 zookeeper]# mkdir {data,datalog}
[root@zkKafka1 zookeeper]# vi docker-compose.yml
version: '3.8'
services:
zookeeper1:
image: zookeeper #镜像
restart: always # 重启
container_name: zookeeper1 #容器名
volumes:
- /usr/local/zookeeper/data:/data
- /usr/local/zookeeper/datalog:/datalog
environment:
ZOO_MY_ID: 1 # id
ZOO_SERVERS: server.1=192.168.235.3:2888:3888;2181 server.2=192.168.235.4:2888:3888;2181 server.3=192.168.235.5:2888:3888;2181
network_mode: "host"
启动zookeeper
[root@zkKafka1 zookeeper]# docker-compose up -d
[root@zkKafka1 zookeeper]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c4b572c7882 zookeeper "/docker-entrypoint.…" 3 minutes ago Up 3 minutes zookeeper1
[root@zkKafka1 zookeeper]# docker exec -it zookeeper1 /bin/bash
root@zkKafka1:/apache-zookeeper-3.7.0-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
192.168.235.4
[root@zkKafka2 ~]# mkdir /usr/local/zookeeper
[root@zkKafka2 ~]# cd /usr/local/zookeeper/
[root@zkKafka2 zookeeper]# mkdir {data,datalog}
[root@zkKafka2 zookeeper]# vi docker-compose.yml
version: '3.8'
services:
zookeeper2:
image: zookeeper #镜像
restart: always # 重启
container_name: zookeeper2 #容器名
volumes:
- /usr/local/zookeeper/data:/data
- /usr/local/zookeeper/datalog:/datalog
environment:
ZOO_MY_ID: 2 # id
ZOO_SERVERS: server.1=192.168.235.3:2888:3888;2181 server.2=192.168.235.4:2888:3888;2181 server.3=192.168.235.5:2888:3888;2181
network_mode: "host"
启动zookeeper
[root@zkKafka2 zookeeper]# docker-compose up -d
[root@zkKafka2 zookeeper]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bf7fc646dac zookeeper "/docker-entrypoint.…" 5 minutes ago Up 5 minutes zookeeper2
[root@zkKafka2 zookeeper]# docker exec -it zookeeper2 /bin/bash
root@zkKafka2:/apache-zookeeper-3.7.0-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
192.168.235.5
[root@zkKafka3 ~]# mkdir /usr/local/zookeeper
[root@zkKafka3 ~]# cd /usr/local/zookeeper/
[root@zkKafka3 zookeeper]# mkdir {data,datalog}
[root@zkKafka3 zookeeper]# vi docker-compose.yml
version: '3.8'
services:
zookeeper3:
image: zookeeper #镜像
restart: always # 重启
container_name: zookeeper3 #容器名
volumes:
- /usr/local/zookeeper/data:/data
- /usr/local/zookeeper/datalog:/datalog
environment:
ZOO_MY_ID: 3 # id
ZOO_SERVERS: server.1=192.168.235.3:2888:3888;2181 server.2=192.168.235.4:2888:3888;2181 server.3=192.168.235.5:2888:3888;2181
network_mode: "host"
启动zookeeper
[root@zkKafka3 zookeeper]# docker-compose up -d
[root@zkKafka3 zookeeper]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8633d1c7e6cd zookeeper "/docker-entrypoint.…" 8 minutes ago Up 8 minutes zookeeper3
[root@zkKafka3 zookeeper]# docker exec -it zookeeper3 /bin/bash
root@zkKafka3:/apache-zookeeper-3.7.0-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
五、部署kafka集群
192.168.235.3
准备挂载目录
[root@zkKafka1 ~]# mkdir /usr/local/kafka
[root@zkKafka1 ~]# cd /usr/local/kafka/
[root@zkKafka1 kafka]# mkdir log
[root@zkKafka1 kafka]# vi docker-compose.yml
编写docker-compose.yml文件
version: '3.8'
services:
kafka1:
image: wurstmeister/kafka:2.11-0.11.0.3 # 镜像
restart: always #重启
container_name: kafka1 #容器名
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.235.3:9092 #绑定发布订阅的端口
KAFKA_ZOOKEEPER_CONNECT: 192.168.235.3:2181,192.168.235.4:2181,192.168.235.5:2181 #连接zookeeper的服务地址
KAFKA_ADVERTISED_PORT: 9092 # 暴露在外的端口
KAFKA_BROKER_ID: 0 #kafka的broker.id,区分不同broker
KAFKA_LISTENERS: PLAINTEXT://192.168.235.3:9092 #绑定监听9092端口
JMX_PORT: 9999 # jmx
KAFKA_MESSAGE_MAX_BYTES: 2000000 #单条消息最大字节数
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/kafka/log:/kafka
network_mode: "host"
启动kafka
[root@zkKafka1 kafka]# docker-compose up -d
192.168.235.4
准备挂载目录
[root@zkKafka2 ~]# mkdir /usr/local/kafka
[root@zkKafka2 ~]# cd /usr/local/kafka/
[root@zkKafka2 kafka]# mkdir log
[root@zkKafka2 kafka]# vi docker-compose.yml
编写docker-compose.yml文件
version: '3.8'
services:
kafka2:
image: wurstmeister/kafka:2.11-0.11.0.3 # 镜像
restart: always #重启
container_name: kafka2 #容器名
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.235.4:9092 #绑定发布订阅的端口
KAFKA_ZOOKEEPER_CONNECT: 192.168.235.3:2181,192.168.235.4:2181,192.168.235.5:2181 #连接zookeeper的服务地址
KAFKA_ADVERTISED_PORT: 9092 # 暴露在外的端口
KAFKA_BROKER_ID: 0 #kafka的broker.id,区分不同broker
KAFKA_LISTENERS: PLAINTEXT://192.168.235.4:9092 #绑定监听9092端口
JMX_PORT: 9998 # jmx
KAFKA_MESSAGE_MAX_BYTES: 2000000 #单条消息最大字节数
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/kafka/log:/kafka
network_mode: "host"
启动kafka
[root@zkKafka2 kafka]# docker-compose up -d
192.168.235.5
准备挂载目录
[root@zkKafka3 ~]# mkdir /usr/local/kafka
[root@zkKafka3 ~]# cd /usr/local/kafka/
[root@zkKafka3 kafka]# mkdir log
[root@zkKafka3 kafka]# vi docker-compose.yml
编写docker-compose.yml文件
version: '3.8'
services:
kafka3:
image: wurstmeister/kafka:2.11-0.11.0.3 # 镜像
restart: always #重启
container_name: kafka3 #容器名
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.235.5:9092 #绑定发布订阅的端口
KAFKA_ZOOKEEPER_CONNECT: 192.168.235.3:2181,192.168.235.4:2181,192.168.235.5:2181 #连接zookeeper的服务地址
KAFKA_ADVERTISED_PORT: 9092 # 暴露在外的端口
KAFKA_BROKER_ID: 0 #kafka的broker.id,区分不同broker
KAFKA_LISTENERS: PLAINTEXT://192.168.235.5:9092 #绑定监听9092端口
JMX_PORT: 9997 # jmx
KAFKA_MESSAGE_MAX_BYTES: 2000000 #单条消息最大字节数
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/kafka/log:/kafka
network_mode: "host"
启动kafka
[root@zkKafka3 kafka]# docker-compose up -d
六、部署kafka-manager
[root@localhost ~]# mkdir /usr/local/kafka-manager
[root@localhost ~]# cd /usr/local/kafka-manager/
[root@localhost kafka-manager]# vi docker-compose.yml
编写docker-compose.yml文件
version: '3.8'
services:
kafka-manager:
image: hlebalbau/kafka-manager:stable
restart: always
container_name: kafka-manager
environment:
ZK_HOSTS: 192.168.235.3:2181,192.168.235.4:2181,192.168.235.5:2181
KAFKA_BROKERS: 192.168.235.3:9092,192.168.235.4:9092,192.168.235.5:9092
APPLICATION_SECRET: "random-secret"
KAFKA_MANAGER_AUTH_ENABLED: "true" # 开启验证
KAFKA_MANAGER_USERNAME: "admin" # 用户名
KAFKA_MANAGER_PASSWORD: "admin" # 密码
KM_ARGS: -Djava.net.preferIPv4Stack=true
network_mode: "host"
启动kafka-manager
[root@localhost kafka-manager]# docker-compose up -d
访问192.168.78.155:9000
账号:admin
密码:admin
手动添加集群即可