kubectl操作指南
2020-03-28 本文已影响0人
hyperjiang
概述
kubectl
是控制k8s
集群的命令行工具,它默认会从~/.kube/config
读取配置。
使用格式:
kubectl [command] [TYPE] [NAME] [flags]
-
command
指定要在一个或多个资源上执行的操作,例如create
,get
,describe
,delete
-
TYPE
指定资源类型,不区分大小写,可以是单数、复数或缩写形式,例如以下3个命令是等效的:
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
-
NAME
指定资源的名称,区分大小写,如果省略了名称,则会显示所有资源的详细信息。 -
flags
指定标识,比如-f
指定文件,-w
监控变化,-o yaml
指定输出格式是yaml
等等。
在多个资源上执行操作时,可以通过类型和名称指定每个资源,也可以指定一个或多个文件,比如:
# kubectl [command] TYPE1 name1 name2 name<#>
kubectl get pod example-pod1 example-pod2
# kubectl [command] TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>
kubectl get pod/example-pod1 replicationcontroller/example-rc1
# kubectl [command] [TYPE] -f file1 -f file2 -f file<#>
kubectl get pod -f ./pod1.yaml -f ./pod2.yaml
详细命令说明参考:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
常用命令
-
kubectl apply
从文件或标准输入应用或更新资源:
# Create a service using the definition in example-service.yaml.
kubectl apply -f example-service.yaml
# Create a replication controller using the definition in example-controller.yaml.
kubectl apply -f example-controller.yaml
# Create the objects that are defined in any .yaml, .yml, or .json file within the <directory> directory.
kubectl apply -f <directory>
-
kubectl get
列出一个或多个资源:
# List all pods in plain-text output format.
kubectl get pods
# List all pods in plain-text output format and include additional information (such as node name).
kubectl get pods -o wide
# List the replication controller with the specified name in plain-text output format.
kubectl get rc <rc-name>
# List all replication controllers and services together in plain-text output format.
kubectl get rc,services
# List all daemon sets in plain-text output format.
kubectl get ds
# List all pods running on node server01
kubectl get pods --field-selector=spec.nodeName=server01
-
kubectl describe
显示一个或多个资源的详细状态,默认包括未初始化的资源:
# Display the details of the node with name <node-name>.
kubectl describe nodes <node-name>
# Display the details of the pod with name <pod-name>.
kubectl describe pods/<pod-name>
# Display the details of all the pods that are managed by the replication controller named <rc-name>.
# Remember: Any pods that are created by the replication controller get prefixed with the name of the replication controller.
kubectl describe pods <rc-name>
# Describe all pods
kubectl describe pods
-
kubectl delete
删除资源:
# Delete a pod using the type and name specified in the pod.yaml file.
kubectl delete -f pod.yaml
# Delete all the pods and services that have the label name=<label-name>.
kubectl delete pods,services -l name=<label-name>
# Delete all pods, including uninitialized ones.
kubectl delete pods --all
-
kubectl exec
在pod中的容器执行命令:
# Get output from running 'date' from pod <pod-name>. By default, output is from the first container.
kubectl exec <pod-name> date
# Get output from running 'date' in container <container-name> of pod <pod-name>.
kubectl exec <pod-name> -c <container-name> date
# Get an interactive TTY and run /bin/bash from pod <pod-name>. By default, output is from the first container.
kubectl exec -ti <pod-name> /bin/bash
-
kubectl logs
打印pod中的容器日志:
# Return a snapshot of the logs from pod <pod-name>.
kubectl logs <pod-name>
# Start streaming the logs from pod <pod-name>. This is similar to the 'tail -f' Linux command.
kubectl logs -f <pod-name>
-
kubectl diff
查看提议的集群更新的差异:
# Diff resources included in "pod.json".
kubectl diff -f pod.json
# Diff file read from stdin.
cat service.yaml | kubectl diff -f -