Files
2025-11-28 13:49:45 +08:00

174 lines
5.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 灵境万象系统 - Kubernetes 状态检查脚本
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
NAMESPACE="${K8S_NAMESPACE:-ljwx-system}"
print_header() {
echo ""
echo -e "${BLUE}=========================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}=========================================${NC}"
}
print_info() {
echo -e "${BLUE}ℹ ${NC}$1"
}
print_success() {
echo -e "${GREEN}✅${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
echo -e "${BLUE}=========================================${NC}"
echo -e "${BLUE}📊 灵境万象系统 - K8s 状态检查${NC}"
echo -e "${BLUE}=========================================${NC}"
echo ""
# 检查命名空间是否存在
if ! kubectl get namespace $NAMESPACE &> /dev/null; then
print_warning "命名空间 $NAMESPACE 不存在"
exit 1
fi
print_success "命名空间 $NAMESPACE 存在"
echo ""
# Pods 状态
print_header "Pods 状态"
kubectl get pods -n $NAMESPACE -o wide
echo ""
# 统计 Pod 状态
TOTAL_PODS=$(kubectl get pods -n $NAMESPACE --no-headers 2>/dev/null | wc -l | tr -d ' ')
RUNNING_PODS=$(kubectl get pods -n $NAMESPACE --field-selector=status.phase=Running --no-headers 2>/dev/null | wc -l | tr -d ' ')
READY_PODS=$(kubectl get pods -n $NAMESPACE -o json 2>/dev/null | jq '[.items[] | select(.status.conditions[] | select(.type=="Ready" and .status=="True"))] | length')
print_info "Pod 统计:"
echo " 总计: $TOTAL_PODS"
echo " 运行中: $RUNNING_PODS"
echo " 就绪: $READY_PODS"
echo ""
# Services 状态
print_header "Services"
kubectl get svc -n $NAMESPACE
echo ""
# Deployments 状态
print_header "Deployments"
kubectl get deployments -n $NAMESPACE
echo ""
# StatefulSets 状态
print_header "StatefulSets"
kubectl get statefulsets -n $NAMESPACE 2>/dev/null || echo " 没有 StatefulSets"
echo ""
# PVC 状态
print_header "PersistentVolumeClaims"
kubectl get pvc -n $NAMESPACE 2>/dev/null || echo " 没有 PVCs"
echo ""
# Ingress 状态
print_header "Ingress"
kubectl get ingress -n $NAMESPACE 2>/dev/null || echo " 没有 Ingress"
echo ""
# 最近事件
print_header "最近事件"
kubectl get events -n $NAMESPACE --sort-by='.lastTimestamp' | tail -10
echo ""
# 资源使用情况
print_header "资源使用情况"
if command -v kubectl-top &> /dev/null || kubectl top nodes &> /dev/null 2>&1; then
echo "节点资源:"
kubectl top nodes 2>/dev/null || echo " 无法获取节点资源信息"
echo ""
echo "Pod 资源:"
kubectl top pods -n $NAMESPACE 2>/dev/null || echo " 无法获取 Pod 资源信息"
else
print_warning "未安装 metrics-server,无法显示资源使用情况"
fi
echo ""
# 健康检查
print_header "健康检查"
# 检查不健康的 Pods
UNHEALTHY_PODS=$(kubectl get pods -n $NAMESPACE -o json | jq -r '.items[] | select(.status.phase != "Running" or ([.status.conditions[] | select(.type == "Ready" and .status != "True")] | length) > 0) | .metadata.name' 2>/dev/null)
if [ -z "$UNHEALTHY_PODS" ]; then
print_success "所有 Pods 健康"
else
print_warning "发现不健康的 Pods:"
echo "$UNHEALTHY_PODS" | sed 's/^/ - /'
echo ""
print_info "查看详细信息:"
echo "$UNHEALTHY_PODS" | while read pod; do
if [ -n "$pod" ]; then
echo " kubectl describe pod $pod -n $NAMESPACE"
echo " kubectl logs $pod -n $NAMESPACE"
fi
done
fi
echo ""
# 重启次数检查
print_header "重启次数检查"
RESTART_PODS=$(kubectl get pods -n $NAMESPACE -o json | jq -r '.items[] | select(([.status.containerStatuses[]? | .restartCount] | add // 0) > 0) | "\(.metadata.name): \(([.status.containerStatuses[]? | .restartCount] | add))"' 2>/dev/null)
if [ -z "$RESTART_PODS" ]; then
print_success "没有 Pod 发生重启"
else
print_warning "以下 Pods 有重启记录:"
echo "$RESTART_PODS" | sed 's/^/ /'
fi
echo ""
# 访问信息
print_header "访问信息"
INGRESS_INFO=$(kubectl get ingress -n $NAMESPACE -o json 2>/dev/null | jq -r '.items[] | .spec.rules[]? | " http://\(.host)\(.http.paths[].path)"' 2>/dev/null)
if [ -n "$INGRESS_INFO" ]; then
echo "应用访问地址:"
echo "$INGRESS_INFO"
else
print_info "未配置 Ingress"
echo " 使用 port-forward 访问服务:"
echo " kubectl port-forward svc/ljwx-admin 8080:80 -n $NAMESPACE"
echo " kubectl port-forward svc/ljwx-boot 9998:9998 -n $NAMESPACE"
fi
echo ""
# 快捷命令
print_header "常用命令"
echo "查看日志:"
echo " kubectl logs -f deployment/ljwx-boot -n $NAMESPACE"
echo " kubectl logs -f deployment/ljwx-bigscreen -n $NAMESPACE"
echo ""
echo "进入容器:"
echo " kubectl exec -it deployment/ljwx-boot -n $NAMESPACE -- /bin/sh"
echo ""
echo "端口转发:"
echo " kubectl port-forward svc/ljwx-admin 8080:80 -n $NAMESPACE"
echo ""
echo "扩缩容:"
echo " kubectl scale deployment/ljwx-boot --replicas=3 -n $NAMESPACE"
echo ""
echo "重启服务:"
echo " kubectl rollout restart deployment/ljwx-boot -n $NAMESPACE"
echo ""