Kubernetes之kubectl常用命令使用指南:2:故障排查

本文详述了在Kubernetes集群中使用kubectl进行故障排查的常用命令,包括kubectl version、kubectl api-versions、kubectl explain、kubectl get、kubectl describe、kubectl exec、kubectl cp、kubectl attach和kubectl cluster-info。通过实例展示了如何查看版本信息、获取资源详情、在容器中执行命令、交换文件以及获取实时日志等,以帮助用户高效解决集群中出现的问题。
摘要由CSDN通过智能技术生成

这里写图片描述
kubectl是一个用于操作kubernetes集群的命令行接口,通过利用kubectl的各种命令可以实现各种功能,是在使用kubernetes中非常常用的工具。这里我们会通过一些简单的实例来展现其中一些高频命令的使用方法。
更为重要的是这些命令使用的场景以及能够解决什么样的问题。上篇文章我们介绍了创建和删除相关的几条命令,这篇文章我们来看一下出现问题时最常用的另外九条命令。

常用命令

kubectl故障排查相关,本文将会简单介绍一下如下命令

项番 命令 说明
No.1 version 显示客户端和服务器侧版本信息
No.2 api-versions 以group/version的格式显示服务器侧所支持的API版本
No.3 explain 显示资源文档信息
No.4 get 取得确认对象信息列表
No.5 describe 取得确认对象的详细信息
No.6 logs 取得pod中容器的log信息
No.7 exec 在容器中执行一条命令
No.8 cp 从容器考出或向容器考入文件
No.9 attach Attach到一个运行中的容器上

事前准备

kubectl version

version命令用于确认客户端和服务器侧的版本信息,不同的版本的情况变化可能很大,所以故障排除时首先也需要确认的是现场环境的版本信息。
从下面可以清楚地看到,本文验证时所使用的版本为1.5.2

[root@ku8-1 tmp]# kubectl version
Client Version: version.Info{
   Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"08e099554f3c31f6e6f07b448ab3ed78d0520507", GitTreeState:"clean", BuildDate:"2017-01-12T04:57:25Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{
   Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"08e099554f3c31f6e6f07b448ab3ed78d0520507", GitTreeState:"clean", BuildDate:"2017-01-12T04:52:34Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
[root@ku8-1 tmp]#

集群构成

一主三从的Kubernetes集群

项番 类型 Hostname IP
No.1 Master ku8-1 192.168.32.131
No.1 Node ku8-2 192.168.32.132
No.1 Node ku8-3 192.168.32.133
No.1 Node ku8-4 192.168.32.134
[root@ku8-1 tmp]# kubectl get nodes
NAME             STATUS    AGE
192.168.32.132   Ready     12m
192.168.32.133   Ready     11m
192.168.32.134   Ready     11m
[root@ku8-1 tmp]# 

kubectl api-versions

使用api-versions命令可以列出当前版本的kubernetes的服务器端所支持的api版本信息。

[root@ku8-1 tmp]# kubectl api-versions
apps/v1beta1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1beta1
autoscaling/v1
batch/v1
certificates.k8s.io/v1alpha1
extensions/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1alpha1
storage.k8s.io/v1beta1
v1
[root@ku8-1 tmp]#

kubectl explain

使用kubectl explain可以和kubectl help一样进行辅助的功能确认,使用它可以了解各个部分的说明和组成部分。比如如下可以看到对rc的说明,在故障排除时作用并不具有太大作用,到是可以多读读加深一下对各个部分的理解。

[root@ku8-1 ~]# kubectl explain rc
DESCRIPTION:
ReplicationController represents the configuration of a replication controller.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#types-kinds

   metadata	<Object>
     If the Labels of a ReplicationController are empty, they are defaulted to
     be the same as the Pod(s) that the replication controller manages. Standard
     object's metadata. More info:
     http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata

   spec	<Object>
     Spec defines the specification of the desired behavior of the replication
     controller. More info:
     http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status

   status	<Object>
     Status is the most recently observed status of the replication controller.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值