Kubernetes 100个常用命令

这篇文章是关于使用 Kubectl 进行 Kubernetes 诊断的指南。

列出了 100 个 Kubectl 命令,这些命令对于诊断 Kubernetes 集群中的问题非常有用。这些问题包括但不限于:

• 集群信息

• Pod 诊断

• 服务诊断

• 部署诊断

• 网络诊断

• 持久卷和持久卷声明诊断

• 资源使用情况

• 安全和授权

• 节点故障排除

• 其他诊断命令:文章还提到了许多其他命令,如资源扩展和自动扩展、作业和定时作业诊断、Pod 亲和性和反亲和性规则、RBAC 和安全、服务账号诊断、节点排空和取消排空、资源清理等。

集群信息:

  1. 显示 Kubernetes 版本:kubectl version

  2. 显示集群信息:kubectl cluster-info

  3. 列出集群中的所有节点:kubectl get nodes

  4. 查看一个具体的节点详情:kubectl describe node

  5. 列出所有命名空间:kubectl get namespaces

  6. 列出所有命名空间中的所有 pod:kubectl get pods --all-namespaces

Pod 诊断:

  1. 列出特定命名空间中的 pod:kubectl get pods -n

  2. 查看一个 Pod 详情:kubectl describe pod -n

  3. 查看 Pod 日志:kubectl logs -n

  4. 尾部 Pod 日志:kubectl logs -f -n

  5. 在 pod 中执行命令:kubectl exec -it -n –

Pod 健康检查:

  1. 检查 Pod 准备情况:kubectl get pods -n -o jsonpath=‘{.status.conditions[?(@.type==“Ready”)].status}’

  2. 检查 Pod 事件:kubectl get events -n --field-selector involvedObject.name=

Service诊断:

  1. 列出命名空间中的所有服务:kubectl get svc -n

  2. 查看一个服务详情:kubectl describe svc -n

Deployment诊断:

  1. 列出命名空间中的所有Deployment:kubectl get deployments -n

  2. 查看一个Deployment详情:kubectl describe deployment -n

  3. 查看滚动发布状态:kubectl rollout status deployment/ -n

  4. 查看滚动发布历史记录:kubectl rollout history deployment/ -n

StatefulSet诊断:

  1. 列出命名空间中的所有 StatefulSet:kubectl get statefulsets -n

  2. 查看一个 StatefulSet详情:kubectl describe statefulset -n

ConfigMap 和Secret诊断:

  1. 列出命名空间中的 ConfigMap:kubectl get configmaps -n

  2. 查看一个ConfigMap详情:kubectl describe configmap -n

  3. 列出命名空间中的 Secret:kubectl get secrets -n

  4. 查看一个Secret详情:kubectl describe secret -n

命名空间诊断:

  1. 查看一个命名空间详情:kubectl describe namespace

资源使用情况:

  1. 检查 pod 的资源使用情况:kubectl top pod -n

  2. 检查节点资源使用情况:kubectl top nodes

网络诊断:

  1. 显示命名空间中 Pod 的 IP 地址:kubectl get pods -n -o custom-columns=POD:metadata.name,IP:status.podIP --no-headers

  2. 列出命名空间中的所有网络策略:kubectl get networkpolicies -n

  3. 查看一个网络策略详情:kubectl describe networkpolicy -n

持久卷 (PV) 和持久卷声明 (PVC) 诊断:

  1. 列出PV:kubectl get pv

  2. 查看一个PV详情:kubectl describe pv

  3. 列出命名空间中的 PVC:kubectl get pvc -n

  4. 查看PVC详情:kubectl describe pvc -n

节点诊断:

  1. 获取特定节点上运行的 Pod 列表:kubectl get pods --field-selector spec.nodeName= -n

资源配额和限制:

  1. 列出命名空间中的资源配额:kubectl get resourcequotas -n

  2. 查看一个资源配额详情:kubectl describe resourcequota -n

自定义资源定义 (CRD) 诊断:

  1. 列出命名空间中的自定义资源:kubectl get -n

  2. 查看自定义资源详情:kubectl describe -n

使用这些命令时,请记住将, , , , , , , , , , , , , , 和替换为你的特定值。

这些命令应该可以帮助你诊断 Kubernetes 集群以及在其中运行的应用程序。

资源伸缩和自动伸缩

  1. Deployment伸缩:kubectl scale deployment --replicas= -n

  2. 设置Deployment的自动伸缩:kubectl autoscale deployment --min= --max= --cpu-percent= -n

  3. 检查水平伸缩器状态:kubectl get hpa -n

作业和 CronJob 诊断:

  1. 列出命名空间中的所有作业:kubectl get jobs -n

  2. 查看一份工作详情:kubectl describe job -n

  3. 列出命名空间中的所有 cron 作业:kubectl get cronjobs -n

  4. 查看一个 cron 作业详情:kubectl describe cronjob -n

容量诊断:

  1. 列出按容量排序的持久卷 (PV):kubectl get pv --sort-by=.spec.capacity.storage

  2. 查看PV回收策略:kubectl get pv -o=jsonpath=‘{.spec.persistentVolumeReclaimPolicy}’

  3. 列出所有存储类别:kubectl get storageclasses

Ingress和服务网格诊断:

  1. 列出命名空间中的所有Ingress:kubectl get ingress -n

  2. 查看一个Ingress详情:kubectl describe ingress -n

  3. 列出命名空间中的所有 VirtualServices (Istio):kubectl get virtualservices -n

  4. 查看一个 VirtualService (Istio)详情:kubectl describe virtualservice -n

Pod 网络故障排除:

  1. 运行网络诊断 Pod(例如 busybox)进行调试:kubectl run -it --rm --restart=Never --image=busybox net-debug-pod – /bin/sh

  2. 测试从 Pod 到特定端点的连接:kubectl exec -it -n – curl

  3. 跟踪从一个 Pod 到另一个 Pod 的网络路径:kubectl exec -it -n – traceroute

  4. 检查 Pod 的 DNS 解析:kubectl exec -it -n – nslookup

配置和资源验证:

  1. 验证 Kubernetes YAML 文件而不应用它:kubectl apply --dry-run=client -f

  2. 验证 pod 的安全上下文和功能:kubectl auth can-i list pods --as=system:serviceaccount::

RBAC 和安全性:

  1. 列出命名空间中的角色和角色绑定:kubectl get roles,rolebindings -n

  2. 查看角色或角色绑定详情:kubectl describe role -n

服务帐户诊断:

  1. 列出命名空间中的服务帐户:kubectl get serviceaccounts -n

  2. 查看一个服务帐户详情:kubectl describe serviceaccount -n

清空节点和解除封锁:

  1. 清空节点以进行维护:kubectl drain --ignore-daemonsets

  2. 解除对节点的封锁:kubectl uncordon

资源清理:

  1. 强制删除 pod(不推荐):kubectl delete pod -n --grace-period=0 --force

Pod 亲和性和反亲和性:

  1. 列出 pod 的 pod 亲和性规则:kubectl get pod -n -o=jsonpath=‘{.spec.affinity}’

  2. 列出 pod 的 pod 反亲和性规则:kubectl get pod -n -o=jsonpath=‘{.spec.affinity.podAntiAffinity}’

Pod 安全策略 (PSP):

  1. 列出所有 Pod 安全策略(如果启用):kubectl get psp

事件:

  1. 查看最近的集群事件:kubectl get events --sort-by=.metadata.creationTimestamp

  2. 按特定命名空间过滤事件:kubectl get events -n

节点故障排除:

  1. 检查节点情况:kubectl describe node | grep Conditions -A5

  2. 列出节点容量和可分配资源:kubectl describe node | grep -E “Capacity|Allocatable”

临时容器(Kubernetes 1.18+):

  1. 运行临时调试容器:kubectl debug -it -n --image= – /bin/sh

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

  1. 获取 Pod 的 CPU 和内存使用情况:kubectl top pod -n

kuelet诊断:

  1. 查看节点上的kubelet日志:kubectl logs -n kube-system kubelet-

使用Telepresence 进行高级调试:

  1. 使用 Telepresence 调试 pod:telepresence --namespace --swap-deployment

Kubeconfig 和上下文:

  1. 列出可用的上下文:kubectl config get-contexts

  2. 切换到不同的上下文:kubectl config use-context

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

  1. 列出 PodSecurityPolicy (PSP) 违规行为:kubectl get psp -A | grep -vE ‘NAME|REVIEWED’

Pod 中断预算 (PDB) 诊断:

  1. 列出命名空间中的所有 PDB:kubectl get pdb -n

  2. 查看一个PDB详情:kubectl describe pdb -n

资源锁诊断(如果使用资源锁):

  1. 列出命名空间中的资源锁:kubectl get resourcelocks -n

服务端点和 DNS:

  1. 列出服务的服务端点:kubectl get endpoints -n

  2. 检查 Pod 中的 DNS 配置:kubectl exec -it -n – cat /etc/resolv.conf

自定义指标(Prometheus、Grafana):

  1. 查询Prometheus指标:用于kubectl port-forward访问Prometheus和Grafana服务来查询自定义指标。

Pod 优先级和抢占:

  1. 列出优先级:kubectl get priorityclasses

Pod 开销(Kubernetes 1.18+):

  1. 列出 pod 中的开销:kubectl get pod -n -o=jsonpath=‘{.spec.overhead}’

存储卷快照诊断(如果使用存储卷快照):

  1. 列出存储卷快照:kubectl get volumesnapshot -n

  2. 查看存储卷快照详情:kubectl describe volumesnapshot -n

资源反序列化诊断:

  1. 反序列化并打印 Kubernetes 资源:kubectl get -n -o=json

节点污点:

  1. 列出节点污点:kubectl describe node | grep Taints

更改和验证 Webhook 配置:

  1. 列出变异 webhook 配置:kubectl get mutatingwebhookconfigurations

  2. 列出验证 Webhook 配置:kubectl get validatingwebhookconfigurations

Pod 网络策略:

  1. 列出命名空间中的 pod 网络策略:kubectl get networkpolicies -n

节点条件(Kubernetes 1.17+):

  1. 自定义查询输出:kubectl get nodes -o custom-columns=NODE:.metadata.name,READY:.status.conditions[?(@.type==“Ready”)].status -l ‘node-role.kubernetes.io/worker=’

审核日志:

  1. 检索审核日志(如果启用):检查 Kubernetes 审核日志配置以了解审核日志的位置。

节点操作系统详细信息:

  1. 获取节点的操作系统信息:kubectl get node -o jsonpath=‘{.status.nodeInfo.osImage}’

这些命令应该涵盖 Kubernetes 中的各种诊断场景。确保将、、等占位符替换为你的集群和用例的实际值。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优质网络系统领域创作者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值