Shell - [11] 开源Apache Zookeeper集群启停脚本

 

一、集群角色部署

当前有Zookeeper集群如下

主机名 ctos79-01 ctos79-02 ctos79-03
Zookeeper

 

二、脚本使用

 

三、脚本内容

#!/bin/bash

# 定义ZooKeeper服务器列表
SERVERS=("ctos79-01" "ctos79-02" "ctos79-03")

# 定义ZooKeeper安装路径
INSTALL_PATH="/opt/module/apache-zookeeper-3.5.7-bin/bin"

# 定义操作函数
function zk_operation() {
    operation=$1

    # 遍历所有服务器
    for server in "${SERVERS[@]}"; do
        echo "在 ${server} 上执行 $operation 操作..."
        ssh ${server} "source /etc/profile; ${INSTALL_PATH}/zkServer.sh $operation"
        if [ $? -eq 0 ]; then
            echo "[$server] ZooKeeper $operation 成功."
        else
            echo "[$server] ZooKeeper $operation 失败,请检查错误."
        fi
    done
}

# 主控制逻辑
case "$1" in
    "status")
        zk_operation "status"
        ;;
    "start")
        zk_operation "start"
        ;;
    "stop")
        zk_operation "stop"
        ;;
    "restart")
        zk_operation "restart"
        ;;
    *)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
        ;;
esac

exit 0

 

 

— 要养成终生学习的习惯 —

posted @ 2024-04-10 10:27  HOUHUILIN  阅读(6)  评论(0编辑  收藏  举报