集群启动脚本
xsync
if [ $
then
echo Not Enough Arguement!
exit;
fi
for host in hadoop01 hadoop02 hadoop03
do
echo -e "\n==================== $host ===================="
for file in $@
do
if [ -e $file ]
then
pdir=$(cd -P $(dirname $file); pwd)
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exist!
fi
done
done
echo -e "\n"
jpsall
for host in hadoop01 hadoop02 hadoop03
do
echo -e "\n=============== $host ==============="
ssh $host "export JAVA_HOME=/opt/module/jdk1.8.0_391 && export PATH=\$JAVA_HOME/bin:\$PATH && jps"
done
echo -e "\n"
zookeeper
zkserver
echo "$1 zkServer ..."
for i in {hadoop02,hadoop01,hadoop03}
do
ssh $i "source /etc/profile && zkServer.sh $1"
done
zkstatus
for host in {hadoop01,hadoop02,hadoop03}
do
status=$(ssh $host 'source /etc/profile && zkServer.sh status 2>&1 | grep Mode')
if [[ $status == "Mode: follower" ]];then
echo "$host是从节点"
elif [[ $status == "Mode: leader" ]];then
echo "$host是主节点"
else
echo "未查询到$host节点zookeeper状态,请检查服务"
fi
done
hadoop
myhadoop
HADOOP_HOME=/opt/module/hadoop-3.3.5
check_zookeeper_status() {
echo "检查 $1 上的 ZooKeeper 状态..."
status=$(ssh $1 "source /etc/profile && zkServer.sh status 2>&1 | grep Mode")
if [[ $status == "Mode: follower" ]]; then
echo "$1 是从节点"
elif [[ $status == "Mode: leader" ]]; then
echo "$1 是主节点"
else
echo "未能获取 $1 节点的 ZooKeeper 状态,请检查服务。"
fi
}
start_zookeeper() {
echo "在 $1 上启动 ZooKeeper..."
ssh $1 "source /etc/profile && zkServer.sh start"
sleep 5
check_zookeeper_status $1
}
stop_zookeeper() {
echo "在 $1 上启动 ZooKeeper..."
ssh $1 "source /etc/profile && zkServer.sh stop"
sleep 5
check_zookeeper_status $1
}
case $1 in
"start")
echo -e "\n================= 启动 Hadoop 集群 ================="
for node in hadoop01 hadoop02 hadoop03; do
check_zookeeper_status $node
done
read -p "是否启动 ZooKeeper 服务?(y/n): " zk_start_choice
if [ "$zk_start_choice" == "y" ]; then
for node in hadoop02 hadoop01 hadoop03; do
start_zookeeper $node
done
else
read -p "是否继续启动 Hadoop 集群?(y/n): " choice
[ "$choice" != "y" ] && echo "用户选择不启动 Hadoop 集群,退出脚本" && exit 1
fi
echo " ------------------ 启动 HDFS --------------------"
$HADOOP_HOME/sbin/start-dfs.sh
tries=0
while ! hdfs haadmin -checkHealth nn1 && [ "$tries" -lt 5 ]; do
sleep 10
tries=$((tries + 1))
done
if [ "$tries" -eq 5 ]; then
echo "等待 NameNode 启动超时,退出脚本"
exit 1
fi
ACTIVE_NODE=$(hdfs haadmin -getServiceState nn1)
if [ "$ACTIVE_NODE" == "active" ]; then
echo "hadoop01 是 Active 节点,启动 YARN、ZKFC 和 HistoryServer"
echo "------------------- 启动 YARN --------------------"
$HADOOP_HOME/sbin/start-yarn.sh
echo "--------- 启动 HistoryServer on hadoop01 ---------"
ssh hadoop01 "$HADOOP_HOME/bin/mapred --daemon start historyserver"
echo "--------- 启动 HistoryServer on hadoop02 ---------"
ssh hadoop02 "$HADOOP_HOME/bin/mapred --daemon start historyserver"
else
echo "hadoop01 不是 Active 节点,正在进行节点切换"
hdfs haadmin -failover nn2 nn1
sleep 30
echo "------------------- 启动 YARN --------------------"
$HADOOP_HOME/sbin/start-yarn.sh
echo "--------- 启动 HistoryServer on hadoop01 ---------"
ssh hadoop01 "$HADOOP_HOME/bin/mapred --daemon start historyserver"
echo "--------- 启动 HistoryServer on hadoop02 ---------"
ssh hadoop02 "$HADOOP_HOME/bin/mapred --daemon start historyserver"
fi
echo -e "\n"
;;
"stop")
echo -e "\n================= 关闭 Hadoop 集群 ================="
read -p "是否确实要停止 Hadoop 服务?(y/n): " confirm_stop
[ "$confirm_stop" != "y" ] && echo "用户选择不停止 Hadoop 集群,退出脚本" && exit 0
echo "--------- 关闭 HistoryServer on hadoop01 ----------"
ssh hadoop01 "$HADOOP_HOME/bin/mapred --daemon stop historyserver"
echo "--------- 关闭 HistoryServer on hadoop02 ----------"
ssh hadoop02 "$HADOOP_HOME/bin/mapred --daemon stop historyserver"
echo "------------------- 关闭 YARN ---------------------"
$HADOOP_HOME/sbin/stop-yarn.sh
echo "------------------- 关闭 HDFS ---------------------"
$HADOOP_HOME/sbin/stop-dfs.sh
read -p "是否关闭 ZooKeeper 服务?(y/n): " zk_stop_choice
if [ "$zk_stop_choice" == "y" ]; then
for node in hadoop02 hadoop01 hadoop03; do
stop_zookeeper $node
done
fi
echo -e "\n"
;;
*)
echo "输入参数错误!"
exit 1
;;
esac
spark
myspark
SPARK_HOME=/opt/module/spark-3.2.4
start_spark() {
echo "Starting Spark Cluster..."
$SPARK_HOME/sbin/start-all.sh
$SPARK_HOME/sbin/start-history-server.sh
ssh hadoop02 "source /etc/profile && $SPARK_HOME/sbin/start-master.sh && $SPARK_HOME/sbin/start-history-server.sh"
echo "Spark Cluster started successfully."
}
check_spark_status() {
remote_master_status_hadoop01=$(ssh hadoop01 'source /etc/profile && ss -ntulp | grep 8090')
if [[ -n "$remote_master_status_hadoop01" ]]; then
echo "Spark Master is running on hadoop01."
else
echo "Spark Master is not running on hadoop01."
fi
remote_master_status_hadoop02=$(ssh hadoop02 'source /etc/profile && ss -ntulp | grep 8090')
if [[ -n "$remote_master_status_hadoop02" ]]; then
echo "Spark Master is running on hadoop02."
else
echo "Spark Master is not running on hadoop02."
fi
}
stop_spark() {
echo "Stopping Spark Cluster..."
$SPARK_HOME/sbin/stop-all.sh
$SPARK_HOME/sbin/stop-history-server.sh
ssh hadoop02 "source /etc/profile && $SPARK_HOME/sbin/stop-master.sh && $SPARK_HOME/sbin/stop-history-server.sh"
echo "Spark Cluster stopped successfully."
}
case "$1" in
start)
start_spark
;;
status)
check_spark_status
;;
stop)
stop_spark
;;
*)
echo "Usage: $0 {start|status|stop}"
exit 1
;;
esac
exit 0
hive
myhive
HIVE_LOG_DIR="/opt/module/hive-3.1.3/logs"
SERVERS=("hadoop02" "hadoop03")
case $1 in
"start")
echo "Starting Hive services..."
for SERVER in "${SERVERS[@]}"; do
ssh "$SERVER" "source /etc/profile;" "nohup bash -s" < <(cat <<'END_SCRIPT'
HIVE_LOG_DIR="/opt/module/hive-3.1.3/logs"
if [ ! -d "$HIVE_LOG_DIR" ]; then
mkdir -p "$HIVE_LOG_DIR"
fi
function check_process() {
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_start() {
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
[ -z "$metapid" ] && eval $cmd || echo "Metastore服务已启动"
server2pid=$(check_process HiveServer2 10001)
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}
function hive_stop() {
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10001)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
hive_start
END_SCRIPT
)
done
;;
"stop")
echo "Stopping Hive services..."
for SERVER in "${SERVERS[@]}"; do
ssh "$SERVER" "source /etc/profile;" "bash -s" < <(cat <<'END_SCRIPT'
function check_process() {
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_stop() {
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10001)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
hive_stop
END_SCRIPT
)
done
;;
"restart")
echo "Restarting Hive services..."
for SERVER in "${SERVERS[@]}"; do
ssh "$SERVER" "source /etc/profile;" "bash -s" < <(cat <<'END_SCRIPT'
function check_process() {
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_stop() {
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10001)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
function hive_start() {
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
[ -z "$metapid" ] && eval $cmd || echo "Metastore服务已启动"
server2pid=$(check_process HiveServer2 10001)
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}
hive_stop
sleep 2
hive_start
END_SCRIPT
)
done
;;
"status")
echo "Checking Hive services status..."
for SERVER in "${SERVERS[@]}"; do
ssh "$SERVER" "source /etc/profile;" "bash -s" < <(cat <<'END_SCRIPT'
function check_process() {
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo "Checking process: $1, Port: $2, PID: $pid, PPID: $ppid"
netstat -nltp | grep $2
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
check_process HiveMetastore 9083 >/dev/null && echo "Metastore服务运行正常" || echo "Metastore服务运行异常"
check_process HiveServer2 10001 >/dev/null && echo "HiveServer2服务运行正常" || echo "HiveServer2服务运行异常"
END_SCRIPT
)
done
;;
*)
echo "Invalid Args!"
echo 'Usage: myhive start|stop|restart|status'
;;
esac
hbase
myhbase
HBASE_HOME=/opt/module/hbase-2.4.17
HBASE_MASTER="hadoop01 hadoop02"
HBASE_REGIONSERVER="hadoop01 hadoop02 hadoop03"
start_hbase_cluster() {
for master in $HBASE_MASTER; do
ssh $master "$HBASE_HOME/bin/hbase-daemon.sh start master"
done
for regionserver in $HBASE_REGIONSERVER; do
ssh $regionserver "$HBASE_HOME/bin/hbase-daemon.sh start regionserver"
done
sleep 5
}
check_hbase_status() {
remote_master_status=$(ssh hadoop01 "source /etc/profile && ss -ntulp | grep 60010 && echo 'HBase Master is running on hadoop01.' || echo 'HBase Master is not running on hadoop01.'")
remote_master_status+="\n$(ssh hadoop02 "source /etc/profile && ss -ntulp | grep 60010 && echo 'HBase Master is running on hadoop02.' || echo 'HBase Master is not running on hadoop02.'")"
echo -e "$remote_master_status"
}
stop_hbase_cluster() {
for regionserver in $HBASE_REGIONSERVER; do
ssh $regionserver "$HBASE_HOME/bin/hbase-daemon.sh stop regionserver"
done
for master in $HBASE_MASTER; do
ssh $master "$HBASE_HOME/bin/hbase-daemon.sh stop master"
done
}
case "$1" in
"start")
start_hbase_cluster
;;
"status")
check_hbase_status
;;
"stop")
stop_hbase_cluster
;;
*)
echo "Usage: $0 {start|status|stop}"
exit 1
;;
esac
exit 0