docker - 如何在容器外部调整/编辑/添加/更改 kafka docker 容器参数

标签 docker apache-kafka

我们有如下的 kafka docker 容器(在 Linux redhat 7.5 上)

我们根据 - https://docs.confluent.io/5.0.0/installation/docker/docs/installation/single-node-client.html 在容器上创建了 kafka 服务

docker-compose ps
               Name                           Command            State                     Ports
-------------------------------------------------------------------------------------------------------------------
kafka-single-node_kafka_1            /etc/confluent/docker/run   Up      0.0.0.0:9092->9092/tcp

docker ps
CONTAINER ID        IMAGE                                             COMMAND                  CREATED             STATUS              PORTS                                        NAMES
     0.0.0.0:8081->8081/tcp                       kafka-single-node_schemaregistry_1
de584963bb5a        confluentinc/cp-kafka:latest                      "/etc/confluent/dock…"   6 hours ago         Up 6 hours          0.0.0.0:9092->9092/tcp                       kafka-single-node_kafka_1

关于 - docker-compose.yml

pwd
/home/cp-docker-images/examples/kafka-single-node

more docker-compose.yml

kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 9092:9092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

我们想在容器之外控制 kafka docker 容器配置 - /etc/kafka/server.properties,(或从操作系统本身)

假设我想在 server.properties(kafka 容器的)中添加一些参数或更改一些参数

那么为什么最好在 kafka 容器之外进行配置

在下面的示例中,我展示了如何访问 server.properties ,在 docker kafka 容器中

docker exec -it de584963bb5a    bash

现在我们可以访问/读取文件 - server.properties

root@de584963bb5a:/# more /etc/kafka/server.properties
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# see kafka.server.KafkaConfig for additional details and defaults

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
.
.
.

##################### Confluent Proactive Support ######################
# If set to true, and confluent-support-metrics package is installed
# then the feature to collect and report support metrics
# ("Metrics") is enabled.  If set to false, the feature is disabled.
#
confluent.support.metrics.enable=true


# The customer ID under which support metrics will be collected and
# reported.
#
# When the customer ID is set to "anonymous" (the default), then only a
# reduced set of metrics is being collected and reported.
#
# Confluent customers
# -------------------
# If you are a Confluent customer, then you should replace the default
# value with your actual Confluent customer ID.  Doing so will ensure
# that additional support metrics will be collected and reported.
#
confluent.support.customer.id=anonymous

############################# Group Coordinator Settings #############################

# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startu
p.
group.initial.rebalance.delay.ms=0

最佳答案

We want to control the kafka docker container configuration - /etc/kafka/server.properties outside from the container

我会避免从主机安装 server.properties 文件,因为容器中的每个设置都可以通过环境变量进行配置,例如 KAFKA_(variable) - https://docs.confluent.io/current/installation/docker/config-reference.html#confluent-kafka-configuration

相反,您可以 use env_file for the compose file并将所有变量移到那里

kafka:
   image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 9092:9092
    env_file:
      - broker1.env

broker1.env

KAFKA_BROKER_ID=1
KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1

并且您可以使用此模式添加任何代理配置 - https://kafka.apache.org/documentation/#brokerconfigs

关于docker - 如何在容器外部调整/编辑/添加/更改 kafka docker 容器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721206/

相关文章:

hash - Docker层信息发布v1.10

docker - ADD 和 COPY 将目录的内容与构建中已有的内容合并

apache-kafka - Kafka 连接号码类型字段

java - 带有 Zookeeper 的 Kafka 3.5.7 崩溃 NoSuchMethodError : java. nio.ByteBuffer.flip()

spring - 当所有消费者都被删除时,Kafka 消费者组会发生什么

docker - 为什么需要 Docker 多架构(而不是 Docker 引擎抽象差异)

laravel - 无法找到用户 sail : no matching entries in passwd file

java - Quarkus中有没有向Kafka发送消息的函数

docker - Project Atomic - Cockpit 不通过 Vagrant 安装

apache-kafka - 动物园管理员和卡夫卡的领导人选举