Kafka是否有正式的方式(例如init.d脚本)在系统启动时启动Kafka?
启动卡夫卡的唯一官方方法我见过:
nohup ~/kafka/bin/kafka-server-start.sh ~/kafka/config/server.properties > ~/kafka/kafka.log 2>&1 &
我试图使用@reboot
任务,crontab -e
但它没有启动Kafka.有些人还编写了自定义init.d
还有一些自定义的init.d脚本可用(例如一个,两个,三个),但它们都是不同的,我对init.d不够熟悉,无法理解哪一个,如果有的话.
如何在系统启动时启动Kafka?
以下是我如何配置Kafka在Ubuntu 14.04上自动启动:
sudo su cp -R ~/kafka_2.11-0.10.0.1 /opt ln -s /opt/kafka_2.11-0.10.0.1 /opt/kafka
将以下init脚本复制到/etc/init.d/kafka:
DAEMON_PATH=/opt/kafka/ PATH=$PATH:$DAEMON_PATH/bin # See how we were called. case "$1" in start) # Start daemon. echo "Starting Zookeeper"; nohup $DAEMON_PATH/bin/zookeeper-server-start.sh -daemon /$DAEMON_PATH/config/zookeeper.properties 2> /dev/null && \ echo "Starting Kafka"; nohup $DAEMON_PATH/bin/kafka-server-start.sh -daemon /$DAEMON_PATH/config/server.properties 2> /dev/null ;; stop) # Stop daemons. echo "Shutting down Zookeeper"; pid=`ps ax | grep -i 'org.apache.zookeeper.server' | grep -v grep | awk '{print $1}'` if [ -n "$pid" ] then kill -9 $pid else echo "Zookeeper was not Running" fi echo "Shutting down Kafka"; pid=`ps ax | grep -i 'kafka.Kafka' | grep -v grep | awk '{print $1}'` if [ -n "$pid" ] then kill -9 $pid else echo "Kafka was not Running" fi ;; restart) $0 stop sleep 2 $0 start ;; status) pid=`ps ax | grep -i 'org.apache.zookeeper.server' | grep -v grep | awk '{print $1}'` if [ -n "$pid" ] then echo "Zookeeper is Running as PID: $pid" else echo "Zookeeper is not Running" fi pid=`ps ax | grep -i 'kafka.Kafka' | grep -v grep | awk '{print $1}'` if [ -n "$pid" ] then echo "Kafka is Running as PID: $pid" else echo "Kafka is not Running" fi ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 esac exit 0
使用以下命令创建kafka服务:
chmod 755 /etc/init.d/kafka update-rc.d kafka defaults
现在您应该可以像这样启动和停止kafka服务:
sudo service kafka start sudo service kafka status sudo service kafka stop
如果您想稍后删除Kafka服务,请运行update-rc.d -f kafka remove
.
一种简单的方法是使用systemd。您应注意,启动时尚未加载JAVA_HOME之类的环境变量,因此我们应将其引入系统。一个好的解决方案是创建一个名为的文件,profile
并向其中添加所有必需的变量:
# /home/kafka/profile JAVA_HOME=/opt/jdk8 KAFKA_HOME=/opt/kafka
假设您已经在path上安装了Kafka /opt/kafka
,为了让Kafka在Ubuntu启动后自动运行(在Ubuntu 16.04和centOS7上进行了测试,我猜它在systemd的支持下可以在任何发行版上运行)执行以下命令:
sudo nano /etc/systemd/system/kafka.service # open file to add service informations
现在将以下内容添加到文件中
[Unit] Description=Kafka Daemon Wants=syslog.target # suppose you have a service named zookeeper that it start zookeeper and we want Kafka service run after the zookeeper service After=zookeeper.service [Service] Type=forking # the user whom you want run the Kafka start and stop command under User=kafka # the file path that contains envirnment variables EnvironmentFile=/home/kafka/profile # the directory that the commands will run there WorkingDirectory=/home/kafka/ # Kafka server start command ExecStart=/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties # Kafka server stop command ExecStop=/opt/kafka/bin/kafka-server-stop.sh -daemon TimeoutSec=30 Restart=on-failure [Install] WantedBy=multi-user.target
注意:由于Kafka在启动时需要Zookeeper进行连接,因此我以为我们也有Zookeeper服务,因此我将Kafka服务设置为在Zookeeper服务启动后运行。
现在,保存kafka.service
文件后,只需运行以下命令即可创建Kafka服务的链接,该链接将在每次重启操作系统时启动:
sudo systemctl enable kafka
现在,您可以使用以下命令启动Kafka服务:
sudo systemctl start kafka.service
并检查服务状态:
sudo systemctl status kafka.service