常用命令
Kubernetes Cheat Sheet
Viewing Resource Information //查看资源信息
Nodes
代码语言:javascript复制$ kubectl get no
$ kubectl get no -o wide
$ kubectl describe no
$ kubectl get no - o yaml
$ kubectl get node - - sel ect or =[ l abel _name]
$ kubectl get nodes -o jsonpath=' {.items[*].status.addresses [?(@.type=="ExternalIP")].address}'
$ kubectl top node [node_name]
Pods
代码语言:javascript复制$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po
$ kubectl get po --show-labels
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] - o yaml --export
$ kubect l get pod [ pod_name] - o yaml --export > nameoffile.yaml
$ kubectl get pods -- field-selector status.phase=Running
Namespaces
代码语言:javascript复制$ kubectl get ns
$ kubectl get ns - o yaml
$ kubectl describe ns
Deployments
代码语言:javascript复制$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy - o wide
$ kubectl get deploy - o yaml
Services
代码语言:javascript复制$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc - o wide
$ kubectl get svc - o yaml
$ kubectl get svc --show-labels
DaemonSets
代码语言:javascript复制$ kubectl get ds
$ kubectl get ds --all-namespaces
$ kubectl describe ds [daemonset_name] - n [namespce_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml
Events
代码语言:javascript复制$ kubectl get events
$ kubectl get events -n kube-system
$ kubectl get events -w
logs
代码语言:javascript复制$ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail =20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log
Service Accounts
代码语言:javascript复制$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml
ReplicaSets
代码语言:javascript复制$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide
$ kubectl get rs -o yaml
Roles
代码语言:javascript复制$ kubectl get roles --all -namespaces
$ kubectl get roles --all -namespaces -o yaml
Secrets
代码语言:javascript复制$ kubectl get secrets
$ kubectl get secrets --all -namespaces
$ kubectl get secrets -o yaml
ConfigMaps
代码语言:javascript复制$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml
Ingress
代码语言:javascript复制$ kubectl get ing
$ kubectl get ing --all-namespaces
PersistentVolume
代码语言:javascript复制$ kubectl get pv
$ kubectl describe pv
PersistentVolumeClaim
代码语言:javascript复制$ kubectl get pvc
$ kubectl describe pvc
StorageClass
代码语言:javascript复制$ kubectl get sc
$ kubectl get sc -o yaml
Multiple Resources
代码语言:javascript复制$ kubectl get svc,po
$ kubectl get deploy,no
$ kubectl get all
$ kubectl get all --all -namespaces
Changing Resource Attributes //改变资源属性
Taint
代码语言:javascript复制$ kubectl taint [node_name] [taint_name]
Labels
代码语言:javascript复制$ kubectl label [node_name] disktype=ssd
$ kubectl label [pod_name] env=prod
Cordon/Uncordon
代码语言:javascript复制$ kubectl cordon [node_name]
$ kunectl uncordon [node_name]
Drain
代码语言:javascript复制$ kubectl drain [node_name]
Nodes/Pods
代码语言:javascript复制$ kubectl delet enode [node_name]
$ kubectl delet epod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]
Deployments/Namespaces
代码语言:javascript复制$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expse deploy [deploy_name] --por=80 -type=NodePort
$ kubectl scale deploy [deploy_name] --repicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]
Services
代码语言:javascript复制$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]
DaemonSets
代码语言:javascript复制$ kubectl edit ds [ds_name] -n kube-system
$ kubectl delete ds [ds_name]
Service Accounts
代码语言:javascript复制$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]
Annotate
代码语言:javascript复制$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name]
Adding Resources //添加资源
Creating a Pod
代码语言:javascript复制$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --resart=Never
$ kubectl run [pod_name] --geneator=run-pod/v1 --image=nginx
$ kubectl run [pod_name] --image=nginx --restart=Never
Creating a Service
代码语言:javascript复制$ kubectl create svc nodeport [svc_name] --tcp=8080: 80
Creating a Deployment
代码语言:javascript复制$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=ngi nx
Interactive Pod
代码语言:javascript复制$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh
Output YAML to a File
代码语言:javascript复制$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > depl oy. yaml
$ kubectl get po [pod_name] -o yaml --export > pod. yaml
Getting Help
代码语言:javascript复制$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec
Requests //请求
API Call
代码语言:javascript复制$ kubectl get --raw /apis/metrics.k8s.io/
Cluster Info
代码语言:javascript复制$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatuses