kafka 2.4.1单机版部署及使用

本文详细介绍Kafka集群的安装、配置与监控过程,包括下载KafkaEagle、解压、环境变量设置、配置文件修改及启动验证。涵盖topic创建、producer与consumer启动、topic信息查看及消费情况检查等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载,解压,配置环境变量,修改配置,启动验证。

使用Kafka Eagle监控kafka集群:kafka监控系统kafka eagle安装使用

1.下载2.4.1版本

写文章的时候看到2.5.0已经发版的,部署方式一样。

Apache Kafka

2.解压tar包

tar -zxvf kafka_2.11-2.4.1.tgz -C .

3.配置环境变量

vim /etc/profile

export JAVA_HOME=/opt/app/jdk1.8.0_181
export KAFKA_HOME=/opt/app/kafka_2.11-2.4.1
export KE_HOME=/opt/app/kafka-eagle-web-1.4.6

export PATH=$KE_HOME/bin:$KAFKA_HOME/bin:$PATH

souce /etc/profile

4.修改配置文件

vim $KAFKA_HOME/config/server.properties

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

############################# Socket Server Settings #############################
listeners=PLAINTEXT://sdakf01:9092

message.max.bytes=100000000

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600

############################# Log Basics #############################

# A comma separated list of directories under which to store log files
#log.dirs=/tmp/kafka-logs
log.dirs=/opt/app/kafka_2.11-2.4.1/log

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
#zookeeper.connect=localhost:2181
zookeeper.connect=sdakf01:2181 
# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000


############################# 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 startup.
group.initial.rebalance.delay.ms=0

5.启动验证

1.启动自带的zookeeper:

bin/zookeeper-server-start.sh [-daemon]   config/zookeeper.properties 

2.启动kafka:

bin/kafka-server-start.sh [-daemon]  config/server.properties

这里的-daemon为可选参数,加上就是后台启动,不加就是前台启动会打印到控制台。

3.验证:

创建topic:

./kafka-topics.sh --create --zookeeper sdakf01:2181 --replication-factor 1 --partitions 3 --topic wyk_csdn

这里的--replication-factor当有几个broker节点才可以最多有几个备份,因为我是单机的,所以指定的是1。

-------2021.12.13:更新------

之前忘记贴创建topic成功的截图,有人在评论区质疑其可用性和原创性。所以专门重装了一个执行一下,给出截图证明。可以质疑,但请不要这种态度@Amazing66

 

 -----------------------------------

列所有topic:

./kafka-topics.sh --list --zookeeper sdakf01:2181

启动producer:

./kafka-console-producer.sh --broker-list sdakf01:9092  --topic wyk_csdn

启动consumer:

./kafka-console-consumer.sh --bootstrap-server sdakf01:9092 --zookeeper sdakf01:2181 --topic wyk_csdn

消费数据:

查看topic信息:

./kafka-topics.sh --describe  --zookeeper sdakf01:2181 --topic wyk_csdn

查看消费情况:

./kafka-consumer-groups.sh --describe --all-groups --all-topics --bootstrap-server sdakf01:9092

./kafka-consumer-groups.sh --describe --all-topics --bootstrap-server sdakf01:9092 --group noah_nds2

重置offset:

./kafka-consumer-groups.sh --reset-offsets --to-earliest --bootstrap-server sdakf01:9092 --all-topics --group noah_nds2 --execute

<think>好的,我现在需要帮助用户在CentOS 7服务器上通过多实例的方式部署Kafka 2.4.1集群。用户已经安装了JDK,但还没有部署ZooKeeper。我需要一步步指导用户完成接下来的操作。 首先,用户提到要在一台服务器上部署多个Kafka实例来实现集群。这意味着需要配置多个Kafka broker实例,每个实例使用不同的端口和配置。同时,ZooKeeper作为Kafka的依赖,也需要进行配置,可能也是单机多实例或者单实例。不过,单机多实例的ZooKeeper可能不如集群可靠,但用户可能只是测试环境,所以可以接受。 接下来,我需要考虑步骤的顺序。通常,Kafka依赖ZooKeeper,所以应该先部署ZooKeeper。用户还没有部署ZooKeeper,所以可能需要先安装和配置ZooKeeper。不过,Kafka 2.4.1版本自带的脚本可以启动一个单节点的ZooKeeper实例,但如果是生产环境或需要更高可靠性,可能需要独立的ZooKeeper集群。这里用户可能只是为了测试,所以可以建议使用Kafka自带的ZooKeeper,或者单独部署多实例ZooKeeper。 不过用户可能更倾向于独立部署ZooKeeper,所以需要指导用户如何安装和配置ZooKeeper的多实例。或者,如果用户希望简化,可以每个Kafka实例使用不同的ZooKeeper端口,但这样可能不太合适,因为通常ZooKeeper集群需要奇数个节点。不过,由于用户是在单机上运行,可能需要配置ZooKeeper的单机多实例,形成伪集群。 但ZooKeeper的伪集群部署可能会比较复杂,因为需要不同的配置文件和数据目录,以及不同的客户端端口、领导选举端口等。如果用户只是想快速搭建Kafka集群,可能更简单的方式是让每个Kafka broker使用同一个ZooKeeper实例,或者使用Kafka自带的ZooKeeper。但Kafka自带的ZooKeeper通常用于测试,可能不适合多broker的情况,不过根据Kafka文档,只要ZooKeeper配置正确,应该可以支持。 这里可能需要分步骤:首先部署ZooKeeper(单实例或多实例),然后部署多个Kafka broker实例,每个使用不同的端口和配置。 现在,我需要明确用户的需求是否允许使用单个ZooKeeper实例,或者是否需要多实例ZooKeeper。如果用户只是测试,可能单实例ZooKeeper足够,这样步骤更简单。但如果是生产环境,可能需要ZooKeeper集群。不过用户的问题中没有明确说明环境类型,所以可能需要建议两种方案,但考虑到用户是在单机部署,可能更倾向于单实例ZooKeeper。 不过,Kafka集群需要ZooKeeper来管理元数据,所以如果ZooKeeper宕机,整个Kafka集群将不可用。因此,生产环境中ZooKeeper需要集群,但用户可能只是测试,所以可以接受单实例。 接下来,步骤可能是: 1. 下载并安装ZooKeeper,配置单实例,并启动。 2. 下载Kafka 2.4.1,解压多次到不同目录,每个目录对应一个broker实例。 3. 为每个broker实例配置不同的server.properties文件,包括broker.id、port、log.dirs等。 4. 启动每个broker实例,并验证集群状态。 但需要详细说明每个步骤的具体操作,包括配置文件的修改,端口的选择,目录结构等。 例如,ZooKeeper的安装可能需要用户下载ZooKeeper,配置zoo.cfg,设置数据目录,然后启动服务。如果用户使用Kafka自带的ZooKeeper,可能需要使用Kafka的脚本启动,但可能版本差异需要注意。 不过,Kafka 2.4.1版本可能需要特定版本的ZooKeeper,需要确认是否兼容。一般来说,Kafka会自带兼容的ZooKeeper,但用户可能需要独立安装。例如,Kafka 2.4.1可能内置ZooKeeper 3.5.x,但需要确认。 现在,假设用户选择独立安装ZooKeeper,那么步骤可能如下: 1. 下载并解压ZooKeeper,例如3.4.14版本(需要确认兼容性)。 2. 配置zoo.cfg,设置数据目录、客户端端口等。 3. 启动ZooKeeper服务。 然后,对于Kafka1. 下载Kafka 2.4.1,解压到多个目录,如kafka1kafka2kafka3。 2. 修改每个目录中的server.properties,设置不同的broker.id、listeners、log.dirs等。 3. 启动每个Kafka实例,并检查日志确认是否成功。 此外,需要注意防火墙设置,确保端口开放,如ZooKeeper2181端口,以及每个Kafka实例的监听端口(如9092, 9093, 9094)等。 但用户可能在一台服务器上,所以防火墙可能已经关闭或允许本地连接,但需要提醒用户注意端口冲突问题。 另外,每个Kafka实例的配置文件中需要指向ZooKeeper的连接地址,例如localhost:2181,如果ZooKeeper是单实例的话。 现在,需要详细写出每个步骤的具体命令和配置修改。 例如,ZooKeeper的安装步骤: 下载ZooKeeper: wget http://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz tar -zxvf zookeeper-3.4.14.tar.gz cd zookeeper-3.4.14/conf cp zoo_sample.cfg zoo.cfg 然后编辑zoo.cfg,设置dataDir=/path/to/zookeeper/data 然后启动:bin/zkServer.sh start 对于Kafka部分: 下载Kafka: wget https://archive.apache.org/dist/kafka/2.4.1/kafka_2.12-2.4.1.tgz tar -zxvf kafka_2.12-2.4.1.tgz 然后复制三份: cp -r kafka_2.12-2.4.1 kafka1 cp -r kafka_2.12-2.4.1 kafka2 cp -r kafka_2.12-2.4.1 kafka3 然后每个目录中的config/server.properties需要修改: 例如kafka1/config/server.properties: broker.id=1 listeners=PLAINTEXT://:9092 log.dirs=/tmp/kafka-logs-1 zookeeper.connect=localhost:2181 kafka2/config/server.properties: broker.id=2 listeners=PLAINTEXT://:9093 log.dirs=/tmp/kafka-logs-2 zookeeper.connect=localhost:2181 kafka3/config/server.properties: broker.id=3 listeners=PLAINTEXT://:9094 log.dirs=/tmp/kafka-logs-3 zookeeper.connect=localhost:2181 然后分别启动每个Kafka实例: cd kafka1 bin/kafka-server-start.sh config/server.properties & 同样对于kafka2kafka3。 但需要注意,如果ZooKeeper是单实例,那么所有Kafka实例都连接到同一个ZooKeeper,这在单机情况下是可行的,但需要注意ZooKeeper的性能和可靠性。 另外,用户可能想用Kafka自带的ZooKeeper,这时候每个Kafka实例可能需要配置不同的ZooKeeper端口,或者共享同一个。不过,如果使用Kafka自带的ZooKeeper,可能需要修改Kafka的启动脚本,但可能更复杂,所以建议独立安装ZooKeeper。 总结步骤: 1. 安装和配置ZooKeeper单实例。 2. 下载并解压Kafka,复制多份。 3. 修改每个Kafka实例的配置文件,设置不同的broker.id、端口、日志目录。 4. 启动每个Kafka实例。 5. 验证集群状态。 在验证时,可以使用Kafka的命令行工具创建topic,并查看是否在所有broker上正确分布。 可能还需要提醒用户注意内存和资源限制,因为单机上运行多个实例可能导致资源竞争。 另外,如果用户希望ZooKeeper也是多实例,那么需要指导如何配置ZooKeeper集群,但这会增加步骤的复杂性。如果用户接受单点ZooKeeper,可以简化流程。 现在,将这些步骤整理成用户容易理解的指南,包括具体的命令和配置示例。</think>以下是在单台CentOS7服务器部署Kafka 2.4.1多实例集群的详细步骤: --- ### 一、部署ZooKeeper单实例 1. **下载安装包** ```bash wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz tar -zxvf zookeeper-3.4.14.tar.gz mv zookeeper-3.4.14 /opt/zookeeper ``` 2. **配置ZooKeeper** ```bash cd /opt/zookeeper/conf cp zoo_sample.cfg zoo.cfg vi zoo.cfg # 修改以下配置: ``` ```properties dataDir=/opt/zookeeper/data clientPort=2181 ``` 3. **启动ZooKeeper** ```bash /opt/zookeeper/bin/zkServer.sh start ``` --- ### 二、部署Kafka多实例 1. **下载Kafka** ```bash wget https://archive.apache.org/dist/kafka/2.4.1/kafka_2.12-2.4.1.tgz tar -zxvf kafka_2.12-2.4.1.tgz mv kafka_2.12-2.4.1 /opt/kafka ``` 2. **创建3个实例目录** ```bash cp -r /opt/kafka /opt/kafka1 cp -r /opt/kafka /opt/kafka2 cp -r /opt/kafka /opt/kafka3 ``` 3. **配置每个实例** - **实例1配置** (`/opt/kafka1/config/server.properties`): ```properties broker.id=1 listeners=PLAINTEXT://:9092 log.dirs=/tmp/kafka-logs-1 zookeeper.connect=localhost:2181 ``` - **实例2配置** (`/opt/kafka2/config/server.properties`): ```properties broker.id=2 listeners=PLAINTEXT://:9093 log.dirs=/tmp/kafka-logs-2 zookeeper.connect=localhost:2181 ``` - **实例3配置** (`/opt/kafka3/config/server.properties`): ```properties broker.id=3 listeners=PLAINTEXT://:9094 log.dirs=/tmp/kafka-logs-3 zookeeper.connect=localhost:2181 ``` 4. **启动所有实例** ```bash # 启动实例1 /opt/kafka1/bin/kafka-server-start.sh -daemon /opt/kafka1/config/server.properties # 启动实例2 /opt/kafka2/bin/kafka-server-start.sh -daemon /opt/kafka2/config/server.properties # 启动实例3 /opt/kafka3/bin/kafka-server-start.sh -daemon /opt/kafka3/config/server.properties ``` --- ### 三、验证集群状态 1. **查看ZooKeeper注册信息** ```bash /opt/zookeeper/bin/zkCli.sh ls /brokers/ids # 应输出 [1,2,3] ``` 2. **创建Topic测试** ```bash /opt/kafka1/bin/kafka-topics.sh --create --bootstrap-server localhost:9092 \ --replication-factor 3 --partitions 1 --topic test-topic ``` 3. **查看Topic分布** ```bash /opt/kafka1/bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test-topic ``` --- ### 注意事项 1. **资源限制** 单机部署多实例需注意内存分配,建议修改`kafka-server-start.sh`中的`KAFKA_HEAP_OPTS`参数(如`-Xmx512M -Xms512M`)。 2. **端口冲突** 确保每个实例的`listeners`端口(9092/9093/9094)未被占用。 3. **持久化存储** 生产环境建议将`log.dirs`设置为独立磁盘路径(如`/data/kafka-logs-1`)。 --- 通过以上步骤,您已成功在单台服务器部署Kafka多实例集群。后续可通过客户端工具连接任意实例端口(9092/9093/9094)操作集群。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王义凯_Rick

遇见即是缘,路过就给个评论吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值