《kubectl 常用命令》

《kubectl 常用命令》

kubectl 是与 Kubernetes 集群交互的主要命令行工具。本文整理工作中最常用的命令。

1 Pod 相关

# 查询指定 namespace 的 pod 信息 kubectl get pods -n <namespace> -o wide | grep <pod-name> # 查看 pod 详情(事件、调度情况) kubectl describe pod <pod-name> -n <namespace> # 查看 pod 日志 kubectl logs -f <pod-name> -n <namespace> # 进入容器执行命令 kubectl exec -it <pod-name> -n <namespace> -- bash # 删除 pod kubectl delete pod <pod-name> -n <namespace>

-o wide 会额外输出节点 IP、Pod IP、所在 Node 等字段,排查时非常有用。

2 工作负载与资源

# Deployment kubectl get deployment -n <namespace> kubectl scale deployment <name> --replicas=3 -n <namespace> # Service / Endpoints kubectl get svc -n <namespace> kubectl get endpoints -n <namespace> # ConfigMap / Secret kubectl get configmap -n <namespace> kubectl get secret -n <namespace> # 查看资源占用 kubectl top pod -n <namespace>

3 节点与集群

kubectl get nodes -o wide kubectl get events --sort-by='.lastTimestamp' -n <namespace>

4 配置与上下文

# 查看当前上下文与集群 kubectl config view kubectl config get-contexts # 切换上下文 kubectl config use-context <context-name>

5 小技巧

  • grep 过滤 + -o wide 是确认外部访问、节点归属的最快方式,例如 kubectl get pods -n ns -o wide | grep redis。
  • 事件(events)是排障的第一入口,优先查看 Failed to pull image、Back-off restarting failed container 等关键事件。
阅读 — · 全站 —
🎸 我的歌单 0 首