一、kubernetes集群命令分类:kubectl --help
kubectl controls the Kubernetes cluster manager. Find more information at https://github.com/kubernetes/kubernetes.
- Basic Commands (Beginner):基础命令初级
create Create a resource from a file or from stdin. 通过文件名或者标准输入创建资源。
expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的k8s服务
run 在集群中运行一个指定的镜像,也可能是副本
set 为 objects 设置一个指定的特征run-container 在集群中运行一个指定的镜像. This command is deprecated, use "run" instead- Basic Commands (Intermediate):基础命令中级
get 显示一个或更多 resources(资源分为pod、instance、service等很多种)
explain 查看资源的文档
edit 在服务器上编辑一个资源
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector- Deploy Commands:部署命令
rollout Manage the rollout of a resource
rolling-update 完成指定的 ReplicationController 的滚动升级
scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量- Cluster Management Commands:集群管理命令
certificate 修改 certificate 资源.
cluster-info 显示集群信息
top Display Resource (CPU/Memory/Storage) usage.
cordon 标记 node 为 unschedulable
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance
taint 更新一个或者多个 node 上的 taints- Troubleshooting and Debugging Commands:故障诊断和调试命令
describe 显示一个指定 resource 或者 group 的 resources 详情
logs 输出容器在 pod 中的日志
attach Attach 到一个运行中的 container
exec 在一个 container 中执行一个命令
port-forward Forward one or more local ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
auth Inspect authorization- Advanced Commands:高级命令
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch 使用 strategic merge patch 更新一个资源的 field(s)
replace 通过 filename 或者 stdin替换一个资源
convert 在不同的 API versions 转换配置文件- Settings Commands:设置命令
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)- Other Commands:其他命令
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 文件
help Help about any command
plugin Runs a command-line plugin
version 输出 client 和 server 的版本信息
二、kubernetes集群常用命令kubectl
本文主要介绍kubernetes排查问题时经常用到的命令。这里主要借助kubectl命令来实现。以下列出常用命令,后面会对每个命令进行详细解释,并举例:
kubectl常用命令
- get 获取列出一个或多个资源的信息。(资源分为pod、instance、service等很多种)
- describe 输出指定的一个/多个资源的详细信息。(一般describe状态有问题节点,如Pending等)
- logs 输出pod中一个容器的日志。(如果pod只包含一个容器则可以省略容器名)
- create 指定Yaml或Json,创建资源。(通过文件或者控制台输入)
- edit 编辑服务器上定义的资源。(文件默认输出格式为YAML。要以JSON格式编辑,请指定“-o json”选项。)
- rolling-update 执行指定ReplicationController的滚动更新。(不中断业务的更新方式)
- delete 删除一个资源(可以是pod、instance等)
- exec 在容器内部执行命令
2.1、kubectl get 命令
- 获取所有namespace:kubectl get ns
- 在指定的namespace下获取资源:kubectl -n {$nameSpace} get pods
- 以yaml格式输出资源:kubectl -n {$nameSpace} -o yaml
- 通用格式:kubectl get {$sourceType} --all-namespaces
常用的资源类型({$resourceType})有:
- po(pod)
- ns(命名空间namespace)
- instance(实例)
- svc(service服务):定义了一个 Pod 的逻辑分组,一种可以访问它们的策略(微服务)。
- cm(configMap):存储全局配置变量的,将分布式系统中不同模块的环境变量统一到一个对象中管理。
- ds(deamonSet):在每台计算节点上运行一个守护进程(如日志采集等),有时pod处于pending可能是因为某个deamonSet没起来。
- deploy(deployment):用于启动(上线/部署)一个Pod或者ReplicaSet。这个如果有问题,那么其他依赖它来部署的资源就肯定不会正常了。
2.2、kubectl describe 命令
描述一个资源:kubectl {$nameSpace} describe {$resourceType} {$resourceName}
- eg:kubectl describe pods nginx
2.3、kubectl los 命令
- 查看日志,--tail指定只看最后1000行:kubectl -n {$nameSpace} logs --tail=1000 {$podName} | less
- eg: kubectl logs --tail=20 nginx|less
2.4、kubectl create 命令
通过配置文件名创建一个集群资源对象: create -f {$yamlorJsonFilePath}
- 定义pod_nginx.yml文件:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: hub.c.163.com/library/nginx:latest
ports:
- containerPort: 80
- 创建pod:kubectl create -f pod_nginx.yml
- 删除pod:kubectl delete -f pod_nginx.yml
- 查看所有pod:kubectl get pods
- 查看详细pod:kubectl get pods -o wide
2.5、kubectl edit 命令
编辑一个资源文件,这里以编辑configMap资源对象为例(yaml格式):
kubectl -n {$nameSpace} edit {$resourceType} {$resourceName} -o yaml
2.6、kubectl rolling-update 命令
执行指定ReplicationController的滚动更新。该命令会创建一个新的RC,等新pod完全起来后删除一个旧的pod,之后重复操作,直到替换掉所有的pod。
kubectl rolling-update {$resourceName} -f {$yamlFile}
2.7、kubectl delete 命令
指定资源类型和名字删除一个资源:kubectl -n {$nameSpace} delete {$resourceType} {$resourceName}
批量删除资源:kubectl -n {$nameSpace} delete {$resourceType} -l {$label}={$labelValue}
2.8、kubectl exec命令
exec主要作用是在容器内部执行命令(一般为查看容器内部日志),这里以一个小例子说明kubectl exec命令的作用。
- 查看版本:kubectl version
- 查看nodes:kubectl get nodes
- 查看pods:kubectl get pods
- 拉取官方的镜像(内存有点低比较慢)
kubectl run kubernetes-bootcamp --image=jocatalin/kubernetes-bootcamp:v1 --port=8080
- 查看deploy的详细信息:kubectl describe deploy kubernetes-bootcamp