for 循环简单实现
#!/bin/bash cd /root/health-check/ > /tmp/bomp-check-result.json for i in $(ls ./check*.sh) do bash ./$i done
通过端口判断kubelet是否存活
#!/bin/bash nodeip=$(kubectl get nodes -o wide | awk 'NR>1 {print $6}') kubelet_start=0 kubelet_end=0 failed_ip="" for i in ${nodeip[@]} do let kubelet_end++ line=$(echo "" |telnet ${i} 10250 2>/dev/null |wc -l) if [ $line -eq 3 ] ;then let kubelet_start++ else failed_ip="$failed_ip $i" fi done if [ ${kubelet_start} -eq ${kubelet_end} ];then status=true else status=false fi
通过状态检测服务是否存活
#!/bin/bash #etcdIP=$(cat test.txt |grep "ETCD_INITIAL_CLUSTER=" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') master=$(kubectl get nodes -o wide|grep master |awk 'NR>1{print $6}') old=0 new=1 apiserver_failed="" scheduler_failed="" controller_failed="" proxy_failed="" for i in ${master[@]} do sta=$(kubectl get pods -o wide -n kube-system |grep kube-apiserver |grep $i |awk '{print $3}') if [ $sta == "Running" ];then apiserver_status=true else apiserver_status=false apiserver_failed="$apiserver_failed $i" fi sta_sch=$(kubectl get pods -o wide -n kube-system |grep scheduler |grep $i |awk '{print $3}') if [ $sta_sch == "Running" ];then scheduler_status=true else scheduler_status=false scheduler_failed="$scheduler_failed $i" fi sta_contro=$(kubectl get pods -o wide -n kube-system |grep controller-manager |grep $i |awk '{print $3}') if [ $sta_contro == "Running" ];then controller_status=true else controller_status=false controller_failed="$controller_failed $i" fi done sta_proxy=$(kubectl get pods -o wide -n kube-system |grep kube-proxy |awk '{print $3}') proxy_old=0 proxy_new=0 for i in ${sta_proxy[@]} do let proxy_old++ if [ $i == "Running" ];then let proxy_new++ else proxy_failed="$proxy_failed $i" fi done if [ $proxy_old -eq $proxy_new ];then proxy_status=true else proxy_status=false fi coredns_status=$(kubectl get pods -o wide -n kube-system |grep coredns |awk '{print $3}') coredns_old=0 coredns_new=0 for i in ${coredns_status[@]} do let coredns_old++ if [ $i == "Running" ];then let coredns_new++ fi done if [ $coredns_old -eq $coredns_new ];then coredns_status=true else coredns_status=false fi
shell脚本多个选项的实现方式 (指定开关)
#!/bin/sh set -o pipefail red(){ echo -e "\033[31m \t$1 \033[0m" } green(){ echo -e "\033[32m \t$1 \033[0m" } yellow(){ echo -e "\033[33m \t$1 \033[0m" } blue(){ echo -e "\033[34m $1 \033[0m" } usage() { cat << EOF Rolling upgrade kernel Usage : $(basename $0) -v <version> -f <yes> -h | --help : Show this message -v | --version : Specify kernel version -y | --yes : Upgrade confirmation switch Example : $(basename $0) -v 406 -y yes EOF } while (($#)); do case "$1" in -h | --help) usage; exit 0 ;; -v | --verison) value=${2}; shift 2 ;; -y | --yes) switch=${2}; shift 2 ;; *) usage echo "ERROR : Unknown option" exit 3 ;; esac done if [[ ${switch} != "yes" ]];then red "Please turn on the switch,View help through -h or --help" exit 5 fi
标签:脚本,status,shell,grep,变量,failed,proxy,fi,coredns From: https://www.cnblogs.com/gsda/p/17165269.html