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 進程了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章