#!/bin/bash
ZK_URL=”https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz”
KAFKA_URL=”https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.8.1/kafka_2.13-2.8.1.tgz”
NODE1=10.0.0.17
NODE2=10.0.0.27
NODE3=10.0.0.47
HOST=$(hostname -I | awk ‘{print $1}’)
# 加载/etc/os-release,得到变量ID=centos或ID=ubuntu
. /etc/os-release
install_jdk(){
if [ $ID = “centos” ]; then
yum -y install java-1.8.0-openjdk-devel || { echo “install JDK failed”; exit 1; }
elif [ $ID = “ubuntu” ]; then
apt update
apt -y install openjdk-8-jdk || { echo “install JDK failed”; exit 1; }
fi
java -version
}
install_zookeeper() {
wget -P /usr/local/src/ –no-check-certificate $ZK_URL || { echo “download zookeeper failed”; exit 1; }
tar xf /usr/local/src/${ZK_URL##*/} -C /usr/local
ln -s /usr/local/apache-zookeeper-*-bin/ /usr/local/zookeeper
echo ‘PATH=/usr/local/zookeeper/bin:$PATH’ > /etc/profile.d/zookeeper.sh
. /etc/profile.d/zookeeper.sh
mkdir -p /usr/local/zookeeper/data
echo $ZKID > /usr/local/zookeeper/data/myid
cat > /usr/local/zookeeper/conf/zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/data
clientPort=2181
maxCientCnxns=128
autopurge.snapRetainCount=3
autopurge.purgeInterval=24
4lw.commands.whitelist=*
#zookeeper集群
server.1=${NODE1}:2888:3888
server.2=${NODE2}:2888:3888
server.3=${NODE3}:2888:3888
EOF
cat > /lib/systemd/system/zookeeper.service <<EOF
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
#Environment=/usr/local/zookeeper
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable –now zookeeper.service
systemctl is-active zookeeper.service
if [ $? -eq 0 ]; then
echo “zookeeper 安装成功”
else
echo “zookeeper 安装失败”
exit 1
fi
}
install_kafka() {
wget -P /usr/local/src –no-check-certificate $KAFKA_URL
tar xf /usr/local/src/${KAFKA_URL##*/} -C /usr/local/
ln -s /usr/local/kafka_*/ /usr/local/kafka
echo ‘PATH=/usr/local/kafka/bin:$PATH’ > /etc/profile.d/kafka.sh
. /etc/profile.d/kafka.sh
cat > /usr/local/kafka/config/server.properties <<EOF
broker.id=$ZKID
listeners=PLAINTEXT://${HOST}:9092
log.dirs=/usr/local/kafka/data
num.partitions=1
log.retention.hours=168
zookeeper.connect=${NODE1}:2181,${NODE2}:2181,${NODE3}:2181
zookeeper.connection.timeout.ms=6000
EOF
mkdir /usr/local/kafka/data
cat > /lib/systemd/system/kafka.service <<EOF
[Unit]
Description=Apache Kafka
After=network.target
[Service]
Type=simple
#Environment=JAVA_HOME=/data/server/java
#PIDFILE=/usr/local/kafka/kafka.pid
ExecStart=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties
ExecStop=/bin/kill -TERM /${MAINPID}
Restart=always
RestartSec=20
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable –now kafka.service
systemctl is-active kafka.service
if [ $? -eq 0 ]; then
echo “kafka 安装成功”
else
echo “kafka 安装失败”
exit 1
fi
}
read -p “请输入zookeeper node编号(默认为1): ” ZKID
if [ -z “$ZKID” ]; then
ZKID=1
elif [[ ! “$ZKID” =~ ^[0-9]+$ ]]; then
echo “请输入正确的zookeeper node编号” 1
exit
else
true
fi
install_jdk
install_zookeeper
install_kafka
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/273628.html