Kafka 3.6.1 集群安装与部署

1.集群规划

hadoop02(192.168.58.130) hadoop03(192.168.58.131) hadoop04(192.168.58.132)
zookeeper zookeeper zookeeper
kafka kafka kafka

2.集群部署

1.下载kafka二进制包

https://kafka.apache.org/downloads

2.解压

mkdir /usr/kafka
tar -zxvf /home/kafka_2.13-3.6.1.tgz -C /usr/kafka/

3.修改配置文件(以192.168.58.130上节点的配置为例)

cd /usr/kafka/kafka_2.13-3.6.1/config
vi 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.

#
# This configuration file is intended for use in ZK-based mode, where Apache ZooKeeper is required.
# See kafka.server.KafkaConfig for additional details and defaults
#

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

# broker 的全局唯一编号,不能重复,只能是数字。
broker.id=0

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

# 套接字服务器侦听的地址。如果未配置,主机名将等于的值
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# 侦听器名称、主机名和代理将向客户端公布的端口。
# 如果未设置,则使用“listeners”的值。
#advertised.listeners=PLAINTEXT://your.host.name:9092

# 将侦听器名称映射到安全协议,默认情况下它们是相同的。有关更多详细信息,请参阅配置文档
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# 处理网络请求的线程数量(服务器用于从网络接收请求并向网络发送响应的线程数)
num.network.threads=3

# 用来处理磁盘 IO 的线程数量
num.io.threads=8

# 发送套接字的缓冲区大小
socket.send.buffer.bytes=102400

# 接收套接字的缓冲区大小
socket.receive.buffer.bytes=102400

# 请求套接字的缓冲区最大大小
socket.request.max.bytes=104857600


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

# kafka 运行日志(数据)存放的路径,路径不需要提前创建,kafka 自动帮你创建,可以配置多个磁盘路径,路径与路径之间可以用","分隔
log.dirs=/usr/kafka/kafka_2.13-3.6.1/datas

# 每个topic在当前 broker上的默认分区数。更多的分区允许更大的并行性以供使用,但这也会导致代理之间有更多的文件。
num.partitions=1

# 启动时用于日志恢复和关闭时用于刷新的每个数据目录的线程数。(用来恢复和清理 data 下数据的线程数量)对于数据目录位于RAID阵列中的安装,建议增加此值。
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings  #############################
# 每个 topic 创建时的副本数,默认时 1 个副本,对于开发测试以外的环境,建议使用大于1的值以确保可用性,如3
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# 强制将数据刷新到磁盘之前要接受的消息数
#log.flush.interval.messages=10000

# 在强制刷新之前,消息可以在日志中停留的最长时间
#log.flush.interval.ms=1000

############################# 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.

# segment 文件保留的最长时间,超时将被删除
log.retention.hours=168

# 基于大小的日志保留策略。除非剩余的
# segments下降到 log.retention.bytes 以下。独立于log.retention.hours的函数.
#log.retention.bytes=1073741824

# 每个 segment 文件的最大大小,默认最大 1G ,当达到此大小时,将创建一个新的segment。
log.segment.bytes=1073741824

# 检查日志段以查看是否可以根据保留策略删除它们的间隔(检查过期数据的时间,默认 5 分钟检查一次是否数据过期)
log.retention.check.interval.ms=300000

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

# Zookeeper集群连接字符串,一个以逗号分隔的'主机:端口'对,每个对对应一个zk服务器。可以在url中附加一个可选的chroot字符串,以指定所有kafka-znode的根目录。
zookeeper.connect=192.168.58.130:2181,192.168.58.131:2181,192.168.58.132:2181/kafka

# 连接到zookeeper 的超时时间(毫秒)
zookeeper.connection.timeout.ms=18000


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

# 以下配置指定GroupCoordinator将延迟初始使用者重新平衡的时间(以毫秒为单位)。
# 随着新成员加入组,再平衡将进一步延迟group.initial.rebalance.delay.ms的值,最大值为max.poll.interval.ms。
# 默认值为3秒。
# 我们在这里将其覆盖为0,因为它为开发和测试提供了更好的开箱即用体验。
# 但是,在生产环境中,默认值3秒更合适,因为这将有助于避免在应用程序启动期间进行不必要的、可能代价高昂的重新平衡。
group.initial.rebalance.delay.ms=3

4.在其他节点上修改配置文件

在 192.168.58.131 和 192.168.58.132 上修改配置文件server.properties中的 broker.id
注:broker.id 不得重复,整个集群中唯一。

# broker 的全局唯一编号,不能重复,只能是数字。
broker.id=1
# broker 的全局唯一编号,不能重复,只能是数字。
broker.id=2

5.配置环境变量

在/etc/profile.d中配置

1.新建kafka.sh

vi /etc/profile.d/kafka.sh
# KAFKA_HOME
export KAFKA_HOME=/usr/kafka/kafka_2.13-3.6.1
export PATH=$PATH:$KAFKA_HOME/bin

2.授予文件执行权限

chmod u+x /etc/profile.d/kafka.sh

3.刷新环境变量

source /etc/profile

6.配置zookeeper

参考
ZooKeeper 3.9.1 集群模式安装
Zookeeper集群一键启停脚本
来配置外置Zookeeper或者内置的Zookeeper。

7.启动集群

1.启动 Zookeeper 集群

此处使用上方的 Zookeeper集群一键启停脚本

zk start

2.在节点上依次启动 Kafka

/usr/kafka/kafka_2.13-3.6.1/bin/kafka-server-start.sh -daemon /usr/kafka/kafka_2.13-3.6.1/config/server.properties

2*.kafka一键启停脚本

1.创建脚本
vi /usr/bin/kafka
#! /bin/bash

if [ $# -lt 1 ]; then
	echo "No Args Input..."
	exit
fi

case $1 in
"start") {
	for i in 192.168.58.130 192.168.58.131 192.168.58.132; do
		echo " --------启动 $i Kafka-------"
		ssh $i "source /etc/profile;/usr/kafka/kafka_2.13-3.6.1/bin/kafka-server-start.sh -daemon /usr/kafka/kafka_2.13-3.6.1/config/server.properties"
	done
} ;;
"stop") {
	for i in 192.168.58.130 192.168.58.131 192.168.58.132; do
		echo " --------停止 $i Kafka-------"
		ssh $i "source /etc/profile;/usr/kafka/kafka_2.13-3.6.1/bin/kafka-server-stop.sh"
	done
} ;;
*)
	echo "Input Args Error..."
	;;
esac
2.添加执行权限
chmod +x /usr/bin/kafka
3.使用
kafka start/stop

8.关闭集群

/usr/kafka/kafka_2.13-3.6.1/bin/kafka-server-stop.sh

注意:停止 Kafka 集群时,一定要等 Kafka 所有节点进程全部停止后再停止 Zookeeper集群。因为 Zookeeper 集群当中记录着 Kafka 集群相关信息,Zookeeper 集群一旦先停止,Kafka 集群就没有办法再获取停止进程的信息,只能手动杀死 Kafka 进程了。

posted @ 2024-02-10 09:06  SpringCore  阅读(328)  评论(0编辑  收藏  举报