如何在Ubuntu系统启动时自动启动Kafka?

时间:2015-12-29 13:40:12

标签: ubuntu apache-kafka init.d

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脚本(例如onetwothree)但是它们都不同,我对init.d不够熟悉一,如果有任何实施。

如何在系统启动时启动Kafka?

3 个答案:

答案 0 :(得分:17)

以下是我如何配置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

答案 1 :(得分:4)

一种简单的方法是使用systemd。您应注意,启动时尚未加载诸如 JAVA_HOME 之类的环境变量,因此我们应将其引入系统。一个好的解决方案是创建一个名为profile的文件,并向其中添加所有必需的变量:

# /home/kafka/profile
JAVA_HOME=/opt/jdk8
KAFKA_HOME=/opt/kafka

假设您已经在路径/opt/kafka上安装了Kafka,以便在Ubuntu启动后自动运行Kafka(在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

答案 2 :(得分:1)

下载Kafka

cd /opt
wget http://mirror.dkm.cz/apache/kafka/2.2.1/kafka_2.11-2.2.1.tgz
sudo tar -zxvf kafka_2.11-2.2.1.tgz
sudo rm kafka_2.11-2.2.1.tgz
sudo mv kafka_2.11-2.2.1 kafka
cd kafka

安装Zookeeper

sudo vi /etc/systemd/system/zookeeper.service

编辑zookeeper.service

[Unit]
Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
Type=simple
User=root
ExecStart=/opt/kafka/bin/zookeeper-server-start.sh opt/kafka/config/zookeeper.properties
ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh
Restart=on-abnormal

[Install]
WantedBy=multi-user.target

启动Zookeeper

sudo systemctl enable zookeeper.service

sudo systemctl start zookeeper.service

sudo systemctl status zookeeper.service

活动(运行)

安装Kafka

sudo vi /etc/systemd/system/kafka.service

编辑kafka.service

[Unit]
Requires=zookeeper.service
After=zookeeper.service

[Service]
Type=simple
User=root
ExecStart=/bin/sh -c '/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties > /opt/kafka/kafka.log 2>&1'
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=on-abnormal

[Install]
WantedBy=multi-user.target

启动Kafka

sudo systemctl enable kafka.service

sudo systemctl start kafka.service

sudo systemctl status kafka.service

活动(运行)

测试Kafka是否有效

创建主题

sudo bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test-topic

将邮件放入主题

sudo bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic  
> test message1
> test messate2
^C

从主题中阅读消息

sudo bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test-topic
test message1
test messate2
^C