kubectl命令
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain 查看资源的文档
get 显示一个或更多 resources
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
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:
diff Diff live version against would-be applied version
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch 使用 strategic merge patch 更新一个资源的 field(s)
replace 通过 filename 或者 stdin替换一个资源
wait Experimental: Wait for a specific condition on one or many resources.
convert 在不同的 API versions 转换配置文件
kustomize Build a kustomization target from a directory or a remote url.
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 文件
plugin Provides utilities for interacting with plugins.
version 输出 client 和 server 的版本信息
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
kubectl 常用命令例子
命令 | 描述 |
---|---|
kubectl create -f mysource.yml | 通过yml文件创建资源对象 |
kubectl get pod -n default -o wide kubectl get deployment -w kubectl get pod -l app=nginx | 查看资源信息,-n 命名空间-w监视 -o 格式输出 -l 标签过滤 metadata.lebel:app=nginx |
kubectl log mypod kubectl log -f mypod kubectl -n default log mypod | 查看pod日志,-f 追踪 |
kubectl -n default exec -it mypod -c app1 /bin/sh | 进入到pod容器内部,pod多容器需要 -c 指定容器 |
kubectl get ing | 查看ingress |
kubectl get deployment | 查看deployment |
kubectl get node | 查看节点信息 |
kubectl create namespace dev | 创建命名空间 |
kubectl get namespace | 获取命名空间 |
kubectl delete namespace dev | 删除命名空间 |
kubectl get pod,svc,deployments,node,endpoints | 查看多个资源,资源类型用逗号隔开 |
kubectl get -f mysource.yml | 通过yml文件获取资源信息 |
kubect describe pod mypod | 查看资源描述 |
kubectl cluster-info | 查看集群信息 |
kubectl edit pod mypod | 编辑资源 |
kubectl version | 查看版本 |
kubectl explain pod kubectl explain pod.spec kubectl explain deployments | 查看资源文档说明(用于编写资源清单) |