如何在ubuntu系统启动时自动启动kafka?

0wi1tuuw  于 2021-06-07  发布在  Kafka
关注(0)|答案(3)|浏览(1023)

Kafka是否有正式的方式(例如init.d脚本)在系统启动时启动Kafka?
我见过的唯一一种正式的开始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脚本可用(例如1、2、3),但是它们都是不同的,我对init.d不够熟悉,不知道要实现哪一个。
如何在系统启动时启动kafka?

p4tfgftt

p4tfgftt1#

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

p5cysglq

p5cysglq2#

一个简单的方法是使用systemd。您应该注意,在启动时,像javahome这样的环境变量还没有加载,所以我们应该将它们引入系统。一个好的解决方案是创建一个名为 profile 再加上所有必要的变量:


# /home/kafka/profile

JAVA_HOME=/opt/jdk8
KAFKA_HOME=/opt/kafka

假设你在路上安装了Kafka /opt/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
ttisahbt

ttisahbt3#

下载Kafka

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

活动(运行)
安装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

相关问题