Files

40 lines
1.7 KiB
Markdown
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.
---
name: "k8s-status"
description: "巡检 K3s/K8s 集群:kubectl 读取节点状态、资源占用、Pod 分布、异常项,解析输出核心字段。"
---
# K8s/K3s 集群状态巡检
通过 kubectl 读取多节点集群运行状态,解析核心字段并输出结构化报告。
适用场景:用户问「集群/节点/服务现在什么状态」「帮我看看机器跑得怎么样」。
## 用法
```bash
# vps1(control-plane,kubectl 直接可用)
bash scripts/k8s-status.sh
# 指定 kubeconfig(从任意机器远程跑)
KUBECONFIG=/etc/rancher/k3s/k3s.yaml bash scripts/k8s-status.sh
```
环境变量:
- `KUBECTL` — kubectl 路径,默认 `kubectl`
- `KUBECONFIG` — kubeconfig 路径,默认当前上下文
## 输出内容
1. **节点状态全量** — `kubectl get nodes -o wide`
2. **节点健康解析** — jsonpath 精确提取:名称/Ready状态/角色(control-plane|worker)/版本/内网IP/公网IP/OS/内核/运行时
3. **资源占用** — `kubectl top nodes`(metrics-server 不可用时自动跳过)
4. **Pod 总数 & 节点分布**
5. **异常 Pod** — 非 Running/Completed 状态列表
6. **Warning 事件** — 最近 5 条
## 注意事项
- 用 jsonpath + tab 分隔提取字段,避免 `-o wide` 空格切分错位(OS-IMAGE 含空格会坑 awk)
- 角色判定:`node-role.kubernetes.io/control-plane` label 为 true → control-plane,否则 worker
- metrics-server 对部分节点返回 `<unknown>`:通常是跨节点网络问题(如 flannel VXLAN 不通),不是脚本 bug
- Warning 事件 `InvalidDiskCapacity`(invalid capacity 0 on image filesystem)常见于容器内 kubelet 或磁盘识别异常,需单独排查