编辑
2025-02-18
云计算-Kubernetes
00
请注意,本文编写于 416 天前,最后修改于 0 天前,其中某些信息可能已经过时。

目录

集群信息
Pod
Pod 健康检查
Service
Deployment
Statefulset
ConfigMap和Secret
namespace
资源使用情况
网络
持久卷(PV)和持久卷声明(PVC)
节点
资源配额和限制
自定义资源定义(CRD)
资源伸缩和自动伸缩
作业和CronJob
容量
Ingress和服务网络
Pod 网络故障排除
配置和资源验证
RBAC和安全性
服务账户诊断
清空节点和解除封锁
资源清理
Pod 亲和性和反亲和性
Pod 安全策略(PSP)
事件
节点故障排除
临时容器(Kubernetes 1.18+)
资源指标(需要指标服务器)
kubelet
Kubeconfig 和上下文
Pod 安全标准(PodSecurity 准入控制器)
Pod 中断预算 (PDB)
资源锁(如果使用资源锁)
服务端点和DNS
Pod 优先级和抢占
Pod 开销(Kubernetes 1.18+)
存储卷快照(如果使用存储卷快照)
资源反序列化
节点污点
更改和验证WebHook配置
Pod 网络策略
节点条件(Kubernetes 1.17+)
节点操作系统信息

kubectl 是 Kubernetes 集群管理的核心命令行工具,掌握常用命令是日常运维与故障排查的基础。本文整理了一份全面的 kubectl 命令速查清单,涵盖集群信息查看(versioncluster-infoget nodes)、Pod 管理(get podsdescribe podlogsexec、健康检查)、Service/Deployment/StatefulSet 操作、ConfigMap/Secret 管理、资源使用监控(top podtop nodes)、网络诊断(IP 查询、网络策略、DNS 解析、curl/traceroute 调试)、PV/PVC 管理、节点维护(drainuncordon)、资源伸缩(scaleautoscale、HPA)、Job/CronJob、Ingress、RBAC 安全、事件查看、临时容器调试、kubelet 日志、kubeconfig 上下文切换、优先级类、存储卷快照、节点污点与条件、Webhook 配置等。

本文适用于 Kubernetes 运维人员与开发者的日常参考。

集群信息

shell
# 显示 Kubernetes 版本 kubectl version # 显示集群信息 kubectl cluster-info # 列出集群中的所有节点 kubectl get nodes # 查看一个具体的节点详情 kubectl describe node <node-name> # 列出所有命名空间 kubectl get namespaces # 列出所有命名空间中的所有 pod kubectl get pods --all-namespaces kubectl get pod -A

Pod

shell
# 列出特定命名空间中的 pod kubectl get pods -n <namespace> # 查看一个 Pod 详情 kubectl describe pod <pod-name> -n <namespace> # 查看 Pod 日志 kubectl logs <pod-name> -n <namespace> # 尾部 Pod 日志 kubectl logs -f <pod-name> -n <namespace> # 在 pod 中执行命令 kubectl exec -it <pod-name> -n <namespace> -# <command>

Pod 健康检查

shell
# 检查 Pod 准备情况 kubectl get pods <pod-name> -n <namespace> -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' # 检查 Pod 事件 kubectl get events -n <namespace> --field-selector involvedObject.name=<pod-name>

Service

shell
# 列出命名空间中的所有服务 kubectl get svc -n <namespace> # 查看一个服务详情 kubectl describe svc <service-name> -n <namespace>

Deployment

shell
# 列出命名空间中的所有Deployment kubectl get deployments -n <namespace> # 查看一个Deployment详情 kubectl describe deployment <deployment-name> -n <namespace> # 查看滚动发布状态 kubectl rollout status deployment/<deployment-name> -n <namespace> # 查看滚动发布历史记录 kubectl rollout history deployment/<deployment-name> -n <namespace>

Statefulset

shell
# 列出命名空间中的所有 StatefulSet kubectl get statefulsets -n <namespace> # 查看一个 StatefulSet详情 kubectl describe statefulset <statefulset-name> -n <namespace>

ConfigMap和Secret

shell
# 列出命名空间中的 ConfigMap kubectl get configmaps -n <namespace> # 查看一个ConfigMap详情 kubectl describe configmap <configmap-name> -n <namespace> # 列出命名空间中的 Secret kubectl get secrets -n <namespace> # 查看一个Secret详情 kubectl describe secret <secret-name> -n <namespace>

namespace

shell
# 查看一个命名空间详情 kubectl describe namespace <namespace-name>

资源使用情况

shell
# 检查 pod 的资源使用情况 kubectl top pod <pod-name> -n <namespace> # 检查节点资源使用情况 kubectl top nodes

网络

shell
# 显示命名空间中 Pod 的 IP 地址 kubectl get pods -n <namespace> -o custom-columns=POD:metadata.name,IP:status.podIP --no-headers # 列出命名空间中的所有网络策略 kubectl get networkpolicies -n <namespace> # 查看一个网络策略详情 kubectl describe networkpolicy <network-policy-name> -n <namespace>

持久卷(PV)和持久卷声明(PVC)

shell
# 列出PV kubectl get pv # 查看一个PV详情 kubectl describe pv <pv-name> # 列出命名空间中的 PVC kubectl get pvc -n <namespace> # 查看PVC详情 kubectl describe pvc <pvc-name> -n <namespace>

节点

shell
# 获取特定节点上运行的 Pod 列表 kubectl get pods --field-selector spec.nodeName=<node-name> -n <namespace>

资源配额和限制

shell
# 列出命名空间中的资源配额 kubectl get resourcequotas -n <namespace> # 查看一个资源配额详情 kubectl describe resourcequota <resource-quota-name> -n <namespace>

自定义资源定义(CRD)

shell
# 列出命名空间中的自定义资源 kubectl get <custom-resource-name> -n <namespace> # 查看自定义资源详情 kubectl describe <custom-resource-name> <custom-resource-instance-name> -n <namespace>

资源伸缩和自动伸缩

shell
# Deployment伸缩 kubectl scale deployment <deployment-name> --replicas=<replica-count> -n <namespace> # 设置Deployment的自动伸缩 kubectl autoscale deployment <deployment-name> --min=<min-pods> --max=<max-pods> --cpu-percent=<cpu-percent> -n <namespace> # 检查水平伸缩器状态 kubectl get hpa -n <namespace>

作业和CronJob

shell
# 列出命名空间中的所有作业 kubectl get jobs -n <namespace> # 查看一份工作详情 kubectl describe job <job-name> -n <namespace> # 列出命名空间中的所有 cron 作业 kubectl get cronjobs -n <namespace> # 查看一个 cron 作业详情 kubectl describe cronjob <cronjob-name> -n <namespace>

容量

shell
# 列出按容量排序的持久卷 (PV) kubectl get pv --sort-by=.spec.capacity.storage # 查看PV回收策略 kubectl get pv <pv-name> -o=jsonpath='{.spec.persistentVolumeReclaimPolicy}' # 列出所有存储类别 kubectl get storageclasses

Ingress和服务网络

shell
# 列出命名空间中的所有Ingress kubectl get ingress -n <namespace> # 查看一个Ingress详情 kubectl describe ingress <ingress-name> -n <namespace> # 列出命名空间中的所有 VirtualServices (Istio) kubectl get virtualservices -n <namespace> # 查看一个 VirtualService (Istio)详情 kubectl describe virtualservice <virtualservice-name> -n <namespace>

Pod 网络故障排除

shell
# 运行网络诊断 Pod(例如 busybox)进行调试 kubectl run -it --rm --restart=Never --image=busybox net-debug-pod -# /bin/sh # 测试从 Pod 到特定端点的连接 kubectl exec -it <pod-name> -n <namespace> -# curl <endpoint-url> # 跟踪从一个 Pod 到另一个 Pod 的网络路径 kubectl exec -it <source-pod-name> -n <namespace> -# traceroute <destination-pod-ip> # 检查 Pod 的 DNS 解析 kubectl exec -it <pod-name> -n <namespace> -# nslookup <domain-name>

配置和资源验证

shell
# 验证 Kubernetes YAML 文件而不应用 kubectl apply --dry-run=client -f <yaml-file> # 验证 pod 的安全上下文和功能 kubectl auth can-i list pods --as=system:serviceaccount:<namespace>:<serviceaccount-name>

RBAC和安全性

shell
# 列出命名空间中的角色和角色绑定 kubectl get roles,rolebindings -n <namespace> # 查看角色或角色绑定详情 kubectl describe role <role-name> -n <namespace>

服务账户诊断

shell
# 列出命名空间中的服务帐户 kubectl get serviceaccounts -n <namespace> # 查看一个服务帐户详情 kubectl describe serviceaccount <serviceaccount-name> -n <namespace>

清空节点和解除封锁

shell
# 清空节点以进行维护 kubectl drain <node-name> --ignore-daemonsets # 解除对节点的封锁 kubectl uncordon <node-name>

资源清理

shell
# 强制删除 pod(不推荐) kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --force

Pod 亲和性和反亲和性

shell
# 列出 pod 的 pod 亲和性规则 kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity}' # 列出 pod 的 pod 反亲和性规则 kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity.podAntiAffinity}'

Pod 安全策略(PSP)

shell
# 列出所有 Pod 安全策略(如果启用) kubectl get psp

事件

shell
# 查看最近的集群事件 kubectl get events --sort-by=.metadata.creationTimestamp # 按特定命名空间过滤事件 kubectl get events -n <namespace>

节点故障排除

shell
# 检查节点情况 kubectl describe node <node-name> | grep Conditions -A5 # 列出节点容量和可分配资源 kubectl describe node <node-name> | grep -E "Capacity|Allocatable"

临时容器(Kubernetes 1.18+)

shell
# 运行临时调试容器 kubectl debug -it <pod-name> -n <namespace> --image=<debug-image> -# /bin/sh

资源指标(需要指标服务器)

shell
# 获取 Pod 的 CPU 和内存使用情况 kubectl top pod -n <namespace>

kubelet

shell
# 查看节点上的kubelet日志 kubectl logs -n kube-system kubelet-<node-name>

Kubeconfig 和上下文

shell
# 列出可用的上下文 kubectl config get-contexts # 切换到不同的上下文 kubectl config use-context <context-name>

Pod 安全标准(PodSecurity 准入控制器)

shell
# 列出 PodSecurityPolicy (PSP) 违规行为 kubectl get psp -A | grep -vE 'NAME|REVIEWED'

Pod 中断预算 (PDB)

shell
# 列出命名空间中的所有 PDB kubectl get pdb -n <namespace> # 查看一个PDB详情 kubectl describe pdb <pdb-name> -n <namespace>

资源锁(如果使用资源锁)

shell
# 列出命名空间中的资源锁 kubectl get resourcelocks -n <namespace>

服务端点和DNS

shell
# 列出服务的服务端点 kubectl get endpoints <service-name> -n <namespace> # 检查 Pod 中的 DNS 配置 kubectl exec -it <pod-name> -n <namespace> -# cat /etc/resolv.conf

Pod 优先级和抢占

shell
# 列出优先级 kubectl get priorityclasses

Pod 开销(Kubernetes 1.18+)

shell
# 列出 pod 中的开销 kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.overhead}'

存储卷快照(如果使用存储卷快照)

shell
# 列出存储卷快照 kubectl get volumesnapshot -n <namespace> # 查看存储卷快照详情 kubectl describe volumesnapshot <snapshot-name> -n <namespace>

资源反序列化

shell
# 反序列化并打印 Kubernetes 资源 kubectl get <resource-type> <resource-name> -n <namespace> -o=json

节点污点

shell
# 列出节点污点 kubectl describe node <node-name> | grep Taints

更改和验证WebHook配置

shell
# 列出变异 webhook 配置 kubectl get mutatingwebhookconfigurations # 列出验证 Webhook 配置 kubectl get validatingwebhookconfigurations

Pod 网络策略

shell
# 列出命名空间中的 pod 网络策略 kubectl get networkpolicies -n <namespace>

节点条件(Kubernetes 1.17+)

shell
# 定义查询输出 kubectl get nodes -o custom-columns=NODE:.metadata.name,READY:.status.conditions[?(@.type=="Ready")].status -l 'node-role.kubernetes.io/worker='

节点操作系统信息

shell
# 获取节点的操作系统信息 kubectl get node <node-name> -o jsonpath='{.status.nodeInfo.osImage}'
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Odboy

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC 4.0 BY-SA 许可协议。转载请注明出处!