要在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]#