1. 前期准备
- 安装包下载:下载链接
- 脚本复制:修改脚本中参数后拷贝脚本,将下面的脚本内容复制,在虚拟机的任意路径下执行命令vim kafka_manager.sh -> 按下键盘的Inster键 ->鼠标右键粘贴或使用键盘上的shift+Insert组合快捷键粘贴->按键盘ESC键->输入:wq!
- 执行脚本安装kafka: sh kafka_manager.sh install
- 执行脚本启动kafka(注意,要先安装zookeeper,参照链接):sh kafka_manager.sh start
2. 脚本
#!/usr/bin/env bash
#######################################
# This shell is for manager kafka
# Author: Coline
# Date: 2020/02/27 00:17
#######################################
readonly SCRIPT_PATH=$(cd `dirname $0`; pwd)
SHELL_NAME=$0
ACTION=$1
CURRENT_USER=`whoami`
hostIp=$(/sbin/ip route get 1| sed 's/^.*src \(\S*\).*$/\1/;q')
# declare where the install package
readonly INSTALL_PACKAGE_WAY=/root/kafka_2.13-2.4.0.tgz
# declare the install way
readonly INSTALL_WAY=/opt/cloud/middleware
# declare the kafka owner
readonly USER=service
# declare the kafka owner group
readonly USER_GROUP=servicegroup
# declare kafka log dir
readonly KAFKA_LOG_DIR=/opt/cloud/logs/kafka
# declare zookeeper host and port
readonly ZOOKEEPER_HOST_PORT=127.0.0.1:2181
function usage() {
echo "Usage: $SHELL_NAME {start|stop|restart|install|uninstall}"
exit 2
}
function log_info() {
echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
}
function log_error() {
echo -e "[$(date +"%Y-%m-%d %H:%M:%S %Z%:z")] [ERROR] $* \n"
exit 1
}
function check_result()
{
local ret_code=$1
shift
local error_msg=$*
if [[ ${ret_code} -ne 0 ]]; then
log_error ${error_msg}
fi
}
function normal_check() {
# check current director whether have kafka package
if [[ ! -f ${INSTALL_PACKAGE_WAY} ]]; then
log_error "pleas check whether install package exists"
fi
# check param whether empty
if [[ -z ${INSTALL_WAY} ]] || [[ -z ${USER} ]] || [[ -z ${USER_GROUP} ]] || [[ -z ${KAFKA_LOG_DIR} ]] || [[ -z ${ZOOKEEPER_HOST_PORT} ]]; then
log_error "param can not be empty"
fi
}
function config_properties() {
local key=$1
local value=$2
if [[ $(cat ${INSTALL_WAY}/kafka/config/server.properties |grep "^${key}") ]]; then
sed -i "s/^${key}=.*/${key}=${value//\//\\/}/g" ${INSTALL_WAY}/kafka/config/server.properties
else
echo "${key}=${value}" >> ${INSTALL_WAY}/kafka/config/server.properties
fi
}
# do something before install
function pre_install(){
# make install way
mkdir -p ${INSTALL_WAY}
#create group ${USER}
grep "^${USER_GROUP}" /etc/group &> /dev/null
if [[ $? -ne 0 ]]; then
log_info "add user group"
groupadd ${USER_GROUP}
fi
#create user group
id ${USER} &> /dev/null
if [[ $? -ne 0 ]]; then
log_info "add user"
useradd -g ${USER_GROUP} ${USER}
chage -M 99999 ${USER}
fi
}
# do install job
function install_kafka(){
# unzip the package to install way
tar -zxvf ${INSTALL_PACKAGE_WAY} -C ${INSTALL_WAY}
}
function post_install() {
# create ln of kafka
local kafka_dir=$(ls -lt ${INSTALL_WAY}|grep kafka|head -1|awk '{print $9}')
ln -s ${kafka_dir} ${INSTALL_WAY}/kafka
mkdir -p ${KAFKA_LOG_DIR}
config_properties broker.id 0
config_properties port 9092
config_properties host.name ${hostIp}
config_properties log.dirs ${KAFKA_LOG_DIR}
config_properties zookeeper.connect ${ZOOKEEPER_HOST_PORT}
# change the owner of install way
chown -R ${USER}:${USER_GROUP} ${INSTALL_WAY}
chown -R ${USER}:${USER_GROUP} ${KAFKA_LOG_DIR}
}
function install() {
log_info "------------normal_check------------"
normal_check
check_result $? "normal_check failed."
log_info "normal_check success"
log_info "------------pre_install-------------"
pre_install
check_result $? "pre_install failed."
log_info "pre_install success"
log_info "------------install_kafka-------------"
install_kafka
check_result $? "install_kafka failed."
log_info "install_kafka success"
log_info "------------post_install-------------"
post_install
check_result $? "post_install failed."
log_info "post_install success"
}
# uninstall kafka
function uninstall() {
local kafka_pid=$(ps -ef|grep kafka|grep -vwE "grep|vi|vim|tail|cat|sh"|awk '{print $2}')
if [[ -n ${kafka_pid} ]] ;then
log_error "please stop kafka firstly"
fi
# delete the kafka install way
rm -rf ${INSTALL_WAY}/kafka
rm -rf ${INSTALL_WAY}/*kafka*
}
# start kafka
function start() {
local zookeeper_pid=$(ps -ef|grep zookeeper|grep -vwE "grep|vi|vim|tail|cat|sh"|awk '{print $2}')
if [[ -z ${zookeeper_pid} ]] ;then
log_info "please start zookeeper firstly"
return 0
fi
local kafka_pid=$(ps -ef|grep kafka|grep -vwE "grep|vi|vim|tail|cat|sh"|awk '{print $2}')
if [[ -n ${kafka_pid} ]] ;then
log_info "kafka has been start"
return 0
fi
if [[ ${CURRENT_USER} = ${USER} ]]; then
source /etc/profile && ${INSTALL_WAY}/kafka/bin/kafka-server-start.sh ${INSTALL_WAY}/kafka/config/server.properties &
else
su - ${USER} -c "source /etc/profile && ${INSTALL_WAY}/kafka/bin/kafka-server-start.sh ${INSTALL_WAY}/kafka/config/server.properties &"
fi
}
# stop kafka
function stop() {
local kafka_pid=$(ps -ef|grep kafka|grep -vwE "grep|vi|vim|tail|cat|sh"|awk '{print $2}')
if [[ -z ${kafka_pid} ]] ;then
log_info "kafka process non-exists"
return 0
fi
${INSTALL_WAY}/kafka/bin/kafka-server-stop.sh ${INSTALL_WAY}/kafka/config/server.properties
sleep 5
local kafka_pid=$(ps -ef|grep kafka|grep -vwE "grep|vi|vim|tail|cat|sh"|awk '{print $2}')
if [[ -n ${kafka_pid} ]] ;then
log_info "kafka pid still exists pid= ${kafka_pid}"
kill -9 ${kafka_pid}
fi
}
case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
install)
install
;;
uninstall)
uninstall
;;
*)
usage
;;
esac
参数修改:
INSTALL_PACKAGE_WAY | 安装包路径,需要全路径,例如:/root/kafka_2.13-2.4.0.tgz |
INSTALL_WAY | kafka安装路径,例如:/opt/cloud/middleware |
USER | 服务用户属主,例如:root |
USER_GROUP | 服务用户用户组,例如:root |
KAFKA_LOG_DIR | kafka日志路径,例如:/opt/cloud/logs/kafka |
ZOOKEEPER_HOST_PORT | zookeeper的ip加端口,例如:192.168.167.75:2181 |
脚本命令:
sh kafka_manager.sh install | 安装kafka |
sh kafka_manager.sh uninstall | 卸载kafka |
sh kafka_manager.sh start | 启动kafka |
sh kafka_manager.sh stop | 关闭kafka |
sh kafka_manager.sh check_status | 检查kafka |
sh kafka_manager.sh restart | 重启kafka |