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

How to automatically start Kafka upon system startup in Ubuntu?

Kafka 是否有官方方式(例如 init.d 脚本)在系统启动时启动 Kafka?

我见过的唯一官方启动Kafka的方法是:

nohup ~/kafka/bin/kafka-server-start.sh ~/kafka/config/server.properties > ~/kafka/kafka.log 2>&1 &

我曾尝试在 crontab -e 中使用 @reboot 任务,但它没有启动 Kafka。也有人写了custom init.d

还有可用的自定义 init.d 脚本(例如 one, two, three),但它们各不相同,我对 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

将以下初始化脚本复制到/etc/init。d/kafka:

DAEMON_PATH=/opt/kafka/
PATH=$PATH:$DAEMON_PATH/bin

# See how we were called.
case "" 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 }'`
        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 }'`
        if [ -n "$pid" ]
          then
          kill -9 $pid
        else
          echo "Kafka was not Running"
        fi
        ;;
  restart)
        [=11=] stop
        sleep 2
        [=11=] start
        ;;
  status)
        pid=`ps ax | grep -i 'org.apache.zookeeper.server' | grep -v grep | awk '{print }'`
        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 }'`
        if [ -n "$pid" ]
          then
          echo "Kafka is Running as PID: $pid"
        else
          echo "Kafka is not Running"
        fi
        ;;
  *)
        echo "Usage: [=11=] {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

假设你已经在路径 /opt/kafka 上安装了 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服务设置为运行动物园管理员服务启动后。

保存kafka.service文件后,只需运行下面的命令就可以创建一个link的Kafka服务,每次重启OS:

sudo systemctl enable kafka

现在可以使用命令启动Kafka服务了:

sudo systemctl start kafka.service

并检查服务状态:

sudo systemctl status kafka.service

下载卡夫卡

cd /opt
sudo wget http://mirror.hosting90.cz/apache/kafka/2.5.0/kafka-2.5.0-src.tgz
sudo tar -zxvf kafka-2.5.0-src.tgz
sudo mv kafka-2.5.0-src kafka
sudo rm kafka-2.5.0-src.tgz
cd kafka
sudo ./gradlew jar -PscalaVersion=2.11.12

安装 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

活跃 (运行)

安装卡夫卡

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