1 配置路径

2 自启动配置

2.1 第一种方式(设置service)

2.1.1 配置zookeeper.service

配置路径:/etc/systemd/system

[Unit]
Description=zookeeper.service
After=network.target
 
[Service]
Type=forking
User=root
Group=root
Environment=ZOO_LOG_DIR=/usr/local/zookeeper-3.5.9/bin
Environment=JAVA_HOME=/usr/local/jdk-1.8.0
ExecStart=/usr/local/zookeeper-3.5.9/bin/zkServer.sh start 
ExecStop=/usr/local/zookeeper-3.5.9/bin/zkServer.sh stop
Restart=always
RestartSec=5
StartLimitInterval=0
 
[Install]
WantedBy=multi-user.target

2.1.2 配置kafka.service

[Unit]
Description=kafka.service
After=network.target  zookeeper.service

[Service]
Type=forking
User=root
Group=root
Environment=JAVA_HOME=/usr/local/jdk-1.8.0
ExecStart=/usr/local/kafka_2.12-2.4.1/bin/kafka-server-start.sh -daemon  /usr/local/kafka_2.12-2.4.1/config/server.properties
ExecStop=/usr/local/kafka_2.12-2.4.1/bin/kafka-server-stop.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

2.1.3 配置cmak.service

2.1.3.1 start.sh
#!/bin/bash
if [ -f RUNNING_PID ];then echo -e "Delete RUNNING_PID !\n";rm RUNNING_PID;fi
/usr/local/cmak-3.0.0.5/bin/cmak   2>&1 >/dev/null &
2.1.3.2 stop.sh
#!/bin/bash
jps|grep -w 'ProdServerStart'|awk '{print $1}'|xargs kill -9
if [ -f RUNNING_PID ];then echo -e "Delete RUNNING_PID!停止cmak 服务\n";rm RUNNING_PID;fi
2.1.3.3 cmak.service
[Unit]
Description=cmak.service
Requires=network.target   

[Service]
Type=forking
User=root
Group=root
Environment=JAVA_HOME=/usr/local/jdk-1.8.0
WorkingDirectory=/usr/local/cmak-3.0.0.5/
ExecStart=/usr/local/cmak-3.0.0.5/start.sh
ExecStop=/usr/local/cmak-3.0.0.5/stop.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

2.1.4 配置kafka-manager.service

2.1.5 设置

systemctl daemon-reload

注意:启动kafka需要先启动zookeeper

2.1.5 开机自启动
systemctl enable cmak
systemctl enable zookeeper
systemctl enable kafka
systemctl enable kafka-manager
2.1.6 重启服务
systemctl restart cmak
systemctl restart zookeeper
systemctl restart kafka
systemctl restart kafka-manager

#查看状态
systemctl status zookeeper

2.1.7 重启

reboot 验证是否开机自启

2.2 第二种方式

路径: cd /etc/rc.d/init.d/

2.2.1 配置zookeeper

2.2.1.1 编辑zk
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
export JAVA_HOME=/usr/java/jdk1.8.0_172
ZOOKEEPER_HOME=/usr/local/softwareinstall/zookeeper-3.4.10
case $1 in
        start) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start;;
        stop) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh stop;;
        status) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh status;;
        restart) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh restart;;
        *) echo "require start|stop|status|restart" ;;
esac

后续可通过service zk start/stop/status/restart命令 启动zk/停止zk/查看zk状态/重启zk

其中#中的注释必须要加

#chkconfig:2345 20 90
其中2345是默认启动级别,级别有0-6共7个级别。
0:表示关机   
1:单用户模式   
2:无网络连接的多用户命令行模式   
3:有网络连接的多用户命令行模式   
4:不可用   
5:带图形界面的多用户模式   
6:重新启动

20是启动优先级,90是停止优先级,优先级范围是0-100,数字越大,优先级越低。

2.2.1.2 添加权限
chmod +x zk
2.2.1.3 设置开机启动
chkconfig --add zk

添加到开机自启动列表,chkconfig --list可查看服务器开启的开机自启动服务

2.2.1.4 重启验证

服务器重启后,使用service zk status验证zk状态。(也可使用jps查看,验证是否存在zk进程)

2.2.2 配置kafka

#!/bin/bash
#chkconfig:2345 60 20
#description:kafka

export JAVA_HOME=/usr/java/jdk1.8.0_172
KAFKA_HOME=/usr/local/softwareinstall/kafka_2.10-0.8.2.1
case $1 in    
        start) su root ${KAFKA_HOME}/bin/kafka-server-start.sh ${KAFKA_HOME}/config/server.properties;;    
        stop) su root ${KAFKA_HOME}/bin/kafka-server-stop.sh;;    
        *) echo "require start|stop" ;;    
esac

2.2.3 配置cmak

2.2.3.1 start.sh
#!/bin/bash
if [ -f RUNNING_PID ];then echo -e "Delete RUNNING_PID !\n";rm RUNNING_PID;fi
/usr/local/cmak-3.0.0.5/bin/cmak   2>&1 >/dev/null &
2.2.3.2 stop.sh
#!/bin/bash
jps|grep -w 'ProdServerStart'|awk '{print $1}'|xargs kill -9
if [ -f RUNNING_PID ];then echo -e "Delete RUNNING_PID!停止cmak 服务\n";rm RUNNING_PID;fi

2.2.4 配置kafka-manager

#!/bin/bash
#chkconfig:2345 60 20
#description:kafka-manager

export JAVA_HOME=/usr/java/jdk1.8.0_172
KAFKA_MANAGER_HOME=/usr/local/softwareinstall/kafka-manager-2.0.0.2
case $1 in    
        start) su root ${KAFKA_HOME}/bin/kafka-manager;;     
        *) echo "require start" ;;    
esac

3 验证

重启服务:reboot进行验证

4 遇到问题

4.1 Executable path is not absolute

Nov 09 17:39:54 test01-prd systemd[1]: /etc/systemd/system/cmak.service:13: Executable path is not absolute: kill -9  $(cat /usr/local/cmak-3.0.0.5/RUNNING_PID)

4.2 自启动中simple和forking的区别

Type=simple类型的服务只适合那些在shell下运行在前台的命令。也就是说,当一个命令本身会以daemon模式运行时,将不能使用simple,而应该使用Type=forking。如选择后台重启服务,选用Type=forking。

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐