Skip to content

Instantly share code, notes, and snippets.

@eriknyk
Last active June 4, 2022 22:43
Show Gist options
  • Save eriknyk/d6b84e90a741591f50cc5f326178daad to your computer and use it in GitHub Desktop.
Save eriknyk/d6b84e90a741591f50cc5f326178daad to your computer and use it in GitHub Desktop.
install-kafka
#!/bin/bash
VERSION=2.8.0
WORKING_DIR=/opt/kafka
echo "Verifying installation..."
if /opt/kafka/bin/kafka-topics.sh --version 2>/dev/null | grep "Commit" ; then
echo "apache kafka is ready!"
sudo systemctl status zookeeper --no-pager
sudo systemctl status kafka --no-pager
else
if [ -d "$WORKING_DIR" ]; then
echo "Removing prev (incomplete) Installation..."
sudo rm -rf $WORKING_DIR;
fi
sudo mkdir -p $WORKING_DIR
sudo mkdir -p $WORKING_DIR/logs
cd /opt/kafka
echo ""
echo "Downloading apache kafka ($VERSION)..."
sudo wget https://downloads.apache.org/kafka/$VERSION/kafka_2.13-$VERSION.tgz
echo "Installing kafka..."
sudo tar -xvzf kafka_2.13-$VERSION.tgz --strip 1
sudo rm kafka_2.13-$VERSION.tgz
sudo rm -f /etc/systemd/system/zookeeper.service
sudo rm -f /etc/systemd/system/kafka.service
{
echo "[Unit]"
echo "Requires=network.target remote-fs.target"
echo "After=network.target remote-fs.target"
echo ""
echo "[Service]"
echo "Type=simple"
#echo "User=root"
echo "ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties"
echo "ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh"
echo "Restart=on-abnormal"
echo ""
echo "[Install]"
echo "WantedBy=multi-user.target"
echo ""
} >> /etc/systemd/system/zookeeper.service
{
echo "[Unit]"
echo "Requires=zookeeper.service"
echo "After=zookeeper.service"
echo ""
echo "[Service]"
echo "Type=simple"
#echo "User=root"
echo "ExecStart=/bin/sh -c '/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties > /opt/kafka/logs/kafka.log 2>&1'"
echo "ExecStop=/opt/kafka/bin/kafka-server-stop.sh"
echo "Restart=on-abnormal"
echo ""
echo "[Install]"
echo "WantedBy=multi-user.target"
echo ""
} >> /etc/systemd/system/kafka.service
sudo systemctl daemon-reload
sudo systemctl start kafka
sudo systemctl enable kafka
sudo systemctl daemon-reload
sudo systemctl status zookeeper --no-pager
sudo systemctl status kafka --no-pager
if sudo cat /etc/environment 2>&1 >/dev/null | grep "kafka" ; then
echo "Kafka PATH is already added"
else
sudo echo "PATH=\"\$PATH:/opt/kafka/bin\"" >> /etc/environment
echo "Kafka bin path added to /etc/environment"
source /etc/environment
fi
echo "kafka (v$VERSION) installed sucessfully!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment