Kubernetes学习笔记(一)
参考:
kubectl Cheat Sheet | Kubernetes
Kubernetes kubectl 命令表 _ Kubernetes(K8S)中文文档_Kubernetes中文社区
Play with Kubernetes (play-with-k8s.com) (模拟环境)
[k8s系统情况]
1) kubernetes config指令:
>kubectl config view # Show Merged kubeconfig settings
>kubectl config get-contexts # display list of contexts
>kubectl config current-context # display the current-context
2) API resource指令:
>kubectl api-resources # for a complete list of supported resources
>kubectl api-resources --verbs=list,get # All resources that support the "list" and "get" request verbs
3) node详细情况:
>kubectl describe node <node name>
# 含有该node的详细信息,例如:NetworkUnavailable /MemoryPressure /DiskPressure /PIDPressure /Ready;
OS版本信息;cpu/momery/pods数量;IP地址;各个pod的cpu/memory利用率;Event事件
4)
>kubectl describe <api resource> #获取api对象详细信息
>kubectl explain pods #Get the documentation of the resource and its fields
>kubectl explain pods.spec.containers #JSONPath identifier
>kubectl get node -o wide #output带wide参数,获取详细信息,含node ip地址信息
>kubectl get nodes --show-labels #含failure domain信息
>kubectl cluster-info
>kubectl get componentstatus #component状态信息
>kubectl get apiservices
>kubectl diff -f <filename> # Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied.
[查看各种API Resource]
1) Get commands with basic output:
>kubectl get services -n <namespace> # List all services in the namespace
>kubectl get pods --all-namespaces # List all pods in all namespaces
>kubectl get pods -A # List all pods in all namespaces
>kubectl get deployment my-dep # List a particular deployment
>kubetctl get rc,pods # resource type will be added before the resource name
2) Describe commands with verbose output:
>kubectl describe nodes my-node
>kubectl describe pods my-pod #包含详细信息,Events项会提供重要信息
>kubectl describe pods -A #所有pods
3) 排序输出:
>kubectl get pods -A --sort-by=.metadata.name
>kubectl get pods -n <ns> --sort-by='.status.containerStatuses[0].restartCount' #按重启次数
>kubectl get pv --sort-by=.spec.capacity.storage #按容量排序
[查看pod信息]
>kubectl get pods -o name -A #output,显示name
>kubectl get pods -o wide #output,显示详细信息,含pod的IP地址,所在node
>kubectl get pods -n <ns> -owide -w #观察pod的创建终止等动态变化:After listing/getting the requested object, watch for changes.
>kubectl get pods -o json #output,以json格式显示详细信息
>kubectl get pod <pod name> -o yaml #Get a pod's YAML
>kubectl get pod -n <namespace> #List all pods in the current namespace
>kubectl get pods --show-labels #Show labels for all pods (or any other Kubernetes object that supports labelling)
>kubectl get pods --no-headers #不显示column名
>kubectl get pods -L app #output,显示Label=app信息
>kubectl get pods -L role #output,显示pod的role信息:如master,replica,data,logger等
>kubectl get pods -L label1,lablel2 或者
>kubectl get pods -L label1 -L label2
>kubectl get pods -l app #selector过滤
>kubectl get pods -A #获取所有namespace的pods
>kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' # List pods Sorted by Restart Count?
>kubectl get pod my-con --template="{{range .status.containerStatuses}}{{.name}}:{{.restartCount}}{{end}}" #查看某个pod的每个container的重启次数
>kubectl get pods --field-selector=status.phase=Running # Get all running pods in the namespace
>kubectl get pod mysql --template="{{.status.phase}}" #查看pod生命周期
>kubectl get pods --all-namespaces -o jsonpath='{range.items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3
# List all containerIDs of initContainer of all pods
# Helpful when cleaning up stopped containers, while avoiding removal of initContainers.
>kubectl exec <pod> -- <command> #在pod里运行command
>kubectl exec <pod> -c <container> -- <command> #在pod的container里运行command