KAFKA install cluster

#!/bin/bash
#input textbox
echo_yellow() {
    echo -e "\e[93m$1\e[0m"
}

# parameter seting
KAFKA_VERSION="3.6.1"
SCALA_VERSION="2.13"
KAFKA_BINARY_URL="https://archive.apache.org/dist/kafka/${KAFKA_VERSION}/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz"

KAFKA_INSTALL_DIR="/usr/local"
KAFKA_DIR="${KAFKA_INSTALL_DIR}/kafka_${SCALA_VERSION}-${KAFKA_VERSION}"
KAFKA_SERVICE_FILE="/usr/lib/systemd/system/kafka.service"
KAFKA_CONFIG_FILE="/usr/local/kafka/config/server.properties"

# used read command get Zookeeper node
echo_yellow "input Zookeeper node please , used comma (for example:0.0.0.0,0.0.0.1,0.0.0.2) :"
read -r ZOOKEEPER_NODES_INPUT

# get words from user input 
echo_yellow " please input Node ID : "
read -r node_id

#Setting kafka log expired hours 
echo_yellow "please configure Kafka logs will be expired in some hours : "
read  -r KAFKA_LOG_EXPIREHOURS

#1. Install JDK
if command -v java &>/dev/null; then
   echo_yellow "Java duplicate"
else  
  echo_yellow "install Java…"
  wget -q http://file.abcmoreonline.com:9899/download/java/jdk-8u211-linux-x64.rpm  -P /data
  yum install -y /data//jdk-8u211-linux-x64.rpm 
  echo_yellow "Java install complete successful"
fi

# 2. download  and configure Zookeeper

if   [ ! -d "${KAFKA_DIR}" ]; then
   echo_yellow  "download and unzip Kafka…"
   if [ ! -f "/opt/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz" ]; then
      wget -q "${KAFKA_BINARY_URL}" -P /opt/
  fi
  tar -zxf "/opt/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz" -C "${KAFKA_INSTALL_DIR}"
  ln -s "${KAFKA_DIR}" "${KAFKA_INSTALL_DIR}/kafka"
  echo_yellow  "kafka install successful"
fi

#configure Zookeeper
ZOOKEEPEER_CONFIG_FILE="/usr/local/kafka/config/zookeeper.properties"

cat >${ZOOKEEPER_CONFIG_FILE} <<EOF
tickTime=2000
initLimit=20
syncLimit=5
dataDir=/data/zookeeper
clientPort=2181
autopurge.snapRetainCount=30
autopurge.purgeInterval=72
EOF

IFS=',' read -ra NODES <<<"$ZOOKEEPER_NODES_INPUT"
#FOR EACH ARGS AND CONNECT MAKE A EXPLEAM FORMAT
ZOOKEEPER_INFORMATION=""
for NODE in "${NODES[@]}"; do
      ZOOKEEPER_INFORMATION+="$(echo "$NODE" | tr -d ' '):2181,"
done 

# remove the ending ,
ZOOKEEPER_INFORMATION="${ZOOKEEPER_INFORMATION%,}"
echo  $ZOOKEEPER_INFORMATION
IFS=',' read -ra  ZOOKEEPER_NODES  <<<"${ZOOKEEPER_NODES_INPUT}"

for  i in "${!ZOOKEEPER_NODES[@]}"; do
      echo  "server.$((i + 1))=${ZOOKEEPER_NODES[i]}:2888:3888" >>"${ZOOKEEPER_CONFIG_FILE}"
done

myid_file="/data/zookeeper/myid"

if  [ ! -d "/data/zookeeper/" ]; then
   mkdir /data/zookeeper/
fi

# valid input is words
if  [[ ! $node_id =~ ^[0-9]+$ ]]; then
   echo "Error: please enter a valid numeric ID."
   exit 1
fi

# input words write myid file
echo "$node_id" >"$myid_file"
echo "Node_ID $node_id has been written to $myid_file"
LOCAL_IP=$(hostname -I | awk '{print $1}')
BROKER_ID=$(echo $LOCAL_IP | tr -d '.')

# configure Zookeeper service
echo_yellow  "begin configure Zookeeper service and start service… "
cat >/usr/lib/systemd/system/zookeeper.service <<EOF
[Unit]
Description=Zookeeper Service
After=network.target remote-fs.target
[Service]
Type=forking
ExecStart=/usr/local/kafka/bin/zookeeper-server-start.sh -daemon /usr/local/kafka/config/zookeeper.properties
ExecStop=/usr/local/kafka/bin/zookeeper-server-stop.sh
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable zookeeper && systemctl start zookeeper && systemctl status zookeeper
echo_yellow "configure Zookeeper complete"

#configure Kafka
echo_yellow "begin configure Kafka service and start service…"

useradd kafka

if [ ! -d "/data/kafka/logs" ]; then
  mkdir -p /data/kafka/logs
fi
LOCAL_IP=$(hostname -I | awk '{print $1}')
cat >$KAFKA_CONFIG_FILE <<EOF
port=9092
listeners=PLAINTEXT://${LOCAL_IP}:9092
auto.create.topics.enable=false
unclean.leader.election.enable=false
auto.leader.rebalance.enable=false
num.network.threads=3
num.io.threads=8
message.max.bytes=10000120
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/data/kafka/logs
num.partitions=10
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=1
log.retention.hours=${KAFKA_LOG_EXPIREHOURS}
log.clean.policy=delete
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=${ZOOKEEPER_INFORMATION}
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
min.insync.replicas=1
default.replication.factor=3
num.replica.fetchers=2
controlled.shutdown.enable=true
EOF
# modify JVM MEMORY CONFIGURE
# INPUT MIN MEMORY PLEASE
echo "Enter the memory size for Kafka (e.g., 2G, 512M):"
read memory_size

# please confirm check input min memory size 
if [[ -z "$memory_size" ]]; then
   echo "Memory size input is empty. Please provide a valid size (e.g, 2G, 512M)."
   exit
fi

# used sed common replace kafka-server-start.sh file inner memory size
# sed -i "s/-Xmx[0-9]*[MG]/-Xmx$memory_size/g" /usr/local/kafka/bin/kafka-server-start.sh
# sed -i "s/-Xms[0-9]*[MG]/-Xms$memory_size/g" /usr/local/kafka/bin/kafka-server-start.sh


echo "Kafka heap size has been updated to $memory_size."

# configure Kafka server
cat >$KAFKA_SERVICE_FILE <<EOF
[Unit]
Description=Kafka Service
After=network.target remote-fs.target zookeeper.service
[Service]
Type=forking
User=kafka
Group=kafka
Environment="JMX_PORT=9999"
Environment="KAFKA_HEAP_OPTS=-Xmx${memory_size} -Xms${memory_size}"
ExecStart=/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
ExecStop=/usr/local/kafka/bin/kafka-srever-stop.sh
LimitNOFILE=1024000
LimitMEMLOCK=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
chown kafka. /usr/local/kafka/ -R
chown kafka. /data/kafka/ -R
systemctl enable kafka
systemctl start kafka
systemctl status kafka
echo_yellow "configure kafka complete…"

echo ""
echo ""
echo_yellow "################################################################"
echo_yellow "the install content : ${ZOOKEEPER_NODES_INPUT} "
echo_yellow "the server node content : ${node_id} "
echo_yellow "the configure log expired times (HOURS) : ${KAFKA_LOG_EXPIREHOURS} "
echo_yellow "configure JVM : ${memory_size} "
echo ""

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值