You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

优化K8s节点状态查询Bash脚本:实现友好表格化输出

优化K8s节点状态查询脚本的表格输出

问题背景

我编写了一款用于查询K8s节点Ready/NotReady状态的Bash脚本,脚本功能正常但输出不够友好,希望将节点状态输出改为规范的框式表格格式。

现有脚本

#!/bin/bash

# colors
blanc="\033[1;37m"
gray="\033[0;37m"
magento="\033[0;35m"
red="\033[1;31m"
green="\033[1;32m"
amarillo="\033[1;33m"
azul="\033[1;34m"
rescolor="\e[0m"

listNodes=$(kubectl get nodes | awk 'NR>1{print $1}')
readarray  node_arr <<<  $listNodes

ok=0
notok=0
echo -e "\nSit Down and Wait  \U1F602 :\n"
for i in ${node_arr[@]}
do 
echo -ne "$i ... " 
status=$(kubectl get node $i | grep $i | awk '{print $2}')
    if [[ ! $status =~ ^Ready$|^NotReady$  ]]  ; then
        echo -e "\e[1;31mOh Shit !"$rescolor""
    else
        echo -e "\e[1;32mOK!"$rescolor""
    fi
done

echo -e "\nNODE STATUS:\n"
echo "+---------------+---------------+"
printf "|$green%-15s$rescolor|$red%-15s$rescolor|\n" "Names" "Status"
echo "+---------------+---------------+"
printf "|%-15s|%-15s|\n" "$ok" "$notok"
echo "+---------------+---------------+"
echo -e "\n"

当前输出

Sit Down and Wait  😂 :

node-1.compute.internal ... OK!
node-2.compute.internal ... OK!

NODE STATUS:

+---------------+---------------+
|Names          |Status         |
+---------------+---------------+
|0              |0              |
+---------------+---------------+

期望输出

Sit Down and Wait  😂 :

node-1.compute.internal ... OK!
node-2.compute.internal ... OK!

NODE STATUS:

+---------------------------+---------------+
|Names                      |Status         |
+---------------------------+---------------+
|node-1.compute.internal    | Ready         |
+---------------------------+---------------+

修改后的脚本

#!/bin/bash

# 颜色定义
blanc="\033[1;37m"
gray="\033[0;37m"
magento="\033[0;35m"
red="\033[1;31m"
green="\033[1;32m"
amarillo="\033[1;33m"
azul="\033[1;34m"
rescolor="\e[0m"

# 获取节点列表并存储到数组
readarray -t node_arr < <(kubectl get nodes | awk 'NR>1{print $1}')

# 存储节点状态的关联数组
declare -A node_status

echo -e "\nSit Down and Wait  \U1F602 :\n"
for node in "${node_arr[@]}"
do 
    echo -ne "$node ... " 
    # 获取节点状态,简化命令避免重复调用kubectl
    status=$(kubectl get node "$node" --no-headers | awk '{print $2}')
    node_status["$node"]="$status"
    if [[ ! $status =~ ^Ready$|^NotReady$ ]]; then
        echo -e "${red}Oh Shit !${rescolor}"
    else
        echo -e "${green}OK!${rescolor}"
    fi
done

echo -e "\nNODE STATUS:\n"
# 定义表格列宽,适配节点名称长度
COL1_WIDTH=30
COL2_WIDTH=15
# 打印表格分隔线
printf "+-%s-+-%s-+\n" "$(printf "%${COL1_WIDTH}s" | tr ' ' '-')" "$(printf "%${COL2_WIDTH}s" | tr ' ' '-')"
# 打印表头
printf "| ${green}%-${COL1_WIDTH}s${rescolor} | ${red}%-${COL2_WIDTH}s${rescolor} |\n" "Names" "Status"
# 打印分隔线
printf "+-%s-+-%s-+\n" "$(printf "%${COL1_WIDTH}s" | tr ' ' '-')" "$(printf "%${COL2_WIDTH}s" | tr ' ' '-')"
# 遍历节点状态,打印每行数据
for node in "${!node_status[@]}"
do
    # 根据状态设置颜色
    if [[ ${node_status[$node]} == "Ready" ]]; then
        status_color="${green}"
    elif [[ ${node_status[$node]} == "NotReady" ]]; then
        status_color="${red}"
    else
        status_color="${amarillo}"
    fi
    printf "| %-${COL1_WIDTH}s | ${status_color}%-${COL2_WIDTH}s${rescolor} |\n" "$node" "${node_status[$node]}"
    # 打印每行之间的分隔线
    printf "+-%s-+-%s-+\n" "$(printf "%${COL1_WIDTH}s" | tr ' ' '-')" "$(printf "%${COL2_WIDTH}s" | tr ' ' '-')"
done

echo -e "\n"

修改说明

  1. 存储节点状态:新增关联数组node_status,遍历节点时同步记录每个节点的状态,避免后续重复调用kubectl。
  2. 优化表格格式:定义适配节点名称长度的列宽,动态生成匹配列宽的表格分隔线,避免内容截断。
  3. 状态颜色区分:根据节点的Ready/NotReady/异常状态设置对应颜色,状态展示更直观。
  4. 简化命令:将kubectl get node $i | grep $i | awk '{print $2}'简化为kubectl get node "$node" --no-headers | awk '{print $2}',减少不必要的grep调用。

最终输出示例

Sit Down and Wait  😂 :

node-1.compute.internal ... OK!
node-2.compute.internal ... OK!

NODE STATUS:

+------------------------------+---------------+
| Names                         | Status        |
+------------------------------+---------------+
| node-1.compute.internal      | Ready         |
+------------------------------+---------------+
| node-2.compute.internal      | Ready         |
+------------------------------+---------------+

内容的提问来源于stack exchange,提问作者Gowmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:44:54