工作随笔 - shell脚本安装鉴权版本zookeeper/kafka集群

记录日常工作中的zk/kafka安装

组件 版本 安装包名
openjdk 1.8.0_345
zookeeper 3.7.1 apache-zookeeper-3.7.1-bin.tar.gz
kafka 2.12-2.5.1 kafka_2.12-2.5.1.tgz
centos 7.6

文件夹列表

.
├── apache-zookeeper-3.7.1-bin.tar.gz
├── install_zk_kafka.sh
├── jaas
│   ├── zk_client.conf
│   └── zk_server.conf
├── jaas.conf
├── java.env
├── kafka_2.12-2.5.1.tgz
└── kafka_client.properties

jaas.conf

KafkaServer {
    org.apache.kafka.common.security.plain.PlainLoginModule required
    username="admin"
    password="pwd4test"
    user_admin="pwd4test"
    user_kafka="pwd4test";
};

ZkClient {
    org.apache.zookeeper.server.auth.DigestLoginModule required
    username="kafka"
    password="pwd4test";

java.env

SERVER_JVMFLAGS="-Djava.security.auth.login.config=/home/test/App/zookeeper/conf/jaas/zk_server.conf -Dzookeeper.allowSaslFailedClients=false -Dzookeeper.sessionRequireClientSASLAuth=true"
CLIENT_JVMFLAGS="${CLIENT_JVMFLAGS} -Djava.security.auth.login.config=/home/test/App/zookeeper/conf/jaas/zk_client.conf"

kafka_client.properties

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="pwd4test";

jaas/zk_client.conf

Client {
    org.apache.zookeeper.server.auth.DigestLoginModule required
    username="kafka"
    password="pwd4test";
};

jaas/zk_server.conf

QuorumServer {
    org.apache.zookeeper.server.auth.DigestLoginModule required
    user_admin="pwd4test";
};

QuorumLearner {
    org.apache.zookeeper.server.auth.DigestLoginModule required
    username="admin"
    password="pwd4test";
};

Server{
    org.apache.zookeeper.server.auth.DigestLoginModule required
    user_kafka="pwd4test";
};

install_zk_kafka.sh

#!/bin/bash
# usage  : sh install_zk_kafka.sh zookeeper|kafka|clean

BASE_DIR=$(cd `dirname $0`; pwd)
ZK_PACKAGE="apache-zookeeper-3.7.1-bin.tar.gz"
ZK_SERVER=(192.168.1.1 92.168.1.2 92.168.1.3)
LOCAL_IP=$(ifconfig|grep inet|grep -v 100.100| sed -n '1p'|awk '{print $2}')
ZK_PORT="2181"
INSTALL_BASE_PATH="/home/test"
KAFKA_PACKAGE="kafka_2.12-2.5.1.tgz"
KAFKA_PORT="9092"
RUN_ID="test"

function log_error() {
    echo -e "\033[31m [ERROR] $@ \033[0m"
}

function log_info() {
    echo -e "\033[32m [INFO] $@ \033[0m"
}

function log_warn() {
    echo -e "\033[33m [WARN] $@ \033[0m"
}

function check_jdk() {
    java_version=$(java -version 2>&1|grep version|awk -F '"' '{print $2}')
    
    if [ "${java_version}"x == x ];then
        log_error "未找到JDK,请事先安装JDK"
        exit 1
    else
        log_info "Java版本为:java_version"
    fi
    
}

function prepare_zk() {
    log_info "创建zookeeper所需文件夹"
    mkdir -pv ${INSTALL_BASE_PATH}/App ${INSTALL_BASE_PATH}/Logs/zookeeper ${INSTALL_BASE_PATH}/Data/zookeeper    
}

function install_zk() {
    log_info "解压安装包${ZK_PACKAGE}"
    tar -xzvf ${BASE_DIR}/${ZK_PACKAGE} -C  ${INSTALL_BASE_PATH}/App/ 2>&1 > /dev/null
    mv ${INSTALL_BASE_PATH}/App/apache-zookeeper-3.7.1-bin ${INSTALL_BASE_PATH}/App/zookeeper

    log_info "创建配置文件${INSTALL_BASE_PATH}/App/zookeeper/conf/zoo.cfg"
cat >> ${INSTALL_BASE_PATH}/App/zookeeper/conf/zoo.cfg <> ${INSTALL_BASE_PATH}/App/zookeeper/conf/zoo.cfg
    echo "dataLogDir=${INSTALL_BASE_PATH}/Logs/zookeeper"  >> ${INSTALL_BASE_PATH}/App/zookeeper/conf/zoo.cfg
    echo "clientPort=${ZK_PORT}" >> ${INSTALL_BASE_PATH}/App/zookeeper/conf/zoo.cfg

    # get the server id
    server_id=""

    # update the server ip in config file
    i=0
    while [ $i -lt ${#ZK_SERVER[@]} ]
    do
        num=$i
        let i++
        echo "server.$i=${ZK_SERVER[$num]}:2888:3888" >> ${INSTALL_BASE_PATH}/App/zookeeper/conf/zoo.cfg
        [ "${LOCAL_IP}" == "${ZK_SERVER[$num]}" ] && log_info "server ID is $i" && server_id=$i && echo "$server_id" >> ${INSTALL_BASE_PATH}/Data/zookeeper/myid
    done

    # 判断local_ip是否属于节点中的一个,否则中断
    if [[ x"${server_id}" == "x"  ]];then
            log_error "脚本获取的LOCAL_IP(${LOCAL_IP})不在列表${ZK_SERVER[@]}中,请手动设置LOCAL_IP变量"
        exit 1
    fi

    log_info "添加zookeeper到环境变量"
    echo "export ZK_HOME=${INSTALL_BASE_PATH}/App/zookeeper" >> /etc/profile
    echo "export PATH=\$ZK_HOME/bin:\$PATH" >> /etc/profile

    log_info "添加zookeeper鉴权文件"
    cp -r ${BASE_DIR}/jaas ${INSTALL_BASE_PATH}/App/zookeeper/conf/
    cp ${BASE_DIR}/java.env ${INSTALL_BASE_PATH}/App/zookeeper/conf/
    sed -ri "s#/home/finance#${INSTALL_BASE_PATH}#g" ${INSTALL_BASE_PATH}/App/zookeeper/conf/java.env
    chmod 755 ${INSTALL_BASE_PATH}/App ${INSTALL_BASE_PATH}/Logs ${INSTALL_BASE_PATH}/Data
    chown -R ${RUN_ID}:${RUN_ID} ${INSTALL_BASE_PATH}/App/zookeeper ${INSTALL_BASE_PATH}/Logs/zookeeper ${INSTALL_BASE_PATH}/Data/zookeeper
}

function start_zk() {
    log_info "启动zookeeper..."
    su - ${RUN_ID} -c "source /etc/profile;${INSTALL_BASE_PATH}/App/zookeeper/bin/zkServer.sh start"
    sleep 5

    
    log_info "##########################"
    log_info "### 请记录以下鉴权信息 ###"
    log_info "zookeeper鉴权: kafka / pwd4test"
    log_info "##########################"
    log_warn "[注意]: 请在[第三个节点]按照完成之后,执行命令查看启动状态:source /etc/profile;${INSTALL_BASE_PATH}/App/zookeeper/bin/zkServer.sh status"

}

function stop_zk() {
    log_info "停止zookeeper..."
    su - ${RUN_ID} -c "source /etc/profile;${INSTALL_BASE_PATH}/App/zookeeper/bin/zkServer.sh stop"
    sleep 5
}

function status_zk() {
    log_info "zookeeper的status如下:"
    su - ${RUN_ID} -c "source /etc/profile;${INSTALL_BASE_PATH}/App/zookeeper/bin/zkServer.sh status"
}

function prepare_kafka() {
    log_info "创建kafka所需文件夹"
    mkdir -pv ${INSTALL_BASE_PATH}/App ${INSTALL_BASE_PATH}/Logs/kafka ${INSTALL_BASE_PATH}/Data/kafka    
}

function check_zk() {
    zk_status=$(source /etc/profile;${INSTALL_BASE_PATH}/App/zookeeper/bin/zkServer.sh status)
    zk_mode=$(echo $zk_status | grep Mode)

    if [ "${zk_mode}"x == x ];then
        log_error "未找到zookeeper,请事先安装zookeeper"
        exit 1
    else
        log_info "发现zookeeper运行,继续执行安装kafka"
    fi

}    

function install_kafka() {
    log_info "解压安装包${KAFKA_PACKAGE}"
    tar -xzvf ${BASE_DIR}/${KAFKA_PACKAGE} -C  ${INSTALL_BASE_PATH}/App/ 2>&1 > /dev/null
    #mv ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1 ${INSTALL_BASE_PATH}/App/kafka

    log_info "创建配置文件${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties"

cat >> ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties <> ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties
    done

    # 判断local_ip是否属于节点中的一个,否则中断
    if [[ x"${server_id}" == "x"  ]];then
            log_error "脚本获取的LOCAL_IP(${LOCAL_IP})不在列表${ZK_SERVER[@]}中,请手动设置LOCAL_IP变量"
        exit 1
    fi

    echo "listeners=SASL_PLAINTEXT://${LOCAL_IP}:${KAFKA_PORT}" >> ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties
    echo "advertised.listeners=SASL_PLAINTEXT://${LOCAL_IP}:${KAFKA_PORT}" >> ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties
    echo "log.dirs=${INSTALL_BASE_PATH}/Data/kafka" >> ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties
    echo "zookeeper.connect=${ZK_SERVER[0]}:${ZK_PORT},${ZK_SERVER[1]}:${ZK_PORT},${ZK_SERVER[2]}:${ZK_PORT}" >> ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties

    log_info "创建kafka鉴权文件"
    cp  ${BASE_DIR}/jaas.conf  ${BASE_DIR}/kafka_client.properties ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/

    log_info "更改kafka日志路径为: ${INSTALL_BASE_PATH}/Logs/kafka"
    sed -ri "s#LOG_DIR=\".*#LOG_DIR=\"${INSTALL_BASE_PATH}\/Logs\/kafka\"#g" ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/bin/kafka-run-class.sh

    log_info "修改配置,开启kafka鉴权"
    sed -ri "s#KAFKA_OPTS=\"\"#KAFKA_OPTS=\"$KAFKA_OPTS -Djava.security.auth.login.config=${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/jaas.conf\"#g" ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/bin/kafka-run-class.sh
    sed -ri "s#KAFKA_JVM_PERFORMANCE_OPTS=\"-server#KAFKA_JVM_PERFORMANCE_OPTS=\"-server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:MaxInlineLevel=15 -Djava.awt.headless=true -Dzookeeper.sasl.client=true -Dzookeeper.sasl.clientconfig=ZkClient -Dzookeeper.sasl.client.username=kafka -Djava.security.auth.login.config=${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/jaas.conf#g" ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/bin/kafka-run-class.sh


    chmod 755 ${INSTALL_BASE_PATH}/App ${INSTALL_BASE_PATH}/Logs ${INSTALL_BASE_PATH}/Data
    chown -R ${RUN_ID}:${RUN_ID} ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1 ${INSTALL_BASE_PATH}/Logs/kafka ${INSTALL_BASE_PATH}/Data/kafka
}

function start_kafka() {
    log_info "启动kafka..."
    su - ${RUN_ID} -c "source /etc/profile;${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/bin/kafka-server-start.sh -daemon ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/config/server.properties"
    sleep 5

    log_info "##########################"
    log_info "### 请记录以下鉴权信息 ###"
    log_info "kafka鉴权: admin / pwd4test"
    log_info "##########################"

    log_info "待三个节点启动完成,自行创建topic..."
}

function stop_kafka() {
    log_info "停止kafka..."
    su - ${RUN_ID} -c "source /etc/profile;${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1/bin/kafka-server-stop.sh"
    sleep 5
}

function clean() {
    log_warn "清理zookeeper..."
    rm -rf ${INSTALL_BASE_PATH}/App/zookeeper ${INSTALL_BASE_PATH}/Logs/zookeeper ${INSTALL_BASE_PATH}/Data/zookeeper
    log_warn "清理kafka..."
    rm -rf ${INSTALL_BASE_PATH}/App/kafka_2.12-2.5.1 ${INSTALL_BASE_PATH}/Logs/kafka ${INSTALL_BASE_PATH}/Data/kafka
    log_warn "清理完成..."
}

case "$1" in
  zookeeper)
        check_jdk
        prepare_zk
        install_zk
        start_zk
        #status_zk
        ;;
  kafka)
        check_jdk
        check_zk
        prepare_kafka
        install_kafka
        start_kafka
        ;;
  clean)
        stop_kafka
        stop_zk
        clean
        ;;        
  *)
        echo $"Usage: $0 {zookeeper|kafka|clean}"
        exit 2
esac

exit 0 

使用方法

# 修改脚本中IP列表,确保三台服务器IP列表顺序一致
ZK_SERVER=(192.168.1.1 92.168.1.2 92.168.1.3)
# 三台服务器依次安装zookeeper
sh install_zk_kafka.sh zookeeper
# 待三台zookeeper安装完成,再依次安装kafka
sh install_zk_kafka.sh kafka

# [慎重执行]清理操作,清理所有zookeeper/kafka安装目录,以便重装
sh install_zk_kafka.sh clean

# zookeeper鉴权: kafka / pwd4test
# kafka鉴权: admin / pwd4test以下为纯手动安装配置方式

你可能感兴趣的:(工作随笔 - shell脚本安装鉴权版本zookeeper/kafka集群)