gpt4 book ai didi

Simple K8s Pod checks using Shell script(使用外壳脚本的简单K8S Pod检查)

转载 作者:bug小助手 更新时间:2023-10-27 20:15:36 26 4
gpt4 key购买 nike



I'm using simple base script to check the k8s pod health check status running or not. Below script running as expected from default namespace, but I want to check the pod status across all the namespace inside my cluster.

我正在使用简单的基本脚本来检查K8S Pod健康检查状态是否正在运行。下面的脚本按预期从默认命名空间运行,但我想检查集群内所有命名空间的Pod状态。


#!/bin/bash 

# colores
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"

listPods=$(kubectl get po | awk 'NR>1{print $1}')
#echo "$listPods"
readarray arr <<< $listPods

ok=0
notok=0
echo -e "\nSit Down and Wait \U1F602 :\n"
for i in ${arr[@]}
do
echo -ne "$i ... "
status=$(kubectl get po $i | grep $i | awk '{print $3}')
if [[ ! $status =~ ^Running$|^Completed$ ]] ; then
echo -e "\e[1;31mOh Shit !"$rescolor""
notify-send "Pods Health" "$i was Checked" -t 10000
let notok=notok+1
else
echo -e "\e[1;32mOK!"$rescolor""
#notify-send "Pod $i Is Good :)"
let ok=ok+1
fi
done
echo -e "\nSTATS:\n"
echo "+---------------+---------------+"
printf "|$green%-15s$rescolor|$red%-15s$rescolor|\n" "Healthy Pods" "Unhealthy Pods"
echo "+---------------+---------------+"
printf "|%-15s|%-15s|\n" "$ok" "$notok"
echo "+---------------+---------------+"
echo -e "\n"

更多回答

Replace kubectl get pod with kubectl get pod -A to get all pods from all namespaces. Does that get you what you want?

用kubectl get pod-A替换kubectl get pod,以从所有名称空间获取所有pod。这能让你如愿以偿吗?

优秀答案推荐

first of all try to retrieve the list of namespaces using the kubectl get namespaces and then, iterate over each namespace and retrieve the pods in that namespace using kubectl get po -n $ns!

首先,尝试使用kubectl get名称空间检索名称空间列表,然后迭代每个名称空间并使用kubectl get po-n$ns检索该名称空间中的Pod!


something like this:

大概是这样的:


#!/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"

namespaces=$(kubectl get namespaces -o jsonpath='{.items[*].metadata.name}')

ok=0
notok=0
echo -e "\nSit Down and Wait \U1F602 :\n"
for ns in ${namespaces[@]}
do
echo -e "Namespace: $ns"
listPods=$(kubectl get po -n $ns | awk 'NR>1{print $1}')
readarray arr <<< $listPods

for i in ${arr[@]}
do
echo -ne "$i ... "
status=$(kubectl get po -n $ns $i | grep $i | awk '{print $3}')
if [[ ! $status =~ ^Running$|^Completed$ ]]; then
echo -e "\e[1;31mOh Shit !"$rescolor""
notify-send "Pods Health" "$i was Checked" -t 10000
let notok=notok+1
else
echo -e "\e[1;32mOK!"$rescolor""
#notify-send "Pod $i Is Good :)"
let ok=ok+1
fi
done
done

echo -e "\nSTATS:\n"
echo "+---------------+---------------+"
printf "|$green%-15s$rescolor|$red%-15s$rescolor|\n" "Healthy Pods" "Unhealthy Pods"
echo "+---------------+---------------+"
printf "|%-15s|%-15s|\n" "$ok" "$notok"
echo "+---------------+---------------+"
echo -e "\n"

更多回答

Thank you so much above iteration working fine as expected, thanks lot again.

非常感谢以上迭代工作正常,再次感谢。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com