ubuntu16.04
1. /etc/init.d$ vi kafka.sh
#!/bin/bash #export KAFKA_HOME=$PATH export KAFKA_HOME=/opt/Kafka/kafka_2.11-1.1.1 #chkconfig:2345 30 80 #description:kafka #processname:kafka
case $1 in start) # chmod -R 777 $KAFKA_HOME/logs # chmod -R 777 /data/kafka $KAFKA_HOME/bin/zookeeper-server-start.sh $KAFKA_HOME/config/zookeeper.properties /tmp/zookeeper.log 2>&1 &
sleep 5s; $KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties /tmp/kafka.log 2>&1 & ;; stop) $KAFKA_HOME/bin/zookeeper-server-stop.sh $KAFKA_HOME/bin/kafka-server-stop.sh ;; *) echo "require start|stop" ;; esac
2. 重新载入配置文件(ubuntu 18.04)
systemctl reload kafka
3. 设置开机自动启动
systemctl enable kafka
4. 重启,检查是否启动
jps
(ubuntu16.04)
chkconfig --add kafka 添加到服务器中
chkconfig kafka on 设置开机自动启动
官方脚本:
cd /etc/rc.d/init.d
touch kafka
chmod +x kafka
#!/bin/sh # # chkconfig: 345 99 01 # description: Kafka # # File : Kafka # # Description: Starts and stops the Kafka server # source /etc/rc.d/init.d/functions KAFKA_HOME=/opt/kafka KAFKA_USER=kafka export LOG_DIR=/var/log/kafka [ -e /etc/sysconfig/kafka ] && . /etc/sysconfig/kafka # See how we were called. case "$1" in start) echo -n "Starting Kafka:" /sbin/runuser -s /bin/sh $KAFKA_USER -c "nohup $KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties > $LOG_DIR/server.out 2> $LOG_DIR/server.err &" echo " done." exit 0 ;; stop) echo -n "Stopping Kafka: " /sbin/runuser -s /bin/sh $KAFKA_USER -c "ps -ef | grep kafka.Kafka | grep -v grep | awk '{print \$2}' | xargs kill" echo " done." exit 0 ;; hardstop) echo -n "Stopping (hard) Kafka: " /sbin/runuser -s /bin/sh $KAFKA_USER -c "ps -ef | grep kafka.Kafka | grep -v grep | awk '{print \$2}' | xargs kill -9" echo " done." exit 0 ;; status) c_pid=`ps -ef | grep kafka.Kafka | grep -v grep | awk '{print $2}'` if [ "$c_pid" = "" ] ; then echo "Stopped" exit 3 else echo "Running $c_pid" exit 0 fi ;; restart) stop start ;; *) echo "Usage: kafka {start|stop|hardstop|status|restart}" exit 1 ;; esac
tip:
ubuntu18.04: (单机:https://blog.csdn.net/muguku/article/details/107641803)
第4步 - 创建系统单元文件并启动Kafka服务器
在本节中,我们将为Kafka服务创建systemd单元文件。这将帮助我们执行常见的服务操作,例如以与其他Linux服务一致的方式启动,停止和重新启动Kafka。
Zookeeper是Kafka用于管理其集群状态和配置的服务。它通常在许多分布式系统中用作不可或缺的组件。如果您想了解更多信息,请访问官方Zookeeper文档。
为zookeeper
创建单位文件:
sudo nano /etc/systemd/system/zookeeper.service
在文件中输入以下单位定义:
[Unit] Requires=network.target remote-fs.target After=network.target remote-fs.target [Service] Type=simple User=kafka ExecStart=/home/kafka/kafka/bin/zookeeper-server-start.sh /home/kafka/kafka/config/zookeeper.properties ExecStop=/home/kafka/kafka/bin/zookeeper-server-stop.sh Restart=on-abnormal [Install] WantedBy=multi-user.target
该[Unit]
部分指定Zookeeper在启动之前需要网络并且文件系统准备就绪。
该[Service]
部分指定systemd应使用zookeeper-server-start.sh
和zookeeper-server-stop.sh
shell文件来启动和停止服务。它还指定Zookeeper如果异常退出则应自动重启。
接下来,为以下kafka
内容创建systemd服务文件:
sudo nano /etc/systemd/system/kafka.service
[Unit] Requires=zookeeper.service After=zookeeper.service [Service] Type=simple User=kafka ExecStart=/bin/sh -c '/home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server.properties > /home/kafka/kafka/kafka.log 2>&1' ExecStop=/home/kafka/kafka/bin/kafka-server-stop.sh Restart=on-abnormal [Install] WantedBy=multi-user.target
该[Unit]
部分指定此单元文件依赖于zookeeper.service
。这将确保zookeeper
在kafa
服务启动时自动启动。
该[Service]
部分指定systemd应使用kafka-server-start.sh
和kafka-server-stop.sh
shell文件来启动和停止服务。它还指定如果Kafka异常退出则应自动重启。
现在已经定义了单元,使用以下命令启动Kafka:
sudo systemctl start kafka
要确保服务器已成功启动,请检查设备的日志日志kafka
:
journalctl -u kafka
您应该看到类似于以下内容的输出:
Jul 17 18:38:59 kafka-ubuntu systemd[1]: Started kafka.service.
虽然我们已启动该kafka
服务,但如果我们要重新启动服务器,它将不会自动启动。要kafka
在服务器启动时启用,请运行:
sudo systemctl enable kafka