kafka部署配置與啟停

明天OoO你好發表於2024-03-31

一、部署kafka
deploy-kafka.sh

#!/bin/bash

// 需要首先修改正確的叢集機器ip
server1="host1"
server2="host2"
server3="host3"
// 每臺機器設定一個獨立的index id
SERVER_INDEX="1"

if [ -f "kafka_2.13-3.7.0.tgz" ]; then
    echo "檔案存在: kafka_2.13-3.7.0.tgz"
else
    echo "開始下載kafka_2.13-3.7.0.tgz..." 
    wget https://downloads.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz
fi
mkdir -p /data/kafka/
tar -xzvf kafka_2.13-3.7.0.tgz -C /data/kafka/
mkdir -p /data/kafka/kafka_2.13-3.7.0/data
useradd -m -s /bin/bash kafka
chown kafka -R /data/kafka/kafka_2.13-3.7.0 /data/kafka/kafka_2.13-3.7.0/data

su - kafka <<EOF
uuid=$(/data/kafka/kafka_2.13-3.7.0/bin/kafka-storage.sh random-uuid)
echo "CLUSTER_UUID: $uuid"
echo "$uuid" > /data/kafka/kafka_2.13-3.7.0/CLUSTER_UUID
export CURRENT_SERVER_INDEX=$SERVER_INDEX
export CLUSTER_UUID=$uuid

// 修改controller配置
sed -i "s#node.id=.*#node.id=${CURRENT_SERVER_INDEX}#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/controller.properties
sed -i "s#controller.quorum.voters=.*#controller.quorum.voters=1@$server1:9093,2@$server2:9093,3@$server3:9093#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/controller.properties
sed -i "s#log.dirs=.*#log.dirs=/data/kafka/kafka_2.13-3.7.0/data#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/controller.properties
// 格式化controller
/data/kafka/kafka_2.13-3.7.0/bin/kafka-storage.sh format -t ${CLUSTER_UUID} -c /data/kafka/kafka_2.13-3.7.0/config/kraft/controller.properties

// 修改broker配置
sed -i "s#node.id=.*#node.id=${CURRENT_SERVER_INDEX}#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/broker.properties
sed -i "s#controller.quorum.voters=.*#controller.quorum.voters=1@$server1:9093,2@$server2:9093,3@$server3:9093#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/broker.properties
sed -i "s#listeners=.*#listeners=PLAINTEXT://server${CURRENT_SERVER_INDEX}:9092#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/broker.properties
sed -i "s#log.dirs=.*#log.dirs=/data/kafka/kafka_2.13-3.7.0/data#g" /data/kafka/kafka_2.13-3.7.0/config/kraft/broker.properties
// 格式化broker
/data/kafka/kafka_2.13-3.7.0/bin/kafka-storage.sh format -t $uuid -c /data/kafka/kafka_2.13-3.7.0/config/kraft/broker.properties

EOF

二、使用supervisord管理kafka
kafkacontrl.conf

[program:kafka]
command=/data/kafka/kafka_2.13-3.7.0/bin/kafka-server-start.sh /data/kafka/kafka_2.13-3.7.0/config/kraft/controller.properties
autostart=true
autorestart=true
user=kafka
#stdout_logfile=/data/kafka/kafka_2.13-3.7.0/logs/kafka.log
#stderr_logfile=/data/kafka/kafka_2.13-3.7.0/logs/kafka.err

kafkabroker.conf

[program:kafka]
command=/data/kafka/kafka_2.13-3.7.0/bin/kafka-server-start.sh /data/kafka/kafka_2.13-3.7.0/config/kraft/broker.properties
autostart=true
autorestart=true
user=kafka

相關文章