k8s_status_access.sh
· 1.6 KiB · Bash
Неформатований
#!/usr/bin/env bash
# 用途:查看 Kubernetes 服務狀態與實際連線方式
# 包含:Pod、Service(NodePort)、Ingress(HTTP/HTTPS)、PVC
# 適用:k3s + Traefik
set -euo pipefail
echo
echo "=== Pods ==="
kubectl get pods -o wide
echo
echo "=== Services ==="
kubectl get svc
echo
echo "=== Node IPs ==="
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{" "}{end}{"\n"}{end}'
echo
echo "=== NodePort Access ==="
NODE_IPS=$(kubectl get nodes -o jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}')
NODEPORT_SVCS=$(kubectl get svc -o jsonpath='{range .items[?(@.spec.type=="NodePort")]}{.metadata.name}{"\t"}{.spec.ports[0].nodePort}{"\n"}{end}')
if [ -z "$NODEPORT_SVCS" ]; then
echo "(無 NodePort 服務)"
else
for ip in $NODE_IPS; do
while IFS=$'\t' read -r svc port; do
echo "- $ip:$port ($svc)"
done <<< "$NODEPORT_SVCS"
done
fi
echo
echo "=== Ingress (Access URLs) ==="
INGRESS_LIST=$(kubectl get ingress -o name 2>/dev/null || true)
if [ -z "$INGRESS_LIST" ]; then
echo "(無 Ingress)"
else
for ing in $INGRESS_LIST; do
NAME=$(kubectl get "$ing" -o jsonpath='{.metadata.name}')
HOSTS=$(kubectl get "$ing" -o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}')
TLS=$(kubectl get "$ing" -o jsonpath='{.spec.tls}')
echo "- $NAME"
while read -r host; do
[ -z "$host" ] && continue
if [ -n "$TLS" ]; then
echo " https://$host"
else
echo " http://$host"
fi
done <<< "$HOSTS"
done
fi
echo
echo "=== PVC ==="
kubectl get pvc
| 1 | #!/usr/bin/env bash |
| 2 | # 用途:查看 Kubernetes 服務狀態與實際連線方式 |
| 3 | # 包含:Pod、Service(NodePort)、Ingress(HTTP/HTTPS)、PVC |
| 4 | # 適用:k3s + Traefik |
| 5 | |
| 6 | set -euo pipefail |
| 7 | |
| 8 | echo |
| 9 | echo "=== Pods ===" |
| 10 | kubectl get pods -o wide |
| 11 | |
| 12 | echo |
| 13 | echo "=== Services ===" |
| 14 | kubectl get svc |
| 15 | |
| 16 | echo |
| 17 | echo "=== Node IPs ===" |
| 18 | kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{" "}{end}{"\n"}{end}' |
| 19 | |
| 20 | echo |
| 21 | echo "=== NodePort Access ===" |
| 22 | NODE_IPS=$(kubectl get nodes -o jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}') |
| 23 | NODEPORT_SVCS=$(kubectl get svc -o jsonpath='{range .items[?(@.spec.type=="NodePort")]}{.metadata.name}{"\t"}{.spec.ports[0].nodePort}{"\n"}{end}') |
| 24 | |
| 25 | if [ -z "$NODEPORT_SVCS" ]; then |
| 26 | echo "(無 NodePort 服務)" |
| 27 | else |
| 28 | for ip in $NODE_IPS; do |
| 29 | while IFS=$'\t' read -r svc port; do |
| 30 | echo "- $ip:$port ($svc)" |
| 31 | done <<< "$NODEPORT_SVCS" |
| 32 | done |
| 33 | fi |
| 34 | |
| 35 | echo |
| 36 | echo "=== Ingress (Access URLs) ===" |
| 37 | INGRESS_LIST=$(kubectl get ingress -o name 2>/dev/null || true) |
| 38 | |
| 39 | if [ -z "$INGRESS_LIST" ]; then |
| 40 | echo "(無 Ingress)" |
| 41 | else |
| 42 | for ing in $INGRESS_LIST; do |
| 43 | NAME=$(kubectl get "$ing" -o jsonpath='{.metadata.name}') |
| 44 | HOSTS=$(kubectl get "$ing" -o jsonpath='{range .spec.rules[*]}{.host}{"\n"}{end}') |
| 45 | TLS=$(kubectl get "$ing" -o jsonpath='{.spec.tls}') |
| 46 | |
| 47 | echo "- $NAME" |
| 48 | while read -r host; do |
| 49 | [ -z "$host" ] && continue |
| 50 | if [ -n "$TLS" ]; then |
| 51 | echo " https://$host" |
| 52 | else |
| 53 | echo " http://$host" |
| 54 | fi |
| 55 | done <<< "$HOSTS" |
| 56 | done |
| 57 | fi |
| 58 | |
| 59 | echo |
| 60 | echo "=== PVC ===" |
| 61 | kubectl get pvc |
| 62 | |
| 63 |