KafKa 集群【docker compose】

文章目录

主机准备

IP controller id broker id
192.168.142.157 1 5
192.168.142.156 2 6
192.168.142.155 3 7
192.168.142.158 4 8

部署

创建文件夹

mkdir -p /docker/kafka/{data,data1,data2}

chmod 777 -R {data,data1,data2}

编辑 docker-compose.yml

controller

docker-compose.yml

services:
  controller:
    image: bitnami/kafka:3.8.0
    container_name: controller
    network_mode: host
    restart: always
    volumes:
      - /docker/kafka/data:/data
      - /etc/localtime:/etc/localtime:ro
    environment:
      KAFKA_CFG_NODE_ID: 1
      KAFKA_CFG_PROCESS_ROLES: controller
      KAFKA_CFG_LISTENERS: CONTROLLER://192.168.142.157:9093
      KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
      KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@192.168.142.157:9093,2@192.168.142.156:9093,3@192.168.142.155:9093,4@192.168.142.158:9093
      KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_CFG_LOG_DIRS: /data

broker

image: bitnami/kafka:3.8.0
    container_name: broker
    network_mode: host
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /docker/kafka/data1:/data1
      - /docker/kafka/data2:/data2
    environment:
      KAFKA_CFG_NODE_ID: 5
      KAFKA_CFG_PROCESS_ROLES: broker
      KAFKA_CFG_LISTENERS: PLAINTEXT://192.168.142.157:9092
      KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://192.168.142.157:9092
      KAFKA_CFG_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@192.168.142.157:9093,2@192.168.142.156:9093,3@192.168.142.155:9093,4@192.168.142.158:9093
      KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_CFG_LOG_DIRS: "/data1,/data2"

所有的主机这一段基本都是一样的 只有 id 不同,id 可以根据上面表格进行修改

生成cluster_id

root@master:/docker/kafka# docker run -it --rm bitnami/kafka:3.8.0 kafka-storage.sh random-uuid
kafka 06:43:36.79 INFO  ==>
kafka 06:43:36.80 INFO  ==> Welcome to the Bitnami kafka container
kafka 06:43:36.80 INFO  ==> Subscribe to project updates by watching https://github.com/bitnami/containers
kafka 06:43:36.80 INFO  ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
kafka 06:43:36.80 INFO  ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
kafka 06:43:36.80 INFO  ==>

PavLImfXSqaOOdrMgq1teA

拿到 id

PavLImfXSqaOOdrMgq1teA

新增到 docker-compose.yml 文件中

一篇完整的 docker-compose.yml 文件

services:
  controller:
    image: bitnami/kafka:3.8.0
    container_name: controller
    network_mode: host
    restart: always
    volumes:
      - /docker/kafka/data:/data
      - /etc/localtime:/etc/localtime:ro
    environment:
      KAFKA_CFG_NODE_ID: 1
      KAFKA_CFG_PROCESS_ROLES: controller
      KAFKA_CFG_LISTENERS: CONTROLLER://192.168.142.157:9093
      KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
      KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@192.168.142.157:9093,2@192.168.142.156:9093,3@192.168.142.155:9093,4@192.168.142.158:9093
      KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_CFG_LOG_DIRS: /data
      KAFKA_KRAFT_CLUSTER_ID: PavLImfXSqaOOdrMgq1teA
  broker:
    image: bitnami/kafka:3.8.0
    container_name: broker
    network_mode: host
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /docker/kafka/data1:/data1
      - /docker/kafka/data2:/data2
    environment:
      KAFKA_CFG_NODE_ID: 5
      KAFKA_CFG_PROCESS_ROLES: broker
      KAFKA_CFG_LISTENERS: PLAINTEXT://192.168.142.157:9092
      KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://192.168.142.157:9092
      KAFKA_CFG_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@192.168.142.157:9093,2@192.168.142.156:9093,3@192.168.142.155:9093,4@192.168.142.158:9093
      KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_CFG_LOG_DIRS: "/data1,/data2"
      KAFKA_KRAFT_CLUSTER_ID: PavLImfXSqaOOdrMgq1teA

启动

docker compose up -d

查看启动状态

docker ps -a | grep Up | wc -l
2

查看集群状态

使用 kafka-ui 查看

拉取 kafka-ui

只需要在一台主机上部署 ui 就可以了

docker run -d -p 8080:8080 -e DYNAMIC_CONFIG_ENABLED=true provectuslabs/kafka-ui:v0.7.1 

通过 ip + 8080 端口访问

添加集群


查看集群状态

使用命令行查看

root@master:/docker/kafka# docker exec -it broker ./opt/bitnami/kafka/bin/kafka-metadata-quorum.sh --bootstrap-controller 192.168.142.157:9093,192.168.142.156:9093,192.168.142.155:9093,192.168.142.158:9093 describe --status
ClusterId:              PavLImfXSqaOOdrMgq1teA
LeaderId:               1
LeaderEpoch:            22
HighWatermark:          7695
MaxFollowerLag:         0
MaxFollowerLagTimeMs:   0
CurrentVoters:          [1,2,3,4]
CurrentObservers:       [5,6,7,8]

到此集群搭建完成

配置讲解

controller

参数 解释
KAFKA_CFG_NODE_ID 节点 id
KAFKA_CFG_PROCESS_ROLES 节点角色
KAFKA_CFG_LISTENERS 节点监听的 ip 和端口,并且标记为 controller
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP 定义不同监听器使用的加密协议
KAFKA_CFG_CONTROLLER_LISTENER_NAMES 控制器(Controller)用于接收来自其他控制器节点和 broker 节点请求的监听器(Listener)的名称
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS 添加集群节点
KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS 当一个新的消费者加入到一个空的消费组时,协调者(Coordinator)将延迟多长时间再开始重新平衡(Rebalance)的过程
KAFKA_CFG_LOG_DIRS 日志目录
KAFKA_KRAFT_CLUSTER_ID 集群 id

broker

参数 解释
KAFKA_CFG_NODE_ID 节点 id
KAFKA_CFG_PROCESS_ROLES 节点角色
KAFKA_CFG_LISTENERS 节点监听的 ip 和端口,并且标记为 broker
KAFKA_CFG_INTER_BROKER_LISTENER_NAME 集群中 broker 之间通信所使用的监听器名称
KAFKA_CFG_CONTROLLER_LISTENER_NAMES 监听器的名称
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP 定义不同监听器使用的加密协议
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS 添加集群节点
KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS 当一个新的消费者加入到一个空的消费组时,协调者(Coordinator)将延迟多长时间再开始重新平衡(Rebalance)的过程
KAFKA_CFG_LOG_DIRS 日志目录
KAFKA_KRAFT_CLUSTER_ID 集群 id
相关推荐
蜡笔小鑫️9 小时前
金碟中间件-AAS-V10.0安装
中间件
Lspecialnx_11 小时前
文件解析漏洞中间件(iis和Apache)
网络安全·中间件
隔着天花板看星星11 小时前
Spark-Streaming集成Kafka
大数据·分布式·中间件·spark·kafka
Autumn.h1 天前
文件解析漏洞
web安全·网络安全·中间件
武汉联从信息2 天前
Oracle 中间件 Webcenter Portal服务器环境搭建
中间件
小屁不止是运维3 天前
麒麟操作系统服务架构保姆级教程(三)NGINX中间件
linux·运维·服务器·nginx·中间件·架构
what_20183 天前
中间件 mysql安装
mysql·中间件
飞的肖3 天前
在微服务架构中,处理消息的中间件是实现服务间异步通信的关键组件。以下是几种常见的消息中间件及其特点、优点和缺点
微服务·中间件·架构
往日情怀酿做酒 V17639296385 天前
Django基础之中间件
python·中间件·django
what_20185 天前
中间件 redis安装
redis·中间件