欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 14.kafka开机自启动配置

14.kafka开机自启动配置

2025/2/8 0:55:00 来源:https://blog.csdn.net/z19861216/article/details/144192313  浏览:    关键词:14.kafka开机自启动配置

要在Linux(RHEL7.7)系统中设置kafka开机自启动,可以创建一个系统服务单元文件。以下是为详细配置部署,假设你已经安装了kafka并且可以通过kafka-server-start.sh命令启动它。

1.进入/lib/systemd/system目录

命令:

cd /lib/systemd/system

[root@rhel77 system]# cd /lib/systemd/system
[root@rhel77 system]# pwd
/lib/systemd/system
[root@rhel77 system]# 

2.创建kafka.service

命令:

touch kafka.service

[root@rhel77 system]# touch kafka.service 
[root@rhel77 system]# ls kafka.service 
kafka.service
[root@rhel77 system]# 

3.编辑kafka.service文件,添加如下内容并保存

[Unit]
Description=Kafka
After=network.target
After=zookeeper.service[Service]
Type=forking
User=root
Group=rootEnvironment=KAFKA_HOME=/opt/kafka_2.13-3.3.2ExecStart=/opt/kafka_2.13-3.3.2/bin/kafka-server-start.sh -daemon /opt/kafka_2.13-3.3.2/config/server.properties
ExecStop=/opt/kafka_2.13-3.3.2/bin/kafka-server-stop.sh
Restart=on-failure
TimeoutSec=600
LimitNOFILE=4096[Install]
WantedBy=multi-user.target

请确保将User和Group替换为运行kafka进程的用户和组。同样,将Environment、ExecStart、ExecStop设置合适的环境路径。

4.重新加载systemd管理器配置,使新的服务单元生效

命令:

systemctl daemon-reload

[root@rhel77 system]# systemctl daemon-reload
[root@rhel77 system]# 

5.启用kafka服务以在启动时运行

命令:

systemctl enable kafka

[root@rhel77 system]# systemctl enable kafka
[root@rhel77 system]# cd /etc/systemd/system/multi-user.target.wants/
[root@rhel77 multi-user.target.wants]# ls -l kafka.service 
lrwxrwxrwx 1 root root 37 Dec  2 16:11 kafka.service -> /usr/lib/systemd/system/kafka.service
[root@rhel77 multi-user.target.wants]# 

6.启动kafka服务(须先启动zookeeper)

命令:

systemctl start kafka

[root@rhel77 multi-user.target.wants]# systemctl start kafka
[root@rhel77 multi-user.target.wants]# 
[root@rhel77 multi-user.target.wants]# systemctl status kafka
● kafka.service - KafkaLoaded: loaded (/usr/lib/systemd/system/kafka.service; enabled; vendor preset: disabled)Active: active (running) since Mon 2024-12-02 16:53:47 CST; 3s agoProcess: 21633 ExecStop=/opt/kafka_2.13-3.3.2/bin/kafka-server-stop.sh (code=exited, status=0/SUCCESS)Process: 21664 ExecStart=/opt/kafka_2.13-3.3.2/bin/kafka-server-start.sh -daemon /opt/kafka_2.13-3.3.2/config/server.properties (code=exited, status=0/SUCCESS)Main PID: 21997 (java)Tasks: 75CGroup: /system.slice/kafka.service└─21997 java -Xmx1G -Xms1G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+E...Dec 02 16:53:47 rhel77 systemd[1]: Starting Kafka...
Dec 02 16:53:47 rhel77 systemd[1]: Started Kafka.
[root@rhel77 multi-user.target.wants]# 

7.端口验证

命令:

netstat -antulp |grep 9092

[root@rhel77 multi-user.target.wants]# netstat -antulp |grep 9092
tcp6       0      0 :::9092                 :::*                    LISTEN      21997/java          
tcp6       0      0 192.168.10.249:46694    192.168.10.249:9092     ESTABLISHED 21997/java          
tcp6       0      0 192.168.10.249:9092     192.168.10.249:46694    ESTABLISHED 21997/java          
[root@rhel77 multi-user.target.wants]# 

8.停止kafka

命令:

systemctl stop kafka

[root@rhel77 multi-user.target.wants]# systemctl stop kafka
[root@rhel77 multi-user.target.wants]# systemctl status kafka
● kafka.service - KafkaLoaded: loaded (/usr/lib/systemd/system/kafka.service; enabled; vendor preset: disabled)Active: failed (Result: exit-code) since Mon 2024-12-02 16:55:36 CST; 2s agoProcess: 22099 ExecStop=/opt/kafka_2.13-3.3.2/bin/kafka-server-stop.sh (code=exited, status=0/SUCCESS)Process: 21664 ExecStart=/opt/kafka_2.13-3.3.2/bin/kafka-server-start.sh -daemon /opt/kafka_2.13-3.3.2/config/server.properties (code=exited, status=0/SUCCESS)Main PID: 21997 (code=exited, status=143)Dec 02 16:53:47 rhel77 systemd[1]: Starting Kafka...
Dec 02 16:53:47 rhel77 systemd[1]: Started Kafka.
Dec 02 16:55:36 rhel77 systemd[1]: Stopping Kafka...
Dec 02 16:55:36 rhel77 systemd[1]: kafka.service: main process exited, code=exited, status=143/n/a
Dec 02 16:55:36 rhel77 systemd[1]: Stopped Kafka.
Dec 02 16:55:36 rhel77 systemd[1]: Unit kafka.service entered failed state.
Dec 02 16:55:36 rhel77 systemd[1]: kafka.service failed.
[root@rhel77 multi-user.target.wants]# 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com