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.