Kubernets——常用命令

Kubernets命令总结

命令说明

命令格式命令说明
kubectl run 资源名称 -参数 --image=镜像名称:标签创建资源对象,常用参数-i交互,-t终端
kubectl get 查询资源 可选参数 -o wide 显示主机信息常用查询的资源 node|deployment|pod
kubectl exec -it 容器id 执行的命令同 docker exec 指令,进入容器内
kubectl describe 资源类型 资源名称查询资源的详细信息
kubectl attach同 docker attach 指令,连接容器
kubectl logs 容器id查看容器控制台的标准输出
kubectl delete 资源类型 资源名称删除指定的资源
kubectl create|apply -f 资源文件执行指定的资源文件

命令排错三部曲:

  • kubectl get pods -n
  • kubectl describe po -n
  • kuibectl logs -n

1. 集群信息

  1. 显示Kubernetes版本:kubectl version
  2. 显示集群信息:kubectl cluster-info
  3. 列出集群的所有节点:kubectl get nodes
  4. 查看具体节点的信息: kubectl describe node <node-name>
  5. 列出所有命名空间:kubectl get namespaces
  6. 列出所有命名空间的所有pods:kubectl get pods --all-namespace

2. Pod诊断

  1. 列出指定命名空间的pod:kubectl get pods -n <namespace>
  2. 查看一个pod详情: kubectl describe po <pod-name> -n <namespace>
  3. 查看pod日志:kubectl logs <pod-name> -n <namespace>
  4. 尾部pod日志:kubectl logs -f <pod-name> -n <namespace>
  5. 在pod中执行命令:kubectl exec -it <pod-name> -n <namespace> -- command

3. Service 诊断

  1. 列出命名空间里的所有服务:kubectl get svc -n <namespace>
  2. 查看服务详情:kubectl describe svc <service-name> -n <namespace>

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

  1. 列出PV:kubectl get pv
  2. 查看一个PV详情:kubectl describe pv <pv-name>
  3. 列出命名空间中的PVC:kubectl get pvc -n <namespace>
  4. 查看PVC详情:klubectl describe pvc <pvc-name> -n <namespace>

5. 容量诊断

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

6. Ingress和服务网络诊断

  1. 列出命令空间中的Ingress:kubectl get ingress -n <namespace>
  2. 查看一个Ingress的详情:kubectl describe ingreess <ingress-name> -n <namespace>
  3. 列出命名空间中的所有VirtualServices(Istio):kubectl get virtualservices -n <namespace>
  4. 查看一个VirtualServices的详情:kubectl describe virtualservices <virtualservices-name> -n <namespace>

7. 作业和CronJob诊断

  1. 列出命名空间中的所有作业:kubectl get jobs -n <namespace>
  2. 查看作业的详情:kubectl describe job <jobs-name> -n <namespace>
  3. 列出命名空间中的所有cron作业:kubectl get cronjobs -n <namespace>
  4. 查看cron作业的详情:kubectl describe conjobs <cronjobs-name> -n <namespace>

8. Deployment诊断

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

9. StatefulSet诊断

  1. 列出命名空间中所有StatefulSet:kubectl get statefulsets -n <namespace>
  2. 查看StatefulSet详情:kubectl describe statefulset <statefulset-name> -n <namespace>

10. ConfigMap和Secret诊断

  1. 列出命名空间中所有ConfigMap:kubectl get configmaps -n <namespace>
  2. 查看ConfigMap详情:kubectl describe configmap <config-name> -n <namespace>
  3. 列出命名空间中所有Secret:kubectl get secrets -n <namespace>
  4. 查看Secret详情:kubectl describe secret <secret-name> -n <namespace>

11. 节点诊断

  1. 获取特定节点上运行的Pod列表:kubectlget pods --field-selector spec.nodeName=<node-name> -n <namespace>

12. 网络诊断

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

13. Pod网络故障排除

  1. 运行网络Pod进行调试:kbectl run -it --rm --restat=Never --image=busybox net-debug-pod -- /bin/sh
  2. 测试从Pod到特定端点连接:kubectl exec -it <pode-name> --curl <endpoint-url>
  3. 跟踪从一个Pod到另一个Pod的网络路径:kubectl exec -it <source-pod-name> -n <namespace> --traceroute <pod-ip>
  4. 检查Pod的DNS解析:kubectl exec -it <pod-name> -n <namespace> -- nslookup <domain-name>

14. 配置和资源验证

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

15. 资源配额和限制

  1. 列出命名空间中的所有资源配额:kubectl get resourcequotas -n <namespace>
  2. 查看一个资源配额的详情:kubecrtl describe resourcequota <resourcequota-name> -n <namespace>
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值