From 2793bce2391cf36417053994b763ef01e9bf9d2e Mon Sep 17 00:00:00 2001 From: Timmy Date: Wed, 1 Apr 2026 17:06:11 +0800 Subject: [PATCH] improve: make check_access.sh more readable with organized sections --- cluster/check_access.sh | 134 ++++++++++++++++++++++++++++++---------- 1 file changed, 101 insertions(+), 33 deletions(-) diff --git a/cluster/check_access.sh b/cluster/check_access.sh index d47c9b2..a1532b2 100755 --- a/cluster/check_access.sh +++ b/cluster/check_access.sh @@ -1,49 +1,117 @@ #!/usr/bin/env bash -# 用途:快速查看目前所有服務「怎麼連」 -# 不修改任何資源,純顯示資訊 +# 用途:查看所有服務的存取方式(網址、IP、Port) set -euo pipefail -echo -echo "=== Pods ===" -kubectl get pods -o wide +# 顏色定義 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' +NC='\033[0m' # No Color -echo -echo "=== Services ===" -kubectl get svc +echo "==========================================" +echo " K3s 服務存取清單" +echo "==========================================" +echo "" -echo -echo "=== Node IPs ===" -kubectl get nodes -o wide | awk 'NR>1 {print $1 "\t" $6}' +# 節點 IP +echo "🖥️ 節點 IP" +echo "------------------------------------------" +NODE_IPS=$(kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}') +echo "$NODE_IPS" +echo "" -echo -echo "=== NodePort Access ===" +# Ingress (HTTP/HTTPS 服務) +echo "🌐 Web 服務 (Ingress)" +echo "------------------------------------------" +INGRESS_COUNT=$(kubectl get ingress --no-headers 2>/dev/null | wc -l) -NODE_IPS=$(kubectl get nodes -o jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")].address}{" "}{end}') -kubectl get svc --no-headers | awk '$2=="NodePort"' | while read -r name type clusterip external ports age; do - PORTS=$(echo "$ports" | tr ',' '\n' | awk -F: '{print $NF}') - for ip in $NODE_IPS; do - for p in $PORTS; do - echo "- $ip:$p ($name)" - done - done -done +if [ $INGRESS_COUNT -eq 0 ]; then + echo -e "${YELLOW}無 Ingress 服務${NC}" +else + kubectl get ingress --no-headers 2>/dev/null | while read -r name class hosts address ports age; do + # 取得第一個 host + FIRST_HOST=$(kubectl get ingress "$name" -o jsonpath='{.spec.rules[0].host}' 2>/dev/null) -echo -echo "=== Ingress (HTTP/HTTPS) ===" -kubectl get ingress --no-headers | while read -r name class hosts address ports age; do - echo "- $name" - kubectl get ingress "$name" -o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}' | while read -r host; do - TLS=$(kubectl get ingress "$name" -o jsonpath='{.spec.tls}') + # 檢查是否有 TLS + TLS=$(kubectl get ingress "$name" -o jsonpath='{.spec.tls}' 2>/dev/null) if [[ -n "$TLS" && "$TLS" != "[]" ]]; then - echo " https://$host" + PROTO="https" else - echo " http://$host" + PROTO="http" fi + + echo -e "${GREEN}→ $name${NC}" + echo " $PROTO://$FIRST_HOST" + + # 顯示所有 hosts + ALL_HOSTS=$(kubectl get ingress "$name" -o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}' 2>/dev/null) + HOST_COUNT=$(echo "$ALL_HOSTS" | wc -l) + if [ $HOST_COUNT -gt 1 ]; then + echo " 其他域名: $(echo "$ALL_HOSTS" | tail -n +2 | tr '\n' ',' | sed 's/,$//')" + fi + echo "" done +fi + +# Services +echo "🔧 內部服務 (ClusterIP)" +echo "------------------------------------------" +kubectl get svc --no-headers 2>/dev/null | grep "ClusterIP" | while read -r name type clusterip externalip ports age; do + if [[ ! "$name" =~ "kube-" ]] && [[ ! "$name" =~ "svc-" ]]; then + echo -e "$name ${CYAN}→${NC} $clusterip $ports" + fi done +echo "" -echo -echo "=== PVC ===" -kubectl get pvc +# NodePort (如果有) +echo "🔌 NodePort 服務" +echo "------------------------------------------" +NODEPORT_COUNT=$(kubectl get svc --no-headers 2>/dev/null | grep -c "NodePort" || true) +if [ $NODEPORT_COUNT -eq 0 ]; then + echo -e "${YELLOW}無 NodePort 服務${NC}" +else + kubectl get svc --no-headers 2>/dev/null | grep "NodePort" | while read -r name type clusterip externalip ports age; do + echo -e "${GREEN}→ $name${NC}" + echo " Ports: $ports" + echo "$NODE_IPS" | while read -r node ip; do + echo " $ip:$ports" + done + echo "" + done +fi + +# LoadBalancer (如果有) +echo "⚖️ LoadBalancer 服務" +echo "------------------------------------------" +LB_COUNT=$(kubectl get svc --no-headers 2>/dev/null | grep -c "LoadBalancer" || true) + +if [ $LB_COUNT -eq 0 ]; then + echo -e "${YELLOW}無 LoadBalancer 服務${NC}" +else + kubectl get svc --no-headers 2>/dev/null | grep "LoadBalancer" | while read -r name type clusterip externalip ports age; do + echo -e "${GREEN}→ $name${NC}" + echo " External: $externalip" + echo " Ports: $ports" + echo "" + done +fi + +# Pod 概況 +echo "📦 Pod 概況" +echo "------------------------------------------" +POD_TOTAL=$(kubectl get pods -A --no-headers 2>/dev/null | wc -l) +POD_RUNNING=$(kubectl get pods -A --no-headers 2>/dev/null | grep -c "Running" || true) +POD_ISSUE=$((POD_TOTAL - POD_RUNNING)) + +echo "總計: $POD_TOTAL 個 Pods" +echo -e " ${GREEN}Running: $POD_RUNNING${NC}" +if [ $POD_ISSUE -gt 0 ]; then + echo -e " ${YELLOW}非 Running: $POD_ISSUE${NC}" +fi +echo "" + +echo "=========================================="