$npx -y skills add chaterm/terminal-skills --skill pod-managementKubernetes Pod 管理与调试
| 1 | # Pod 管理与调试 |
| 2 | |
| 3 | ## 概述 |
| 4 | Pod 生命周期管理、日志查看、exec 调试等技能。 |
| 5 | |
| 6 | ## Pod 查看 |
| 7 | |
| 8 | ```bash |
| 9 | # 列出 Pod |
| 10 | kubectl get pods |
| 11 | kubectl get pods -o wide # 详细信息 |
| 12 | kubectl get pods -A # 所有命名空间 |
| 13 | kubectl get pods -n namespace |
| 14 | kubectl get pods -l app=nginx # 按标签过滤 |
| 15 | kubectl get pods --field-selector status.phase=Running |
| 16 | |
| 17 | # Pod 详情 |
| 18 | kubectl describe pod pod-name |
| 19 | kubectl get pod pod-name -o yaml |
| 20 | kubectl get pod pod-name -o jsonpath='{.status.phase}' |
| 21 | ``` |
| 22 | |
| 23 | ## 日志查看 |
| 24 | |
| 25 | ```bash |
| 26 | # 查看日志 |
| 27 | kubectl logs pod-name |
| 28 | kubectl logs pod-name -c container-name # 指定容器 |
| 29 | kubectl logs pod-name --all-containers # 所有容器 |
| 30 | |
| 31 | # 实时跟踪 |
| 32 | kubectl logs -f pod-name |
| 33 | kubectl logs -f pod-name --since=1h # 最近1小时 |
| 34 | kubectl logs -f pod-name --tail=100 # 最后100行 |
| 35 | |
| 36 | # 上一个容器的日志(崩溃后) |
| 37 | kubectl logs pod-name --previous |
| 38 | |
| 39 | # 多 Pod 日志 |
| 40 | kubectl logs -l app=nginx # 按标签 |
| 41 | kubectl logs -l app=nginx --max-log-requests=10 |
| 42 | ``` |
| 43 | |
| 44 | ## 容器调试 |
| 45 | |
| 46 | ### exec 进入容器 |
| 47 | ```bash |
| 48 | # 进入容器 |
| 49 | kubectl exec -it pod-name -- /bin/bash |
| 50 | kubectl exec -it pod-name -- sh |
| 51 | kubectl exec -it pod-name -c container-name -- /bin/bash |
| 52 | |
| 53 | # 执行命令 |
| 54 | kubectl exec pod-name -- ls -la |
| 55 | kubectl exec pod-name -- cat /etc/hosts |
| 56 | kubectl exec pod-name -- env |
| 57 | ``` |
| 58 | |
| 59 | ### 调试容器 |
| 60 | ```bash |
| 61 | # 使用 debug 容器(K8s 1.18+) |
| 62 | kubectl debug pod-name -it --image=busybox --target=container-name |
| 63 | |
| 64 | # 复制 Pod 进行调试 |
| 65 | kubectl debug pod-name -it --copy-to=debug-pod --container=debug --image=busybox |
| 66 | |
| 67 | # 节点调试 |
| 68 | kubectl debug node/node-name -it --image=busybox |
| 69 | ``` |
| 70 | |
| 71 | ### 端口转发 |
| 72 | ```bash |
| 73 | # 转发到本地 |
| 74 | kubectl port-forward pod-name 8080:80 |
| 75 | kubectl port-forward pod-name 8080:80 --address 0.0.0.0 |
| 76 | |
| 77 | # 后台运行 |
| 78 | kubectl port-forward pod-name 8080:80 & |
| 79 | ``` |
| 80 | |
| 81 | ## Pod 生命周期 |
| 82 | |
| 83 | ### 创建 Pod |
| 84 | ```yaml |
| 85 | # pod.yaml |
| 86 | apiVersion: v1 |
| 87 | kind: Pod |
| 88 | metadata: |
| 89 | name: nginx-pod |
| 90 | labels: |
| 91 | app: nginx |
| 92 | spec: |
| 93 | containers: |
| 94 | - name: nginx |
| 95 | image: nginx:latest |
| 96 | ports: |
| 97 | - containerPort: 80 |
| 98 | resources: |
| 99 | requests: |
| 100 | memory: "64Mi" |
| 101 | cpu: "250m" |
| 102 | limits: |
| 103 | memory: "128Mi" |
| 104 | cpu: "500m" |
| 105 | livenessProbe: |
| 106 | httpGet: |
| 107 | path: / |
| 108 | port: 80 |
| 109 | initialDelaySeconds: 10 |
| 110 | periodSeconds: 5 |
| 111 | readinessProbe: |
| 112 | httpGet: |
| 113 | path: / |
| 114 | port: 80 |
| 115 | initialDelaySeconds: 5 |
| 116 | periodSeconds: 3 |
| 117 | ``` |
| 118 | |
| 119 | ```bash |
| 120 | kubectl apply -f pod.yaml |
| 121 | kubectl create -f pod.yaml |
| 122 | ``` |
| 123 | |
| 124 | ### 删除 Pod |
| 125 | ```bash |
| 126 | kubectl delete pod pod-name |
| 127 | kubectl delete pod pod-name --force --grace-period=0 # 强制删除 |
| 128 | kubectl delete pods -l app=nginx # 按标签删除 |
| 129 | kubectl delete pods --all -n namespace # 删除所有 |
| 130 | ``` |
| 131 | |
| 132 | ### Pod 状态 |
| 133 | ```bash |
| 134 | # 查看 Pod 状态 |
| 135 | kubectl get pod pod-name -o jsonpath='{.status.phase}' |
| 136 | |
| 137 | # 等待 Pod 就绪 |
| 138 | kubectl wait --for=condition=Ready pod/pod-name --timeout=60s |
| 139 | |
| 140 | # 查看事件 |
| 141 | kubectl get events --field-selector involvedObject.name=pod-name |
| 142 | ``` |
| 143 | |
| 144 | ## 资源管理 |
| 145 | |
| 146 | ### 查看资源使用 |
| 147 | ```bash |
| 148 | # Pod 资源使用 |
| 149 | kubectl top pods |
| 150 | kubectl top pods -n namespace |
| 151 | kubectl top pod pod-name --containers |
| 152 | |
| 153 | # 按资源排序 |
| 154 | kubectl top pods --sort-by=cpu |
| 155 | kubectl top pods --sort-by=memory |
| 156 | ``` |
| 157 | |
| 158 | ### 资源配置 |
| 159 | ```yaml |
| 160 | resources: |
| 161 | requests: |
| 162 | memory: "64Mi" |
| 163 | cpu: "250m" |
| 164 | limits: |
| 165 | memory: "128Mi" |
| 166 | cpu: "500m" |
| 167 | ``` |
| 168 | |
| 169 | ## 常见场景 |
| 170 | |
| 171 | ### 场景 1:排查 Pod 启动失败 |
| 172 | ```bash |
| 173 | # 1. 查看 Pod 状态 |
| 174 | kubectl get pod pod-name -o wide |
| 175 | |
| 176 | # 2. 查看事件 |
| 177 | kubectl describe pod pod-name | grep -A 20 Events |
| 178 | |
| 179 | # 3. 查看日志 |
| 180 | kubectl logs pod-name |
| 181 | kubectl logs pod-name --previous # 如果容器重启 |
| 182 | |
| 183 | # 4. 检查镜像 |
| 184 | kubectl get pod pod-name -o jsonpath='{.spec.containers[*].image}' |
| 185 | ``` |
| 186 | |
| 187 | ### 场景 2:排查 CrashLoopBackOff |
| 188 | ```bash |
| 189 | # 1. 查看退出原因 |
| 190 | kubectl describe pod pod-name | grep -A 5 "Last State" |
| 191 | |
| 192 | # 2. 查看上一次日志 |
| 193 | kubectl logs pod-name --previous |
| 194 | |
| 195 | # 3. 检查资源限制 |
| 196 | kubectl describe pod pod-name | grep -A 10 "Limits" |
| 197 | |
| 198 | # 4. 检查探针配置 |
| 199 | kubectl get pod pod-name -o yaml | grep -A 10 "livenessProbe" |
| 200 | ``` |
| 201 | |
| 202 | ### 场景 3:临时运行调试 Pod |
| 203 | ```bash |
| 204 | # 运行临时 Pod |
| 205 | kubectl run debug --rm -it --image=busybox -- sh |
| 206 | kubectl run debug --rm -it --image=nicolaka/netshoot -- bash |
| 207 | |
| 208 | # 在特定节点运行 |
| 209 | kubectl run debug --rm -it --image=busybox --overrides='{"spec":{"nodeName":"node-name"}}' -- sh |
| 210 | ``` |
| 211 | |
| 212 | ### 场景 4:复制文件 |
| 213 | ```bash |
| 214 | # 从 Pod 复制到本地 |
| 215 | kubectl cp pod-name:/path/to/file ./local-file |
| 216 | kubectl cp namespace/pod-name:/path/to/file ./local-file |
| 217 | |
| 218 | # 从本地复制到 Pod |
| 219 | kubectl cp ./local-file pod-name:/path/to/file |
| 220 | ``` |
| 221 | |
| 222 | ## 故障排查 |
| 223 | |
| 224 | | 状态 | 可能原因 | 排查方法 | |
| 225 | |------|----------|----------| |
| 226 | | Pending | 资源不足/调度问题 | `kubectl describe pod` 查看事件 | |
| 227 | | ImagePullBackOff | 镜像拉取失败 | 检查镜像名、仓库认证 | |
| 228 | | CrashLoopBackOff | 应用崩溃 | `kubectl logs --previous` | |
| 229 | | OOMKilled | 内存超限 | 增加 memory limits | |
| 230 | | Evicted | 节点资源不足 | 检查节点资源、清理 Pod | |
| 231 | | Unknown | 节点失联 | 检查节点状态 | |