Skip to content

Instantly share code, notes, and snippets.

@ghostflare76
Last active August 22, 2023 09:27
Show Gist options
  • Save ghostflare76/56265b6a845a84c38a60e5d1893f9fb7 to your computer and use it in GitHub Desktop.
Save ghostflare76/56265b6a845a84c38a60e5d1893f9fb7 to your computer and use it in GitHub Desktop.
k8s pod 상태 체크 스크립트
#!/bin/bash
if [ -z "$1" ]; then
echo "Error: labal app name is not specified."
exit 1
fi
if [ -z "$2" ]; then
echo "Error : zone is not specified."
exit 1
fi
APP_NAME=$1
ZONE=$2
pod_names=$(kubectl get pods --selector=app=${APP_NAME} --output=jsonpath='{.items[*].metadata.name}')
for pod_name in $pod_names
do
ready_status=$(kubectl get pod $pod_name -o jsonpath="{.status.containerStatuses[0].ready}" | tr '[:upper:]' '[:lower:]')
if [ -z "$ready_status" ]; then
ready_status=$(kubectl get pod $pod_name -o jsonpath="{.status.conditions[0].status}" | tr '[:upper:]' '[:lower:]')
message=$(kubectl get pod $pod_name -o jsonpath="{.status.conditions[0].message}" | sed "s/\"/'/g")
reason=$(kubectl get pod $pod_name -o jsonpath="{.status.conditions[0].reason}")
else
message=$(kubectl get pod $pod_name -o jsonpath="{.status.containerStatuses[0].state.waiting.message}" | sed "s/\"/'/g")
reason=$(kubectl get pod $pod_name -o jsonpath="{.status.containerStatuses[0].state.waiting.reason}")
fi
host_ip=$(kubectl get pod $pod_name -o jsonpath="{.status.hostIP}")
node_name=$(kubectl get pod $pod_name -o jsonpath="{.spec.nodeName}")
#cpu=$(kubectl top pod $pod_name 2>/dev/null | awk 'NR==2{print $2}')
#memory=$(kubectl top pod $pod_name 2>/dev/null | awk 'NR==2{print $3}')
pod_status=$(kubectl get pod $pod_name -o jsonpath="{.status.phase}")
if [ -n "$ready_status" ] && [ "$ready_status" = "false" ]; then
printf "Pod : %s$pod_name\nStatus : %s$pod_status\nMessage : $s$message\nReason : %s$reason\nHost IP : %s$host_ip\nNode Name : %s$node_name\n"
fi
done
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment